_select) { unset($this->_selectOptions['order']); parent::getListSelect(); $this->_select->order(array('parentId', 'title')); } return $this->_select; } protected function _getFromColumns() { return array( 'key' => 'parentId', '*', 'productsCount' => new Zend_Db_Expr('(' . $this->_getProductsCountSubselect() . ')'), 'imagesCount' => new Zend_Db_Expr('(' . $this->_getImagesCountSubselect() . ')'), ); } protected function _getProductsCountSubselect() { $select = $this->_db->select(); $select->from($this->_getPair('Product'), array('COUNT(*)')); $select->where('`Product`.`categoryId` = `' . $this->_tableAlias . '`.`id`'); return $select; } protected function _getImagesCountSubselect() { $select = $this->_db->select(); $select->from($this->_getPair('GalleryImage'), array('COUNT(*)')); $select->where('`GalleryImage`.`productCategoryId` = `' . $this->_tableAlias . '`.`id`'); $select->where('`GalleryImage`.`show` = "y"'); 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 ($this->_categoryTree === null) { $list = $this->getList(); $this->_categoryTree = $this->_buildTree($list); } return $this->_getBranch($this->_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_'), array('industryType' => ''), 'fullAlias'); } $productsPageAlias = Qs_SiteMap::findFirst(null, ['type' => 'ECommerce_Product_'], null, 'alias'); if (array_key_exists($parentId, $list)) { foreach ($list[$parentId] as $item) { $item['subcategoriesProductsCount'] = 0; if (!isset($parentData['subcategoriesProductsCount'])) { $parentData['subcategoriesProductsCount'] = 0; } $item['subcategoriesImagesCount'] = 0; if (!isset($parentData['subcategoriesImagesCount'])) { $parentData['subcategoriesImagesCount'] = 0; } // add menu items if (!empty($parentData['fullAlias'])) { $item['fullTitle'] = $parentData['fullTitle'] . ' / ' . $item['title']; $item['fullAlias'] = $productsPageAlias . '/' . $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') . '/' . $productsPageAlias . '/' . $item['alias']; $item['sub'] = $this->_buildTree($list, $item['id'], $item, Qs_Constant::get('BASE_URL')); $parentData['subcategoriesProductsCount'] += $item['productsCount'] + $item['subcategoriesProductsCount']; $parentData['subcategoriesImagesCount'] += $item['imagesCount'] + $item['subcategoriesImagesCount']; $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; } public function getColors4Select() { $select = $this->_db->select(); $select->from($this->_getPair($this->_signColorTableAlias), array('id', 'title')) ->join($this->_getPair($this->_category2SignColorTableAlias), $this->_signColorTableAlias . '.id = ' . $this->_category2SignColorTableAlias . '.signColorId' ) ->where($this->_category2SignColorTableAlias . '.categoryId = ?', $this->getPrimaryKey()); return $this->_db->fetchPairs($select); } public function getShapes4Select() { $select = $this->_db->select(); $select->from($this->_getPair($this->_signShapeTableAlias), array('id', 'title')) ->join($this->_getPair($this->_category2SignShapeTableAlias), $this->_signShapeTableAlias . '.id = ' . $this->_category2SignShapeTableAlias . '.signShapeId' ) ->where($this->_category2SignShapeTableAlias . '.categoryId = ?', $this->getPrimaryKey()); return $this->_db->fetchPairs($select); } public function getCategoryListFromTree() { $tree = $this->getCategoryTree(); return $this->getCategoryInfo($tree); } public function getCategoryInfo($list) { $result = array(); foreach ($list as $category) { $result[$category['id']] = $category['title']; if(!empty($category['sub'])) { $resultSub = $this->getCategoryInfo($category['sub']); $result = Qs_Array::mergeAssoc($result, $resultSub); } } return $result; } }