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_Flat_Collection extends Mage_Core_Model_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
00052
00053
00054
00055
00056
00057 protected $_storeId = null;
00058
00059 protected function _construct()
00060 {
00061 $this->_init('catalog/category_flat');
00062 $this->setModel('catalog/category');
00063 }
00064
00065 protected function _initSelect()
00066 {
00067 $this->getSelect()->from(
00068 array('main_table' => $this->getResource()->getMainStoreTable($this->getStoreId())),
00069 array('entity_id', 'level', 'path', 'position', 'is_active', 'is_anchor')
00070 );
00071 return $this;
00072 }
00073
00074
00075
00076
00077
00078
00079
00080 public function addIdFilter($categoryIds)
00081 {
00082 if (is_array($categoryIds)) {
00083 if (empty($categoryIds)) {
00084 $condition = '';
00085 } else {
00086 $condition = array('in' => $categoryIds);
00087 }
00088 } elseif (is_numeric($categoryIds)) {
00089 $condition = $categoryIds;
00090 } elseif (is_string($categoryIds)) {
00091 $ids = explode(',', $categoryIds);
00092 if (empty($ids)) {
00093 $condition = $categoryIds;
00094 } else {
00095 $condition = array('in' => $ids);
00096 }
00097 }
00098 $this->addFieldToFilter('entity_id', $condition);
00099 return $this;
00100 }
00101
00102
00103
00104
00105
00106
00107 protected function _beforeLoad()
00108 {
00109 Mage::dispatchEvent($this->_eventPrefix . '_load_before',
00110 array($this->_eventObject => $this));
00111 return parent::_beforeLoad();
00112 }
00113
00114
00115
00116
00117
00118
00119 protected function _afterLoad()
00120 {
00121 Mage::dispatchEvent($this->_eventPrefix . '_load_after',
00122 array($this->_eventObject => $this));
00123
00124 return parent::_afterLoad();
00125 }
00126
00127
00128
00129
00130
00131
00132
00133
00134 public function setStoreId($storeId)
00135 {
00136 $this->_storeId = $storeId;
00137 return $this;
00138 }
00139
00140
00141
00142
00143
00144
00145
00146 public function getStoreId()
00147 {
00148 if (is_null($this->_storeId)) {
00149 return Mage::app()->getStore()->getId();
00150 }
00151 return $this->_storeId;
00152 }
00153
00154
00155
00156
00157
00158
00159
00160 public function addParentPathFilter($parent)
00161 {
00162 $this->addFieldToFilter('path', array('like' => "{$parent}/%"));
00163 return $this;
00164 }
00165
00166
00167
00168
00169
00170
00171 public function addStoreFilter()
00172 {
00173 $this->addFieldToFilter('main_table.store_id', $this->getStoreId());
00174 return $this;
00175 }
00176
00177
00178
00179
00180
00181
00182
00183 public function addSortedField($sorted)
00184 {
00185 if (is_string($sorted)) {
00186 $this->addOrder($sorted, 'ASC');
00187 } else {
00188 $this->addOrder('name', 'ASC');
00189 }
00190 return $this;
00191 }
00192
00193 public function addIsActiveFilter()
00194 {
00195 $this->addFieldToFilter('is_active', 1);
00196 Mage::dispatchEvent($this->_eventPrefix . '_add_is_active_filter',
00197 array($this->_eventObject => $this));
00198 return $this;
00199 }
00200
00201 public function addNameToResult()
00202 {
00203 $this->addAttributeToSelect('name');
00204 return $this;
00205 }
00206
00207
00208
00209
00210
00211
00212
00213 public function addAttributeToSelect($attribute = '*')
00214 {
00215 if ($attribute == '*') {
00216
00217 $columns = $this->getSelect()->getPart(Zend_Db_Select::COLUMNS);
00218 $this->getSelect()->reset(Zend_Db_Select::COLUMNS);
00219 foreach ($columns as $column) {
00220 if ($column[0] == 'main_table') {
00221
00222
00223 continue;
00224 }
00225
00226
00227 if ($column[2] !== null) {
00228 $expression = array($column[2] => $column[1]);
00229 } else {
00230 $expression = $column[2];
00231 }
00232 $this->getSelect()->columns($expression, $column[0]);
00233 }
00234
00235 $this->getSelect()->columns('*', 'main_table');
00236 return $this;
00237 }
00238
00239 if (!is_array($attribute)) {
00240 $attribute = array($attribute);
00241 }
00242
00243 $this->getSelect()->columns($attribute, 'main_table');
00244 return $this;
00245 }
00246
00247
00248
00249
00250
00251
00252 public function getResource()
00253 {
00254 return parent::getResource();
00255 }
00256
00257
00258
00259
00260
00261
00262
00263
00264 public function addAttributeToSort($attribute, $dir='asc')
00265 {
00266 if (!is_string($attribute)) {
00267 return $this;
00268 }
00269 $this->setOrder($attribute, $dir);
00270 return $this;
00271 }
00272
00273
00274
00275
00276
00277
00278
00279
00280 public function addAttributeToFilter($attribute, $condition = null)
00281 {
00282 if (!is_string($attribute) || $condition === null) {
00283 return $this;
00284 }
00285
00286 return $this->addFieldToFilter($attribute, $condition);
00287 }
00288
00289 public function addUrlRewriteToResult()
00290 {
00291 $storeId = Mage::app()->getStore()->getId();
00292 $this->getSelect()->joinLeft(
00293 array('url_rewrite' => $this->getTable('core/url_rewrite')),
00294 'url_rewrite.category_id=main_table.entity_id AND url_rewrite.is_system=1 AND url_rewrite.product_id IS NULL AND url_rewrite.store_id="'.$storeId.'" AND url_rewrite.id_path LIKE "category/%"',
00295 array('request_path')
00296 );
00297 return $this;
00298 }
00299
00300 public function addPathsFilter($paths)
00301 {
00302 if (!is_array($paths)) {
00303 $paths = array($paths);
00304 }
00305 $select = $this->getSelect();
00306 $orWhere = false;
00307 foreach ($paths as $path) {
00308 if ($orWhere) {
00309 $select->orWhere('main_table.path LIKE ?', "$path%");
00310 } else {
00311 $select->where('main_table.path LIKE ?', "$path%");
00312 $orWhere = true;
00313 }
00314 }
00315 return $this;
00316 }
00317
00318 public function addLevelFilter($level)
00319 {
00320 $this->getSelect()->where('main_table.level <= ?', $level);
00321 return $this;
00322 }
00323
00324 public function addOrderField($field)
00325 {
00326 $this->setOrder('main_table.' . $field, 'ASC');
00327 return $this;
00328 }
00329 }