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_Sitemap_Model_Resource_Catalog_Category extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: 38: 39: 40: 41:
42: protected $_select;
43:
44: 45: 46: 47: 48:
49: protected $_attributesCache = array();
50:
51: 52: 53: 54:
55: protected function _construct()
56: {
57: $this->_init('catalog/category', 'entity_id');
58: }
59:
60: 61: 62: 63: 64: 65:
66: public function getCollection($storeId)
67: {
68: $categories = array();
69:
70: $store = Mage::app()->getStore($storeId);
71:
72:
73: if (!$store) {
74: return false;
75: }
76:
77: $this->_select = $this->_getWriteAdapter()->select()
78: ->from($this->getMainTable())
79: ->where($this->getIdFieldName() . '=?', $store->getRootCategoryId());
80: $categoryRow = $this->_getWriteAdapter()->fetchRow($this->_select);
81:
82: if (!$categoryRow) {
83: return false;
84: }
85:
86: $urConditions = array(
87: 'e.entity_id=ur.category_id',
88: $this->_getWriteAdapter()->quoteInto('ur.store_id=?', $store->getId()),
89: 'ur.product_id IS NULL',
90: $this->_getWriteAdapter()->quoteInto('ur.is_system=?', 1),
91: );
92: $this->_select = $this->_getWriteAdapter()->select()
93: ->from(array('e' => $this->getMainTable()), array($this->getIdFieldName()))
94: ->joinLeft(
95: array('ur' => $this->getTable('core/url_rewrite')),
96: join(' AND ', $urConditions),
97: array('url'=>'request_path')
98: )
99: ->where('e.path LIKE ?', $categoryRow['path'] . '/%');
100:
101: $this->_addFilter($storeId, 'is_active', 1);
102:
103: $query = $this->_getWriteAdapter()->query($this->_select);
104: while ($row = $query->fetch()) {
105: $category = $this->_prepareCategory($row);
106: $categories[$category->getId()] = $category;
107: }
108:
109: return $categories;
110: }
111:
112: 113: 114: 115: 116: 117:
118: protected function _prepareCategory(array $categoryRow)
119: {
120: $category = new Varien_Object();
121: $category->setId($categoryRow[$this->getIdFieldName()]);
122: $categoryUrl = !empty($categoryRow['url']) ? $categoryRow['url'] : 'catalog/category/view/id/' . $category->getId();
123: $category->setUrl($categoryUrl);
124: return $category;
125: }
126:
127: 128: 129: 130: 131: 132: 133: 134: 135:
136: protected function _addFilter($storeId, $attributeCode, $value, $type = '=')
137: {
138: if (!isset($this->_attributesCache[$attributeCode])) {
139: $attribute = Mage::getSingleton('catalog/category')->getResource()->getAttribute($attributeCode);
140:
141: $this->_attributesCache[$attributeCode] = array(
142: 'entity_type_id' => $attribute->getEntityTypeId(),
143: 'attribute_id' => $attribute->getId(),
144: 'table' => $attribute->getBackend()->getTable(),
145: 'is_global' => $attribute->getIsGlobal(),
146: 'backend_type' => $attribute->getBackendType()
147: );
148: }
149:
150: $attribute = $this->_attributesCache[$attributeCode];
151:
152: if (!$this->_select instanceof Zend_Db_Select) {
153: return false;
154: }
155:
156: switch ($type) {
157: case '=':
158: $conditionRule = '=?';
159: break;
160: case 'in':
161: $conditionRule = ' IN(?)';
162: break;
163: default:
164: return false;
165: break;
166: }
167:
168: if ($attribute['backend_type'] == 'static') {
169: $this->_select->where('e.' . $attributeCode . $conditionRule, $value);
170: } else {
171: $this->_select->join(
172: array('t1_'.$attributeCode => $attribute['table']),
173: 'e.entity_id=t1_'.$attributeCode.'.entity_id AND t1_'.$attributeCode.'.store_id=0',
174: array()
175: )
176: ->where('t1_'.$attributeCode.'.attribute_id=?', $attribute['attribute_id']);
177:
178: if ($attribute['is_global']) {
179: $this->_select->where('t1_'.$attributeCode.'.value'.$conditionRule, $value);
180: } else {
181: $ifCase = $this->_select->getAdapter()->getCheckSql('t2_'.$attributeCode.'.value_id > 0', 't2_'.$attributeCode.'.value', 't1_'.$attributeCode.'.value');
182: $this->_select->joinLeft(
183: array('t2_'.$attributeCode => $attribute['table']),
184: $this->_getWriteAdapter()->quoteInto('t1_'.$attributeCode.'.entity_id = t2_'.$attributeCode.'.entity_id AND t1_'.$attributeCode.'.attribute_id = t2_'.$attributeCode.'.attribute_id AND t2_'.$attributeCode.'.store_id=?', $storeId),
185: array()
186: )
187: ->where('('.$ifCase.')'.$conditionRule, $value);
188: }
189: }
190:
191: return $this->_select;
192: }
193: }
194: