1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Catalog
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27:
28: /**
29: * SEO tree Categories Sitemap block
30: *
31: * @category Mage
32: * @package Mage_Catalog
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_Catalog_Block_Seo_Sitemap_Tree_Category extends Mage_Catalog_Block_Seo_Sitemap_Category
36: {
37: CONST XML_PATH_LINES_PER_PAGE = 'catalog/sitemap/lines_perpage';
38:
39: protected $_storeRootCategoryPath = '';
40: protected $_storeRootCategoryLevel = 0;
41: protected $_total = 0;
42: protected $_from = 0;
43: protected $_to = 0;
44: protected $_currentPage = 0;
45: protected $_categoriesToPages = array();
46: /**
47: * Initialize categories collection
48: *
49: * @return Mage_Catalog_Block_Seo_Sitemap_Category
50: */
51: protected function _prepareLayout()
52: {
53: $helper = Mage::helper('catalog/category');
54: /* @var $helper Mage_Catalog_Helper_Category */
55: $parent = Mage::getModel('catalog/category')
56: ->setStoreId(Mage::app()->getStore()->getId())
57: ->load(Mage::app()->getStore()->getRootCategoryId());
58: $this->_storeRootCategoryPath = $parent->getPath();
59: $this->_storeRootCategoryLevel = $parent->getLevel();
60: $this->prepareCategoriesToPages();
61: $collection = $this->getTreeCollection();
62: $this->setCollection($collection);
63: return $this;
64: }
65:
66: /**
67: * Init pager
68: *
69: * @param string $pagerName
70: */
71: public function bindPager($pagerName)
72: {
73: $pager = $this->getLayout()->getBlock($pagerName);
74: /* @var $pager Mage_Catalog_Block_Seo_Sitemap_Tree_Pager */
75: if ($pager) {
76: $pager->setAvailableLimit(array(50 => 50));
77: $pager->setTotalNum($this->_total);
78: $pager->setLastPageNum(count($this->_categoriesToPages));
79: if (!$this->_currentPage) {
80: $this->_currentPage = $pager->getCurrentPage();
81: $this->_prepareCollection();
82: }
83: $pager->setFirstNum($this->_from);
84: $pager->setLastNum($this->_to);
85: $pager->setCollection($this->getCollection());
86: $pager->setShowPerPage(false);
87: }
88: }
89:
90: /**
91: * Prepare array of categories separated into pages
92: *
93: * @return Mage_Catalog_Block_Seo_Sitemap_Tree_Category
94: */
95: public function prepareCategoriesToPages()
96: {
97: $linesPerPage = Mage::getStoreConfig(self::XML_PATH_LINES_PER_PAGE);
98: $tmpCollection = Mage::getModel('catalog/category')->getCollection()
99: ->addIsActiveFilter()
100: ->addPathsFilter($this->_storeRootCategoryPath . '/')
101: ->addLevelFilter($this->_storeRootCategoryLevel + 1)
102: ->addOrderField('path');
103: $count = 0;
104: $page = 1;
105: $categories = array();
106: foreach ($tmpCollection as $item) {
107: $children = $item->getChildrenCount()+1;
108: $this->_total += $children;
109: if (($children+$count) >= $linesPerPage) {
110: $categories[$page][$item->getId()] = array(
111: 'path' => $item->getPath(),
112: 'children_count' => $this->_total
113: );
114: $page++;
115: $count = 0;
116: continue;
117: }
118: $categories[$page][$item->getId()] = array(
119: 'path' => $item->getPath(),
120: 'children_count' => $this->_total
121: );
122: $count += $children;
123: }
124: $this->_categoriesToPages = $categories;
125: return $this;
126: }
127:
128: /**
129: * Return collection of categories
130: *
131: * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection
132: */
133: public function getTreeCollection()
134: {
135: $collection = Mage::getModel('catalog/category')->getCollection()
136: ->addNameToResult()
137: ->addUrlRewriteToResult()
138: ->addIsActiveFilter()
139: ->addOrderField('path');
140: return $collection;
141: }
142:
143: /**
144: * Prepare collection filtered by paths
145: *
146: * @return Mage_Catalog_Block_Seo_Sitemap_Tree_Category
147: */
148: protected function _prepareCollection()
149: {
150: $_to = 0;
151: $pathFilter = array();
152: if (isset($this->_categoriesToPages[$this->_currentPage])) {
153: foreach ($this->_categoriesToPages[$this->_currentPage] as $_categoryId=>$_categoryInfo) {
154: $pathFilter[] = $_categoryInfo['path'];
155: $_to = max($_to, $_categoryInfo['children_count']);
156: }
157: }
158: if (empty($pathFilter)) {
159: $pathFilter = $this->_storeRootCategoryPath . '/';
160: }
161: $collection = $this->getCollection();
162: $collection->addPathsFilter($pathFilter);
163: $this->_to = $_to;
164: $this->_from = $_to - $collection->count();
165: return $this;
166: }
167:
168: /**
169: * Return level of indent
170: *
171: * @param Mage_Catalog_Model_Category $item
172: * @param integer $delta
173: * @return integer
174: */
175: public function getLevel($item, $delta = 1)
176: {
177: return (int) ($item->getLevel() - $this->_storeRootCategoryLevel - 1) * $delta;
178: }
179:
180: }
181: