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_Tag_Model_Indexer_Summary extends Mage_Index_Model_Indexer_Abstract
55: {
56: 57: 58:
59: protected $_matchedEntities = array(
60: Mage_Catalog_Model_Product::ENTITY => array(
61: Mage_Index_Model_Event::TYPE_SAVE,
62: Mage_Index_Model_Event::TYPE_DELETE,
63: Mage_Index_Model_Event::TYPE_MASS_ACTION,
64: ),
65: Mage_Tag_Model_Tag::ENTITY => array(
66: Mage_Index_Model_Event::TYPE_SAVE
67: ),
68: Mage_Tag_Model_Tag_Relation::ENTITY => array(
69: Mage_Index_Model_Event::TYPE_SAVE
70: )
71: );
72:
73: 74: 75: 76:
77: protected function _construct()
78: {
79: $this->_init('tag/indexer_summary');
80: }
81:
82: 83: 84: 85: 86:
87: public function getName()
88: {
89: return Mage::helper('tag')->__('Tag Aggregation Data');
90: }
91:
92: 93: 94: 95: 96:
97: public function getDescription()
98: {
99: return Mage::helper('tag')->__('Rebuild Tag aggregation data');
100: }
101:
102: 103: 104: 105: 106:
107: protected function _getProductAttributesDependOn()
108: {
109: return array(
110: 'visibility',
111: 'status',
112: 'website_ids'
113: );
114: }
115:
116: 117: 118: 119: 120:
121: protected function _registerEvent(Mage_Index_Model_Event $event)
122: {
123: if ($event->getEntity() == Mage_Catalog_Model_Product::ENTITY) {
124: $this->_registerCatalogProduct($event);
125: } elseif ($event->getEntity() == Mage_Tag_Model_Tag::ENTITY) {
126: $this->_registerTag($event);
127: } elseif ($event->getEntity() == Mage_Tag_Model_Tag_Relation::ENTITY) {
128: $this->_registerTagRelation($event);
129: }
130: }
131:
132: 133: 134: 135: 136:
137: protected function _registerCatalogProductSaveEvent(Mage_Index_Model_Event $event)
138: {
139:
140: $product = $event->getDataObject();
141: $reindexTag = $product->getForceReindexRequired();
142:
143: foreach ($this->_getProductAttributesDependOn() as $attributeCode) {
144: $reindexTag = $reindexTag || $product->dataHasChangedFor($attributeCode);
145: }
146:
147: if (!$product->isObjectNew() && $reindexTag) {
148: $event->addNewData('tag_reindex_required', true);
149: }
150: }
151:
152: 153: 154: 155: 156:
157: protected function _registerCatalogProductDeleteEvent(Mage_Index_Model_Event $event)
158: {
159: $tagIds = Mage::getModel('tag/tag_relation')
160: ->setProductId($event->getEntityPk())
161: ->getRelatedTagIds();
162: if ($tagIds) {
163: $event->addNewData('tag_reindex_tag_ids', $tagIds);
164: }
165: }
166:
167: 168: 169: 170: 171:
172: protected function _registerCatalogProductMassActionEvent(Mage_Index_Model_Event $event)
173: {
174:
175: $actionObject = $event->getDataObject();
176: $attributes = $this->_getProductAttributesDependOn();
177: $reindexTags = false;
178:
179:
180: $attrData = $actionObject->getAttributesData();
181: if (is_array($attrData)) {
182: foreach ($attributes as $attributeCode) {
183: if (array_key_exists($attributeCode, $attrData)) {
184: $reindexTags = true;
185: break;
186: }
187: }
188: }
189:
190:
191: if ($actionObject->getWebsiteIds()) {
192: $reindexTags = true;
193: }
194:
195:
196: if ($reindexTags) {
197: $tagIds = Mage::getModel('tag/tag_relation')
198: ->setProductId($actionObject->getProductIds())
199: ->getRelatedTagIds();
200: if ($tagIds) {
201: $event->addNewData('tag_reindex_tag_ids', $tagIds);
202: }
203: }
204: }
205:
206: protected function _registerCatalogProduct(Mage_Index_Model_Event $event)
207: {
208: switch ($event->getType()) {
209: case Mage_Index_Model_Event::TYPE_SAVE:
210: $this->_registerCatalogProductSaveEvent($event);
211: break;
212:
213: case Mage_Index_Model_Event::TYPE_DELETE:
214: $this->_registerCatalogProductDeleteEvent($event);
215: break;
216:
217: case Mage_Index_Model_Event::TYPE_MASS_ACTION:
218: $this->_registerCatalogProductMassActionEvent($event);
219: break;
220: }
221: }
222:
223: protected function _registerTag(Mage_Index_Model_Event $event)
224: {
225: if ($event->getType() == Mage_Index_Model_Event::TYPE_SAVE) {
226: $event->addNewData('tag_reindex_tag_id', $event->getEntityPk());
227: }
228: }
229:
230: protected function _registerTagRelation(Mage_Index_Model_Event $event)
231: {
232: if ($event->getType() == Mage_Index_Model_Event::TYPE_SAVE) {
233: $event->addNewData('tag_reindex_tag_id', $event->getDataObject()->getTagId());
234: }
235: }
236:
237: 238: 239: 240: 241:
242: protected function _processEvent(Mage_Index_Model_Event $event)
243: {
244: $this->callEventHandler($event);
245: }
246: }
247: