_select) { unset($this->_selectOptions['order']); parent::getListSelect(); $this->_select->order(array('parentId', 'sorter')); } return $this->_select; } protected function _getFromColumns() { return array( 'key' => 'parentId', '*', 'productsCount' => new Zend_Db_Expr('(' . $this->_getProductsCountSubselect() . ')') ); } protected function _getProductsCountSubselect() { $select = $this->_db->select(); $select->from($this->_getPair('Product2Category'), array('COUNT(*)')); $select->where('`Product2Category`.`categoryId` = `' . $this->_tableAlias . '`.`id`'); return $select; } protected function _getFromDbColumns() { return $this->_getFromColumns(); } public function getList() { $list = $this->_db->fetchAll($this->getListSelect(), array(), Qs_Db::FETCH_GROUP); $this->_prepareList($list); return $list; } /** * Return categories tree * * @param int $parentId * @return array */ public function getCategoryTree($parentId = 0) { if (static::$_categoryTree === null) { $list = $this->getList(); static::$_categoryTree = $this->_buildTree($list); } return $this->_getBranch(static::$_categoryTree, $parentId); } /** * Будує дерево категорій * @param array $list масив категорій погрупований по parentId * @param int $parentId * @param array $parentData * @param string $baseUrl * @return array */ protected function _buildTree(array $list, $parentId = 0, array &$parentData = array(), $baseUrl = null) { $tree = array(); $baseAlias = ''; if (null === $baseUrl) { $baseAlias = Qs_SiteMap::findFirst(null, array('type' => 'ECommerce_Product_Category_'), null, 'fullAlias'); } if (array_key_exists($parentId, $list)) { foreach ($list[$parentId] as $item) { $item['subcategoriesProductsCount'] = 0; if (!isset($parentData['subcategoriesProductsCount'])) { $parentData['subcategoriesProductsCount'] = 0; } // add menu items if (!empty($parentData['fullAlias'])) { $item['fullTitle'] = $parentData['fullTitle'] . ' / ' . $item['title']; $item['fullAlias'] = $parentData['fullAlias'] . '/' . $item['alias']; } else { $item['fullTitle'] = $item['title']; $item['fullAlias'] = (($baseAlias) ? ($baseAlias . '/') : '') . $item['alias']; } $item['menuTitle'] = $item['title']; $item['showInFooter'] = 'n'; $item['url'] = Qs_Constant::get('BASE_URL') . '/' . $item['fullAlias']; $item['sub'] = $this->_buildTree($list, $item['id'], $item, Qs_Constant::get('BASE_URL')); $parentData['subcategoriesProductsCount'] += $item['productsCount'] + $item['subcategoriesProductsCount']; $tree[$item['id']] = $item; } } return $tree; } protected function _getBranch($tree, $parentId) { if (0 == $parentId) { return $tree; } foreach ($tree as $node) { if ($node['id'] == $parentId) { unset($node['sub']); return $node; } if (!empty($node['sub'])) { if (null !== ($subTree = $this->_getBranch($node['sub'], $parentId))) { $node['sub'] = array(); $node['sub'][$subTree['id']] = $subTree; return $node; } } } return null; } public function getNode($tree, $nodeId) { foreach ($tree as $node) { if ($node['id'] == $nodeId) { return $node; } if (!empty($node['sub'])) { if (null !== ($subTree = $this->getNode($node['sub'], $nodeId))) { return $subTree; } } } return null; } /** * Return categories tree for select element * * @param bool $fullNames * @return array */ public function getCategories4Select($fullNames = false) { $tree = $this->getCategoryTree(); $result = array(); if (!empty($tree)) { $result = $this->_prepareCategories4Select($tree, '', $fullNames); if ($fullNames) { foreach ($result as &$row) { $lastItem = array_pop($row); foreach ($row as &$item) { $item = '' . $item . ''; } array_push($row, '' . $lastItem . ''); $row = implode(' » ', $row); } } } return $result; } /** * Prepare categories array * * @param array $tree * @param string|array $prefix * @param bool $fullNames * @return array */ protected function _prepareCategories4Select($tree, $prefix = '', $fullNames = false) { $categories = array(); foreach ($tree as $row) { if ($fullNames) { if (empty($prefix)) { $prefix = array(); } $categories[$row['id']] = $prefix; $categories[$row['id']][] = htmlspecialchars($row['title']); } else { $categories[$row['id']] = $prefix . htmlspecialchars($row['title']); } if (!empty($row['sub'])) { if ($fullNames) { $newPrefix = $categories[$row['id']]; } else { $newPrefix = '  ' . $prefix; } $categories += (array) $this->_prepareCategories4Select($row['sub'], $newPrefix, $fullNames); } } return $categories; } }