PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Catalog/Model/Product/Indexer/Eav.php

https://bitbucket.org/acidel/buykoala
PHP | 268 lines | 137 code | 26 blank | 105 comment | 30 complexity | 8e0d9dd32969a864a765ad5d22fdc670 MD5 | raw file
  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) 2011 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. * Catalog Product Eav Indexer Model
  28. *
  29. * @method Mage_Catalog_Model_Resource_Product_Indexer_Eav _getResource()
  30. * @method Mage_Catalog_Model_Resource_Product_Indexer_Eav getResource()
  31. * @method Mage_Catalog_Model_Product_Indexer_Eav setEntityId(int $value)
  32. * @method int getAttributeId()
  33. * @method Mage_Catalog_Model_Product_Indexer_Eav setAttributeId(int $value)
  34. * @method int getStoreId()
  35. * @method Mage_Catalog_Model_Product_Indexer_Eav setStoreId(int $value)
  36. * @method int getValue()
  37. * @method Mage_Catalog_Model_Product_Indexer_Eav setValue(int $value)
  38. *
  39. * @category Mage
  40. * @package Mage_Catalog
  41. * @author Magento Core Team <core@magentocommerce.com>
  42. */
  43. class Mage_Catalog_Model_Product_Indexer_Eav extends Mage_Index_Model_Indexer_Abstract
  44. {
  45. /**
  46. * @var array
  47. */
  48. protected $_matchedEntities = array(
  49. Mage_Catalog_Model_Product::ENTITY => array(
  50. Mage_Index_Model_Event::TYPE_SAVE,
  51. Mage_Index_Model_Event::TYPE_DELETE,
  52. Mage_Index_Model_Event::TYPE_MASS_ACTION,
  53. ),
  54. Mage_Catalog_Model_Resource_Eav_Attribute::ENTITY => array(
  55. Mage_Index_Model_Event::TYPE_SAVE,
  56. ),
  57. Mage_Catalog_Model_Convert_Adapter_Product::ENTITY => array(
  58. Mage_Index_Model_Event::TYPE_SAVE
  59. )
  60. );
  61. /**
  62. * Retrieve Indexer name
  63. *
  64. * @return string
  65. */
  66. public function getName()
  67. {
  68. return Mage::helper('catalog')->__('Product Attributes');
  69. }
  70. /**
  71. * Retrieve Indexer description
  72. *
  73. * @return string
  74. */
  75. public function getDescription()
  76. {
  77. return Mage::helper('catalog')->__('Index product attributes for layered navigation building');
  78. }
  79. /**
  80. * Initialize resource model
  81. *
  82. */
  83. protected function _construct()
  84. {
  85. $this->_init('catalog/product_indexer_eav');
  86. }
  87. /**
  88. * Register data required by process in event object
  89. *
  90. * @param Mage_Index_Model_Event $event
  91. */
  92. protected function _registerEvent(Mage_Index_Model_Event $event)
  93. {
  94. $entity = $event->getEntity();
  95. if ($entity == Mage_Catalog_Model_Product::ENTITY) {
  96. switch ($event->getType()) {
  97. case Mage_Index_Model_Event::TYPE_DELETE:
  98. $this->_registerCatalogProductDeleteEvent($event);
  99. break;
  100. case Mage_Index_Model_Event::TYPE_SAVE:
  101. $this->_registerCatalogProductSaveEvent($event);
  102. break;
  103. case Mage_Index_Model_Event::TYPE_MASS_ACTION:
  104. $this->_registerCatalogProductMassActionEvent($event);
  105. break;
  106. }
  107. } else if ($entity == Mage_Catalog_Model_Resource_Eav_Attribute::ENTITY) {
  108. switch ($event->getType()) {
  109. case Mage_Index_Model_Event::TYPE_SAVE:
  110. $this->_registerCatalogAttributeSaveEvent($event);
  111. break;
  112. }
  113. } else if ($entity == Mage_Catalog_Model_Convert_Adapter_Product::ENTITY) {
  114. $event->addNewData('catalog_product_eav_reindex_all', true);
  115. }
  116. }
  117. /**
  118. * Check is attribute indexable in EAV
  119. *
  120. * @param Mage_Catalog_Model_Resource_Eav_Attribute|string $attribute
  121. * @return bool
  122. */
  123. protected function _attributeIsIndexable($attribute)
  124. {
  125. if (!$attribute instanceof Mage_Catalog_Model_Resource_Eav_Attribute) {
  126. $attribute = Mage::getSingleton('eav/config')
  127. ->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attribute);
  128. }
  129. return $attribute->isIndexable();
  130. }
  131. /**
  132. * Register data required by process in event object
  133. *
  134. * @param Mage_Index_Model_Event $event
  135. * @return Mage_Catalog_Model_Product_Indexer_Eav
  136. */
  137. protected function _registerCatalogProductSaveEvent(Mage_Index_Model_Event $event)
  138. {
  139. /* @var $product Mage_Catalog_Model_Product */
  140. $product = $event->getDataObject();
  141. $attributes = $product->getAttributes();
  142. $reindexEav = $product->getForceReindexRequired();
  143. foreach ($attributes as $attribute) {
  144. $attributeCode = $attribute->getAttributeCode();
  145. if ($this->_attributeIsIndexable($attribute) && $product->dataHasChangedFor($attributeCode)) {
  146. $reindexEav = true;
  147. break;
  148. }
  149. }
  150. if ($reindexEav) {
  151. $event->addNewData('reindex_eav', $reindexEav);
  152. }
  153. return $this;
  154. }
  155. /**
  156. * Register data required by process in event object
  157. *
  158. * @param Mage_Index_Model_Event $event
  159. * @return Mage_Catalog_Model_Product_Indexer_Eav
  160. */
  161. protected function _registerCatalogProductDeleteEvent(Mage_Index_Model_Event $event)
  162. {
  163. /* @var $product Mage_Catalog_Model_Product */
  164. $product = $event->getDataObject();
  165. $parentIds = $this->_getResource()->getRelationsByChild($product->getId());
  166. if ($parentIds) {
  167. $event->addNewData('reindex_eav_parent_ids', $parentIds);
  168. }
  169. return $this;
  170. }
  171. /**
  172. * Register data required by process in event object
  173. *
  174. * @param Mage_Index_Model_Event $event
  175. * @return Mage_Catalog_Model_Product_Indexer_Eav
  176. */
  177. protected function _registerCatalogProductMassActionEvent(Mage_Index_Model_Event $event)
  178. {
  179. $reindexEav = false;
  180. /* @var $actionObject Varien_Object */
  181. $actionObject = $event->getDataObject();
  182. // check if attributes changed
  183. $attrData = $actionObject->getAttributesData();
  184. if (is_array($attrData)) {
  185. foreach (array_keys($attrData) as $attributeCode) {
  186. if ($this->_attributeIsIndexable($attributeCode)) {
  187. $reindexEav = true;
  188. break;
  189. }
  190. }
  191. }
  192. // check changed websites
  193. if ($actionObject->getWebsiteIds()) {
  194. $reindexEav = true;
  195. }
  196. // register affected products
  197. if ($reindexEav) {
  198. $event->addNewData('reindex_eav_product_ids', $actionObject->getProductIds());
  199. }
  200. return $this;
  201. }
  202. /**
  203. * Register data required by process attribute save in event object
  204. *
  205. * @param Mage_Index_Model_Event $event
  206. * @return Mage_Catalog_Model_Product_Indexer_Eav
  207. */
  208. protected function _registerCatalogAttributeSaveEvent(Mage_Index_Model_Event $event)
  209. {
  210. /* @var $attribute Mage_Catalog_Model_Resource_Eav_Attribute */
  211. $attribute = $event->getDataObject();
  212. if ($attribute->isIndexable()) {
  213. $before = $attribute->getOrigData('is_filterable')
  214. || $attribute->getOrigData('is_filterable_in_search')
  215. || $attribute->getOrigData('is_visible_in_advanced_search');
  216. $after = $attribute->getData('is_filterable')
  217. || $attribute->getData('is_filterable_in_search')
  218. || $attribute->getData('is_visible_in_advanced_search');
  219. if (!$before && $after || $before && !$after) {
  220. $event->addNewData('reindex_attribute', 1);
  221. $event->addNewData('attribute_index_type', $attribute->getIndexType());
  222. $event->addNewData('is_indexable', $after);
  223. }
  224. }
  225. return $this;
  226. }
  227. /**
  228. * Process event
  229. *
  230. * @param Mage_Index_Model_Event $event
  231. */
  232. protected function _processEvent(Mage_Index_Model_Event $event)
  233. {
  234. $data = $event->getNewData();
  235. if (!empty($data['catalog_product_eav_reindex_all'])) {
  236. $this->reindexAll();
  237. }
  238. if (empty($data['catalog_product_eav_skip_call_event_handler'])) {
  239. $this->callEventHandler($event);
  240. }
  241. }
  242. }