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_Model_Resource_Eav_Mysql4_Category_Collection extends Mage_Catalog_Model_Resource_Eav_Mysql4_Collection_Abstract
00036 {
00037
00038
00039
00040
00041
00042 protected $_eventPrefix = 'catalog_category_collection';
00043
00044
00045
00046
00047
00048
00049 protected $_eventObject = 'category_collection';
00050
00051 protected $_productTable;
00052
00053
00054
00055
00056
00057
00058 protected $_productStoreId;
00059
00060 protected $_productWebsiteTable;
00061
00062 protected $_loadWithProductCount = false;
00063
00064
00065
00066
00067
00068 protected function _construct()
00069 {
00070 $this->_init('catalog/category');
00071
00072
00073
00074
00075 $this->_productWebsiteTable = Mage::getSingleton('core/resource')->getTableName('catalog/product_website');
00076 $this->_productTable = Mage::getSingleton('core/resource')->getTableName('catalog/category_product');
00077 }
00078
00079
00080
00081
00082
00083
00084
00085 public function addIdFilter($categoryIds)
00086 {
00087 if (is_array($categoryIds)) {
00088 if (empty($categoryIds)) {
00089 $condition = '';
00090 } else {
00091 $condition = array('in' => $categoryIds);
00092 }
00093 } elseif (is_numeric($categoryIds)) {
00094 $condition = $categoryIds;
00095 } elseif (is_string($categoryIds)) {
00096 $ids = explode(',', $categoryIds);
00097 if (empty($ids)) {
00098 $condition = $categoryIds;
00099 } else {
00100 $condition = array('in' => $ids);
00101 }
00102 }
00103 $this->addFieldToFilter('entity_id', $condition);
00104 return $this;
00105 }
00106
00107
00108
00109
00110
00111
00112
00113 public function setLoadProductCount($flag)
00114 {
00115 $this->_loadWithProductCount = $flag;
00116 return $this;
00117 }
00118
00119
00120
00121
00122
00123
00124
00125 protected function _beforeLoad()
00126 {
00127 Mage::dispatchEvent($this->_eventPrefix . '_load_before',
00128 array($this->_eventObject => $this));
00129 return parent::_beforeLoad();
00130 }
00131
00132
00133
00134
00135
00136
00137 protected function _afterLoad()
00138 {
00139 Mage::dispatchEvent($this->_eventPrefix . '_load_after',
00140 array($this->_eventObject => $this));
00141
00142 return parent::_afterLoad();
00143 }
00144
00145
00146
00147
00148
00149
00150
00151 public function setProductStoreId($storeId)
00152 {
00153 $this->_productStoreId = $storeId;
00154 return $this;
00155 }
00156
00157
00158
00159
00160
00161
00162 public function getProductStoreId()
00163 {
00164 if (is_null($this->_productStoreId)) {
00165 $this->_productStoreId = 0;
00166 }
00167 return $this->_productStoreId;
00168 }
00169
00170
00171
00172
00173
00174
00175
00176
00177 public function load($printQuery = false, $logQuery = false)
00178 {
00179 if ($this->_loadWithProductCount) {
00180 $this->addAttributeToSelect('all_children');
00181 $this->addAttributeToSelect('is_anchor');
00182 }
00183
00184 parent::load($printQuery, $logQuery);
00185
00186 if ($this->_loadWithProductCount) {
00187 $this->_loadProductCount();
00188 }
00189
00190 return $this;
00191 }
00192
00193
00194
00195
00196
00197
00198 protected function _loadProductCount()
00199 {
00200 $this->loadProductCount($this->_items, true, true);
00201 }
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211 public function loadProductCount($items, $countRegular = true, $countAnchor = true)
00212 {
00213 $anchor = array();
00214 $regular = array();
00215
00216 foreach ($items as $item) {
00217 if ($item->getIsAnchor()) {
00218 $anchor[$item->getId()] = $item;
00219 } else {
00220 $regular[$item->getId()] = $item;
00221 }
00222 }
00223
00224 if ($countRegular) {
00225
00226 $regularIds = array_keys($regular);
00227 if (!empty($regularIds)) {
00228 $select = $this->_conn->select();
00229 $select->from(
00230 array('main_table'=>$this->_productTable),
00231 array('category_id', new Zend_Db_Expr('COUNT(main_table.product_id)'))
00232 )
00233 ->where($this->_conn->quoteInto('main_table.category_id IN(?)', $regularIds))
00234 ->group('main_table.category_id');
00235 $counts = $this->_conn->fetchPairs($select);
00236 foreach ($regular as $item) {
00237 if (isset($counts[$item->getId()])) {
00238 $item->setProductCount($counts[$item->getId()]);
00239 } else {
00240 $item->setProductCount(0);
00241 }
00242 }
00243 }
00244 }
00245
00246 if ($countAnchor) {
00247
00248 foreach ($anchor as $item) {
00249 if ($allChildren = $item->getAllChildren()) {
00250 $select = $this->_conn->select();
00251 $select->from(
00252 array('main_table' => $this->_productTable),
00253 new Zend_Db_Expr('COUNT( DISTINCT main_table.product_id)')
00254 )
00255 ->joinInner(array('e' => $this->getTable('catalog/category')), 'main_table.category_id=e.entity_id', array())
00256 ->where('e.entity_id=? OR ' . $this->_conn->quoteInto('e.path LIKE CONCAT(?)', array($item->getPath(), '/%')), $item->getId());
00257 $item->setProductCount((int) $this->_conn->fetchOne($select));
00258 } else {
00259 $item->setProductCount(0);
00260 }
00261 }
00262 }
00263 return $this;
00264 }
00265
00266
00267
00268
00269
00270
00271
00272 public function addPathFilter($regexp)
00273 {
00274 $this->getSelect()->where(new Zend_Db_Expr("path regexp '{$regexp}'"));
00275 return $this;
00276 }
00277
00278
00279
00280
00281
00282
00283 public function joinUrlRewrite()
00284 {
00285 $storeId = Mage::app()->getStore()->getId();
00286 $this->joinTable(
00287 'core/url_rewrite',
00288 'category_id=entity_id',
00289 array('request_path'),
00290 '{{table}}.is_system=1 AND {{table}}.product_id IS NULL AND {{table}}.store_id="'.$storeId.'" AND id_path LIKE "category/%"',
00291 'left'
00292 );
00293 return $this;
00294 }
00295
00296 public function addIsActiveFilter()
00297 {
00298 $this->addAttributeToFilter('is_active', 1);
00299 Mage::dispatchEvent($this->_eventPrefix . '_add_is_active_filter',
00300 array($this->_eventObject => $this));
00301 return $this;
00302 }
00303
00304 public function addNameToResult()
00305 {
00306 $this->addAttributeToSelect('name');
00307 return $this;
00308 }
00309
00310 public function addUrlRewriteToResult()
00311 {
00312 $this->joinUrlRewrite();
00313 return $this;
00314 }
00315
00316 public function addPathsFilter($paths)
00317 {
00318 if (!is_array($paths)) {
00319 $paths = array($paths);
00320 }
00321 $select = $this->getSelect();
00322 $orWhere = false;
00323 foreach ($paths as $path) {
00324 if ($orWhere) {
00325 $select->orWhere('e.path LIKE ?', "$path%");
00326 } else {
00327 $select->where('e.path LIKE ?', "$path%");
00328 $orWhere = true;
00329 }
00330 }
00331 return $this;
00332 }
00333
00334 public function addLevelFilter($level)
00335 {
00336 $this->getSelect()->where('e.level <= ?', $level);
00337 return $this;
00338 }
00339
00340 public function addOrderField($field)
00341 {
00342 $this->setOrder($field, 'ASC');
00343 return $this;
00344 }
00345
00346 }