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: class Mage_Catalog_Model_Product_Indexer_Flat extends Mage_Index_Model_Indexer_Abstract
27: {
28: 29: 30:
31: const EVENT_MATCH_RESULT_KEY = 'catalog_product_flat_match_result';
32:
33: 34: 35: 36: 37:
38: protected $_matchedEntities = array(
39: Mage_Catalog_Model_Product::ENTITY => array(
40: Mage_Index_Model_Event::TYPE_SAVE,
41: Mage_Index_Model_Event::TYPE_MASS_ACTION,
42: ),
43: Mage_Catalog_Model_Resource_Eav_Attribute::ENTITY => array(
44: Mage_Index_Model_Event::TYPE_SAVE,
45: Mage_Index_Model_Event::TYPE_DELETE,
46: ),
47: Mage_Core_Model_Store::ENTITY => array(
48: Mage_Index_Model_Event::TYPE_SAVE,
49: Mage_Index_Model_Event::TYPE_DELETE
50: ),
51: Mage_Core_Model_Store_Group::ENTITY => array(
52: Mage_Index_Model_Event::TYPE_SAVE
53: ),
54: Mage_Catalog_Model_Convert_Adapter_Product::ENTITY => array(
55: Mage_Index_Model_Event::TYPE_SAVE
56: ),
57: Mage_Catalog_Model_Product_Flat_Indexer::ENTITY => array(
58: Mage_Catalog_Model_Product_Flat_Indexer::EVENT_TYPE_REBUILD,
59: ),
60: );
61:
62: 63: 64: 65: 66:
67: public function getName()
68: {
69: return Mage::helper('catalog')->__('Product Flat Data');
70: }
71:
72: 73: 74: 75: 76:
77: public function getDescription()
78: {
79: return Mage::helper('catalog')->__('Reorganize EAV product structure to flat structure');
80: }
81:
82: 83: 84: 85: 86:
87: protected function _getIndexer()
88: {
89: return Mage::getSingleton('catalog/product_flat_indexer');
90: }
91:
92: 93: 94: 95: 96: 97: 98: 99:
100: public function matchEvent(Mage_Index_Model_Event $event)
101: {
102: if (!Mage::helper('catalog/product_flat')->isBuilt()) {
103: return false;
104: }
105:
106: $data = $event->getNewData();
107: if (isset($data[self::EVENT_MATCH_RESULT_KEY])) {
108: return $data[self::EVENT_MATCH_RESULT_KEY];
109: }
110:
111: $entity = $event->getEntity();
112: if ($entity == Mage_Catalog_Model_Resource_Eav_Attribute::ENTITY) {
113:
114: $attribute = $event->getDataObject();
115: $addFilterable = Mage::helper('catalog/product_flat')->isAddFilterableAttributes();
116:
117: $enableBefore = $attribute && (($attribute->getOrigData('backend_type') == 'static')
118: || ($addFilterable && $attribute->getOrigData('is_filterable') > 0)
119: || ($attribute->getOrigData('used_in_product_listing') == 1)
120: || ($attribute->getOrigData('is_used_for_promo_rules') == 1)
121: || ($attribute->getOrigData('used_for_sort_by') == 1));
122:
123: $enableAfter = $attribute && (($attribute->getData('backend_type') == 'static')
124: || ($addFilterable && $attribute->getData('is_filterable') > 0)
125: || ($attribute->getData('used_in_product_listing') == 1)
126: || ($attribute->getData('is_used_for_promo_rules') == 1)
127: || ($attribute->getData('used_for_sort_by') == 1));
128:
129: if ($attribute && $event->getType() == Mage_Index_Model_Event::TYPE_DELETE) {
130: $result = $enableBefore;
131: } elseif ($attribute && $event->getType() == Mage_Index_Model_Event::TYPE_SAVE) {
132: if ($enableAfter || $enableBefore) {
133: $result = true;
134: } else {
135: $result = false;
136: }
137: } else {
138: $result = false;
139: }
140: } else if ($entity == Mage_Core_Model_Store::ENTITY) {
141: if ($event->getType() == Mage_Index_Model_Event::TYPE_DELETE) {
142: $result = true;
143: } else {
144:
145: $store = $event->getDataObject();
146: if ($store && $store->isObjectNew()) {
147: $result = true;
148: } else {
149: $result = false;
150: }
151: }
152: } else if ($entity == Mage_Core_Model_Store_Group::ENTITY) {
153:
154: $storeGroup = $event->getDataObject();
155: if ($storeGroup && $storeGroup->dataHasChangedFor('website_id')) {
156: $result = true;
157: } else {
158: $result = false;
159: }
160: } else {
161: $result = parent::matchEvent($event);
162: }
163:
164: $event->addNewData(self::EVENT_MATCH_RESULT_KEY, $result);
165:
166: return $result;
167: }
168:
169: 170: 171: 172: 173:
174: protected function _registerEvent(Mage_Index_Model_Event $event)
175: {
176: $event->addNewData(self::EVENT_MATCH_RESULT_KEY, true);
177: switch ($event->getEntity()) {
178: case Mage_Catalog_Model_Product::ENTITY:
179: $this->_registerCatalogProductEvent($event);
180: break;
181: case Mage_Catalog_Model_Convert_Adapter_Product::ENTITY:
182: $event->addNewData('catalog_product_flat_reindex_all', true);
183: break;
184: case Mage_Core_Model_Store::ENTITY:
185: if ($event->getType() == Mage_Index_Model_Event::TYPE_DELETE) {
186: $this->_registerCoreStoreEvent($event);
187: break;
188: }
189: case Mage_Catalog_Model_Resource_Eav_Attribute::ENTITY:
190: case Mage_Core_Model_Store_Group::ENTITY:
191: $event->addNewData('catalog_product_flat_skip_call_event_handler', true);
192: $process = $event->getProcess();
193: $process->changeStatus(Mage_Index_Model_Process::STATUS_REQUIRE_REINDEX);
194: break;
195: case Mage_Catalog_Model_Product_Flat_Indexer::ENTITY:
196: switch ($event->getType()) {
197: case Mage_Catalog_Model_Product_Flat_Indexer::EVENT_TYPE_REBUILD:
198: $event->addNewData('id', $event->getDataObject()->getId());
199: }
200: break;
201: }
202: }
203:
204: 205: 206: 207: 208: 209:
210: protected function _registerCatalogProductEvent(Mage_Index_Model_Event $event)
211: {
212: switch ($event->getType()) {
213: case Mage_Index_Model_Event::TYPE_SAVE:
214:
215: $product = $event->getDataObject();
216: $event->addNewData('catalog_product_flat_product_id', $product->getId());
217: break;
218:
219: case Mage_Index_Model_Event::TYPE_MASS_ACTION:
220:
221: $actionObject = $event->getDataObject();
222:
223: $reindexData = array();
224: $reindexFlat = false;
225:
226:
227: $attrData = $actionObject->getAttributesData();
228: if (isset($attrData['status'])) {
229: $reindexFlat = true;
230: $reindexData['catalog_product_flat_status'] = $attrData['status'];
231: }
232:
233:
234: if ($actionObject->getWebsiteIds()) {
235: $reindexFlat = true;
236: $reindexData['catalog_product_flat_website_ids'] = $actionObject->getWebsiteIds();
237: $reindexData['catalog_product_flat_action_type'] = $actionObject->getActionType();
238: }
239:
240: $flatAttributes = array();
241: if (is_array($attrData)) {
242: $flatAttributes = array_intersect($this->_getFlatAttributes(), array_keys($attrData));
243: }
244:
245: if (count($flatAttributes) > 0) {
246: $reindexFlat = true;
247: $reindexData['catalog_product_flat_force_update'] = true;
248: }
249:
250:
251: if ($reindexFlat) {
252: $reindexData['catalog_product_flat_product_ids'] = $actionObject->getProductIds();
253: foreach ($reindexData as $k => $v) {
254: $event->addNewData($k, $v);
255: }
256: }
257: break;
258: }
259:
260: return $this;
261: }
262:
263: 264: 265: 266: 267: 268:
269: protected function _registerCoreStoreEvent(Mage_Index_Model_Event $event)
270: {
271: if ($event->getType() == Mage_Index_Model_Event::TYPE_DELETE) {
272:
273: $store = $event->getDataObject();
274: $event->addNewData('catalog_product_flat_delete_store_id', $store->getId());
275: }
276: return $this;
277: }
278:
279: 280: 281: 282: 283:
284: protected function _processEvent(Mage_Index_Model_Event $event)
285: {
286: $data = $event->getNewData();
287: if ($event->getType() == Mage_Catalog_Model_Product_Flat_Indexer::EVENT_TYPE_REBUILD) {
288: $this->_getIndexer()->getResource()->rebuild($data['id']);
289: return;
290: }
291:
292:
293: if (!empty($data['catalog_product_flat_reindex_all'])) {
294: $this->reindexAll();
295: } else if (!empty($data['catalog_product_flat_product_id'])) {
296:
297: $productId = $data['catalog_product_flat_product_id'];
298: $this->_getIndexer()->saveProduct($productId);
299: } else if (!empty($data['catalog_product_flat_product_ids'])) {
300:
301: $productIds = $data['catalog_product_flat_product_ids'];
302:
303: if (!empty($data['catalog_product_flat_website_ids'])) {
304: $websiteIds = $data['catalog_product_flat_website_ids'];
305: foreach ($websiteIds as $websiteId) {
306: $website = Mage::app()->getWebsite($websiteId);
307: foreach ($website->getStores() as $store) {
308: if ($data['catalog_product_flat_action_type'] == 'remove') {
309: $this->_getIndexer()->removeProduct($productIds, $store->getId());
310: } else {
311: $this->_getIndexer()->updateProduct($productIds, $store->getId());
312: }
313: }
314: }
315: }
316:
317: if (isset($data['catalog_product_flat_status'])) {
318: $status = $data['catalog_product_flat_status'];
319: $this->_getIndexer()->updateProductStatus($productIds, $status);
320: }
321:
322: if (isset($data['catalog_product_flat_force_update'])) {
323: $this->_getIndexer()->updateProduct($productIds);
324: }
325: } else if (!empty($data['catalog_product_flat_delete_store_id'])) {
326: $this->_getIndexer()->deleteStore($data['catalog_product_flat_delete_store_id']);
327: }
328: }
329:
330: 331: 332: 333:
334: public function reindexAll()
335: {
336: $this->_getIndexer()->reindexAll();
337: }
338:
339: 340: 341: 342: 343:
344: protected function _getFlatAttributes()
345: {
346: return Mage::getModel('catalog/product_flat_indexer')->getAttributeCodes();
347: }
348: }
349: