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: class Mage_Tag_Model_Resource_Indexer_Summary extends Mage_Catalog_Model_Resource_Product_Indexer_Abstract
36: {
37: 38: 39: 40:
41: protected function _construct()
42: {
43: $this->_init('tag/summary', 'tag_id');
44: }
45:
46: 47: 48: 49: 50: 51:
52: public function tagSave(Mage_Index_Model_Event $event)
53: {
54: $data = $event->getNewData();
55: if (empty($data['tag_reindex_tag_id'])) {
56: return $this;
57: }
58: return $this->aggregate($data['tag_reindex_tag_id']);
59: }
60:
61: 62: 63: 64: 65: 66:
67: public function tagRelationSave(Mage_Index_Model_Event $event)
68: {
69: $data = $event->getNewData();
70: if (empty($data['tag_reindex_tag_id'])) {
71: return $this;
72: }
73: return $this->aggregate($data['tag_reindex_tag_id']);
74: }
75:
76: 77: 78: 79: 80: 81: 82:
83: public function catalogProductSave(Mage_Index_Model_Event $event)
84: {
85: $data = $event->getNewData();
86: if (empty($data['tag_reindex_required'])) {
87: return $this;
88: }
89:
90: $tagIds = Mage::getModel('tag/tag_relation')
91: ->setProductId($event->getEntityPk())
92: ->getRelatedTagIds();
93:
94: return $this->aggregate($tagIds);
95: }
96:
97: 98: 99: 100: 101: 102: 103:
104: public function catalogProductDelete(Mage_Index_Model_Event $event)
105: {
106: $data = $event->getNewData();
107: if (empty($data['tag_reindex_tag_ids'])) {
108: return $this;
109: }
110: return $this->aggregate($data['tag_reindex_tag_ids']);
111: }
112:
113: 114: 115: 116: 117: 118:
119: public function catalogProductMassAction(Mage_Index_Model_Event $event)
120: {
121: $data = $event->getNewData();
122: if (empty($data['tag_reindex_tag_ids'])) {
123: return $this;
124: }
125: return $this->aggregate($data['tag_reindex_tag_ids']);
126: }
127:
128: 129: 130: 131: 132:
133: public function reindexAll()
134: {
135: return $this->aggregate();
136: }
137:
138: 139: 140: 141: 142: 143:
144: public function aggregate($tagIds = null)
145: {
146: $writeAdapter = $this->_getWriteAdapter();
147: $this->beginTransaction();
148:
149: try {
150: if (!empty($tagIds)) {
151: $writeAdapter->delete(
152: $this->getTable('tag/summary'), array('tag_id IN(?)' => $tagIds)
153: );
154: } else {
155: $writeAdapter->delete($this->getTable('tag/summary'));
156: }
157:
158: $select = $writeAdapter->select()
159: ->from(
160: array('tr' => $this->getTable('tag/relation')),
161: array(
162: 'tr.tag_id',
163: 'tr.store_id',
164: 'customers' => 'COUNT(DISTINCT tr.customer_id)',
165: 'products' => 'COUNT(DISTINCT tr.product_id)',
166: 'popularity' => 'COUNT(tr.customer_id) + MIN('
167: . $writeAdapter->getCheckSql(
168: 'tp.base_popularity IS NOT NULL',
169: 'tp.base_popularity',
170: '0'
171: )
172: . ')',
173: 'uses' => new Zend_Db_Expr(0),
174: 'historical_uses' => new Zend_Db_Expr(0),
175: 'base_popularity' => new Zend_Db_Expr(0)
176: )
177: )
178: ->joinInner(
179: array('cs' => $this->getTable('core/store')),
180: 'cs.store_id = tr.store_id',
181: array()
182: )
183: ->joinInner(
184: array('pw' => $this->getTable('catalog/product_website')),
185: 'cs.website_id = pw.website_id AND tr.product_id = pw.product_id',
186: array()
187: )
188: ->joinInner(
189: array('e' => $this->getTable('catalog/product')),
190: 'tr.product_id = e.entity_id',
191: array()
192: )
193: ->joinLeft(
194: array('tp' => $this->getTable('tag/properties')),
195: 'tp.tag_id = tr.tag_id AND tp.store_id = tr.store_id',
196: array()
197: )
198: ->group(array(
199: 'tr.tag_id',
200: 'tr.store_id'
201: ))
202: ->where('tr.active = 1');
203:
204: $statusCond = $writeAdapter->quoteInto('=?', Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
205: $this->_addAttributeToSelect($select, 'status', 'e.entity_id', 'cs.store_id', $statusCond);
206:
207: $visibilityCond = $writeAdapter
208: ->quoteInto('!=?', Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE);
209: $this->_addAttributeToSelect($select, 'visibility', 'e.entity_id', 'cs.store_id', $visibilityCond);
210:
211: if (!empty($tagIds)) {
212: $select->where('tr.tag_id IN(?)', $tagIds);
213: }
214:
215: Mage::dispatchEvent('prepare_catalog_product_index_select', array(
216: 'select' => $select,
217: 'entity_field' => new Zend_Db_Expr('e.entity_id'),
218: 'website_field' => new Zend_Db_Expr('cs.website_id'),
219: 'store_field' => new Zend_Db_Expr('cs.store_id')
220: ));
221:
222: $writeAdapter->query(
223: $select->insertFromSelect($this->getTable('tag/summary'), array(
224: 'tag_id',
225: 'store_id',
226: 'customers',
227: 'products',
228: 'popularity',
229: 'uses',
230: 'historical_uses',
231: 'base_popularity'
232: ))
233: );
234:
235:
236: $selectedFields = array(
237: 'tag_id' => 'tag_id',
238: 'store_id' => new Zend_Db_Expr(0),
239: 'customers' => 'COUNT(DISTINCT customer_id)',
240: 'products' => 'COUNT(DISTINCT product_id)',
241: 'popularity' => 'COUNT(customer_id)',
242: 'uses' => new Zend_Db_Expr(0),
243: 'historical_uses' => new Zend_Db_Expr(0),
244: 'base_popularity' => new Zend_Db_Expr(0)
245: );
246:
247: $agregateSelect = $writeAdapter->select();
248: $agregateSelect->from($this->getTable('tag/relation'), $selectedFields)
249: ->group('tag_id')
250: ->where('active = 1');
251:
252: if (!empty($tagIds)) {
253: $agregateSelect->where('tag_id IN(?)', $tagIds);
254: }
255:
256: $writeAdapter->query(
257: $agregateSelect->insertFromSelect($this->getTable('tag/summary'), array_keys($selectedFields))
258: );
259: $this->commit();
260: } catch (Exception $e) {
261: $this->rollBack();
262: throw $e;
263: }
264:
265: return $this;
266: }
267: }
268: