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

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

https://gitlab.com/LisovyiEvhenii/ismextensions
PHP | 267 lines | 136 code | 20 blank | 111 comment | 33 complexity | 6bb2b173560b41800c67f1521ff2065c 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@magento.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.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Catalog
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Category products indexer model.
  28. * Responsibility for system actions:
  29. * - Product save (changed assigned categories list)
  30. * - Category save (changed assigned products list or category move)
  31. * - Store save (new store creation, changed store group) - require reindex all data
  32. * - Store group save (changed root category or group website) - require reindex all data
  33. *
  34. * @method Mage_Catalog_Model_Resource_Category_Indexer_Product _getResource()
  35. * @method Mage_Catalog_Model_Resource_Category_Indexer_Product getResource()
  36. * @method int getCategoryId()
  37. * @method Mage_Catalog_Model_Category_Indexer_Product setCategoryId(int $value)
  38. * @method int getProductId()
  39. * @method Mage_Catalog_Model_Category_Indexer_Product setProductId(int $value)
  40. * @method int getPosition()
  41. * @method Mage_Catalog_Model_Category_Indexer_Product setPosition(int $value)
  42. * @method int getIsParent()
  43. * @method Mage_Catalog_Model_Category_Indexer_Product setIsParent(int $value)
  44. * @method int getStoreId()
  45. * @method Mage_Catalog_Model_Category_Indexer_Product setStoreId(int $value)
  46. * @method int getVisibility()
  47. * @method Mage_Catalog_Model_Category_Indexer_Product setVisibility(int $value)
  48. *
  49. * @category Mage
  50. * @package Mage_Catalog
  51. * @author Magento Core Team <core@magentocommerce.com>
  52. */
  53. class Mage_Catalog_Model_Category_Indexer_Product extends Mage_Index_Model_Indexer_Abstract
  54. {
  55. /**
  56. * Data key for matching result to be saved in
  57. */
  58. const EVENT_MATCH_RESULT_KEY = 'catalog_category_product_match_result';
  59. /**
  60. * @var array
  61. */
  62. protected $_matchedEntities = array(
  63. Mage_Catalog_Model_Product::ENTITY => array(
  64. Mage_Index_Model_Event::TYPE_SAVE,
  65. Mage_Index_Model_Event::TYPE_MASS_ACTION
  66. ),
  67. Mage_Catalog_Model_Category::ENTITY => array(
  68. Mage_Index_Model_Event::TYPE_SAVE
  69. ),
  70. Mage_Core_Model_Store::ENTITY => array(
  71. Mage_Index_Model_Event::TYPE_SAVE
  72. ),
  73. Mage_Core_Model_Store_Group::ENTITY => array(
  74. Mage_Index_Model_Event::TYPE_SAVE
  75. ),
  76. Mage_Catalog_Model_Convert_Adapter_Product::ENTITY => array(
  77. Mage_Index_Model_Event::TYPE_SAVE
  78. )
  79. );
  80. /**
  81. * Initialize resource
  82. */
  83. protected function _construct()
  84. {
  85. $this->_init('catalog/category_indexer_product');
  86. }
  87. /**
  88. * Get Indexer name
  89. *
  90. * @return string
  91. */
  92. public function getName()
  93. {
  94. return Mage::helper('catalog')->__('Category Products');
  95. }
  96. /**
  97. * Get Indexer description
  98. *
  99. * @return string
  100. */
  101. public function getDescription()
  102. {
  103. return Mage::helper('catalog')->__('Indexed category/products association');
  104. }
  105. /**
  106. * Check if event can be matched by process.
  107. * Overwrote for specific config save, store and store groups save matching
  108. *
  109. * @param Mage_Index_Model_Event $event
  110. * @return bool
  111. */
  112. public function matchEvent(Mage_Index_Model_Event $event)
  113. {
  114. $data = $event->getNewData();
  115. if (isset($data[self::EVENT_MATCH_RESULT_KEY])) {
  116. return $data[self::EVENT_MATCH_RESULT_KEY];
  117. }
  118. $entity = $event->getEntity();
  119. if ($entity == Mage_Core_Model_Store::ENTITY) {
  120. $store = $event->getDataObject();
  121. if ($store && ($store->isObjectNew() || $store->dataHasChangedFor('group_id'))) {
  122. $result = true;
  123. } else {
  124. $result = false;
  125. }
  126. } elseif ($entity == Mage_Core_Model_Store_Group::ENTITY) {
  127. $storeGroup = $event->getDataObject();
  128. $hasDataChanges = $storeGroup && ($storeGroup->dataHasChangedFor('root_category_id')
  129. || $storeGroup->dataHasChangedFor('website_id'));
  130. if ($storeGroup && !$storeGroup->isObjectNew() && $hasDataChanges) {
  131. $result = true;
  132. } else {
  133. $result = false;
  134. }
  135. } else {
  136. $result = parent::matchEvent($event);
  137. }
  138. $event->addNewData(self::EVENT_MATCH_RESULT_KEY, $result);
  139. return $result;
  140. }
  141. /**
  142. * Register data required by process in event object
  143. * Check if category ids was changed
  144. *
  145. * @param Mage_Index_Model_Event $event
  146. */
  147. protected function _registerEvent(Mage_Index_Model_Event $event)
  148. {
  149. $event->addNewData(self::EVENT_MATCH_RESULT_KEY, true);
  150. $entity = $event->getEntity();
  151. switch ($entity) {
  152. case Mage_Catalog_Model_Product::ENTITY:
  153. $this->_registerProductEvent($event);
  154. break;
  155. case Mage_Catalog_Model_Category::ENTITY:
  156. $this->_registerCategoryEvent($event);
  157. break;
  158. case Mage_Catalog_Model_Convert_Adapter_Product::ENTITY:
  159. $event->addNewData('catalog_category_product_reindex_all', true);
  160. break;
  161. case Mage_Core_Model_Store::ENTITY:
  162. case Mage_Core_Model_Store_Group::ENTITY:
  163. $process = $event->getProcess();
  164. $process->changeStatus(Mage_Index_Model_Process::STATUS_REQUIRE_REINDEX);
  165. break;
  166. }
  167. return $this;
  168. }
  169. /**
  170. * Register event data during product save process
  171. *
  172. * @param Mage_Index_Model_Event $event
  173. */
  174. protected function _registerProductEvent(Mage_Index_Model_Event $event)
  175. {
  176. $eventType = $event->getType();
  177. if ($eventType == Mage_Index_Model_Event::TYPE_SAVE) {
  178. $product = $event->getDataObject();
  179. /**
  180. * Check if product categories data was changed
  181. */
  182. if ($product->getIsChangedCategories() || $product->dataHasChangedFor('status')
  183. || $product->dataHasChangedFor('visibility') || $product->getIsChangedWebsites()) {
  184. $event->addNewData('category_ids', $product->getCategoryIds());
  185. }
  186. } else if ($eventType == Mage_Index_Model_Event::TYPE_MASS_ACTION) {
  187. /* @var $actionObject Varien_Object */
  188. $actionObject = $event->getDataObject();
  189. $attributes = array('status', 'visibility');
  190. $rebuildIndex = false;
  191. // check if attributes changed
  192. $attrData = $actionObject->getAttributesData();
  193. if (is_array($attrData)) {
  194. foreach ($attributes as $attributeCode) {
  195. if (array_key_exists($attributeCode, $attrData)) {
  196. $rebuildIndex = true;
  197. break;
  198. }
  199. }
  200. }
  201. // check changed websites
  202. if ($actionObject->getWebsiteIds()) {
  203. $rebuildIndex = true;
  204. }
  205. // register affected products
  206. if ($rebuildIndex) {
  207. $event->addNewData('product_ids', $actionObject->getProductIds());
  208. }
  209. }
  210. }
  211. /**
  212. * Register event data during category save process
  213. *
  214. * @param Mage_Index_Model_Event $event
  215. */
  216. protected function _registerCategoryEvent(Mage_Index_Model_Event $event)
  217. {
  218. $category = $event->getDataObject();
  219. /**
  220. * Check if product categories data was changed
  221. */
  222. if ($category->getIsChangedProductList()) {
  223. $event->addNewData('products_was_changed', true);
  224. }
  225. /**
  226. * Check if category has another affected category ids (category move result)
  227. */
  228. if ($category->getAffectedCategoryIds()) {
  229. $event->addNewData('affected_category_ids', $category->getAffectedCategoryIds());
  230. }
  231. }
  232. /**
  233. * Process event data and save to index
  234. *
  235. * @param Mage_Index_Model_Event $event
  236. */
  237. protected function _processEvent(Mage_Index_Model_Event $event)
  238. {
  239. $data = $event->getNewData();
  240. if (!empty($data['catalog_category_product_reindex_all'])) {
  241. $this->reindexAll();
  242. }
  243. if (empty($data['catalog_category_product_skip_call_event_handler'])) {
  244. $this->callEventHandler($event);
  245. }
  246. }
  247. }