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

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

https://bitbucket.org/claudiu_marginean/magento-hg-mirror
PHP | 303 lines | 190 code | 24 blank | 89 comment | 62 complexity | cc56de3819f0d9604341372d29e6b9a8 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  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) 2010 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. class Mage_Catalog_Model_Product_Indexer_Flat extends Mage_Index_Model_Indexer_Abstract
  27. {
  28. /**
  29. * Index math Entities array
  30. *
  31. * @var array
  32. */
  33. protected $_matchedEntities = array(
  34. Mage_Catalog_Model_Product::ENTITY => array(
  35. Mage_Index_Model_Event::TYPE_SAVE,
  36. Mage_Index_Model_Event::TYPE_MASS_ACTION,
  37. ),
  38. Mage_Catalog_Model_Resource_Eav_Attribute::ENTITY => array(
  39. Mage_Index_Model_Event::TYPE_SAVE,
  40. Mage_Index_Model_Event::TYPE_DELETE,
  41. ),
  42. Mage_Core_Model_Store::ENTITY => array(
  43. Mage_Index_Model_Event::TYPE_SAVE,
  44. Mage_Index_Model_Event::TYPE_DELETE
  45. ),
  46. Mage_Core_Model_Store_Group::ENTITY => array(
  47. Mage_Index_Model_Event::TYPE_SAVE
  48. ),
  49. Mage_Catalog_Model_Convert_Adapter_Product::ENTITY => array(
  50. Mage_Index_Model_Event::TYPE_SAVE
  51. )
  52. );
  53. /**
  54. * Retrieve Indexer name
  55. *
  56. * @return string
  57. */
  58. public function getName()
  59. {
  60. return Mage::helper('catalog')->__('Product Flat Data');
  61. }
  62. /**
  63. * Retrieve Indexer description
  64. *
  65. * @return string
  66. */
  67. public function getDescription()
  68. {
  69. return Mage::helper('catalog')->__('Reorganize EAV product structure to flat structure');
  70. }
  71. /**
  72. * Retrieve Catalog Product Flat Indexer model
  73. *
  74. * @return Mage_Catalog_Model_Product_Flat_Indexer
  75. */
  76. protected function _getIndexer()
  77. {
  78. return Mage::getSingleton('catalog/product_flat_indexer');
  79. }
  80. /**
  81. * Check if event can be matched by process
  82. * Overwrote for check is flat catalog product is enabled and specific save
  83. * attribute, store, store_group
  84. *
  85. * @param Mage_Index_Model_Event $event
  86. * @return bool
  87. */
  88. public function matchEvent(Mage_Index_Model_Event $event)
  89. {
  90. if (!Mage::helper('catalog/product_flat')->isBuilt()) {
  91. return false;
  92. }
  93. $data = $event->getNewData();
  94. $resultKey = 'catalog_product_flat_match_result';
  95. if (isset($data[$resultKey])) {
  96. return $data[$resultKey];
  97. }
  98. $result = null;
  99. $entity = $event->getEntity();
  100. if ($entity == Mage_Catalog_Model_Resource_Eav_Attribute::ENTITY) {
  101. /* @var $attribute Mage_Catalog_Model_Resource_Eav_Attribute */
  102. $attribute = $event->getDataObject();
  103. $addFilterable = Mage::helper('catalog/product_flat')->isAddFilterableAttributes();
  104. $enableBefore = ($attribute->getOrigData('backend_type') == 'static')
  105. || ($addFilterable && $attribute->getOrigData('is_filterable') > 0)
  106. || ($attribute->getOrigData('used_in_product_listing') == 1)
  107. || ($attribute->getOrigData('used_for_sort_by') == 1);
  108. $enableAfter = ($attribute->getData('backend_type') == 'static')
  109. || ($addFilterable && $attribute->getData('is_filterable') > 0)
  110. || ($attribute->getData('used_in_product_listing') == 1)
  111. || ($attribute->getData('used_for_sort_by') == 1);
  112. if ($event->getType() == Mage_Index_Model_Event::TYPE_DELETE) {
  113. $result = $enableBefore;
  114. } else if ($event->getType() == Mage_Index_Model_Event::TYPE_SAVE) {
  115. if ($enableAfter || $enableBefore) {
  116. $result = true;
  117. } else {
  118. $result = false;
  119. }
  120. } else {
  121. $result = false;
  122. }
  123. } else if ($entity == Mage_Core_Model_Store::ENTITY) {
  124. if ($event->getType() == Mage_Index_Model_Event::TYPE_DELETE) {
  125. $result = true;
  126. } else {
  127. /* @var $store Mage_Core_Model_Store */
  128. $store = $event->getDataObject();
  129. if ($store->isObjectNew()) {
  130. $result = true;
  131. } else {
  132. $result = false;
  133. }
  134. }
  135. } else if ($entity == Mage_Core_Model_Store_Group::ENTITY) {
  136. /* @var $storeGroup Mage_Core_Model_Store_Group */
  137. $storeGroup = $event->getDataObject();
  138. if ($storeGroup->dataHasChangedFor('website_id')) {
  139. $result = true;
  140. } else {
  141. $result = false;
  142. }
  143. } else {
  144. $result = parent::matchEvent($event);
  145. }
  146. $event->addNewData($resultKey, $result);
  147. return $result;
  148. }
  149. /**
  150. * Register data required by process in event object
  151. *
  152. * @param Mage_Index_Model_Event $event
  153. */
  154. protected function _registerEvent(Mage_Index_Model_Event $event)
  155. {
  156. switch ($event->getEntity()) {
  157. case Mage_Catalog_Model_Product::ENTITY:
  158. $this->_registerCatalogProductEvent($event);
  159. break;
  160. case Mage_Catalog_Model_Convert_Adapter_Product::ENTITY:
  161. $event->addNewData('catalog_product_flat_reindex_all', true);
  162. break;
  163. case Mage_Core_Model_Store::ENTITY:
  164. if ($event->getType() == Mage_Index_Model_Event::TYPE_DELETE) {
  165. $this->_registerCoreStoreEvent($event);
  166. break;
  167. }
  168. case Mage_Catalog_Model_Resource_Eav_Attribute::ENTITY:
  169. case Mage_Core_Model_Store_Group::ENTITY:
  170. $event->addNewData('catalog_product_flat_skip_call_event_handler', true);
  171. $process = $event->getProcess();
  172. $process->changeStatus(Mage_Index_Model_Process::STATUS_REQUIRE_REINDEX);
  173. break;
  174. }
  175. }
  176. /**
  177. * Register data required by catalog product process in event object
  178. *
  179. * @param Mage_Index_Model_Event $event
  180. * @return Mage_CatalogSearch_Model_Indexer_Search
  181. */
  182. protected function _registerCatalogProductEvent(Mage_Index_Model_Event $event)
  183. {
  184. switch ($event->getType()) {
  185. case Mage_Index_Model_Event::TYPE_SAVE:
  186. /* @var $product Mage_Catalog_Model_Product */
  187. $product = $event->getDataObject();
  188. $event->addNewData('catalog_product_flat_product_id', $product->getId());
  189. break;
  190. case Mage_Index_Model_Event::TYPE_MASS_ACTION:
  191. /* @var $actionObject Varien_Object */
  192. $actionObject = $event->getDataObject();
  193. $reindexData = array();
  194. $reindexFlat = false;
  195. // check if status changed
  196. $attrData = $actionObject->getAttributesData();
  197. if (isset($attrData['status'])) {
  198. $reindexFlat = true;
  199. $reindexData['catalog_product_flat_status'] = $attrData['status'];
  200. }
  201. // check changed websites
  202. if ($actionObject->getWebsiteIds()) {
  203. $reindexFlat = true;
  204. $reindexData['catalog_product_flat_website_ids'] = $actionObject->getWebsiteIds();
  205. $reindexData['catalog_product_flat_action_type'] = $actionObject->getActionType();
  206. }
  207. // register affected products
  208. if ($reindexFlat) {
  209. $reindexData['catalog_product_flat_product_ids'] = $actionObject->getProductIds();
  210. foreach ($reindexData as $k => $v) {
  211. $event->addNewData($k, $v);
  212. }
  213. }
  214. break;
  215. }
  216. return $this;
  217. }
  218. /**
  219. * Register core store delete process
  220. *
  221. * @param Mage_Index_Model_Event $event
  222. * @return Mage_Catalog_Model_Product_Indexer_Flat
  223. */
  224. protected function _registerCoreStoreEvent(Mage_Index_Model_Event $event)
  225. {
  226. if ($event->getType() == Mage_Index_Model_Event::TYPE_DELETE) {
  227. /* @var $store Mage_Core_Model_Store */
  228. $store = $event->getDataObject();
  229. $event->addNewData('catalog_product_flat_delete_store_id', $store->getId());
  230. }
  231. return $this;
  232. }
  233. /**
  234. * Process event
  235. *
  236. * @param Mage_Index_Model_Event $event
  237. */
  238. protected function _processEvent(Mage_Index_Model_Event $event)
  239. {
  240. $data = $event->getNewData();
  241. if (!empty($data['catalog_product_flat_reindex_all'])) {
  242. $this->reindexAll();
  243. } else if (!empty($data['catalog_product_flat_product_id'])) {
  244. // catalog_product save
  245. $productId = $data['catalog_product_flat_product_id'];
  246. $this->_getIndexer()->saveProduct($productId);
  247. } else if (!empty($data['catalog_product_flat_product_ids'])) {
  248. // catalog_product mass_action
  249. $productIds = $data['catalog_product_flat_product_ids'];
  250. if (!empty($data['catalog_product_flat_website_ids'])) {
  251. $websiteIds = $data['catalog_product_flat_website_ids'];
  252. foreach ($websiteIds as $websiteId) {
  253. $website = Mage::app()->getWebsite($websiteId);
  254. foreach ($website->getStores() as $store) {
  255. if ($data['catalog_product_flat_action_type'] == 'remove') {
  256. $this->_getIndexer()->removeProduct($productIds, $store->getId());
  257. } else {
  258. $this->_getIndexer()->updateProduct($productIds, $store->getId());
  259. }
  260. }
  261. }
  262. }
  263. if (isset($data['catalog_product_flat_status'])) {
  264. $status = $data['catalog_product_flat_status'];
  265. $this->_getIndexer()->updateProductStatus($productIds, $status);
  266. }
  267. } else if (!empty($data['catalog_product_flat_delete_store_id'])) {
  268. $this->_getIndexer()->deleteStore($data['catalog_product_flat_delete_store_id']);
  269. }
  270. }
  271. /**
  272. * Rebuild all index data
  273. *
  274. */
  275. public function reindexAll()
  276. {
  277. $this->_getIndexer()->rebuild();
  278. }
  279. }