/**
  * @return mixed
  * @throws waException
  */
 protected function getCategory()
 {
     $category_model = $this->getModel();
     $url_field = waRequest::param('url_type') == 1 ? 'url' : 'full_url';
     if (waRequest::param('category_id')) {
         $category = $category_model->getById(waRequest::param('category_id'));
         if ($category) {
             $category_url = wa()->getRouteUrl('/frontend/category', array('category_url' => $category[$url_field]));
             if (urldecode(wa()->getConfig()->getRequestUrl(false, true)) !== $category_url) {
                 $q = waRequest::server('QUERY_STRING');
                 $this->redirect($category_url . ($q ? '?' . $q : ''), 301);
             }
         }
     } else {
         $category = $category_model->getByField($url_field, waRequest::param('category_url'));
         if ($category && $category[$url_field] !== urldecode(waRequest::param('category_url'))) {
             $q = waRequest::server('QUERY_STRING');
             $this->redirect(wa()->getRouteUrl('/frontend/category', array('category_url' => $category[$url_field])) . ($q ? '?' . $q : ''), 301);
         }
     }
     $route = wa()->getRouting()->getDomain(null, true) . '/' . wa()->getRouting()->getRoute('url');
     if ($category) {
         $category_routes_model = new shopCategoryRoutesModel();
         $routes = $category_routes_model->getRoutes($category['id']);
     }
     if (!$category || $routes && !in_array($route, $routes)) {
         throw new waException('Category not found', 404);
     }
     $category['subcategories'] = $category_model->getSubcategories($category, $route);
     $category_url = wa()->getRouteUrl('shop/frontend/category', array('category_url' => '%CATEGORY_URL%'));
     foreach ($category['subcategories'] as &$sc) {
         $sc['url'] = str_replace('%CATEGORY_URL%', waRequest::param('url_type') == 1 ? $sc['url'] : $sc['full_url'], $category_url);
     }
     unset($sc);
     // params
     $category_params_model = new shopCategoryParamsModel();
     $category['params'] = $category_params_model->get($category['id']);
     // smarty description
     if ($this->getConfig()->getOption('can_use_smarty') && $category['description']) {
         $category['description'] = wa()->getView()->fetch('string:' . $category['description']);
     }
     return $category;
 }
 private function saveCategorySettings($id, &$data)
 {
     /**
      * @var shopCategoryModel
      */
     $model = $this->getModel('category');
     if (!$id) {
         if (empty($data['url'])) {
             $url = shopHelper::transliterate($data['name'], false);
             if ($url) {
                 $data['url'] = $model->suggestUniqueUrl($url);
             }
         }
         if (empty($data['name'])) {
             $data['name'] = _w('(no-name)');
         }
         $id = $model->add($data, $data['parent_id']);
         $this->logAction('category_add', $id);
     } else {
         $category = $model->getById($id);
         if (!$this->categorySettingsValidate($category, $data)) {
             return false;
         }
         if (empty($data['name'])) {
             $data['name'] = $category['name'];
         }
         if (empty($data['url'])) {
             $data['url'] = $model->suggestUniqueUrl(shopHelper::transliterate($data['name']), $id, $category['parent_id']);
         }
         unset($data['parent_id']);
         $data['edit_datetime'] = date('Y-m-d H:i:s');
         $model->update($id, $data);
         $this->logAction('category_edit', $id);
     }
     if ($id) {
         if (waRequest::post('enable_sorting')) {
             $data['params']['enable_sorting'] = 1;
         }
         $category_params_model = new shopCategoryParamsModel();
         $category_params_model->set($id, !empty($data['params']) ? $data['params'] : null);
         $category_routes_model = new shopCategoryRoutesModel();
         $category_routes_model->setRoutes($id, isset($data['routes']) ? $data['routes'] : array());
         $data['id'] = $id;
         /**
          * @event category_save
          * @param array $category
          * @return void
          */
         wa()->event('category_save', $data);
     }
     return $id;
 }
 /**
  * @param $current_stage
  * @param $count
  * @param $processed
  *
  * @usedby shopYandexmarketPluginRunController::step()
  */
 private function stepProduct(&$current_stage, &$count, &$processed)
 {
     static $products;
     static $sku_model;
     static $categories;
     if (!$products) {
         $products = $this->getCollection()->getProducts($this->getProductFields(), $current_stage, self::PRODUCT_PER_REQUEST, false);
         if (!$products) {
             $current_stage = $count['product'];
         } elseif (!empty($this->data['export']['sku'])) {
             if (empty($sku_model)) {
                 $sku_model = new shopProductSkusModel();
             }
             $skus = $sku_model->getDataByProductId(array_keys($products));
             foreach ($skus as $sku_id => $sku) {
                 if (isset($products[$sku['product_id']])) {
                     if (!isset($products[$sku['product_id']]['skus'])) {
                         $products[$sku['product_id']]['skus'] = array();
                     }
                     $products[$sku['product_id']]['skus'][$sku_id] = $sku;
                     if (count($products[$sku['product_id']]['skus']) > 1) {
                         $group = false;
                         switch (ifset($this->data['export']['sku_group'])) {
                             case 'all':
                                 $group = $sku['product_id'];
                                 break;
                             case 'category':
                                 // user primary product's category property
                                 if (!is_array($categories)) {
                                     $category_params_model = new shopCategoryParamsModel();
                                     $categories = $category_params_model->getByField(array('name' => 'yandexmarket_group_skus', 'value' => 1), 'category_id');
                                     if ($categories) {
                                         $categories = array_fill_keys(array_keys($categories), true);
                                     }
                                 }
                                 if (isset($categories[$products[$sku['product_id']]['category_id']])) {
                                     $group = $sku['product_id'];
                                 }
                                 break;
                             case 'auto':
                                 $group = 'auto';
                                 //use product property yandex_category for it
                                 break;
                             default:
                                 break;
                         }
                         if ($group) {
                             $products[$sku['product_id']]['_group_id'] = $group;
                         }
                     }
                 }
             }
         }
         $params = array('products' => &$products, 'type' => 'YML');
         wa('shop')->event('products_export', $params);
     }
     $check_stock = !empty($this->data['export']['zero_stock']) || !empty($this->data['app_settings']['ignore_stock_count']);
     $chunk = 100;
     while (--$chunk >= 0 && ($product = reset($products))) {
         $check_type = empty($this->data['type_id']) || in_array($product['type_id'], $this->data['type_id']);
         $check_price = $product['price'] >= 0.5;
         $check_category = !empty($product['category_id']) && isset($this->data['categories'][$product['category_id']]);
         if ($check_category && $product['category_id'] != $this->data['categories'][$product['category_id']]) {
             // remap product category
             $product['category_id'] = $this->data['categories'][$product['category_id']];
         }
         if (false && $check_type && $check_price && !$check_category) {
             //debug option
             $this->error("Product #%d [%s] skipped because it's category %s is not available", $product['id'], $product['name'], var_export(ifset($product['category_id']), true));
         }
         if ($check_type && $check_price && $check_category) {
             $type = ifempty($this->data['types'][$product['type_id']], 'simple');
             if (!empty($this->data['export']['sku'])) {
                 $skus = $product['skus'];
                 unset($product['skus']);
                 foreach ($skus as $sku) {
                     $check_sku_price = $sku['price'] >= 0.5;
                     if ($check_sku_price && ($check_stock || $sku['count'] === null || $sku['count'] > 0)) {
                         if (count($skus) == 1) {
                             $product['price'] = $sku['price'];
                             $product['file_name'] = $sku['file_name'];
                             $product['sku'] = $sku['sku'];
                             $increment = false;
                         } else {
                             $increment = true;
                         }
                         $this->addOffer($product, $type, count($skus) > 1 ? $sku : null);
                         ++$processed;
                         if ($increment) {
                             ++$count['product'];
                         }
                     }
                 }
             } else {
                 if ($check_stock || $product['count'] === null || $product['count'] > 0) {
                     $this->addOffer($product, $type);
                     ++$processed;
                 }
             }
         }
         array_shift($products);
         ++$current_stage;
     }
 }
 private function getCategorySettings($id)
 {
     $category_model = new shopCategoryModel();
     $category_params_model = new shopCategoryParamsModel();
     $settings = $category_model->getById($id);
     if (!$settings) {
         return array();
     }
     /**
      * @event backend_category_dialog
      * @param array $category
      * @return array[string][string] $return[%plugin_id%] html output for dialog
      */
     $this->view->assign('event_dialog', wa()->event('backend_category_dialog', $settings));
     $category_routes_model = new shopCategoryRoutesModel();
     $settings['routes'] = $category_routes_model->getRoutes($id);
     $settings['frontend_urls'] = array();
     foreach ($category_model->getFrontendUrls($id) as $frontend_url) {
         $pos = strrpos($frontend_url, $settings['url']);
         $settings['frontend_urls'][] = array('url' => $frontend_url, 'base' => $pos !== false ? rtrim(substr($frontend_url, 0, $pos), '/') . '/' : '');
     }
     $settings['params'] = $category_params_model->get($id);
     if (isset($settings['params']['enable_sorting'])) {
         $settings['enable_sorting'] = 1;
         unset($settings['params']['enable_sorting']);
     } else {
         $settings['enable_sorting'] = 0;
     }
     $feature_model = new shopFeatureModel();
     $selectable_and_boolean_features = $feature_model->select('*')->where("(selectable=1 OR type='boolean' OR type='double' OR type LIKE 'dimension\\.%' OR type LIKE 'range\\.%') AND parent_id IS NULL")->fetchAll('id');
     if ($settings['type'] == shopCategoryModel::TYPE_DYNAMIC) {
         if ($settings['conditions']) {
             $settings['conditions'] = shopProductsCollection::parseConditions($settings['conditions']);
         } else {
             $settings['conditions'] = array();
         }
         $tag_model = new shopTagModel();
         $cloud = $tag_model->getCloud('name');
         if (!empty($settings['conditions']['tag'][1])) {
             foreach ($settings['conditions']['tag'][1] as $tag_name) {
                 $cloud[$tag_name]['checked'] = true;
             }
         }
         $settings['cloud'] = $cloud;
         // extract conditions for features
         foreach ($settings['conditions'] as $name => $value) {
             if (substr($name, -9) === '.value_id') {
                 unset($settings['conditions'][$name]);
                 $settings['conditions']['feature'][substr($name, 0, -9)] = $value;
             }
         }
         $settings['custom_conditions'] = $this->extractCustomConditions($settings['conditions']);
         $settings['features'] = $selectable_and_boolean_features;
         $settings['features'] = $feature_model->getValues($settings['features']);
     }
     $filter = $settings['filter'] !== null ? explode(',', $settings['filter']) : null;
     $feature_filter = array();
     $features['price'] = array('id' => 'price', 'name' => 'Price');
     $features += $selectable_and_boolean_features;
     if (!empty($filter)) {
         foreach ($filter as $feature_id) {
             $feature_id = trim($feature_id);
             if (isset($features[$feature_id])) {
                 $feature_filter[$feature_id] = $features[$feature_id];
                 $feature_filter[$feature_id]['checked'] = true;
                 unset($features[$feature_id]);
             }
         }
     }
     $settings['allow_filter'] = (bool) $filter;
     $settings['filter'] = $feature_filter + $features;
     return $settings;
 }
 /**
  * @param array $category
  */
 public function categorySaveHandler($category)
 {
     if (!empty($category['id'])) {
         $category_id = $category['id'];
         $data = waRequest::post($this->id, array());
         $category_params_model = new shopCategoryParamsModel();
         $name = 'yandexmarket_group_skus';
         $value = ifempty($data['group_skus']) ? 1 : 0;
         if ($value) {
             $category_params_model->insert(compact('category_id', 'name', 'value'), 1);
         } else {
             $category_params_model->deleteByField(compact('category_id', 'name'));
         }
     }
 }
 public function categories($id = 0, $depth = null, $tree = false, $params = false, $route = null)
 {
     if ($id === true) {
         $id = 0;
         $tree = true;
     }
     $category_model = new shopCategoryModel();
     if ($route && !is_array($route)) {
         $route = explode('/', $route, 2);
         $route = $this->getRoute($route[0], isset($route[1]) ? $route[1] : null);
     }
     if (!$route) {
         $route = $this->getRoute();
     }
     if (!$route) {
         return array();
     }
     $cats = $category_model->getTree($id, $depth, false, $route['domain'] . '/' . $route['url']);
     $url = $this->wa->getRouteUrl('shop/frontend/category', array('category_url' => '%CATEGORY_URL%'), false, $route['domain'], $route['url']);
     $hidden = array();
     foreach ($cats as $c_id => $c) {
         if ($c['parent_id'] && $c['id'] != $id && !isset($cats[$c['parent_id']])) {
             unset($cats[$c_id]);
         } else {
             $cats[$c_id]['url'] = str_replace('%CATEGORY_URL%', isset($route['url_type']) && $route['url_type'] == 1 ? $c['url'] : $c['full_url'], $url);
             $cats[$c_id]['name'] = htmlspecialchars($cats[$c_id]['name']);
         }
     }
     if ($id && isset($cats[$id])) {
         unset($cats[$id]);
     }
     if ($params) {
         $category_params_model = new shopCategoryParamsModel();
         $rows = $category_params_model->getByField('category_id', array_keys($cats), true);
         foreach ($rows as $row) {
             $cats[$row['category_id']]['params'][$row['name']] = $row['value'];
         }
     }
     if ($tree) {
         $stack = array();
         $result = array();
         foreach ($cats as $c) {
             $c['childs'] = array();
             // Number of stack items
             $l = count($stack);
             // Check if we're dealing with different levels
             while ($l > 0 && $stack[$l - 1]['depth'] >= $c['depth']) {
                 array_pop($stack);
                 $l--;
             }
             // Stack is empty (we are inspecting the root)
             if ($l == 0) {
                 // Assigning the root node
                 $i = count($result);
                 $result[$i] = $c;
                 $stack[] =& $result[$i];
             } else {
                 // Add node to parent
                 $i = count($stack[$l - 1]['childs']);
                 $stack[$l - 1]['childs'][$i] = $c;
                 $stack[] =& $stack[$l - 1]['childs'][$i];
             }
         }
         return $result;
     } else {
         return $cats;
     }
 }
Beispiel #7
0
 public function delete($id)
 {
     $id = (int) $id;
     $item = $this->getById($id);
     if (!$item) {
         return false;
     }
     $parent_id = (int) $item['parent_id'];
     /**
      * @event category_delete
      */
     wa()->event('category_delete', $item);
     // because all descendants will be thrown one level up
     // it's necessary to ensure uniqueness urls of descendants in new environment (new parent)
     foreach ($this->descendants($item, false)->order("`{$this->depth}`, `{$this->left}`")->query() as $child) {
         $url = $this->suggestUniqueUrl($child['url'], $child['id'], $parent_id);
         if ($url != $child['url']) {
             $this->updateById($child['id'], array('url' => $url, 'full_url' => $this->fullUrl($item['full_url'], $child['url'])));
         }
     }
     // than correct full urls of descendants taking into account full url of new parent
     if (!$parent_id) {
         $this->correctFullUrlOfDescendants($item, '');
     } else {
         $parent = $this->getById($parent_id);
         $this->correctFullUrlOfDescendants($item, $parent['full_url']);
     }
     if (!parent::delete($id)) {
         return false;
     }
     // delete related info
     $category_params_model = new shopCategoryParamsModel();
     $category_params_model->clear($id);
     $category_products_model = new shopCategoryProductsModel();
     $category_products_model->deleteByField('category_id', $id);
     $category_routes_model = new shopCategoryRoutesModel();
     $category_routes_model->deleteByField('category_id', $id);
     $product_model = new shopProductModel();
     $product_model->correctMainCategory(null, $id);
     shopCategories::clear($id);
     $this->clearCache();
     return true;
 }