00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_Catalog_Block_Seo_Sitemap_Tree_Category extends Mage_Catalog_Block_Seo_Sitemap_Category
00036 {
00037 CONST XML_PATH_LINES_PER_PAGE = 'catalog/sitemap/lines_perpage';
00038
00039 protected $_storeRootCategoryPath = '';
00040 protected $_storeRootCategoryLevel = 0;
00041 protected $_total = 0;
00042 protected $_from = 0;
00043 protected $_to = 0;
00044 protected $_currentPage = 0;
00045 protected $_categoriesToPages = array();
00046
00047
00048
00049
00050
00051 protected function _prepareLayout()
00052 {
00053 $helper = Mage::helper('catalog/category');
00054
00055 $parent = Mage::getModel('catalog/category')
00056 ->setStoreId(Mage::app()->getStore()->getId())
00057 ->load(Mage::app()->getStore()->getRootCategoryId());
00058 $this->_storeRootCategoryPath = $parent->getPath();
00059 $this->_storeRootCategoryLevel = $parent->getLevel();
00060 $this->prepareCategoriesToPages();
00061 $collection = $this->getTreeCollection();
00062 $this->setCollection($collection);
00063 return $this;
00064 }
00065
00066
00067
00068
00069
00070
00071 public function bindPager($pagerName)
00072 {
00073 $pager = $this->getLayout()->getBlock($pagerName);
00074
00075 if ($pager) {
00076 $pager->setAvailableLimit(array(50 => 50));
00077 $pager->setTotalNum($this->_total);
00078 $pager->setLastPageNum(count($this->_categoriesToPages));
00079 if (!$this->_currentPage) {
00080 $this->_currentPage = $pager->getCurrentPage();
00081 $this->_prepareCollection();
00082 }
00083 $pager->setFirstNum($this->_from);
00084 $pager->setLastNum($this->_to);
00085 $pager->setCollection($this->getCollection());
00086 $pager->setShowPerPage(false);
00087 }
00088 }
00089
00090
00091
00092
00093
00094
00095 public function prepareCategoriesToPages()
00096 {
00097 $linesPerPage = Mage::getStoreConfig(self::XML_PATH_LINES_PER_PAGE);
00098 $tmpCollection = Mage::getModel('catalog/category')->getCollection()
00099 ->addIsActiveFilter()
00100 ->addPathsFilter($this->_storeRootCategoryPath . '/')
00101 ->addLevelFilter($this->_storeRootCategoryLevel + 1)
00102 ->addOrderField('path');
00103 $count = 0;
00104 $page = 1;
00105 $categories = array();
00106 foreach ($tmpCollection as $item) {
00107 $children = $item->getChildrenCount()+1;
00108 $this->_total += $children;
00109 if (($children+$count) >= $linesPerPage) {
00110 $categories[$page][$item->getId()] = array(
00111 'path' => $item->getPath(),
00112 'children_count' => $this->_total
00113 );
00114 $page++;
00115 $count = 0;
00116 continue;
00117 }
00118 $categories[$page][$item->getId()] = array(
00119 'path' => $item->getPath(),
00120 'children_count' => $this->_total
00121 );
00122 $count += $children;
00123 }
00124 $this->_categoriesToPages = $categories;
00125 return $this;
00126 }
00127
00128
00129
00130
00131
00132
00133 public function getTreeCollection()
00134 {
00135 $collection = Mage::getModel('catalog/category')->getCollection()
00136 ->addNameToResult()
00137 ->addUrlRewriteToResult()
00138 ->addIsActiveFilter()
00139 ->addOrderField('path');
00140 return $collection;
00141 }
00142
00143
00144
00145
00146
00147
00148 protected function _prepareCollection()
00149 {
00150 $_to = 0;
00151 $pathFilter = array();
00152 if (isset($this->_categoriesToPages[$this->_currentPage])) {
00153 foreach ($this->_categoriesToPages[$this->_currentPage] as $_categoryId=>$_categoryInfo) {
00154 $pathFilter[] = $_categoryInfo['path'];
00155 $_to = max($_to, $_categoryInfo['children_count']);
00156 }
00157 }
00158 if (empty($pathFilter)) {
00159 $pathFilter = $this->_storeRootCategoryPath . '/';
00160 }
00161 $collection = $this->getCollection();
00162 $collection->addPathsFilter($pathFilter);
00163 $this->_to = $_to;
00164 $this->_from = $_to - $collection->count();
00165 return $this;
00166 }
00167
00168
00169
00170
00171
00172
00173
00174
00175 public function getLevel($item, $delta = 1)
00176 {
00177 return (int) ($item->getLevel() - $this->_storeRootCategoryLevel - 1) * $delta;
00178 }
00179
00180 }