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: class Mage_Catalog_Model_Product_Indexer_Eav extends Mage_Index_Model_Indexer_Abstract
46: {
47: 48: 49:
50: protected $_matchedEntities = array(
51: Mage_Catalog_Model_Product::ENTITY => array(
52: Mage_Index_Model_Event::TYPE_SAVE,
53: Mage_Index_Model_Event::TYPE_DELETE,
54: Mage_Index_Model_Event::TYPE_MASS_ACTION,
55: ),
56: Mage_Catalog_Model_Resource_Eav_Attribute::ENTITY => array(
57: Mage_Index_Model_Event::TYPE_SAVE,
58: ),
59: Mage_Catalog_Model_Convert_Adapter_Product::ENTITY => array(
60: Mage_Index_Model_Event::TYPE_SAVE
61: )
62: );
63:
64: 65: 66: 67: 68:
69: public function getName()
70: {
71: return Mage::helper('catalog')->__('Product Attributes');
72: }
73:
74: 75: 76: 77: 78:
79: public function getDescription()
80: {
81: return Mage::helper('catalog')->__('Index product attributes for layered navigation building');
82: }
83:
84: 85: 86: 87:
88: protected function _construct()
89: {
90: $this->_init('catalog/product_indexer_eav');
91: }
92:
93: 94: 95: 96: 97:
98: protected function _registerEvent(Mage_Index_Model_Event $event)
99: {
100: $entity = $event->getEntity();
101:
102: if ($entity == Mage_Catalog_Model_Product::ENTITY) {
103: switch ($event->getType()) {
104: case Mage_Index_Model_Event::TYPE_DELETE:
105: $this->_registerCatalogProductDeleteEvent($event);
106: break;
107:
108: case Mage_Index_Model_Event::TYPE_SAVE:
109: $this->_registerCatalogProductSaveEvent($event);
110: break;
111:
112: case Mage_Index_Model_Event::TYPE_MASS_ACTION:
113: $this->_registerCatalogProductMassActionEvent($event);
114: break;
115: }
116: } else if ($entity == Mage_Catalog_Model_Resource_Eav_Attribute::ENTITY) {
117: switch ($event->getType()) {
118: case Mage_Index_Model_Event::TYPE_SAVE:
119: $this->_registerCatalogAttributeSaveEvent($event);
120: break;
121: }
122: } else if ($entity == Mage_Catalog_Model_Convert_Adapter_Product::ENTITY) {
123: $event->addNewData('catalog_product_eav_reindex_all', true);
124: }
125: }
126:
127: 128: 129: 130: 131: 132:
133: protected function _attributeIsIndexable($attribute)
134: {
135: if (!$attribute instanceof Mage_Catalog_Model_Resource_Eav_Attribute) {
136: $attribute = Mage::getSingleton('eav/config')
137: ->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attribute);
138: }
139:
140: return $attribute->isIndexable();
141: }
142:
143: 144: 145: 146: 147: 148:
149: protected function _registerCatalogProductSaveEvent(Mage_Index_Model_Event $event)
150: {
151:
152: $product = $event->getDataObject();
153: $attributes = $product->getAttributes();
154: $reindexEav = $product->getForceReindexRequired();
155: foreach ($attributes as $attribute) {
156: $attributeCode = $attribute->getAttributeCode();
157: if ($this->_attributeIsIndexable($attribute) && $product->dataHasChangedFor($attributeCode)) {
158: $reindexEav = true;
159: break;
160: }
161: }
162:
163: if ($reindexEav) {
164: $event->addNewData('reindex_eav', $reindexEav);
165: }
166:
167: return $this;
168: }
169:
170: 171: 172: 173: 174: 175:
176: protected function _registerCatalogProductDeleteEvent(Mage_Index_Model_Event $event)
177: {
178:
179: $product = $event->getDataObject();
180:
181: $parentIds = $this->_getResource()->getRelationsByChild($product->getId());
182: if ($parentIds) {
183: $event->addNewData('reindex_eav_parent_ids', $parentIds);
184: }
185:
186: return $this;
187: }
188:
189: 190: 191: 192: 193: 194:
195: protected function _registerCatalogProductMassActionEvent(Mage_Index_Model_Event $event)
196: {
197: $reindexEav = false;
198:
199:
200: $actionObject = $event->getDataObject();
201:
202: $attrData = $actionObject->getAttributesData();
203: if (is_array($attrData)) {
204: foreach (array_keys($attrData) as $attributeCode) {
205: if ($this->_attributeIsIndexable($attributeCode)) {
206: $reindexEav = true;
207: break;
208: }
209: }
210: }
211:
212:
213: if ($actionObject->getWebsiteIds()) {
214: $reindexEav = true;
215: }
216:
217:
218: if ($reindexEav) {
219: $event->addNewData('reindex_eav_product_ids', $actionObject->getProductIds());
220: }
221:
222: return $this;
223: }
224:
225: 226: 227: 228: 229: 230:
231: protected function _registerCatalogAttributeSaveEvent(Mage_Index_Model_Event $event)
232: {
233:
234: $attribute = $event->getDataObject();
235: if ($attribute->isIndexable()) {
236: $before = $attribute->getOrigData('is_filterable')
237: || $attribute->getOrigData('is_filterable_in_search')
238: || $attribute->getOrigData('is_visible_in_advanced_search');
239: $after = $attribute->getData('is_filterable')
240: || $attribute->getData('is_filterable_in_search')
241: || $attribute->getData('is_visible_in_advanced_search');
242:
243: if (!$before && $after || $before && !$after) {
244: $event->addNewData('reindex_attribute', 1);
245: $event->addNewData('attribute_index_type', $attribute->getIndexType());
246: $event->addNewData('is_indexable', $after);
247: }
248: }
249:
250: return $this;
251: }
252:
253: 254: 255: 256: 257:
258: protected function _processEvent(Mage_Index_Model_Event $event)
259: {
260: $data = $event->getNewData();
261: if (!empty($data['catalog_product_eav_reindex_all'])) {
262: $this->reindexAll();
263: }
264: if (empty($data['catalog_product_eav_skip_call_event_handler'])) {
265: $this->callEventHandler($event);
266: }
267: }
268: }
269: