1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_Catalog_Model_Resource_Category_Flat_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
36: {
37: 38: 39: 40: 41:
42: protected $_eventPrefix = 'catalog_category_collection';
43:
44: 45: 46: 47: 48:
49: protected $_eventObject = 'category_collection';
50:
51: 52: 53: 54: 55:
56: protected $_storeId = null;
57:
58: 59: 60: 61:
62: protected function _construct()
63: {
64: $this->_init('catalog/category_flat');
65: $this->setModel('catalog/category');
66: }
67:
68: 69: 70: 71: 72:
73: protected function _initSelect()
74: {
75: $this->getSelect()->from(
76: array('main_table' => $this->getResource()->getMainStoreTable($this->getStoreId())),
77: array('entity_id', 'level', 'path', 'position', 'is_active', 'is_anchor')
78: );
79: return $this;
80: }
81:
82: 83: 84: 85: 86: 87:
88: public function addIdFilter($categoryIds)
89: {
90: if (is_array($categoryIds)) {
91: if (empty($categoryIds)) {
92: $condition = '';
93: } else {
94: $condition = array('in' => $categoryIds);
95: }
96: } elseif (is_numeric($categoryIds)) {
97: $condition = $categoryIds;
98: } elseif (is_string($categoryIds)) {
99: $ids = explode(',', $categoryIds);
100: if (empty($ids)) {
101: $condition = $categoryIds;
102: } else {
103: $condition = array('in' => $ids);
104: }
105: }
106: $this->addFieldToFilter('entity_id', $condition);
107: return $this;
108: }
109:
110: 111: 112: 113: 114:
115: protected function _beforeLoad()
116: {
117: Mage::dispatchEvent($this->_eventPrefix . '_load_before',
118: array($this->_eventObject => $this));
119: return parent::_beforeLoad();
120: }
121:
122: 123: 124: 125: 126:
127: protected function _afterLoad()
128: {
129: Mage::dispatchEvent($this->_eventPrefix . '_load_after',
130: array($this->_eventObject => $this));
131:
132: return parent::_afterLoad();
133: }
134:
135: 136: 137: 138: 139: 140:
141: public function setStoreId($storeId)
142: {
143: $this->_storeId = $storeId;
144: return $this;
145: }
146:
147: 148: 149: 150: 151: 152:
153: public function getStoreId()
154: {
155: if (is_null($this->_storeId)) {
156: return Mage::app()->getStore()->getId();
157: }
158: return $this->_storeId;
159: }
160:
161: 162: 163: 164: 165: 166:
167: public function addParentPathFilter($parent)
168: {
169: $this->addFieldToFilter('path', array('like' => "{$parent}/%"));
170: return $this;
171: }
172:
173: 174: 175: 176: 177:
178: public function addStoreFilter()
179: {
180: $this->addFieldToFilter('main_table.store_id', $this->getStoreId());
181: return $this;
182: }
183:
184: 185: 186: 187: 188: 189:
190: public function addSortedField($sorted)
191: {
192: if (is_string($sorted)) {
193: $this->addOrder($sorted, self::SORT_ORDER_ASC);
194: } else {
195: $this->addOrder('name', self::SORT_ORDER_ASC);
196: }
197: return $this;
198: }
199:
200: 201: 202: 203: 204:
205: public function addIsActiveFilter()
206: {
207: $this->addFieldToFilter('is_active', 1);
208: Mage::dispatchEvent($this->_eventPrefix . '_add_is_active_filter',
209: array($this->_eventObject => $this));
210: return $this;
211: }
212:
213: 214: 215: 216: 217:
218: public function addNameToResult()
219: {
220: $this->addAttributeToSelect('name');
221: return $this;
222: }
223:
224: 225: 226: 227: 228: 229:
230: public function addAttributeToSelect($attribute = '*')
231: {
232: if ($attribute == '*') {
233:
234: $columns = $this->getSelect()->getPart(Zend_Db_Select::COLUMNS);
235: $this->getSelect()->reset(Zend_Db_Select::COLUMNS);
236: foreach ($columns as $column) {
237: if ($column[0] == 'main_table') {
238:
239:
240: continue;
241: }
242:
243:
244: if ($column[2] !== null) {
245: $expression = array($column[2] => $column[1]);
246: } else {
247: $expression = $column[2];
248: }
249: $this->getSelect()->columns($expression, $column[0]);
250: }
251:
252: $this->getSelect()->columns('*', 'main_table');
253: return $this;
254: }
255:
256: if (!is_array($attribute)) {
257: $attribute = array($attribute);
258: }
259:
260: $this->getSelect()->columns($attribute, 'main_table');
261: return $this;
262: }
263:
264: 265: 266: 267: 268:
269: public function getResource()
270: {
271: return parent::getResource();
272: }
273:
274: 275: 276: 277: 278: 279: 280:
281: public function addAttributeToSort($attribute, $dir = self::SORT_ORDER_ASC)
282: {
283: if (!is_string($attribute)) {
284: return $this;
285: }
286: $this->setOrder($attribute, $dir);
287: return $this;
288: }
289:
290: 291: 292: 293: 294: 295: 296:
297: public function addAttributeToFilter($attribute, $condition = null)
298: {
299: if (!is_string($attribute) || $condition === null) {
300: return $this;
301: }
302:
303: return $this->addFieldToFilter($attribute, $condition);
304: }
305:
306: 307: 308: 309: 310:
311: public function addUrlRewriteToResult()
312: {
313: $storeId = Mage::app()->getStore()->getId();
314: $this->getSelect()->joinLeft(
315: array('url_rewrite' => $this->getTable('core/url_rewrite')),
316: 'url_rewrite.category_id=main_table.entity_id AND url_rewrite.is_system=1 '.
317: 'AND url_rewrite.product_id IS NULL'.
318: ' AND ' . $this->getConnection()->quoteInto('url_rewrite.store_id=?', $storeId).
319: ' AND ' . $this->getConnection()->quoteInto('url_rewrite.id_path LIKE ?','category/%'),
320: array('request_path')
321: );
322: return $this;
323: }
324:
325: 326: 327: 328: 329: 330:
331: public function addPathsFilter($paths)
332: {
333: if (!is_array($paths)) {
334: $paths = array($paths);
335: }
336: $select = $this->getSelect();
337: $orWhere = false;
338: foreach ($paths as $path) {
339: if ($orWhere) {
340: $select->orWhere('main_table.path LIKE ?', "$path%");
341: } else {
342: $select->where('main_table.path LIKE ?', "$path%");
343: $orWhere = true;
344: }
345: }
346: return $this;
347: }
348:
349: 350: 351: 352: 353: 354:
355: public function addLevelFilter($level)
356: {
357: $this->getSelect()->where('main_table.level <= ?', $level);
358: return $this;
359: }
360:
361: 362: 363: 364: 365: 366:
367: public function addOrderField($field)
368: {
369: $this->setOrder('main_table.' . $field, self::SORT_ORDER_ASC);
370: return $this;
371: }
372:
373: 374: 375: 376: 377: 378: 379:
380: public function setPage($pageNum, $pageSize)
381: {
382: $this->setCurPage($pageNum)
383: ->setPageSize($pageSize);
384: return $this;
385: }
386: }
387: