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: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53:
54: class Mage_Catalog_Model_Category_Indexer_Product extends Mage_Index_Model_Indexer_Abstract
55: {
56: 57: 58:
59: const EVENT_MATCH_RESULT_KEY = 'catalog_category_product_match_result';
60:
61: 62: 63:
64: protected $_matchedEntities = array(
65: Mage_Catalog_Model_Product::ENTITY => array(
66: Mage_Index_Model_Event::TYPE_SAVE,
67: Mage_Index_Model_Event::TYPE_MASS_ACTION
68: ),
69: Mage_Catalog_Model_Category::ENTITY => array(
70: Mage_Index_Model_Event::TYPE_SAVE
71: ),
72: Mage_Core_Model_Store::ENTITY => array(
73: Mage_Index_Model_Event::TYPE_SAVE
74: ),
75: Mage_Core_Model_Store_Group::ENTITY => array(
76: Mage_Index_Model_Event::TYPE_SAVE
77: ),
78: Mage_Catalog_Model_Convert_Adapter_Product::ENTITY => array(
79: Mage_Index_Model_Event::TYPE_SAVE
80: )
81: );
82:
83: 84: 85:
86: protected function _construct()
87: {
88: $this->_init('catalog/category_indexer_product');
89: }
90:
91: 92: 93: 94: 95:
96: public function getName()
97: {
98: return Mage::helper('catalog')->__('Category Products');
99: }
100:
101: 102: 103: 104: 105:
106: public function getDescription()
107: {
108: return Mage::helper('catalog')->__('Indexed category/products association');
109: }
110:
111: 112: 113: 114: 115: 116: 117:
118: public function matchEvent(Mage_Index_Model_Event $event)
119: {
120: $data = $event->getNewData();
121: if (isset($data[self::EVENT_MATCH_RESULT_KEY])) {
122: return $data[self::EVENT_MATCH_RESULT_KEY];
123: }
124:
125: $entity = $event->getEntity();
126: if ($entity == Mage_Core_Model_Store::ENTITY) {
127: $store = $event->getDataObject();
128: if ($store && ($store->isObjectNew() || $store->dataHasChangedFor('group_id'))) {
129: $result = true;
130: } else {
131: $result = false;
132: }
133: } elseif ($entity == Mage_Core_Model_Store_Group::ENTITY) {
134: $storeGroup = $event->getDataObject();
135: $hasDataChanges = $storeGroup && ($storeGroup->dataHasChangedFor('root_category_id')
136: || $storeGroup->dataHasChangedFor('website_id'));
137: if ($storeGroup && !$storeGroup->isObjectNew() && $hasDataChanges) {
138: $result = true;
139: } else {
140: $result = false;
141: }
142: } else {
143: $result = parent::matchEvent($event);
144: }
145:
146: $event->addNewData(self::EVENT_MATCH_RESULT_KEY, $result);
147:
148: return $result;
149: }
150:
151:
152: 153: 154: 155: 156: 157:
158: protected function _registerEvent(Mage_Index_Model_Event $event)
159: {
160: $event->addNewData(self::EVENT_MATCH_RESULT_KEY, true);
161: $entity = $event->getEntity();
162: switch ($entity) {
163: case Mage_Catalog_Model_Product::ENTITY:
164: $this->_registerProductEvent($event);
165: break;
166:
167: case Mage_Catalog_Model_Category::ENTITY:
168: $this->_registerCategoryEvent($event);
169: break;
170:
171: case Mage_Catalog_Model_Convert_Adapter_Product::ENTITY:
172: $event->addNewData('catalog_category_product_reindex_all', true);
173: break;
174:
175: case Mage_Core_Model_Store::ENTITY:
176: case Mage_Core_Model_Store_Group::ENTITY:
177: $process = $event->getProcess();
178: $process->changeStatus(Mage_Index_Model_Process::STATUS_REQUIRE_REINDEX);
179: break;
180: }
181: return $this;
182: }
183:
184: 185: 186: 187: 188:
189: protected function _registerProductEvent(Mage_Index_Model_Event $event)
190: {
191: $eventType = $event->getType();
192: if ($eventType == Mage_Index_Model_Event::TYPE_SAVE) {
193: $product = $event->getDataObject();
194: 195: 196:
197: if ($product->getIsChangedCategories() || $product->dataHasChangedFor('status')
198: || $product->dataHasChangedFor('visibility') || $product->getIsChangedWebsites()) {
199: $event->addNewData('category_ids', $product->getCategoryIds());
200: }
201: } else if ($eventType == Mage_Index_Model_Event::TYPE_MASS_ACTION) {
202:
203: $actionObject = $event->getDataObject();
204: $attributes = array('status', 'visibility');
205: $rebuildIndex = false;
206:
207:
208: $attrData = $actionObject->getAttributesData();
209: if (is_array($attrData)) {
210: foreach ($attributes as $attributeCode) {
211: if (array_key_exists($attributeCode, $attrData)) {
212: $rebuildIndex = true;
213: break;
214: }
215: }
216: }
217:
218:
219: if ($actionObject->getWebsiteIds()) {
220: $rebuildIndex = true;
221: }
222:
223:
224: if ($rebuildIndex) {
225: $event->addNewData('product_ids', $actionObject->getProductIds());
226: }
227: }
228: }
229:
230: 231: 232: 233: 234:
235: protected function _registerCategoryEvent(Mage_Index_Model_Event $event)
236: {
237: $category = $event->getDataObject();
238: 239: 240:
241: if ($category->getIsChangedProductList()) {
242: $event->addNewData('products_was_changed', true);
243: }
244: 245: 246:
247: if ($category->getAffectedCategoryIds()) {
248: $event->addNewData('affected_category_ids', $category->getAffectedCategoryIds());
249: }
250: }
251:
252: 253: 254: 255: 256:
257: protected function _processEvent(Mage_Index_Model_Event $event)
258: {
259: $data = $event->getNewData();
260: if (!empty($data['catalog_category_product_reindex_all'])) {
261: $this->reindexAll();
262: }
263: if (empty($data['catalog_category_product_skip_call_event_handler'])) {
264: $this->callEventHandler($event);
265: }
266: }
267: }
268: