1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Catalog
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27:
28: /**
29: * Catalog Product Eav Indexer Resource Model
30: *
31: * @category Mage
32: * @package Mage_Catalog
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_Catalog_Model_Resource_Product_Indexer_Eav extends Mage_Catalog_Model_Resource_Product_Indexer_Abstract
36: {
37: /**
38: * EAV Indexers by type
39: *
40: * @var array
41: */
42: protected $_types;
43:
44: /**
45: * Define main index table
46: *
47: */
48: protected function _construct()
49: {
50: $this->_init('catalog/product_index_eav', 'entity_id');
51: }
52:
53: /**
54: * Retrieve array of EAV type indexers
55: *
56: * @return array
57: */
58: public function getIndexers()
59: {
60: if (is_null($this->_types)) {
61: $this->_types = array(
62: 'source' => Mage::getResourceModel('catalog/product_indexer_eav_source'),
63: 'decimal' => Mage::getResourceModel('catalog/product_indexer_eav_decimal'),
64: );
65: }
66:
67: return $this->_types;
68: }
69:
70: /**
71: * Retrieve indexer instance by type
72: *
73: * @param string $type
74: * @return Mage_Catalog_Model_Resource_Product_Indexer_Eav_Abstract
75: */
76: public function getIndexer($type)
77: {
78: $indexers = $this->getIndexers();
79: if (!isset($indexers[$type])) {
80: Mage::throwException(Mage::helper('catalog')->__('Unknown EAV indexer type "%s".', $type));
81: }
82: return $indexers[$type];
83: }
84:
85: /**
86: * Process product save.
87: * Method is responsible for index support
88: * when product was saved and assigned categories was changed.
89: *
90: * @param Mage_Index_Model_Event $event
91: * @return Mage_Catalog_Model_Resource_Product_Indexer_Eav
92: */
93: public function catalogProductSave(Mage_Index_Model_Event $event)
94: {
95: $productId = $event->getEntityPk();
96: $data = $event->getNewData();
97:
98: /**
99: * Check if filterable attribute values were updated
100: */
101: if (!isset($data['reindex_eav'])) {
102: return $this;
103: }
104:
105: foreach ($this->getIndexers() as $indexer) {
106: /** @var $indexer Mage_Catalog_Model_Resource_Product_Indexer_Eav_Abstract */
107: $indexer->reindexEntities($productId);
108: }
109:
110: return $this;
111: }
112:
113: /**
114: * Process Product Delete
115: *
116: * @param Mage_Index_Model_Event $event
117: * @return Mage_Catalog_Model_Resource_Product_Indexer_Eav
118: */
119: public function catalogProductDelete(Mage_Index_Model_Event $event)
120: {
121: $data = $event->getNewData();
122: if (empty($data['reindex_eav_parent_ids'])) {
123: return $this;
124: }
125:
126: foreach ($this->getIndexers() as $indexer) {
127: /** @var $indexer Mage_Catalog_Model_Resource_Product_Indexer_Eav_Abstract */
128: $indexer->reindexEntities($data['reindex_eav_parent_ids']);
129: }
130:
131: return $this;
132: }
133:
134: /**
135: * Process Product Mass Update
136: *
137: * @param Mage_Index_Model_Event $event
138: * @return Mage_Catalog_Model_Resource_Product_Indexer_Eav
139: */
140: public function catalogProductMassAction(Mage_Index_Model_Event $event)
141: {
142: $data = $event->getNewData();
143: if (empty($data['reindex_eav_product_ids'])) {
144: return $this;
145: }
146:
147: foreach ($this->getIndexers() as $indexer) {
148: /** @var $indexer Mage_Catalog_Model_Resource_Product_Indexer_Eav_Abstract */
149: $indexer->reindexEntities($data['reindex_eav_product_ids']);
150: }
151:
152: return $this;
153: }
154:
155: /**
156: * Process Catalog Eav Attribute Save
157: *
158: * @param Mage_Index_Model_Event $event
159: * @return Mage_Catalog_Model_Resource_Product_Indexer_Eav
160: */
161: public function catalogEavAttributeSave(Mage_Index_Model_Event $event)
162: {
163: $data = $event->getNewData();
164: if (empty($data['reindex_attribute'])) {
165: return $this;
166: }
167:
168: $indexer = $this->getIndexer($data['attribute_index_type']);
169:
170: $indexer->reindexAttribute($event->getEntityPk(), !empty($data['is_indexable']));
171:
172: return $this;
173: }
174:
175: /**
176: * Rebuild all index data
177: *
178: * @return Mage_Catalog_Model_Resource_Product_Indexer_Eav
179: */
180: public function reindexAll()
181: {
182: $this->useIdxTable(true);
183: foreach ($this->getIndexers() as $indexer) {
184: /** @var $indexer Mage_Catalog_Model_Resource_Product_Indexer_Eav_Abstract */
185: $indexer->reindexAll();
186: }
187:
188: return $this;
189: }
190:
191: /**
192: * Retrieve temporary source index table name
193: *
194: * @param string $table
195: * @return string
196: */
197: public function getIdxTable($table = null)
198: {
199: if ($this->useIdxTable()) {
200: return $this->getTable('catalog/product_eav_indexer_idx');
201: }
202: return $this->getTable('catalog/product_eav_indexer_tmp');
203: }
204: }
205: