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_Category_Indexer_Flat extends Mage_Index_Model_Indexer_Abstract
36: {
37: 38: 39:
40: const EVENT_MATCH_RESULT_KEY = 'catalog_category_flat_match_result';
41:
42: 43: 44: 45: 46:
47: protected $_matchedEntities = array(
48: Mage_Catalog_Model_Category::ENTITY => array(
49: Mage_Index_Model_Event::TYPE_SAVE
50: ),
51: Mage_Core_Model_Store::ENTITY => array(
52: Mage_Index_Model_Event::TYPE_SAVE,
53: Mage_Index_Model_Event::TYPE_DELETE
54: ),
55: Mage_Core_Model_Store_Group::ENTITY => array(
56: Mage_Index_Model_Event::TYPE_SAVE
57: ),
58: );
59:
60: 61: 62: 63: 64:
65: public function getName()
66: {
67: return Mage::helper('catalog')->__('Category Flat Data');
68: }
69:
70: 71: 72: 73: 74:
75: public function getDescription()
76: {
77: return Mage::helper('catalog')->__('Reorganize EAV category structure to flat structure');
78: }
79:
80: 81: 82: 83: 84:
85: protected function _getIndexer()
86: {
87: return Mage::getResourceSingleton('catalog/category_flat');
88: }
89:
90: 91: 92: 93: 94: 95: 96: 97:
98: public function matchEvent(Mage_Index_Model_Event $event)
99: {
100: if (!Mage::helper('catalog/category_flat')->isEnabled(true)) {
101: return false;
102: }
103:
104: $data = $event->getNewData();
105: if (isset($data[self::EVENT_MATCH_RESULT_KEY])) {
106: return $data[self::EVENT_MATCH_RESULT_KEY];
107: }
108:
109: $entity = $event->getEntity();
110: if ($entity == Mage_Core_Model_Store::ENTITY) {
111: if ($event->getType() == Mage_Index_Model_Event::TYPE_DELETE) {
112: $result = true;
113: } elseif ($event->getType() == Mage_Index_Model_Event::TYPE_SAVE) {
114:
115: $store = $event->getDataObject();
116: if ($store && ($store->isObjectNew()
117: || $store->dataHasChangedFor('group_id')
118: || $store->dataHasChangedFor('root_category_id')
119: )) {
120: $result = true;
121: } else {
122: $result = false;
123: }
124: } else {
125: $result = false;
126: }
127: } elseif ($entity == Mage_Core_Model_Store_Group::ENTITY) {
128:
129: $storeGroup = $event->getDataObject();
130: if ($storeGroup
131: && ($storeGroup->dataHasChangedFor('website_id') || $storeGroup->dataHasChangedFor('root_category_id'))
132: ) {
133: $result = true;
134: } else {
135: $result = false;
136: }
137: } else {
138: $result = parent::matchEvent($event);
139: }
140:
141: $event->addNewData(self::EVENT_MATCH_RESULT_KEY, $result);
142:
143: return $result;
144: }
145:
146: 147: 148: 149: 150:
151: protected function _registerEvent(Mage_Index_Model_Event $event)
152: {
153: $event->addNewData(self::EVENT_MATCH_RESULT_KEY, true);
154: switch ($event->getEntity()) {
155: case Mage_Catalog_Model_Category::ENTITY:
156: $this->_registerCatalogCategoryEvent($event);
157: break;
158:
159: case Mage_Core_Model_Store::ENTITY:
160: if ($event->getType() == Mage_Index_Model_Event::TYPE_DELETE) {
161: $this->_registerCoreStoreEvent($event);
162: break;
163: }
164: case Mage_Core_Model_Store_Group::ENTITY:
165: $event->addNewData('catalog_category_flat_skip_call_event_handler', true);
166: $process = $event->getProcess();
167: $process->changeStatus(Mage_Index_Model_Process::STATUS_REQUIRE_REINDEX);
168: break;
169: }
170: }
171:
172: 173: 174: 175: 176: 177:
178: protected function _registerCatalogCategoryEvent(Mage_Index_Model_Event $event)
179: {
180: switch ($event->getType()) {
181: case Mage_Index_Model_Event::TYPE_SAVE:
182:
183: $category = $event->getDataObject();
184:
185: 186: 187:
188: $affectedCategoryIds = $category->getAffectedCategoryIds();
189: if ($affectedCategoryIds) {
190: $event->addNewData('catalog_category_flat_affected_category_ids', $affectedCategoryIds);
191: } else {
192: $event->addNewData('catalog_category_flat_category_id', $category->getId());
193: }
194:
195: break;
196: }
197: return $this;
198: }
199:
200: 201: 202: 203: 204: 205:
206: protected function _registerCoreStoreEvent(Mage_Index_Model_Event $event)
207: {
208: if ($event->getType() == Mage_Index_Model_Event::TYPE_DELETE) {
209:
210: $store = $event->getDataObject();
211: $event->addNewData('catalog_category_flat_delete_store_id', $store->getId());
212: }
213: return $this;
214: }
215:
216: 217: 218: 219: 220:
221: protected function _processEvent(Mage_Index_Model_Event $event)
222: {
223: $data = $event->getNewData();
224:
225: if (!empty($data['catalog_category_flat_reindex_all'])) {
226: $this->reindexAll();
227: } else if (!empty($data['catalog_category_flat_category_id'])) {
228:
229: $categoryId = $data['catalog_category_flat_category_id'];
230: $this->_getIndexer()->synchronize($categoryId);
231: } else if (!empty($data['catalog_category_flat_affected_category_ids'])) {
232: $categoryIds = $data['catalog_category_flat_affected_category_ids'];
233: $this->_getIndexer()->move($categoryIds);
234: } else if (!empty($data['catalog_category_flat_delete_store_id'])) {
235: $storeId = $data['catalog_category_flat_delete_store_id'];
236: $this->_getIndexer()->deleteStores($storeId);
237: }
238: }
239:
240: 241: 242: 243:
244: public function reindexAll()
245: {
246: $this->_getIndexer()->reindexAll();
247: }
248: }
249: