コード例 #1
0
 public function execute()
 {
     $product_id = $this->get('product_id', true);
     $this->getProduct($product_id);
     $file = waRequest::file('file');
     $image = $file->waImage();
     if ($file->uploaded()) {
         $data = array('product_id' => $product_id, 'upload_datetime' => date('Y-m-d H:i:s'), 'width' => $image->width, 'height' => $image->height, 'size' => $file->size, 'original_filename' => basename($file->name), 'ext' => $file->extension, 'description' => waRequest::post('description'));
         $product_images_model = new shopProductImagesModel();
         $image_id = $data['id'] = $product_images_model->add($data);
         if (!$image_id) {
             throw new waAPIException('server_error', 500);
         }
         /**
          * @var shopConfig $config
          */
         $config = wa('shop')->getConfig();
         $image_path = shopImage::getPath($data);
         if (file_exists($image_path) && !is_writable($image_path) || !file_exists($image_path) && !waFiles::create($image_path)) {
             $product_images_model->deleteById($image_id);
             throw new waAPIException(sprintf("The insufficient file write permissions for the %s folder.", substr($image_path, strlen($config->getRootPath()))));
         }
         $file->moveTo($image_path);
         unset($image);
         shopImage::generateThumbs($data, $config->getImageSizes());
         $method = new shopProductImagesGetInfoMethod();
         $_GET['id'] = $image_id;
         $this->response = $method->getResponse(true);
     } else {
         throw new waAPIException('server_error', $file->error);
     }
 }
コード例 #2
0
 protected function save(waRequestFile $file)
 {
     $product_id = waRequest::post('product_id', null, waRequest::TYPE_INT);
     $product_model = new shopProductModel();
     if (!$product_model->checkRights($product_id)) {
         throw new waException(_w("Access denied"));
     }
     // check image
     if (!($image = $file->waImage())) {
         throw new waException('Incorrect image');
     }
     $image_changed = false;
     /**
      * Extend upload proccess
      * Make extra workup
      * @event image_upload
      */
     $event = wa()->event('image_upload', $image);
     if ($event) {
         foreach ($event as $plugin_id => $result) {
             if ($result) {
                 $image_changed = true;
             }
         }
     }
     if (!$this->model) {
         $this->model = new shopProductImagesModel();
     }
     $data = array('product_id' => $product_id, 'upload_datetime' => date('Y-m-d H:i:s'), 'width' => $image->width, 'height' => $image->height, 'size' => $file->size, 'original_filename' => basename($file->name), 'ext' => $file->extension);
     $image_id = $data['id'] = $this->model->add($data);
     if (!$image_id) {
         throw new waException("Database error");
     }
     /**
      * @var shopConfig $config
      */
     $config = $this->getConfig();
     $image_path = shopImage::getPath($data);
     if (file_exists($image_path) && !is_writable($image_path) || !file_exists($image_path) && !waFiles::create($image_path)) {
         $this->model->deleteById($image_id);
         throw new waException(sprintf("The insufficient file write permissions for the %s folder.", substr($image_path, strlen($config->getRootPath()))));
     }
     if ($image_changed) {
         $image->save($image_path);
         // save original
         $original_file = shopImage::getOriginalPath($data);
         if ($config->getOption('image_save_original') && $original_file) {
             $file->moveTo($original_file);
         }
     } else {
         $file->moveTo($image_path);
     }
     unset($image);
     // free variable
     shopImage::generateThumbs($data, $config->getImageSizes());
     return array('id' => $image_id, 'name' => $file->name, 'type' => $file->type, 'size' => $file->size, 'url_thumb' => shopImage::getUrl($data, $config->getImageSize('thumb')), 'url_crop' => shopImage::getUrl($data, $config->getImageSize('crop')), 'url_crop_small' => shopImage::getUrl($data, $config->getImageSize('crop_small')), 'description' => '');
 }
コード例 #3
0
 public function execute()
 {
     $id = waRequest::get('id', 0, waRequest::TYPE_INT);
     if (!$id) {
         throw new waException(_w("Image not found"), 404);
     }
     $product_images_model = new shopProductImagesModel();
     $image = $product_images_model->getById($id);
     if (!$image) {
         throw new waException(_w("Image not found"), 404);
     }
     if (waRequest::get('original')) {
         $path = shopImage::getOriginalPath($image);
     } else {
         $path = shopImage::getPath($image);
     }
     if (!$path || !file_exists($path)) {
         throw new waException(_w("Image not found"), 404);
     }
     waFiles::readFile($path, "Image_{$image['product_id']}_{$image['id']}.{$image['ext']}", true, true);
 }
コード例 #4
0
ファイル: shopProduct.class.php プロジェクト: Lazary/webasyst
 /**
  * @param array $options
  * @return shopProduct
  * @throws waException
  */
 public function duplicate($options = array())
 {
     if (!$this->checkRights()) {
         throw new waRightsException('Access denied');
     }
     $data = $this->data;
     $skip = array('id', 'create_datetime', 'id_1c', 'rating', 'rating_count', 'total_sales', 'image_id', 'contact_id', 'ext', 'count', 'sku_count');
     foreach ($skip as $field) {
         if (isset($data[$field])) {
             unset($data[$field]);
         }
     }
     $duplicate = new shopProduct();
     $this->getStorage(null);
     $sku_files = array();
     $sku_images = array();
     $ignore_select = true;
     foreach (self::$data_storages as $key => $i) {
         $raw = $this->getStorage($key)->getData($this);
         switch ($key) {
             case 'features_selectable':
                 $storage_data = array();
                 if (!$ignore_select) {
                     if ($this->sku_type == shopProductModel::SKU_TYPE_SELECTABLE) {
                         if (!is_array($raw)) {
                             $raw = array();
                         }
                         foreach ($raw as $id => $f) {
                             if (!empty($f['selected'])) {
                                 foreach ($f['values'] as $value_id => &$value) {
                                     if (!empty($value['selected'])) {
                                         $value = array('id' => $value_id);
                                     } else {
                                         unset($f['values'][$value_id]);
                                     }
                                 }
                                 $storage_data[$id] = $f;
                             }
                         }
                     }
                 }
                 break;
             case 'skus':
                 $storage_data = array();
                 $i = 0;
                 foreach ($raw as $sku_id => $sku) {
                     if (!empty($sku['virtual']) || $ignore_select) {
                         if ($file_path = shopProductSkusModel::getPath($sku)) {
                             $sku_files[$sku['id']] = array('file_name' => $sku['file_name'], 'file_description' => $sku['file_description'], 'file_size' => $sku['file_size'], 'file_path' => $file_path);
                         }
                         if (!empty($sku['image_id'])) {
                             $sku_images[$sku['id']] = $sku['image_id'];
                         }
                         foreach (array('id', 'id_1c', 'product_id', 'image_id', 'file_name', 'file_size', 'file_description') as $field) {
                             if (isset($sku[$field])) {
                                 unset($sku[$field]);
                             }
                         }
                         $storage_data[--$i] = $sku;
                     }
                 }
                 break;
             case 'tags':
                 $storage_data = array_values($raw);
                 break;
             case 'categories':
                 $storage_data = array_keys($raw);
                 break;
             default:
                 $storage_data = $raw;
                 break;
         }
         $duplicate->{$key} = $storage_data;
     }
     $counter = 0;
     $data['url'] = shopHelper::genUniqueUrl($this->url, $this->model, $counter);
     $data['name'] = $this->name . sprintf('(%d)', $counter ? $counter : 1);
     $duplicate->save($data);
     $product_id = $duplicate->getId();
     $sku_map = array_combine(array_keys($this->skus), array_keys($duplicate->skus));
     $config = wa('shop')->getConfig();
     $image_thumbs_on_demand = $config->getOption('image_thumbs_on_demand');
     /**
      * @var shopConfig $config
      */
     if ($this->pages) {
         $product_pages_model = new shopProductPagesModel();
         foreach ($this->pages as $page) {
             unset($page['id']);
             unset($page['create_time']);
             unset($page['update_datetime']);
             unset($page['create_contact_id']);
             $page['product_id'] = $duplicate->getId();
             $product_pages_model->add($page);
         }
     }
     #duplicate images
     $product_skus_model = new shopProductSkusModel();
     $images_model = new shopProductImagesModel();
     $images = $images_model->getByField('product_id', $this->getId(), $images_model->getTableId());
     $callback = create_function('$a, $b', 'return (max(-1, min(1, $a["sort"] - $b["sort"])));');
     usort($images, $callback);
     foreach ($images as $id => $image) {
         $source_path = shopImage::getPath($image);
         $original_file = shopImage::getOriginalPath($image);
         $image['product_id'] = $duplicate->getId();
         if ($sku_id = array_search($image['id'], $sku_images)) {
             $sku_id = $sku_map[$sku_id];
         }
         unset($image['id']);
         try {
             if ($image['id'] = $images_model->add($image, $id == $this->image_id)) {
                 waFiles::copy($source_path, shopImage::getPath($image));
                 if (file_exists($original_file)) {
                     waFiles::copy($original_file, shopImage::getOriginalPath($image));
                 }
                 if ($sku_id) {
                     $product_skus_model->updateById($sku_id, array('image_id' => $image['id']));
                 }
                 if (!$image_thumbs_on_demand) {
                     shopImage::generateThumbs($image, $config->getImageSizes());
                     //TODO use dummy copy  with rename files
                 }
             }
         } catch (waDbException $ex) {
             //just ignore it
             waLog::log('Error during copy product: ' . $ex->getMessage(), 'shop.log');
         } catch (waException $ex) {
             if (!empty($image['id'])) {
                 $images_model->deleteById($image['id']);
             }
             waLog::log('Error during copy product: ' . $ex->getMessage(), 'shop.log');
         }
     }
     foreach ($sku_files as $sku_id => $data) {
         $source_path = $data['file_path'];
         unset($data['file_path']);
         $sku_id = $sku_map[$sku_id];
         $sku = array_merge($duplicate->skus[$sku_id], $data);
         $product_skus_model->updateById($sku_id, $data);
         $target_path = shopProductSkusModel::getPath($sku);
         try {
             waFiles::copy($source_path, $target_path);
         } catch (waException $ex) {
             $data = array('file_name' => '', 'file_description' => '', 'file_size' => 0);
             $product_skus_model->updateById($sku_id, $data);
             print $ex->getMessage();
         }
     }
     $product_features_model = new shopProductFeaturesModel();
     $skus_features = $product_features_model->getSkuFeatures($this->id);
     $skus_features_data = array();
     foreach ($skus_features as $sku_id => $features) {
         $sku_id = $sku_map[$sku_id];
         foreach ($features as $feature_id => $feature_value_id) {
             $skus_features_data[] = compact('product_id', 'sku_id', 'feature_id', 'feature_value_id');
         }
     }
     if ($skus_features_data) {
         $product_features_model->multipleInsert($skus_features_data);
     }
     if ($this->sku_type == shopProductModel::SKU_TYPE_SELECTABLE) {
         $product_features_selectable_model = new shopProductFeaturesSelectableModel();
         if ($features_selectable = $product_features_selectable_model->getByField('product_id', $this->id, true)) {
             foreach ($features_selectable as &$feature_selectable) {
                 $feature_selectable['product_id'] = $product_id;
             }
             unset($feature_selectable);
             $product_features_selectable_model->multipleInsert($features_selectable);
         }
     }
     $product_services_model = new shopProductServicesModel();
     if ($services = $product_services_model->getByField('product_id', $this->id, true)) {
         foreach ($services as &$service) {
             unset($service['id']);
             $service['product_id'] = $product_id;
             $service['sku_id'] = ifset($sku_map[$service['sku_id']]);
             unset($service);
         }
         $product_services_model->multipleInsert($services);
     }
     $product_related_model = new shopProductRelatedModel();
     if ($related = $product_related_model->getByField('product_id', $this->id, true)) {
         foreach ($related as &$row) {
             $row['product_id'] = $product_id;
         }
         unset($row);
         $product_related_model->multipleInsert($related);
     }
     $params = array('product' => &$this, 'duplicate' => &$duplicate);
     /**
      * @wa-event product_duplicate
      */
     wa()->event('product_duplicate', $params);
     return $duplicate;
 }
コード例 #5
0
 private function stepImportImage()
 {
     /**
      * @var shopProductImagesModel $model
      */
     static $model;
     if (!is_array($this->data['map'][self::STAGE_IMAGE]) && $this->data['map'][self::STAGE_IMAGE]) {
         $this->data['map'][self::STAGE_IMAGE] = array($this->data['map'][self::STAGE_IMAGE]);
     }
     if ($file = reset($this->data['map'][self::STAGE_IMAGE])) {
         if (!$model) {
             $model = new shopProductImagesModel();
         }
         //TODO store image id & if repeated - skip it
         $target = 'new';
         $u = @parse_url($file);
         $_is_url = false;
         if (!$u || !(isset($u['scheme']) && isset($u['host']) && isset($u['path']))) {
         } elseif (in_array($u['scheme'], array('http', 'https', 'ftp', 'ftps'))) {
             $_is_url = true;
         } else {
             $target = 'skip';
             $file = null;
             $this->error(sprintf('Unsupported file source protocol', $u['scheme']));
         }
         $search = array('product_id' => $this->data['map'][self::STAGE_PRODUCT], 'ext' => pathinfo($file, PATHINFO_EXTENSION));
         try {
             $name = preg_replace('@[^a-zA-Zа-яА-Я0-9\\._\\-]+@', '', basename(urldecode($file)));
             if ($_is_url) {
                 $pattern = sprintf('@/(%d)/images/(\\d+)/\\2\\.(\\d+(x\\d+)?)\\.([^\\.]+)$@', $search['product_id']);
                 if (preg_match($pattern, $file, $matches)) {
                     $image = array('product_id' => $matches[1], 'id' => $matches[2], 'ext' => $matches[5]);
                     if (strpos($file, shopImage::getUrl($image, $matches[3])) !== false && $model->getByField($image)) {
                         #skip local file
                         $target = 'skip';
                         $file = null;
                     }
                 }
                 if ($file) {
                     waFiles::upload($file, $file = wa()->getTempPath('csv/upload/images/' . waLocale::transliterate($name, 'en_US')));
                 }
             } elseif ($file) {
                 $file = $this->data['upload_path'] . $file;
             }
             if ($file && file_exists($file)) {
                 if ($image = waImage::factory($file)) {
                     $search['original_filename'] = $name;
                     $data = array('product_id' => $this->data['map'][self::STAGE_PRODUCT], 'upload_datetime' => date('Y-m-d H:i:s'), 'width' => $image->width, 'height' => $image->height, 'size' => filesize($file), 'original_filename' => $name, 'ext' => pathinfo($file, PATHINFO_EXTENSION));
                     if ($exists = $model->getByField($search)) {
                         $data = array_merge($exists, $data);
                         $thumb_dir = shopImage::getThumbsPath($data);
                         $back_thumb_dir = preg_replace('@(/$|$)@', '.back$1', $thumb_dir, 1);
                         $paths[] = $back_thumb_dir;
                         waFiles::delete($back_thumb_dir);
                         // old backups
                         if (file_exists($thumb_dir)) {
                             if (!(waFiles::move($thumb_dir, $back_thumb_dir) || waFiles::delete($back_thumb_dir)) && !waFiles::delete($thumb_dir)) {
                                 throw new waException(_w("Error while rebuild thumbnails"));
                             }
                         }
                     }
                     $image_changed = false;
                     /**
                      * TODO move it code into product core method
                      */
                     /**
                      * Extend add/update product images
                      * Make extra workup
                      * @event image_upload
                      */
                     $event = wa()->event('image_upload', $image);
                     if ($event) {
                         foreach ($event as $result) {
                             if ($result) {
                                 $image_changed = true;
                                 break;
                             }
                         }
                     }
                     if (empty($data['id'])) {
                         $image_id = $data['id'] = $model->add($data);
                     } else {
                         $image_id = $data['id'];
                         $target = 'update';
                         $model->updateById($image_id, $data);
                     }
                     if (!$image_id) {
                         throw new waException("Database error");
                     }
                     $image_path = shopImage::getPath($data);
                     if (file_exists($image_path) && !is_writable($image_path) || !file_exists($image_path) && !waFiles::create($image_path)) {
                         $model->deleteById($image_id);
                         throw new waException(sprintf("The insufficient file write permissions for the %s folder.", substr($image_path, strlen($this->getConfig()->getRootPath()))));
                     }
                     if ($image_changed) {
                         $image->save($image_path);
                         /**
                          * @var shopConfig $config
                          */
                         $config = $this->getConfig();
                         if ($config->getOption('image_save_original') && ($original_file = shopImage::getOriginalPath($data))) {
                             waFiles::copy($file, $original_file);
                         }
                     } else {
                         waFiles::copy($file, $image_path);
                     }
                     $this->data['processed_count'][self::STAGE_IMAGE][$target]++;
                 } else {
                     $this->error(sprintf('Invalid image file', $file));
                 }
             } elseif ($file) {
                 $this->error(sprintf('File %s not found', $file));
                 $target = 'skip';
                 $this->data['processed_count'][self::STAGE_IMAGE][$target]++;
             } else {
                 $this->data['processed_count'][self::STAGE_IMAGE][$target]++;
             }
         } catch (Exception $e) {
             $this->error($e->getMessage());
             //TODO skip on repeated error
         }
         array_shift($this->data['map'][self::STAGE_IMAGE]);
         ++$this->data['current'][self::STAGE_IMAGE];
         if ($_is_url) {
             waFiles::delete($file);
         }
     }
     return true;
 }
コード例 #6
0
 public function execute()
 {
     $id = waRequest::get('id', null, waRequest::TYPE_INT);
     if (!$id) {
         throw new waException("Unknown image");
     }
     $direction = waRequest::post('direction', 'left', waRequest::TYPE_STRING_TRIM);
     if (!isset($this->angles[$direction])) {
         throw new waException("Can't rotate image");
     }
     $product_images_model = new shopProductImagesModel();
     $image = $product_images_model->getById($id);
     if (!$image) {
         throw new waException("Unknown image");
     }
     // check rights
     $product_model = new shopProductModel();
     if (!$product_model->checkRights($image['product_id'])) {
         throw new waException(_w("Access denied"));
     }
     $image_path = shopImage::getPath($image);
     $paths = array();
     try {
         $result_image_path = preg_replace('/(\\.[^\\.]+)$/', '.result$1', $image_path);
         $backup_image_path = preg_replace('/(\\.[^\\.]+)$/', '.backup$1', $image_path);
         $paths[] = $result_image_path;
         if ($this->rotate($image_path, $result_image_path, $this->angles[$direction])) {
             $count = 0;
             while (!file_exists($result_image_path) && ++$count < 5) {
                 sleep(1);
             }
             if (!file_exists($result_image_path)) {
                 throw new waException(_w("Error while rotate. I/O error"));
             }
             if (!waFiles::move($image_path, $backup_image_path)) {
                 throw new waException(_w("Error while rotate. Operation canceled"));
             }
             $paths[] = $backup_image_path;
             if (!waFiles::move($result_image_path, $image_path)) {
                 if (!waFiles::move($backup_image_path, $image_path)) {
                     throw new waException(_w("Error while rotate. Original file corupted but backuped"));
                 }
                 throw new waException(_w("Error while rotate. Operation canceled"));
             }
             $datetime = date('Y-m-d H:i:s');
             $data = array('edit_datetime' => $datetime, 'width' => $image['height'], 'height' => $image['width']);
             $product_images_model->updateById($id, $data);
             $image = array_merge($image, $data);
             $thumb_dir = shopImage::getThumbsPath($image);
             $back_thumb_dir = preg_replace('@(/$|$)@', '.back$1', $thumb_dir, 1);
             $paths[] = $back_thumb_dir;
             waFiles::delete($back_thumb_dir);
             if (!(waFiles::move($thumb_dir, $back_thumb_dir) || waFiles::delete($back_thumb_dir)) && !waFiles::delete($thumb_dir)) {
                 throw new waException(_w("Error while rebuild thumbnails"));
             }
             $config = $this->getConfig();
             try {
                 shopImage::generateThumbs($image, $config->getImageSizes());
             } catch (Exception $e) {
                 waLog::log($e->getMessage());
             }
             $this->response = $image;
             $edit_datetime_ts = strtotime($image['edit_datetime']);
             $this->response['url_big'] = shopImage::getUrl($image, $config->getImageSize('big')) . '?' . $edit_datetime_ts;
             $this->response['url_crop'] = shopImage::getUrl($image, $config->getImageSize('crop')) . '?' . $edit_datetime_ts;
         }
         foreach ($paths as $path) {
             waFiles::delete($path);
         }
     } catch (Exception $e) {
         foreach ($paths as $path) {
             waFiles::delete($path);
         }
         throw $e;
     }
 }
コード例 #7
0
 public function execute()
 {
     $id = waRequest::post('id', null, waRequest::TYPE_INT);
     if (!$id) {
         throw new waException("Can't restore image");
     }
     $product_images_model = new shopProductImagesModel();
     $image = $product_images_model->getById($id);
     if (!$image) {
         throw new waException("Can't restore image");
     }
     // check rights
     $product_model = new shopProductModel();
     if (!$product_model->checkRights($image['product_id'])) {
         throw new waException(_w("Access denied"));
     }
     $original_image_path = shopImage::getOriginalPath($image);
     if (!wa('shop')->getConfig()->getOption('image_save_original') || !file_exists($original_image_path)) {
         throw new waException("Can't restore image. Original image doesn't exist");
     }
     $image_path = shopImage::getPath($image);
     $paths = array();
     try {
         $backup_image_path = preg_replace('/(\\.[^\\.]+)$/', '.backup$1', $image_path);
         if (!waFiles::move($image_path, $backup_image_path)) {
             throw new waException("Error while restore. Operation canceled");
         }
         $paths[] = $backup_image_path;
         if (!waFiles::move($original_image_path, $image_path)) {
             if (!waFiles::move($backup_image_path, $image_path)) {
                 throw new waException("Error while restore. Current file corupted but backuped");
             }
             throw new waException("Error while restore. Operation canceled");
         }
         $data = $this->getData($image_path);
         $product_images_model->updateById($id, $data);
         $image = array_merge($image, $data);
         $thumb_dir = shopImage::getThumbsPath($image);
         $back_thumb_dir = preg_replace('@(/$|$)@', '.back$1', $thumb_dir, 1);
         $paths[] = $back_thumb_dir;
         waFiles::delete($back_thumb_dir);
         // old backups
         if (!(waFiles::move($thumb_dir, $back_thumb_dir) || waFiles::delete($back_thumb_dir)) && !waFiles::delete($thumb_dir)) {
             throw new waException(_w("Error while rebuild thumbnails"));
         }
         /**
          * @var shopConfig $config
          */
         $config = $this->getConfig();
         try {
             shopImage::generateThumbs($image, $config->getImageSizes());
         } catch (Exception $e) {
             waLog::log($e->getMessage());
         }
         $this->response = $image;
         $edit_datetime_ts = strtotime($image['edit_datetime']);
         $this->response['url_big'] = shopImage::getUrl($image, $config->getImageSize('big')) . '?' . $edit_datetime_ts;
         $this->response['url_crop'] = shopImage::getUrl($image, $config->getImageSize('crop')) . '?' . $edit_datetime_ts;
         foreach ($paths as $path) {
             waFiles::delete($path);
         }
     } catch (Exception $e) {
         foreach ($paths as $path) {
             waFiles::delete($path);
         }
         throw $e;
     }
 }
コード例 #8
0
 /**
  * Delete one image
  * @param int $id ID of image
  */
 public function delete($id)
 {
     $id = (int) $id;
     if (!$id) {
         return false;
     }
     $image = $this->getById($id);
     if (!$image) {
         return false;
     }
     $product_id = $image['product_id'];
     // first of all try delete files from disk
     waFiles::delete(shopImage::getThumbsPath($image));
     waFiles::delete(shopImage::getPath($image));
     waFiles::delete(shopImage::getOriginalPath($image));
     if (!$this->deleteById($id)) {
         return false;
     }
     // first image for this product is main image for this product
     $main_image = $this->query("SELECT id AS image_id, ext FROM {$this->table} WHERE product_id = {$product_id} ORDER BY sort LIMIT 1")->fetchAssoc();
     if (!$main_image) {
         $main_image = array('image_id' => null, 'ext' => null);
     }
     $product_model = new shopProductModel();
     $product_model->updateById($product_id, $main_image);
     // make NULL image_id for that skus of this product which have image_id equals this image ID
     $this->exec("\n            UPDATE `shop_product_skus` SET image_id = NULL\n            WHERE product_id = {$product_id} AND image_id = {$id}\n        ");
     return true;
 }