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: class Mage_Catalog_Model_Product_Indexer_Price extends Mage_Index_Model_Indexer_Abstract
54: {
55: 56: 57:
58: const EVENT_MATCH_RESULT_KEY = 'catalog_product_price_match_result';
59:
60: 61: 62:
63: const EVENT_TYPE_REINDEX_PRICE = 'catalog_reindex_price';
64:
65: 66: 67: 68: 69:
70: protected $_matchedEntities = array(
71: Mage_Catalog_Model_Product::ENTITY => array(
72: Mage_Index_Model_Event::TYPE_SAVE,
73: Mage_Index_Model_Event::TYPE_DELETE,
74: Mage_Index_Model_Event::TYPE_MASS_ACTION,
75: self::EVENT_TYPE_REINDEX_PRICE,
76: ),
77: Mage_Core_Model_Config_Data::ENTITY => array(
78: Mage_Index_Model_Event::TYPE_SAVE
79: ),
80: Mage_Catalog_Model_Convert_Adapter_Product::ENTITY => array(
81: Mage_Index_Model_Event::TYPE_SAVE
82: ),
83: Mage_Customer_Model_Group::ENTITY => array(
84: Mage_Index_Model_Event::TYPE_SAVE
85: )
86: );
87:
88: protected $_relatedConfigSettings = array(
89: Mage_Catalog_Helper_Data::XML_PATH_PRICE_SCOPE,
90: Mage_CatalogInventory_Model_Stock_Item::XML_PATH_MANAGE_STOCK
91: );
92:
93: 94: 95: 96:
97: protected function _construct()
98: {
99: $this->_init('catalog/product_indexer_price');
100: }
101:
102: 103: 104: 105: 106:
107: public function getName()
108: {
109: return Mage::helper('catalog')->__('Product Prices');
110: }
111:
112: 113: 114: 115: 116:
117: public function getDescription()
118: {
119: return Mage::helper('catalog')->__('Index product prices');
120: }
121:
122: 123: 124: 125: 126:
127: protected function _getDependentAttributes()
128: {
129: return array(
130: 'price',
131: 'special_price',
132: 'special_from_date',
133: 'special_to_date',
134: 'tax_class_id',
135: 'status',
136: 'required_options',
137: 'force_reindex_required'
138: );
139: }
140:
141: 142: 143: 144: 145: 146: 147:
148: public function matchEvent(Mage_Index_Model_Event $event)
149: {
150: $data = $event->getNewData();
151: if (isset($data[self::EVENT_MATCH_RESULT_KEY])) {
152: return $data[self::EVENT_MATCH_RESULT_KEY];
153: }
154:
155: if ($event->getEntity() == Mage_Core_Model_Config_Data::ENTITY) {
156: $data = $event->getDataObject();
157: if ($data && in_array($data->getPath(), $this->_relatedConfigSettings)) {
158: $result = $data->isValueChanged();
159: } else {
160: $result = false;
161: }
162: } elseif ($event->getEntity() == Mage_Customer_Model_Group::ENTITY) {
163: $result = $event->getDataObject() && $event->getDataObject()->isObjectNew();
164: } else {
165: $result = parent::matchEvent($event);
166: }
167:
168: $event->addNewData(self::EVENT_MATCH_RESULT_KEY, $result);
169:
170: return $result;
171: }
172:
173: 174: 175: 176: 177:
178: protected function _registerCatalogProductDeleteEvent(Mage_Index_Model_Event $event)
179: {
180:
181: $product = $event->getDataObject();
182:
183: $parentIds = $this->_getResource()->getProductParentsByChild($product->getId());
184: if ($parentIds) {
185: $event->addNewData('reindex_price_parent_ids', $parentIds);
186: }
187: }
188:
189: 190: 191: 192: 193:
194: protected function _registerCatalogProductSaveEvent(Mage_Index_Model_Event $event)
195: {
196:
197: $product = $event->getDataObject();
198: $attributes = $this->_getDependentAttributes();
199: $reindexPrice = $product->getIsRelationsChanged() || $product->getIsCustomOptionChanged()
200: || $product->dataHasChangedFor('tier_price_changed')
201: || $product->getIsChangedWebsites()
202: || $product->getForceReindexRequired();
203:
204: foreach ($attributes as $attributeCode) {
205: $reindexPrice = $reindexPrice || $product->dataHasChangedFor($attributeCode);
206: }
207:
208: if ($reindexPrice) {
209: $event->addNewData('product_type_id', $product->getTypeId());
210: $event->addNewData('reindex_price', 1);
211: }
212: }
213:
214: protected function _registerCatalogProductMassActionEvent(Mage_Index_Model_Event $event)
215: {
216:
217: $actionObject = $event->getDataObject();
218: $attributes = $this->_getDependentAttributes();
219: $reindexPrice = false;
220:
221:
222: $attrData = $actionObject->getAttributesData();
223: if (is_array($attrData)) {
224: foreach ($attributes as $attributeCode) {
225: if (array_key_exists($attributeCode, $attrData)) {
226: $reindexPrice = true;
227: break;
228: }
229: }
230: }
231:
232:
233: if ($actionObject->getWebsiteIds()) {
234: $reindexPrice = true;
235: }
236:
237:
238: if ($reindexPrice) {
239: $event->addNewData('reindex_price_product_ids', $actionObject->getProductIds());
240: }
241: }
242:
243: 244: 245: 246: 247:
248: protected function _registerEvent(Mage_Index_Model_Event $event)
249: {
250: $event->addNewData(self::EVENT_MATCH_RESULT_KEY, true);
251: $entity = $event->getEntity();
252:
253: if ($entity == Mage_Core_Model_Config_Data::ENTITY || $entity == Mage_Customer_Model_Group::ENTITY) {
254: $process = $event->getProcess();
255: $process->changeStatus(Mage_Index_Model_Process::STATUS_REQUIRE_REINDEX);
256: } else if ($entity == Mage_Catalog_Model_Convert_Adapter_Product::ENTITY) {
257: $event->addNewData('catalog_product_price_reindex_all', true);
258: } else if ($entity == Mage_Catalog_Model_Product::ENTITY) {
259: switch ($event->getType()) {
260: case Mage_Index_Model_Event::TYPE_DELETE:
261: $this->_registerCatalogProductDeleteEvent($event);
262: break;
263:
264: case Mage_Index_Model_Event::TYPE_SAVE:
265: $this->_registerCatalogProductSaveEvent($event);
266: break;
267:
268: case Mage_Index_Model_Event::TYPE_MASS_ACTION:
269: $this->_registerCatalogProductMassActionEvent($event);
270: break;
271: case self::EVENT_TYPE_REINDEX_PRICE:
272: $event->addNewData('id', $event->getDataObject()->getId());
273: break;
274: }
275:
276:
277: $indexers = $this->_getResource()->getTypeIndexers();
278: foreach ($indexers as $indexer) {
279: $indexer->registerEvent($event);
280: }
281: }
282: }
283:
284: 285: 286: 287: 288:
289: protected function _processEvent(Mage_Index_Model_Event $event)
290: {
291: $data = $event->getNewData();
292: if ($event->getType() == self::EVENT_TYPE_REINDEX_PRICE) {
293: $this->_getResource()->reindexProductIds($data['id']);
294: return;
295: }
296: if (!empty($data['catalog_product_price_reindex_all'])) {
297: $this->reindexAll();
298: }
299: if (empty($data['catalog_product_price_skip_call_event_handler'])) {
300: $this->callEventHandler($event);
301: }
302: }
303: }
304: