public function execute() { $parent_id = waRequest::get('parent_id'); $category_model = new shopCategoryModel(); $cats = $category_model->getTree($parent_id, waRequest::get('depth', null, 'int')); $stack = array(); $result = array(); foreach ($cats as $c) { $c['categories'] = 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]['categories']); $stack[$l - 1]['categories'][$i] = $c; $stack[] =& $stack[$l - 1]['categories'][$i]; } } $this->response = $result; $this->response['_element'] = 'category'; }
/** * @param $current_stage * @param $count * @param $processed * * @usedby shopYandexmarketPluginRunController::step() */ private function stepCategory(&$current_stage, &$count, &$processed) { static $categories = null; static $model; if ($categories === null) { $model = new shopCategoryModel(); if (empty($this->data['export']['hidden_categories'])) { $categories = $model->getTree(0, null, false, $this->data['domain']); } else { $sql = <<<SQL SELECT c.* FROM shop_category c LEFT JOIN shop_category_routes cr ON (c.id = cr.category_id) WHERE (cr.route IS NULL) OR (cr.route = s:domain) ORDER BY c.left_key SQL; $categories = $model->query($sql, $this->data)->fetchAll($model->getTableId()); } // экспортируется только список статических категорий, поэтому фильтруем данные foreach ($categories as $id => $category) { if ($category['type'] != shopCategoryModel::TYPE_STATIC) { unset($categories[$id]); } } if ($current_stage) { $categories = array_slice($categories, $current_stage); } } $chunk = 100; while (--$chunk >= 0 && ($category = reset($categories))) { if (empty($category['parent_id']) || isset($this->data['categories'][$category['parent_id']])) { $category_xml = $this->dom->createElement("category", str_replace('&', '&', $category['name'])); $category['id'] = intval($category['id']); $category_xml->setAttribute('id', $category['id']); if ($category['parent_id']) { $category_xml->setAttribute('parentId', $category['parent_id']); } $nodes = $this->dom->getElementsByTagName('categories'); $nodes->item(0)->appendChild($category_xml); $this->data['categories'][$category['id']] = $category['id']; if (!empty($category['include_sub_categories']) && $category['right_key'] - $category['left_key'] > 1) { //remap hidden subcategories $descendants = $model->descendants($category['id'], true)->where('type = i:type', array('type' => shopCategoryModel::TYPE_STATIC))->fetchAll('id'); if ($descendants) { $remap = array_fill_keys(array_map('intval', array_keys($descendants)), (int) $category['id']); $this->data['categories'] += $remap; } } ++$processed; } array_shift($categories); ++$current_stage; } }
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; } }