public function execute()
 {
     $id = $this->get('id', true);
     $product_model = new shopProductModel();
     $data = $product_model->getById($id);
     if (!$data) {
         throw new waAPIException('invalid_param', 'Product not found', 404);
     }
     $this->response = $data;
     $p = new shopProduct($data);
     if ($p['image_id']) {
         $this->response['image_url'] = shopImage::getUrl(array('product_id' => $id, 'id' => $p['image_id'], 'ext' => $p['ext']), wa('shop')->getConfig()->getImageSize('default'), true);
     }
     $this->response['skus'] = array_values($p->skus);
     foreach ($this->response['skus'] as &$sku) {
         $stocks = array();
         foreach ($sku['stock'] as $stock_id => $count) {
             $stocks[] = array('id' => $stock_id, 'count' => $count);
         }
         unset($sku['stock']);
         $sku['stocks'] = $stocks;
     }
     unset($sku);
     $this->response['categories'] = array_values($p->categories);
     $this->response['images'] = array_values($p->getImages('thumb', true));
     $this->response['features'] = array();
     foreach ($p->features as $f => $v) {
         if (is_array($v)) {
             $this->response['features'][$f] = array_values($v);
         } else {
             $this->response['features'][$f] = (string) $v;
         }
     }
 }
 protected function step()
 {
     $image_model = new shopProductImagesModel();
     $create_thumbnails = waRequest::post('create_thumbnails');
     $chunk_size = 50;
     if ($create_thumbnails) {
         $chunk_size = 10;
     }
     $sizes = wa('shop')->getConfig()->getImageSizes();
     $images = $image_model->getAvailableImages($this->data['offset'], $chunk_size);
     foreach ($images as $i) {
         if ($this->data['product_id'] != $i['product_id']) {
             sleep(0.2);
             $this->data['product_id'] = $i['product_id'];
             $this->data['product_count'] += 1;
         }
         try {
             $path = shopImage::getThumbsPath($i);
             if (!waFiles::delete($path)) {
                 throw new waException(sprintf(_w('Error when delete thumbnails for image %d'), $i['id']));
             }
             if ($create_thumbnails) {
                 shopImage::generateThumbs($i, $sizes);
             }
             $this->data['image_count'] += 1;
             // image count - count of successful progessed images
         } catch (Exception $e) {
             $this->error($e->getMessage());
         }
         $this->data['offset'] += 1;
     }
 }
 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);
     }
 }
 public function execute()
 {
     $hash = $this->get('hash');
     $collection = new shopProductsCollection($hash);
     $offset = waRequest::get('offset', 0, 'int');
     if ($offset < 0) {
         throw new waAPIException('invalid_param', 'Param offset must be greater than or equal to zero');
     }
     $limit = waRequest::get('limit', 100, 'int');
     if ($limit < 0) {
         throw new waAPIException('invalid_param', 'Param limit must be greater than or equal to zero');
     }
     if ($limit > 1000) {
         throw new waAPIException('invalid_param', 'Param limit must be less or equal 1000');
     }
     $this->response['count'] = $collection->count();
     $this->response['offset'] = $offset;
     $this->response['limit'] = $limit;
     $this->response['products'] = array_values($collection->getProducts('*', $offset, $limit));
     $image_size = wa('shop')->getConfig()->getImageSize('thumb');
     foreach ($this->response['products'] as &$p) {
         if ($p['image_id']) {
             $p['image_url'] = shopImage::getUrl(array('id' => $p['image_id'], 'product_id' => $p['id'], 'ext' => $p['ext']), $image_size, true);
         }
     }
     unset($p);
 }
 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' => '');
 }
 public function execute()
 {
     $app_settings_model = new waAppSettingsModel();
     $settings = $app_settings_model->get(array('shop', 'yoss'));
     if ($settings['status'] === 'on') {
         $query = waRequest::post('query', '', waRequest::TYPE_STRING_TRIM);
         $page = waRequest::post('page', 1, 'int');
         $result = array();
         $result['products'] = array();
         $result['product_count'] = 0;
         $collection = new shopProductsCollection('search/query=' . $query);
         $product_limit = $settings['product_limit'];
         if (!$product_limit) {
             $product_limit = $this->getConfig()->getOption('products_per_page');
         }
         $products = $collection->getProducts('*', ($page - 1) * $product_limit, $product_limit);
         if ($products) {
             $brands = array();
             $categories = array();
             $feature_model = new shopFeatureModel();
             $result['searh_all_url'] = wa()->getRouteUrl('/frontend/search/query=') . '?query=' . $query;
             foreach ($products as $p) {
                 $brand_feature = $feature_model->getByCode('brand');
                 $brand = '';
                 if ($brand_feature) {
                     $feature_value_model = $feature_model->getValuesModel($brand_feature['type']);
                     $product_brands = $feature_value_model->getProductValues($p['id'], $brand_feature['id']);
                     $brands = array();
                     foreach ($product_brands as $k => $v) {
                         $brand_id = $feature_value_model->getValueId($brand_feature['id'], $v);
                         $brands[] = array('id' => $brand_id, 'brand' => '<a href="' . wa()->getRouteUrl('shop/frontend/brand', array('brand' => str_replace('%2F', '/', urlencode($v)))) . '">' . $v . '</a>');
                     }
                 }
                 $category_model = new shopCategoryModel();
                 $category = $category_model->getById($p['category_id']);
                 $res_category = '';
                 if ($category) {
                     $res_category = '<a href="' . wa()->getRouteUrl('/frontend/category', array('category_url' => $category['full_url'])) . '">' . $category['name'] . '</a>';
                 }
                 $result['products'][] = array("name" => $p['name'], "url" => $p['frontend_url'], "image" => $p['image_id'] ? "<img src='" . shopImage::getUrl(array("product_id" => $p['id'], "id" => $p['image_id'], "ext" => $p['ext']), "48x48") . "' />" : "", "price" => shop_currency_html($p['price'], true), "brands" => $brands, "category" => $res_category);
             }
             $product_model = new shopProductModel();
             $product_count = $collection->count();
             $result['product_count'] = $product_count;
             if ($product_count > ($page - 1) * $product_limit + $product_limit) {
                 $result['next_page'] = $page + 1;
             } else {
                 $result['next_page'] = false;
             }
         }
         $this->response = $result;
     } else {
         $this->response = false;
     }
 }
 public function execute()
 {
     $id = $this->get('id', true);
     $images_model = new shopProductImagesModel();
     $image = $images_model->getById($id);
     if (!$image) {
         throw new waAPIException('invalid_param', 'Product image not found', 404);
     }
     $this->response = $image;
     $size = waRequest::get('size', wa('shop')->getConfig()->getImageSize('thumb'));
     $this->response['url_thumb'] = shopImage::getUrl($image, $size, true);
 }
 public function workup(&$product, $sku_stocks)
 {
     if (!$product['image_id']) {
         $product['url_crop_small'] = null;
     } else {
         $product['url_crop_small'] = shopImage::getUrl(array('id' => $product['image_id'], 'product_id' => $product['id'], 'ext' => $product['ext']), $this->getConfig()->getImageSize('crop_small'));
     }
     // aggregated stocks count icon for product
     $product['icon'] = shopHelper::getStockCountIcon($product['count'], null, true);
     foreach ($product['skus'] as &$sku) {
         $this->workupSku($sku, $sku_stocks);
     }
     unset($sku);
 }
 public function execute()
 {
     $app_settings_model = new waAppSettingsModel();
     $query = waRequest::get('term');
     $count = $app_settings_model->get($this->plugin_id, 'autocomplete_count');
     $collection = new shopProductsCollection('search/query=' . $query);
     $products = $collection->getProducts('*', 0, $count);
     $result = array();
     foreach ($products as $product) {
         $size = $app_settings_model->get($this->plugin_id, 'img_size');
         $product['value'] = $product['name'];
         $product['price_str'] = shop_currency($product['price']);
         $product['img_url'] = $product['image_id'] ? shopImage::getUrl(array('id' => $product['image_id'], 'product_id' => $product['id'], 'filename' => $product['image_filename'], 'ext' => $product['ext']), $size) : '';
         array_push($result, $product);
     }
     $this->response = $result;
 }
 protected function formatSizes($sizes)
 {
     $result = array();
     foreach ($sizes as $size) {
         $size_info = shopImage::parseSize((string) $size);
         $type = $size_info['type'];
         $width = $size_info['width'];
         $height = $size_info['height'];
         if ($type == 'max' || $type == 'crop' || $type == 'width') {
             $result[] = array($type => $width);
         } else {
             if ($type == 'height') {
                 $result[] = array($type => $height);
             } elseif ($type == 'rectangle') {
                 $result[] = array('rectangle' => array($width, $height));
             }
         }
     }
     return $result;
 }
 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);
 }
 public function execute()
 {
     $id = waRequest::get('id', 0, waRequest::TYPE_INT);
     if (!$id) {
         throw new waException("Unknown product");
     }
     $product = new shopProduct($id);
     $sizes = $this->getConfig()->getImageSizes('system');
     $images = $product->getImages($sizes);
     $param = waRequest::get('param', array(), waRequest::TYPE_ARRAY_INT);
     $image_id = !empty($param[0]) ? $param[0] : 0;
     if (isset($images[$image_id])) {
         $image = $images[$image_id];
         if ($image['size']) {
             $image['size'] = waFiles::formatSize($image['size'], '%0.2f', 'B,KB,MB,GB');
         }
         $this->setTemplate('ProductImage');
         $images = array_values($images);
         array_unshift($images, null);
         array_push($images, null);
         $offset = 0;
         foreach ($images as $k => $img) {
             if ($image['id'] == $img['id']) {
                 $offset = $k;
             }
         }
         $next = $images[$offset + 1];
         $image['dimensions'] = shopImage::getThumbDimensions($image, $sizes['big']);
         $this->view->assign(array('image' => $image, 'next' => $next, 'offset' => $offset, 'original_exists' => file_exists(shopImage::getOriginalPath($image)), 'photostream' => waRequest::get('ps', array())));
     }
     $this->view->assign(array('sizes' => $sizes, 'images' => $images, 'count' => count($images) - 2, 'product_id' => $id, 'product' => $product));
     /**
      * @event backend_product_edit
      * @return array[string][string]string $return[%plugin_id%]['images'] html output
      */
     $this->view->assign('backend_product_edit', wa()->event('backend_product_edit', $product));
 }
示例#13
0
 public function execute()
 {
     $offset = waRequest::get('offset', 0, waRequest::TYPE_INT);
     $total_count = waRequest::get('total_count', null, waRequest::TYPE_INT);
     $lazy = waRequest::get('lazy', false, waRequest::TYPE_INT);
     $product_reivews_model = new shopProductReviewsModel();
     $reviews_per_page = $this->getConfig()->getOption('reviews_per_page_total');
     /*
     $reviews = $product_reivews_model->getList(
         $offset,
         $reviews_per_page,
         array('is_new' => true)
     );
     */
     $reviews = $product_reivews_model->getList('*,is_new,contact,product', array('offset' => $offset, 'limit' => $reviews_per_page));
     // TODO: move to model
     $product_ids = array();
     foreach ($reviews as $review) {
         $product_ids[] = $review['product_id'];
     }
     $product_ids = array_unique($product_ids);
     $product_model = new shopProductModel();
     $products = $product_model->getByField('id', $product_ids, 'id');
     $image_size = wa()->getConfig()->getImageSize('crop_small');
     foreach ($reviews as &$review) {
         if (isset($products[$review['product_id']])) {
             $product = $products[$review['product_id']];
             $review['product_name'] = $product['name'];
             if ($product['image_id']) {
                 $review['product_url_crop_small'] = shopImage::getUrl(array('id' => $product['image_id'], 'product_id' => $product['id'], 'ext' => $product['ext']), $image_size);
             } else {
                 $review['product_url_crop_small'] = null;
             }
         }
     }
     $this->view->assign(array('total_count' => $total_count ? $total_count : $product_reivews_model->countAll(), 'count' => count($reviews), 'offset' => $offset, 'reviews' => $reviews, 'current_author' => shopProductReviewsModel::getAuthorInfo(wa()->getUser()->getId()), 'reply_allowed' => true, 'lazy' => $lazy, 'sidebar_counters' => array('new' => $product_reivews_model->countNew(!$offset))));
 }
 private function stepExportProduct(&$current_stage, &$count, &$processed)
 {
     static $products;
     static $product_feature_model;
     static $feature_model;
     static $tags_model;
     static $size;
     if (!$products) {
         $offset = $current_stage[self::STAGE_PRODUCT] - ifset($this->data['map'][self::STAGE_PRODUCT], 0);
         $fields = '*';
         if (!empty($this->data['options']['images'])) {
             $fields .= ', images';
         }
         $products = $this->getCollection()->getProducts($fields, $offset, 50, false);
     }
     $chunk = 5;
     $non_sku_fields = array('summary', 'meta_title', 'meta_keywords', 'meta_description', 'description', 'sort', 'tags', 'images');
     while ($chunk-- > 0 && ($product = reset($products))) {
         $exported = false;
         /* check rights per product type && settlement options */
         $rights = empty($product['type_id']) || in_array($product['type_id'], $this->data['types']);
         $category_id = isset($product['category_id']) ? intval($product['category_id']) : null;
         /* check category match*/
         $category_match = !$this->data['export_category'] || $category_id === $this->data['map'][self::STAGE_CATEGORY];
         if ($rights && $category_match) {
             $shop_product = new shopProduct($product);
             if (!empty($this->data['options']['features'])) {
                 if (!isset($product['features'])) {
                     if (!$product_feature_model) {
                         $product_feature_model = new shopProductFeaturesModel();
                     }
                     $product['features'] = $product_feature_model->getValues($product['id']);
                 }
                 foreach ($product['features'] as $code => &$feature) {
                     if (!empty($this->data['composite_features'][$code])) {
                         $feature = str_replace('×', 'x', $feature);
                     }
                     unset($feature);
                 }
             }
             if (!isset($product['tags'])) {
                 if (!$tags_model) {
                     $tags_model = new shopProductTagsModel();
                 }
                 $product['tags'] = implode(',', $tags_model->getTags($product['id']));
             }
             if (!empty($this->data['options']['images'])) {
                 if (isset($product['images'])) {
                     if (!$size) {
                         /**
                          * @var shopConfig $config
                          */
                         $config = $this->getConfig();
                         $size = $config->getImageSize('big');
                     }
                     foreach ($product['images'] as &$image) {
                         $image = 'http://' . ifempty($this->data['base_url'], 'localhost') . shopImage::getUrl($image, $size);
                     }
                     $product['images'] = array_values($product['images']);
                 }
             }
             $product['type_name'] = $shop_product->type['name'];
             $skus = $shop_product->skus;
             if (false && $product['sku_id']) {
                 #default SKU reorder
                 if (isset($skus[$product['sku_id']])) {
                     $sku = $skus[$product['sku_id']];
                     $sku['stock'][0] = $sku['count'];
                     $product['skus'] = array(-1 => $sku);
                     unset($skus[$product['sku_id']]);
                 }
                 $this->writer->write($product);
                 if (!empty($this->data['options']['images'])) {
                     if (isset($product['images'])) {
                         $processed[self::STAGE_IMAGE] += count($product['images']);
                     }
                 }
                 $exported = true;
                 if (!empty($this->data['options']['features'])) {
                     unset($product['features']);
                 }
             }
             if (!empty($product['tax_id'])) {
                 $product['tax_name'] = ifset($this->data['taxes'][$product['tax_id']]);
             }
             if (!isset($product['features'])) {
                 $product['features'] = array();
             }
             foreach ($skus as $sku_id => $sku) {
                 if ($exported) {
                     foreach ($non_sku_fields as $field) {
                         if (isset($product[$field])) {
                             unset($product[$field]);
                         }
                     }
                 }
                 $sku['stock'][0] = $sku['count'];
                 if (!empty($this->data['options']['features'])) {
                     $sku['features'] = $product_feature_model->getValues($product['id'], -$sku_id);
                     if ($product['sku_type'] == shopProductModel::SKU_TYPE_SELECTABLE) {
                         if (!$exported) {
                             $features_selectable_model = new shopProductFeaturesSelectableModel();
                             if ($selected = $features_selectable_model->getByProduct($product['id'])) {
                                 if (!$feature_model) {
                                     $feature_model = new shopFeatureModel();
                                 }
                                 $features = $feature_model->getById(array_keys($selected));
                                 $enclosure = $this->writer->enclosure;
                                 $pattern = sprintf("/(?:%s|%s|%s)/", preg_quote(',', '/'), preg_quote($enclosure, '/'), preg_quote($enclosure, '/'));
                                 foreach ($features as $feature_id => $feature) {
                                     $values = shopFeatureModel::getValuesModel($feature['type'])->getValues(array('feature_id' => $feature_id, 'id' => $selected[$feature_id]));
                                     if (!empty($values[$feature['id']])) {
                                         $f_values = $values[$feature['id']];
                                         if (!isset($product['features'])) {
                                             $product['features'] = array();
                                         }
                                         if (isset($sku['features'][$feature['code']])) {
                                             array_unshift($f_values, (string) $sku['features'][$feature['code']]);
                                         }
                                         foreach ($f_values as &$value) {
                                             if (preg_match($pattern, $value)) {
                                                 $value = $enclosure . str_replace($enclosure, $enclosure . $enclosure, $value) . $enclosure;
                                             }
                                             unset($value);
                                         }
                                         $f_values = array_unique($f_values);
                                         $product['features'][$feature['code']] = '<{' . implode(',', $f_values) . '}>';
                                     }
                                 }
                             }
                             $virtual_product = $product;
                             if (isset($skus[$product['sku_id']])) {
                                 $virtual_product['skus'] = array(-1 => $skus[$product['sku_id']]);
                             } else {
                                 $virtual_product['skus'] = array(-1 => $sku);
                             }
                             $virtual_product['skus'][-1]['stock'] = array(0 => $product['count']);
                             $this->writer->write($virtual_product);
                         }
                         $product['features'] = $sku['features'];
                     } else {
                         if (!$exported) {
                             foreach ($product['features'] as $code => &$values) {
                                 if (isset($sku['features'][$code])) {
                                     $values = array_unique(array_merge($values, $sku['features'][$code]));
                                 }
                                 unset($values);
                             }
                         } else {
                             $product['features'] = $sku['features'];
                         }
                     }
                 }
                 $product['skus'] = array(-1 => $sku);
                 $this->writer->write($product);
                 if (isset($product['images'])) {
                     $processed[self::STAGE_IMAGE] += count($product['images']);
                 }
                 $exported = true;
                 ++$current_stage[self::STAGE_SKU];
                 ++$processed[self::STAGE_SKU];
             }
         } elseif (count($products) > 1) {
             ++$chunk;
         }
         array_shift($products);
         ++$current_stage[self::STAGE_PRODUCT];
         if ($exported) {
             ++$processed[self::STAGE_PRODUCT];
         }
     }
     return $current_stage[self::STAGE_PRODUCT] < $count[self::STAGE_PRODUCT];
 }
 /**
  * @param string $field
  * @param mixed $value
  * @param array $info
  * @param array $data
  * @param null $sku_data
  * @return mixed|string
  */
 private function format($field, $value, $info = array(), $data = array(), $sku_data = null)
 {
     /**
      * @todo cpa field
      */
     /**
      * <yml_catalog>
      * <shop>
      * <currencies>
      * <categories>
      * <local_delivery_cost>
      * <offers>
      * <picture>
      * <description> и <name>
      * <delivery>, <pickup> и <store>
      * <adult>
      * <barcode>
      * <cpa> TODO
      * <rec>
      * <param> (name,unit,value)
      * <vendor>
      */
     static $currency_model;
     static $size;
     switch ($field) {
         case 'group_id':
             if ($value === 'auto') {
                 if (!empty($data['market_category'])) {
                     $value = $this->plugin()->isGroupedCategory($data['market_category']) ? $data['id'] : null;
                 } else {
                     $info['format'] = false;
                 }
             }
             break;
         case 'market_category':
             //it's product constant field
             //TODO verify it
             break;
         case 'name':
             if (!empty($sku_data['name']) && !empty($data['name']) && $sku_data['name'] != $data['name']) {
                 $value = sprintf('%s (%s)', $value, $sku_data['name']);
             }
             $value = preg_replace('/<br\\/?\\s*>/', "\n", $value);
             $value = preg_replace("/[\r\n]+/", "\n", $value);
             $value = strip_tags($value);
             $value = trim($value);
             if (mb_strlen($value) > 255) {
                 $value = mb_substr($value, 0, 252) . '...';
             }
             break;
         case 'description':
             $value = preg_replace('/<br\\/?\\s*>/', "\n", $value);
             $value = preg_replace("/[\r\n]+/", "\n", $value);
             $value = strip_tags($value);
             $value = trim($value);
             if (mb_strlen($value) > 512) {
                 $value = mb_substr($value, 0, 509) . '...';
             }
             break;
         case 'barcode':
             //может содержать несколько элементов
             $value = preg_replace('@\\D+@', '', $value);
             if (!in_array(strlen($value), array(8, 12, 13))) {
                 $value = null;
             }
             break;
         case 'sales_notes':
             $value = trim($value);
             if (mb_strlen($value) > 50) {
                 $value = mb_substr($value, 0, 50);
             }
             break;
         case 'typePrefix':
             $model = new shopTypeModel();
             if ($type = $model->getById($value)) {
                 $value = $type['name'];
             }
             break;
         case 'url':
             //max 512
             $value = preg_replace_callback('@([^\\[\\]a-zA-Z\\d_/-\\?=%&,\\.]+)@i', array(__CLASS__, 'rawurlencode'), $value);
             if ($this->data['utm']) {
                 $value .= (strpos($value, '?') ? '&' : '?') . $this->data['utm'];
             }
             $value = 'http://' . ifempty($this->data['base_url'], 'localhost') . $value;
             break;
         case 'oldprice':
             if (empty($value) || empty($this->data['export']['compare_price'])) {
                 $value = null;
                 break;
             }
         case 'price':
             if (!$currency_model) {
                 $currency_model = new shopCurrencyModel();
             }
             if ($sku_data) {
                 if (!in_array($data['currency'], $this->data['currency'])) {
                     $value = $currency_model->convert($value, $data['currency'], $this->data['primary_currency']);
                     $data['currency'] = $this->data['primary_currency'];
                 }
             } else {
                 if (!in_array($data['currency'], $this->data['currency'])) {
                     #value in default currency
                     if ($this->data['default_currency'] != $this->data['primary_currency']) {
                         $value = $currency_model->convert($value, $this->data['default_currency'], $this->data['primary_currency']);
                     }
                     $data['currency'] = $this->data['primary_currency'];
                 } elseif ($this->data['default_currency'] != $data['currency']) {
                     $value = $currency_model->convert($value, $this->data['default_currency'], $data['currency']);
                 }
             }
             break;
         case 'currencyId':
             if (!in_array($value, $this->data['currency'])) {
                 $value = $this->data['primary_currency'];
             }
             break;
         case 'rate':
             if (!in_array($value, array('CB', 'CBRF', 'NBU', 'NBK'))) {
                 $info['format'] = '%0.4f';
             }
             break;
         case 'available':
             if (!empty($sku_data) && isset($sku_data['available']) && empty($sku_data['available'])) {
                 $value = 'false';
             }
             if (is_object($value)) {
                 switch (get_class($value)) {
                     case 'shopBooleanValue':
                         /**
                          * @var $value shopBooleanValue
                          */
                         $value = $value->value ? 'true' : 'false';
                         break;
                 }
             }
             $value = ($value <= 0 || $value === 'false' || empty($value)) && $value !== null && $value !== 'true' ? 'false' : 'true';
             break;
         case 'store':
         case 'pickup':
         case 'delivery':
         case 'adult ':
             if (is_object($value)) {
                 switch (get_class($value)) {
                     case 'shopBooleanValue':
                         /**
                          * @var $value shopBooleanValue
                          */
                         $value = $value->value ? 'true' : 'false';
                         break;
                 }
             }
             $value = empty($value) || $value === 'false' ? 'false' : 'true';
             break;
         case 'picture':
             //max 512
             $values = array();
             $limit = 10;
             if (!empty($sku_data['image_id'])) {
                 $value = array(ifempty($value[$sku_data['image_id']]));
             }
             while (is_array($value) && ($image = array_shift($value)) && $limit--) {
                 if (!$size) {
                     $shop_config = wa('shop')->getConfig();
                     /**
                      * @var $shop_config shopConfig
                      */
                     $size = $shop_config->getImageSize('big');
                 }
                 $values[] = 'http://' . ifempty($this->data['base_url'], 'localhost') . shopImage::getUrl($image, $size);
             }
             $value = $values;
             break;
         case 'page_extent':
             $value = max(1, intval($value));
             break;
         case 'seller_warranty':
         case 'manufacturer_warranty':
         case 'expiry':
             /**
              * ISO 8601, например: P1Y2M10DT2H30M
              */
             $pattern = '@P((\\d+S)?(\\d+M)(\\d+D)?)?(T(\\d+H)?(\\d+M)(\\d+S)?)?@';
             $class = is_object($value) ? get_class($value) : false;
             switch ($class) {
                 case 'shopBooleanValue':
                     /**
                      * @var $value shopBooleanValue
                      */
                     $value = $value->value ? 'true' : 'false';
                     break;
                 case 'shopDimensionValue':
                     /**
                      * @var $value shopDimensionValue
                      */
                     $value = $value->convert('s', false);
                     /**
                      * @var $value int
                      */
                     if (empty($value)) {
                         $value = 'false';
                     } else {
                         $value = $this->formatCustom($value, 'ISO8601');
                     }
                     break;
                 default:
                     $value = (string) $value;
                     if (empty($value) || $value == 'false') {
                         $value = 'false';
                     } elseif (preg_match('@^\\d+$@', trim($value))) {
                         $value = $this->formatCustom(intval($value) * 3600 * 24, 'ISO8601');
                     } elseif (!preg_match($pattern, $value)) {
                         $value = 'true';
                     }
                     break;
             }
             break;
         case 'year':
             if (empty($value)) {
                 $value = null;
             }
             break;
         case 'ISBN':
             /**
              * @todo verify format
              * Код книги, если их несколько, то указываются через запятую.
              * Форматы ISBN и SBN проверяются на корректность. Валидация кодов происходит не только по длине,
              * также проверяется контрольная цифра (check-digit) – последняя цифра кода должна согласовываться
              * с остальными цифрами по определенной формуле. При разбиении ISBN на части при помощи дефиса
              * (например, 978-5-94878-004-7) код проверяется на соответствие дополнительным требованиям к
              * количеству цифр в каждой из частей.
              * Необязательный элемент.
              **/
             break;
         case 'recording_length':
             /**
              * Время звучания задается в формате mm.ss (минуты.секунды).
              **/
             if (is_object($value)) {
                 switch (get_class($value)) {
                     case 'shopDimensionValue':
                         /**
                          * @var $value shopDimensionValue
                          */
                         $value = $value->convert('s', false);
                         break;
                     default:
                         $value = (int) $value;
                         break;
                 }
             }
             $value = sprintf('%02d.%02d', floor($value / 60), $value % 60);
             break;
         case 'weight':
             /**
              * Элемент предназначен для указания веса товара. Вес указывается в килограммах с учетом упаковки.
              * Формат элемента: положительное число с точностью 0.001, разделитель целой и дробной части — точка.
              * При указании более высокой точности значение автоматически округляется следующим способом:
              * — если 4-ый знак после разделителя меньше 5, то 3-й знак сохраняется, а все последующие обнуляются;
              * — если 4-ый знак после разделителя больше или равен 5, то 3-й знак увеличивается на единицу, а все последующие обнуляются.
              **/
             if (is_object($value)) {
                 switch (get_class($value)) {
                     case 'shopDimensionValue':
                         /**
                          * @var $value shopDimensionValue
                          */
                         if ($value->type == 'weight') {
                             $value = $value->convert('kg', '%0.3f');
                         }
                         break;
                     default:
                         $value = floatval($value);
                         break;
                 }
             } else {
                 $value = floatval($value);
             }
             break;
         case 'dimensions':
             /**
              *
              * Элемент предназначен для указания габаритов товара (длина, ширина, высота) в упаковке. Размеры указываются в сантиметрах.
              * Формат элемента: три положительных числа с точностью 0.001, разделитель целой и дробной части — точка. Числа должны быть разделены символом «/» без пробелов.
              * При указании более высокой точности значение автоматически округляется следующим способом:
              * — если 4-ый знак после разделителя меньше 5, то 3-й знак сохраняется, а все последующие обнуляются;
              * — если 4-ый знак после разделителя больше или равен 5, то 3-й знак увеличивается на единицу, а все последующие обнуляются.
              **/
             /**
              * @todo use cm
              *
              */
             $parsed_value = array();
             $class = is_object($value) ? get_class($value) : false;
             switch ($class) {
                 case 'shopCompositeValue':
                     /**
                      * @var $value shopCompositeValue
                      */
                     for ($i = 0; $i < 3; $i++) {
                         $value_item = $value[$i];
                         $class_item = is_object($value_item) ? get_class($value_item) : false;
                         switch ($class_item) {
                             case 'shopDimensionValue':
                                 /**
                                  * @var $value_item shopDimensionValue
                                  */
                                 if ($value_item->type == '3d.length') {
                                     $parsed_value[] = $value_item->convert('cm', '%0.4f');
                                 } else {
                                     $parsed_value[] = sprintf('%0.4f', (string) $value_item);
                                 }
                                 break;
                             default:
                                 $parsed_value[] = sprintf('%0.4f', (string) $value_item);
                                 break;
                         }
                     }
                     break;
                 default:
                     $parsed_value = array_map('floatval', explode(':', preg_replace('@[^\\d\\.,]+@', ':', $value), 3));
                     break;
             }
             foreach ($parsed_value as &$p) {
                 $p = str_replace(',', '.', sprintf('%0.4f', $p));
                 unset($p);
             }
             $value = implode('/', $parsed_value);
             break;
         case 'age':
             /**
              * @todo
              * unit="year": 0, 6, 12, 16, 18
              * unit="month": 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
              */
             if (is_object($value)) {
                 switch (get_class($value)) {
                     case 'shopDimensionValue':
                         /**
                          * @var $value shopDimensionValue
                          */
                         if ($value->type == 'time') {
                             $value = $value->convert('month', false);
                         }
                         break;
                     default:
                         $value = intval($value);
                         break;
                 }
             } else {
                 /**
                  * @var $value shopDimensionValue
                  */
                 if (preg_match('@^(year|month)s?:(\\d+)$@', trim($value), $matches)) {
                     $value = array('unit' => $matches[1], 'value' => intval($matches[2]));
                 } else {
                     $value = intval($value);
                 }
             }
             if (!is_array($value)) {
                 if ($value > 12) {
                     $value = array('unit' => 'year', 'value' => floor($value / 12));
                 } else {
                     $value = array('unit' => 'month', 'value' => intval($value));
                 }
             }
             break;
         case 'country_of_origin':
             /**
              * @todo
              * @see http://partner.market.yandex.ru/pages/help/Countries.pdf
              */
             break;
         case 'local_delivery_cost':
             if ($value !== '') {
                 $value = max(0, floatval($value));
             }
             break;
         case 'days':
             $value = max(1, intval($value));
             break;
         case 'dataTour':
             /**
              * @todo
              * Даты заездов.
              * Необязательный элемент. Элемент <offer> может содержать несколько элементов <dataTour>.
              **/
             break;
         case 'hotel_stars':
             /**
              * @todo
              * Звезды отеля.
              * Необязательный элемент.
              **/
             break;
         case 'room':
             /**
              * @todo
              * Тип комнаты (SNG, DBL, ...).
              * Необязательный элемент.
              **/
             break;
         case 'meal':
             /**
              * @todo
              * Тип питания (All, HB, ...).
              * Необязательный элемент.
              **/
             break;
         case 'date':
             /**
              * @todo
              * Дата и время сеанса. Указываются в формате ISO 8601: YYYY-MM-DDThh:mm.
              **/
             break;
         case 'hall':
             /**
              * @todo
              * max 512
              * Ссылка на изображение с планом зала.
              **/
             //plan - property
             break;
         case 'param':
             $unit = null;
             $name = ifset($info['source_name'], '');
             if ($value instanceof shopDimensionValue) {
                 $unit = $value->unit_name;
                 $value = $value->format('%s');
             } elseif (is_array($value)) {
                 $_value = reset($value);
                 if ($_value instanceof shopDimensionValue) {
                     $unit = $_value->unit_name;
                     $values = array();
                     foreach ($value as $_value) {
                         /**
                          * @var shopDimensionValue $_value
                          */
                         $values[] = $_value->convert($unit, '%s');
                     }
                     $value = implode(', ', $values);
                 } else {
                     if (preg_match('@^(.+)\\s*\\(([^\\)]+)\\)\\s*$@', $name, $matches)) {
                         //feature name based unit
                         $unit = $matches[2];
                         $name = $matches[1];
                     }
                     $value = implode(', ', $value);
                 }
             } elseif (preg_match('@^(.+)\\s*\\(([^\\)]+)\\)\\s*$@', $name, $matches)) {
                 //feature name based unit
                 $unit = $matches[2];
                 $name = $matches[1];
             }
             $value = trim((string) $value);
             if (in_array($value, array(null, false, ''), true)) {
                 $value = null;
             } else {
                 $value = array('name' => $name, 'unit' => $unit, 'value' => trim((string) $value));
             }
             break;
     }
     $format = ifempty($info['format'], '%s');
     if (is_array($value)) {
         /**
          * @var $value array
          */
         reset($value);
         if (key($value) == 0) {
             foreach ($value as &$item) {
                 $item = str_replace('&nbsp;', ' ', $item);
                 $item = str_replace('&', '&amp;', $item);
                 $item = $this->sprintf($format, $item);
             }
             unset($item);
         }
         if (!in_array($field, array('email', 'picture', 'dataTour', 'additional', 'barcode', 'param', 'related_offer'))) {
             $value = implode(', ', $value);
         }
     } elseif ($value !== null) {
         /**
          * @var $value string
          */
         $value = str_replace('&nbsp;', ' ', $value);
         $value = str_replace('&', '&amp;', $value);
         $value = $this->sprintf($format, $value);
     }
     return $value;
 }
 public function rotate($src_path, $dst_path, $angle)
 {
     $image = new shopImage($src_path);
     return $image->rotate($angle)->save($dst_path);
 }
 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;
     }
 }
示例#18
0
 /**
  * Get aggregated data about placing products(skus) in stocks
  *
  * @see getProductStocks
  *
  * @param int|array $product_id
  * @param string $order
  * @return array
  */
 public function getProductStocksByProductId($product_id, $order = 'desc')
 {
     if (!$product_id) {
         return array();
     }
     $product_ids = (array) $product_id;
     $product_ids_str = implode(',', $product_ids);
     $order = $order == 'desc' || $order == 'DESC' ? 'DESC' : 'ASC';
     // necessary models
     $stock_model = new shopStockModel();
     $product_images_model = new shopProductImagesModel();
     // stock ids of items ordered by sort
     $stock_ids = array_keys($stock_model->getAll('id'));
     // get products
     $sql = "\n            SELECT id, name, count, image_id\n            FROM {$this->table}\n            WHERE id IN ( {$product_ids_str} )\n            ORDER BY count {$order}\n        ";
     $data = array();
     $image_ids = array();
     foreach ($this->query($sql) as $item) {
         $data[$item['id']] = array('id' => $item['id'], 'name' => $item['name'], 'url_crop_small' => null, 'count' => $item['count'], 'skus' => array(), 'stocks' => array());
         if ($item['image_id'] != null) {
             $image_ids[] = $item['image_id'];
         }
     }
     if (!$data) {
         return array();
     }
     $product_ids = array_keys($data);
     $product_ids_str = implode(',', $product_ids);
     $images = $product_images_model->getByField('id', $image_ids, 'product_id');
     $size = wa()->getConfig()->getImageSize('crop_small');
     // get for skus number of stocks in which it presents
     $sql = "\n            SELECT sk.id, COUNT(sk.id) num_of_stocks\n            FROM shop_product_skus sk\n            JOIN shop_product_stocks st ON sk.id = st.sku_id\n            WHERE sk.product_id IN ( {$product_ids_str} )\n            GROUP BY sk.id\n        ";
     $num_of_stocks = $this->query($sql)->fetchAll('id', true);
     // get info about skus and stocks
     $sql = "SELECT\n                    sk.product_id,\n                    sk.id AS sku_id,\n                    sk.name AS sku_name,\n                    sk.count,\n\n                    pst.stock_id,\n                    pst.count AS stock_count\n                FROM shop_product_skus sk\n                LEFT JOIN shop_product_stocks pst ON pst.sku_id = sk.id\n                WHERE sk.product_id IN ( {$product_ids_str} )\n                ORDER BY sk.product_id, sk.count {$order}, sk.id";
     $stocks_count = count($stock_ids);
     // temporary aggragating info about stocks
     $sku_stocks = array();
     if ($stocks_count) {
         $sku_stocks = array_fill(0, $stocks_count, array());
     }
     $sku_id = 0;
     $product_id = 0;
     $p_product = null;
     foreach ($this->query($sql) as $item) {
         // another product
         if ($product_id != $item['product_id']) {
             $product_id = $item['product_id'];
             $p_product =& $data[$product_id];
             if (isset($images[$product_id])) {
                 $p_product['url_crop_small'] = shopImage::getUrl($images[$product_id], $size);
             }
         }
         // another sku
         if ($sku_id != $item['sku_id']) {
             $sku_id = $item['sku_id'];
             $p_product['skus'][$sku_id] = array('id' => $sku_id, 'name' => $item['sku_name'], 'count' => $item['count'], 'num_of_stocks' => isset($num_of_stocks[$sku_id]) ? $num_of_stocks[$sku_id] : 0);
         }
         // aggregate info about stocks
         if ($item['stock_id'] !== null) {
             $sku_stocks[$item['stock_id']][$sku_id] = $item['stock_count'];
         }
     }
     // lay out stocks info
     if (!empty($sku_stocks)) {
         foreach ($data as &$product) {
             foreach ($stock_ids as $stock_id) {
                 foreach ($product['skus'] as $sku_id => $sku) {
                     $product['stocks'][$stock_id][$sku_id] = array('id' => $sku_id, 'name' => $sku['name'], 'count' => isset($sku_stocks[$stock_id][$sku_id]) ? $sku_stocks[$stock_id][$sku_id] : null, 'num_of_stocks' => $sku['num_of_stocks']);
                 }
             }
         }
         unset($product);
     }
     return $data;
 }
示例#19
0
文件: thumb.php 项目: Lazary/webasyst
        foreach ($size as &$s) {
            $s *= 2;
        }
        unset($s);
        $size = implode('x', $size);
    }
}
wa()->getStorage()->close();
$original_path = $protected_path . $file;
$thumb_path = $public_path . $request_file;
if ($file && file_exists($original_path) && !file_exists($thumb_path)) {
    $thumbs_dir = dirname($thumb_path);
    if (!file_exists($thumbs_dir)) {
        waFiles::create($thumbs_dir);
    }
    $max_size = $app_config->getOption('image_max_size');
    if ($max_size && $enable_2x) {
        $max_size *= 2;
    }
    $image = shopImage::generateThumb($original_path, $size, $max_size);
    if ($image) {
        $image->save($thumb_path, $app_config->getSaveQuality($enable_2x));
        clearstatcache();
    }
}
if ($file && file_exists($thumb_path)) {
    waFiles::readFile($thumb_path);
} else {
    header("HTTP/1.0 404 Not Found");
    exit;
}
 private function workupProducts(&$products = array(), $escape)
 {
     // Names of fields that must be converted to float values
     $float = array('min_price', 'max_price', 'total_sales', 'base_price_selectable', 'rating', 'price', 'compare_price');
     foreach ($products as &$p) {
         foreach ($float as $field) {
             if (isset($p[$field])) {
                 $p[$field] = (double) $p[$field];
             }
         }
         if ($this->is_frontend && $p['compare_price'] && $p['compare_price'] <= $p['price']) {
             $p['compare_price'] = 0;
         }
         // escape
         if ($escape) {
             $p['name'] = htmlspecialchars($p['name']);
             $p['url'] = htmlspecialchars($p['url']);
         }
     }
     unset($p);
     if (!empty($this->options['params'])) {
         $product_params_model = new shopProductParamsModel();
         $rows = $product_params_model->getByField('product_id', array_keys($products), true);
         foreach ($rows as $row) {
             $products[$row['product_id']]['params'][$row['name']] = $row['value'];
         }
     }
     if ($this->post_fields) {
         $ids = array_keys($products);
         foreach ($this->post_fields as $table => $fields) {
             if ($table == '_internal') {
                 if ($this->is_frontend && waRequest::param('url_type') == 2) {
                     $cat_ids = array();
                     foreach ($products as &$p) {
                         if (!empty($p['category_id'])) {
                             $cat_ids[] = $p['category_id'];
                         }
                     }
                     $cat_ids = array_unique($cat_ids);
                     if ($cat_ids) {
                         $categories = $this->getModel('category')->getById($cat_ids);
                         foreach ($products as &$p) {
                             if (!empty($p['category_id'])) {
                                 $p['category_url'] = $categories[$p['category_id']]['full_url'];
                             }
                         }
                     }
                 }
                 foreach ($fields as $i => $f) {
                     if ($f == 'images' || $f == 'image') {
                         if ($f == 'images') {
                             $product_images_model = new shopProductImagesModel();
                             $product_images = $product_images_model->getImages($ids, 'thumb', 'product_id');
                             foreach ($product_images as $product_id => $images) {
                                 $products[$product_id]['images'] = $images;
                             }
                         } elseif ($f == 'image') {
                             $thumb_size = wa('shop')->getConfig()->getImageSize('thumb');
                             $big_size = wa('shop')->getConfig()->getImageSize('big');
                             foreach ($products as &$p) {
                                 if ($p['image_id']) {
                                     $tmp = array('id' => $p['image_id'], 'product_id' => $p['id'], 'ext' => $p['ext']);
                                     $p['image']['thumb_url'] = shopImage::getUrl($tmp, $thumb_size, isset($this->options['absolute']) ? $this->options['absolute'] : false);
                                     $p['image']['big_url'] = shopImage::getUrl($tmp, $big_size, isset($this->options['absolute']) ? $this->options['absolute'] : false);
                                 }
                             }
                         }
                     } elseif ($f == 'frontend_url') {
                         foreach ($products as &$p) {
                             $route_params = array('product_url' => $p['url']);
                             if (isset($p['category_url'])) {
                                 $route_params['category_url'] = $p['category_url'];
                             } elseif (isset($this->info['hash']) && $this->info['hash'] == 'category') {
                                 if (isset($this->info['subcategories']) && $this->info['id'] != $p['category_id']) {
                                     if (isset($this->info['subcategories'][$p['category_id']])) {
                                         $route_params['category_url'] = $this->info['subcategories'][$p['category_id']]['full_url'];
                                     }
                                 } else {
                                     $route_params['category_url'] = $this->info['full_url'];
                                 }
                             }
                             $p['frontend_url'] = wa()->getRouteUrl('shop/frontend/product', $route_params);
                         }
                         unset($p);
                     }
                 }
             }
         }
     }
 }
示例#21
0
 public function productImgUrl($product, $size)
 {
     if (!$product['image_id']) {
         return '';
     }
     return shopImage::getUrl(array('product_id' => $product['id'], 'id' => $product['image_id'], 'ext' => $product['ext']), $size);
 }
示例#22
0
 /**
  * @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;
 }
示例#23
0
 private function workupItems(&$item, $sku_stocks)
 {
     $size = $this->getCropSize();
     if (empty($item['image_id'])) {
         $item['url_crop_small'] = null;
     } else {
         $item['url_crop_small'] = shopImage::getUrl(array('id' => $item['image_id'], 'product_id' => $item['id'], 'ext' => $item['ext']), $size);
     }
     // aggregated stocks count icon for product
     if (empty($item['fake'])) {
         $item['icon'] = shopHelper::getStockCountIcon($item['count'], null, true);
     }
     foreach ($item['skus'] as &$sku) {
         if (empty($sku['fake'])) {
             // detaled stocks count icon for sku
             if (empty($sku_stocks[$sku['id']])) {
                 $sku['icon'] = shopHelper::getStockCountIcon($sku['count'], null, true);
             } else {
                 $icons = array();
                 foreach ($sku_stocks[$sku['id']] as $stock_id => $stock) {
                     $icon =& $icons[$stock_id];
                     $icon = shopHelper::getStockCountIcon($stock['count'], $stock_id) . " ";
                     $icon .= $stock['count'] . " ";
                     $icon .= "<span class='small'>@" . htmlspecialchars($stock['name']) . "</span>";
                     unset($icon);
                 }
                 //$sku['icon'] = implode(', ', $icons);
                 $sku['icon'] = shopHelper::getStockCountIcon($sku['count'], null, true);
                 $sku['icons'] = $icons;
             }
         }
     }
     unset($sku);
 }
 private function workupList(&$data, $fields, $escape)
 {
     $extract_contact_info = false;
     foreach (explode(',', $fields) as $field) {
         if ($field == 'contact') {
             $contact_ids = array();
             foreach ($data as $item) {
                 if ($item['contact_id']) {
                     $contact_ids[] = $item['contact_id'];
                 }
             }
             $contact_ids = array_unique($contact_ids);
             $contacts = self::getAuthorInfo($contact_ids);
             foreach ($data as &$item) {
                 $author = array('name' => $item['name'], 'email' => $item['email'], 'site' => $item['site']);
                 $item['author'] = array_merge($author, isset($contacts[$item['contact_id']]) ? $contacts[$item['contact_id']] : array());
                 if ($escape) {
                     $item['author']['name'] = htmlspecialchars($item['author']['name']);
                 }
             }
             unset($item);
         }
         if ($field == 'is_new') {
             $this->checkForNew($data);
         }
         if ($field == 'product') {
             $product_ids = array();
             foreach ($data as $item) {
                 $product_ids[] = $item['product_id'];
             }
             $product_ids = array_unique($product_ids);
             $product_model = new shopProductModel();
             $products = $product_model->getByField('id', $product_ids, 'id');
             $image_size = wa()->getConfig()->getImageSize('crop_small');
             foreach ($data as &$item) {
                 if (isset($products[$item['product_id']])) {
                     $product = $products[$item['product_id']];
                     $item['product_name'] = $product['name'];
                     if ($product['image_id']) {
                         $item['product_url_crop_small'] = shopImage::getUrl(array('id' => $product['image_id'], 'product_id' => $product['id'], 'ext' => $product['ext']), $image_size);
                     } else {
                         $item['product_url_crop_small'] = null;
                     }
                 }
             }
         }
     }
     foreach ($data as &$item) {
         // recursive workuping
         if (!empty($item['comments'])) {
             $this->extendItems($item['comments'], $options);
         }
     }
     unset($item);
 }
示例#25
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;
 }