/**
  * @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;
     }
 }
Example #2
0
 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;
     }
 }