public function execute()
 {
     $feature_id = waRequest::post('feature_id');
     $feature_model = new shopFeatureModel();
     $feature = $feature_model->getById($feature_id);
     if (!$feature) {
         throw new waException(_w('Unknown feature'));
     }
     if (waRequest::post('subtype')) {
         $to = waRequest::post('subtype');
     } else {
         $to = waRequest::post('type');
     }
     $to = array('type' => $to, 'selectable' => (int) waRequest::post('selectable', 0, waRequest::TYPE_INT), 'multiple' => (int) waRequest::post('multiple', 0, waRequest::TYPE_INT));
     $result = shopFeatureValuesConverter::run($feature_id, $to);
     if (!$result) {
         $this->errors[] = _w('Feature type conversion is not allowed or failed');
     } else {
         if ($feature = $feature_model->getById($feature_id)) {
             $this->response = array($feature_id => &$feature);
             $feature['selectable'] = (int) $feature['selectable'];
             $feature['multiple'] = (int) $feature['multiple'];
             if ($feature['selectable']) {
                 $this->response = $feature_model->getValues($this->response, true);
                 $sort = 0;
                 foreach ($feature['values'] as $id => &$value) {
                     if (!is_object($value)) {
                         $value = array('id' => $id, 'value' => $value, 'sort' => $sort++);
                         unset($value);
                     } else {
                         if (method_exists($value, 'getRaw')) {
                             $value = $value->getRaw();
                         } else {
                             $value = array('id' => $id, 'value' => (string) $value, 'sort' => $sort++);
                         }
                     }
                 }
                 $feature['values'] = array_values($feature['values']);
             }
             shopFeatureModel::appendTypeNames($this->response);
             $type_features_model = new shopTypeFeaturesModel();
             $type_features_model->fillTypes($this->response);
             $feature['types'] = array_keys($feature['types']);
             sort($feature['types']);
         }
     }
 }
 public function execute()
 {
     if (!$this->getUser()->getRights('shop', 'settings')) {
         throw new waRightsException(_w('Access denied'));
     }
     $model = new shopFeatureModel();
     if (($id = waRequest::get('id', waRequest::TYPE_INT)) && ($feature = $model->getById($id))) {
         $feature['values'] = $model->getFeatureValues($feature);
         $this->view->assign('feature', $feature);
     } else {
         throw new waException(_w('Product feature not found'), 404);
     }
 }
 public function execute()
 {
     if (!$this->getRights('settings')) {
         throw new waAPIException('access_denied', 403);
     }
     $feature_model = new shopFeatureModel();
     if ($id = $this->post('id')) {
         $feature = $feature_model->getById($id);
     } elseif ($code = $this->post('code')) {
         $feature = $feature_model->getByCode($code);
     } else {
         throw new waAPIException('invalid_param', 'Required parameter is missing: id or code', 400);
     }
     if (!$feature) {
         throw new waAPIException('invalid_param', 'Feature not found', 404);
     }
     $this->response = (bool) $feature_model->delete($feature['id']);
 }
 public function execute()
 {
     $feature_model = new shopFeatureModel();
     if ($id = $this->get('id')) {
         $feature = $feature_model->getById($id);
     } elseif ($code = $this->get('code')) {
         $feature = $feature_model->getByCode($code);
     } else {
         throw new waAPIException('invalid_param', 'Required parameter is missing: id or code', 400);
     }
     if (!$feature) {
         throw new waAPIException('invalid_param', 'Feature not found', 404);
     }
     if ($feature['selectable']) {
         $feature['values'] = array_values($feature_model->getFeatureValues($feature));
         $feature['values']['_element'] = 'value';
     }
     $this->response = $feature;
 }
 private function bindToProducts($feature, $values)
 {
     if (is_array($feature)) {
         $feature_id = $feature['id'];
     } else {
         $feature_id = (int) $feature;
         $feature = $this->feature_model->getById($feature_id);
     }
     $is_multidimensional = $this->isMultidimensional($feature);
     // bind products with values of new feature
     foreach ($values as $v) {
         if ($feature['type'] !== 'boolean') {
             if (isset($v['original_id'])) {
                 $new_id = $v['original_id'];
             } else {
                 $new_id = $v['id'];
             }
             if (isset($v['original_id'])) {
                 $old_id = -$v['id'];
             } else {
                 $old_id = -$v['insert_id'];
             }
         } else {
             $old_id = -$v['id'];
             $new_id = $v['value'];
         }
         if (!$is_multidimensional) {
             $key = array('feature_id' => $feature_id, 'feature_value_id' => $old_id);
             $value = array('feature_value_id' => $new_id);
         } else {
             $key = array('feature_id' => $v['feature_id'], 'feature_value_id' => $old_id);
             $value = array('feature_value_id' => $new_id);
         }
         $this->product_features_model->updateByField($key, $value);
         $key['value_id'] = $key['feature_value_id'];
         unset($key['feature_value_id']);
         $value['value_id'] = $value['feature_value_id'];
         unset($value['feature_value_id']);
         $this->product_features_selectable_model->updateByField($key, $value);
     }
 }
 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 int $id
  * @param array $data
  * @param bool $correct
  * @param shopProduct $product
  * @return array
  */
 protected function updateSku($id = 0, $data, $correct = true, shopProduct $product = null)
 {
     /**
      * @var shopProductStocksModel $stocks_model
      */
     static $stocks_model;
     /**
      * @var bool $multi_stock
      */
     static $multi_stock = null;
     /**
      * @var shopFeatureModel $feature_model
      */
     static $feature_model;
     /**
      * @var shopProductFeaturesModel $product_features_model
      */
     static $product_features_model;
     if (isset($data['price'])) {
         $data['price'] = $this->castValue('double', $data['price']);
     }
     if (isset($data['purchase_price'])) {
         $data['purchase_price'] = $this->castValue('double', $data['purchase_price']);
     }
     if (isset($data['compare_price'])) {
         $data['compare_price'] = $this->castValue('double', $data['compare_price']);
     }
     if ($id > 0) {
         if ($product && (!isset($data['virtual']) || !empty($data['virtual']))) {
             #check changes for virtual SKU
             $virtual_sku_defaults = array('price' => $product->base_price_selectable, 'purchase_price' => $product->purchase_price_selectable, 'compare_price' => $product->compare_price_selectable, 'count' => 0);
             $virtual = null;
             foreach ($virtual_sku_defaults as $field => $default) {
                 if (isset($data[$field])) {
                     $value = $data[$field];
                     if (is_array($value)) {
                         $value = max($value);
                     }
                     if ($value != $default) {
                         if ($virtual === null) {
                             $virtual = isset($product->skus[$id]) && !empty($product->skus[$id]['virtual']);
                         }
                         if ($virtual) {
                             $data['virtual'] = 0;
                             $virtual = false;
                         }
                         if (!$virtual) {
                             break;
                         }
                     }
                 }
             }
         }
         if (empty($data['eproduct']) && !empty($data['file_name'])) {
             $file_path = shopProduct::getPath($data['product_id'], "sku_file/{$id}." . pathinfo($data['file_name'], PATHINFO_EXTENSION));
             waFiles::delete($file_path);
             $data['file_name'] = '';
             $data['file_description'] = '';
         } elseif (isset($data['file_name'])) {
             unset($data['file_name']);
         }
         $this->updateById($id, $data);
     } else {
         if (!isset($data['sku'])) {
             $data['sku'] = '';
         }
         $id = $this->insert($data);
     }
     $data['id'] = $id;
     $sku_count = false;
     // if stocking for this sku
     if (isset($data['stock']) && count($data['stock'])) {
         if ($multi_stock === null) {
             $stock_model = new shopStockModel();
             $stocks = $stock_model->getAll($stock_model->getTableId());
             $multi_stock = $stocks ? array_keys($stocks) : false;
         }
         // not multistocking
         if (!$multi_stock || isset($data['stock'][0])) {
             $sku_count = self::castStock($data['stock'][0]);
             unset($data['stock']);
             $this->logCount($data['product_id'], $id, $sku_count);
             // multistocking
         } else {
             $sku_count = 0;
             $missed = array_combine($multi_stock, $multi_stock);
             if (!$stocks_model) {
                 $stocks_model = new shopProductStocksModel();
             }
             // need for track transition from aggregating mode to multistocking mode
             $has_any_stocks = $stocks_model->hasAnyStocks($id);
             if (!$has_any_stocks) {
                 $this->writeOffCount($data['product_id'], $id);
             }
             foreach ($data['stock'] as $stock_id => $count) {
                 if ($stock_id > 0 && isset($missed[$stock_id])) {
                     unset($missed[$stock_id]);
                     $field = array('sku_id' => $id, 'stock_id' => $stock_id, 'product_id' => $data['product_id']);
                     $count = self::castStock($count);
                     $stock = array('count' => $count);
                     if ($count === null) {
                         $sku_count = null;
                     } else {
                         // Once turned into NULL value is not changed
                         if ($sku_count !== null) {
                             $sku_count += $count;
                         }
                     }
                     // there is taking into account stocks log inside this method
                     $stocks_model->set(array_merge($field, $stock));
                     $data['stock'][$stock_id] = $count;
                 }
             }
             //get stock_count for missed stocks
             if ($sku_count !== null && !empty($missed)) {
                 $search = array('stock_id' => $missed, 'sku_id' => $id, 'product_id' => $data['product_id']);
                 foreach ($stocks_model->getByField($search, 'stock_id') as $stock_id => $row) {
                     $count = $row['count'];
                     $data['stock'][$stock_id] = $count;
                     if ($count === null) {
                         $sku_count = null;
                     } else {
                         // Once turned into NULL value is not changed
                         if ($sku_count !== null) {
                             $sku_count += $count;
                         }
                     }
                 }
             }
         }
     }
     if ($sku_count !== false) {
         $data['count'] = $sku_count;
         $this->updateById($id, array('count' => $sku_count));
     }
     if (isset($data['features'])) {
         if (!$feature_model) {
             $feature_model = new shopFeatureModel();
         }
         if (!$product_features_model) {
             $product_features_model = new shopProductFeaturesModel();
         }
         $features = $data['features'];
         $data['features'] = array();
         $skip_values = array('', false, null);
         foreach ($features as $code => $value) {
             if ($feature = $feature_model->getByField('code', $code)) {
                 $model = shopFeatureModel::getValuesModel($feature['type']);
                 $field = array('product_id' => $data['product_id'], 'sku_id' => $id, 'feature_id' => $feature['id']);
                 $product_features_model->deleteByField($field);
                 if (is_array($value)) {
                     if (!empty($value['id'])) {
                         $field['feature_value_id'] = $value['id'];
                     } elseif (isset($value['value']) && !in_array($value['value'], $skip_values, true)) {
                         $field['feature_value_id'] = $model->getId($feature['id'], $code == 'weight' ? $value : $value['value'], $feature['type']);
                     }
                 } elseif (!in_array($value, $skip_values, true)) {
                     $field['feature_value_id'] = $model->getId($feature['id'], $value, $feature['type']);
                     $value = array('value' => $value, 'id' => $field['feature_value_id']);
                 }
                 if (!empty($field['feature_value_id'])) {
                     $product_features_model->insert($field);
                     $data['features'][$code] = $value;
                 }
             } elseif (is_numeric($code) && is_numeric($value)) {
                 if ($feature = $feature_model->getById($code)) {
                     $field = array('product_id' => $data['product_id'], 'sku_id' => $id, 'feature_id' => $code);
                     $product_features_model->deleteByField($field);
                     if (empty($value)) {
                         continue;
                     }
                     $field['feature_value_id'] = $value;
                     $product_features_model->insert($field);
                     $data['features'][$feature['code']] = $feature_model->getValuesModel($feature['type'])->getFeatureValue($value);
                 }
             }
         }
     }
     if ($correct) {
         $product_model = new shopProductModel();
         $product_model->correct($data['product_id']);
     }
     return $data;
 }
 /**
  * @param shopProduct $product
  * @return array|bool
  * @example <pre>
  * array(
  *  'feature_1'=>array(
  *      'selected'=>int,
  *      'values'=>array(
  *         %value_id%=> array(
  *              'value_id'=>int,
  *              'value'=>string,
  *              'selected'=>bool,
  *          ),
  *      ),
  *  ),
  * )
  * </pre>
  */
 public function getData(shopProduct $product)
 {
     $feature_model = new shopFeatureModel();
     if ($product->sku_type == shopProductModel::SKU_TYPE_SELECTABLE) {
         $selected = $this->getByProduct($product->id);
         $env = wa()->getEnv();
         if ($env == 'backend') {
             $features = $feature_model->getMultipleSelectableFeaturesByType($product->type_id);
         } else {
             $features = $feature_model->getById(array_keys($selected));
             if ($product->type_id) {
                 $types = array($product->type_id => true);
                 $type_features_model = new shopTypeFeaturesModel();
                 $type_features_model->fillTypes($features, $types);
                 foreach ($features as &$feature) {
                     unset($feature['types']);
                     $feature['sort'] = ifset($feature['sort'][$product->type_id]);
                     unset($feature);
                 }
                 uasort($features, create_function('$a,$b', 'return max(-1,min(1,$a["sort"]-$b["sort"]));'));
             }
         }
         // attach values
         $features = $feature_model->getValues($features);
         foreach ($features as &$f) {
             $count = 0;
             foreach ($f['values'] as $id => &$v) {
                 $is_selected = isset($selected[$f['id']][$id]);
                 if ($env == 'backend') {
                     if (is_object($v)) {
                         $v['selected'] = $is_selected;
                     } else {
                         $v = array('value' => htmlentities((string) $v, ENT_NOQUOTES, 'utf-8'), 'selected' => $is_selected);
                     }
                     unset($v);
                     if ($is_selected) {
                         $count += 1;
                     }
                 } elseif (!$is_selected) {
                     unset($f['values'][$id]);
                 }
             }
             $f['selected'] = $count;
             unset($f);
         }
     } else {
         if (wa()->getEnv() == 'backend') {
             $features = $feature_model->isTypeMultipleSelectable($product->type_id);
         } else {
             $features = array();
         }
     }
     return $features;
     //Frontend
     $data = $feature_model->getValues($data);
     foreach ($data as $code => $f) {
         foreach ($f['values'] as $id => $v) {
             if (!isset($v_id, $selected[$code][$id])) {
                 unset($data[$code]['values'][$id]);
             }
         }
     }
     return $data;
 }
 public function execute()
 {
     $category = $this->getCategory();
     $this->addCanonical();
     // breadcrumbs
     $root_category_id = $category['id'];
     if ($category['parent_id']) {
         $breadcrumbs = array();
         $path = array_reverse($this->getModel()->getPath($category['id']));
         $root_category = reset($path);
         $root_category_id = $root_category['id'];
         foreach ($path as $row) {
             $breadcrumbs[] = array('url' => wa()->getRouteUrl('/frontend/category', array('category_url' => waRequest::param('url_type') == 1 ? $row['url'] : $row['full_url'])), 'name' => $row['name']);
         }
         if ($breadcrumbs) {
             $this->view->assign('breadcrumbs', $breadcrumbs);
         }
     }
     $this->view->assign('root_category_id', $root_category_id);
     // sort
     if ($category['type'] == shopCategoryModel::TYPE_DYNAMIC && !$category['sort_products']) {
         $category['sort_products'] = 'create_datetime DESC';
     }
     if ($category['sort_products'] && !waRequest::get('sort')) {
         $sort = explode(' ', $category['sort_products']);
         $this->view->assign('active_sort', $sort[0] == 'count' ? 'stock' : $sort[0]);
     } elseif (!$category['sort_products'] && !waRequest::get('sort')) {
         $this->view->assign('active_sort', '');
     }
     $this->view->assign('category', $category);
     // products
     $collection = new shopProductsCollection('category/' . $category['id']);
     // filters
     if ($category['filter']) {
         $filter_ids = explode(',', $category['filter']);
         $feature_model = new shopFeatureModel();
         $features = $feature_model->getById(array_filter($filter_ids, 'is_numeric'));
         if ($features) {
             $features = $feature_model->getValues($features);
         }
         $category_value_ids = $collection->getFeatureValueIds();
         $filters = array();
         foreach ($filter_ids as $fid) {
             if ($fid == 'price') {
                 $range = $collection->getPriceRange();
                 if ($range['min'] != $range['max']) {
                     $filters['price'] = array('min' => shop_currency($range['min'], null, null, false), 'max' => shop_currency($range['max'], null, null, false));
                 }
             } elseif (isset($features[$fid]) && isset($category_value_ids[$fid])) {
                 $filters[$fid] = $features[$fid];
                 $min = $max = $unit = null;
                 foreach ($filters[$fid]['values'] as $v_id => $v) {
                     if (!in_array($v_id, $category_value_ids[$fid])) {
                         unset($filters[$fid]['values'][$v_id]);
                     } else {
                         if ($v instanceof shopRangeValue) {
                             $begin = $this->getFeatureValue($v->begin);
                             if ($min === null || $begin < $min) {
                                 $min = $begin;
                             }
                             $end = $this->getFeatureValue($v->end);
                             if ($max === null || $end > $max) {
                                 $max = $end;
                                 if ($v->end instanceof shopDimensionValue) {
                                     $unit = $v->end->unit;
                                 }
                             }
                         } else {
                             $tmp_v = $this->getFeatureValue($v);
                             if ($min === null || $tmp_v < $min) {
                                 $min = $tmp_v;
                             }
                             if ($max === null || $tmp_v > $max) {
                                 $max = $tmp_v;
                                 if ($v instanceof shopDimensionValue) {
                                     $unit = $v->unit;
                                 }
                             }
                         }
                     }
                 }
                 if (!$filters[$fid]['selectable'] && ($filters[$fid]['type'] == 'double' || substr($filters[$fid]['type'], 0, 6) == 'range.' || substr($filters[$fid]['type'], 0, 10) == 'dimension.')) {
                     if ($min == $max) {
                         unset($filters[$fid]);
                     } else {
                         $type = preg_replace('/^[^\\.]*\\./', '', $filters[$fid]['type']);
                         if ($type != 'double') {
                             $filters[$fid]['base_unit'] = shopDimension::getBaseUnit($type);
                             $filters[$fid]['unit'] = shopDimension::getUnit($type, $unit);
                             if ($filters[$fid]['base_unit']['value'] != $filters[$fid]['unit']['value']) {
                                 $dimension = shopDimension::getInstance();
                                 $min = $dimension->convert($min, $type, $filters[$fid]['unit']['value']);
                                 $max = $dimension->convert($max, $type, $filters[$fid]['unit']['value']);
                             }
                         }
                         $filters[$fid]['min'] = $min;
                         $filters[$fid]['max'] = $max;
                     }
                 }
             }
         }
         $this->view->assign('filters', $filters);
         $this->setCollection($collection);
         // fix prices
         $products = $this->view->getVars('products');
         $product_ids = array();
         foreach ($products as $p_id => $p) {
             if ($p['sku_count'] > 1) {
                 $product_ids[] = $p_id;
             }
         }
         if ($product_ids) {
             $min_price = $max_price = null;
             $tmp = array();
             foreach ($filters as $fid => $f) {
                 if ($fid == 'price') {
                     $min_price = waRequest::get('price_min');
                     if (!empty($min_price)) {
                         $min_price = (double) $min_price;
                     } else {
                         $min_price = null;
                     }
                     $max_price = waRequest::get('price_max');
                     if (!empty($max_price)) {
                         $max_price = (double) $max_price;
                     } else {
                         $max_price = null;
                     }
                 } else {
                     $fvalues = waRequest::get($f['code']);
                     if ($fvalues && !isset($fvalues['min']) && !isset($fvalues['max'])) {
                         $tmp[$fid] = $fvalues;
                     }
                 }
             }
             $product_skus = array();
             if ($tmp) {
                 $pf_model = new shopProductFeaturesModel();
                 $product_skus = $pf_model->getSkusByFeatures($product_ids, $tmp);
             } elseif ($min_price || $max_price) {
                 $ps_model = new shopProductSkusModel();
                 $rows = $ps_model->getByField('product_id', $product_ids, true);
                 foreach ($rows as $row) {
                     $product_skus[$row['product_id']][] = $row;
                 }
             }
             $default_currency = $this->getConfig()->getCurrency(true);
             if ($product_skus) {
                 foreach ($product_skus as $product_id => $skus) {
                     $currency = $products[$product_id]['currency'];
                     usort($skus, array($this, 'sortSkus'));
                     $k = 0;
                     if ($min_price || $max_price) {
                         foreach ($skus as $i => $sku) {
                             if ($min_price) {
                                 $tmp_price = shop_currency($min_price, true, $currency, false);
                                 if ($sku['price'] < $tmp_price) {
                                     continue;
                                 }
                             }
                             if ($max_price) {
                                 $tmp_price = shop_currency($max_price, true, $currency, false);
                                 if ($sku['price'] > $tmp_price) {
                                     continue;
                                 }
                             }
                             $k = $i;
                             break;
                         }
                     }
                     $sku = $skus[$k];
                     if ($products[$product_id]['sku_id'] != $sku['id']) {
                         $products[$product_id]['sku_id'] = $sku['id'];
                         $products[$product_id]['frontend_url'] .= '?sku=' . $sku['id'];
                         $products[$product_id]['price'] = shop_currency($sku['price'], $currency, $default_currency, false);
                         $products[$product_id]['compare_price'] = shop_currency($sku['compare_price'], $currency, $default_currency, false);
                     }
                 }
                 $this->view->assign('products', $products);
             }
         }
     } else {
         $this->setCollection($collection);
     }
     //отображение дополнительных размеров
     $products = $this->view->getVars('products');
     $product_features_model = new shopProductFeaturesSelectableModel();
     foreach ($products as &$p) {
         $sku_features = $product_features_model->getByProduct($p['id']);
         $sizes = $sku_features[3];
         if (!$sizes) {
             $p['sizes'] = array();
             continue;
         }
         $pf_names = $product_features_model->query("SELECT `id`, `value` FROM shop_feature_values_varchar where `id` IN (" . implode(',', $sizes) . ') ORDER BY `sort`;')->fetchAll();
         foreach ($pf_names as $key => $val) {
             $sizes[$val['id']] = $val['value'];
         }
         $p['sizes'] = $pf_names;
     }
     //отображение всех картинок
     foreach ($products as &$p) {
         $images_full = shopViewHelper::images($p['id']);
         if (isset($images_full[$p['id']])) {
             $p['image_ids'] = array_keys($images_full[$p['id']]);
         }
     }
     $this->view->assign('products', $products);
     // set meta
     $title = $category['meta_title'] ? $category['meta_title'] : $category['name'];
     wa()->getResponse()->setTitle($title);
     wa()->getResponse()->setMeta('keywords', $category['meta_keywords']);
     wa()->getResponse()->setMeta('description', $category['meta_description']);
     /**
      * @event frontend_category
      * @return array[string]string $return[%plugin_id%] html output for category
      */
     $this->view->assign('frontend_category', wa()->event('frontend_category', $category));
     $this->setThemeTemplate('category.html');
 }