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

https://bitbucket.org/acidel/buykoala · PHP · 300 lines · 143 code · 41 blank · 116 comment · 36 complexity · dd74f175fedbb447a9dcfb5e0deae14d 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 Flat observer
  28. *
  29. * @category Mage
  30. * @package Mage_Catalog
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Catalog_Model_Product_Flat_Observer
  34. {
  35. /**
  36. * Retrieve Catalog Product Flat Helper
  37. *
  38. * @return Mage_Catalog_Helper_Product_Flat
  39. */
  40. protected function _getHelper()
  41. {
  42. return Mage::helper('catalog/product_flat');
  43. }
  44. /**
  45. * Retrieve Catalog Product Flat Indexer model
  46. *
  47. * @return Mage_Catalog_Model_Product_Flat_Indexer
  48. */
  49. protected function _getIndexer() {
  50. return Mage::getSingleton('catalog/product_flat_indexer');
  51. }
  52. /**
  53. * Catalog Entity attribute after save process
  54. *
  55. * @param Varien_Event_Observer $observer
  56. * @return Mage_Catalog_Model_Product_Flat_Observer
  57. */
  58. public function catalogEntityAttributeSaveAfter(Varien_Event_Observer $observer)
  59. {
  60. if (!$this->_getHelper()->isBuilt()) {
  61. return $this;
  62. }
  63. $attribute = $observer->getEvent()->getAttribute();
  64. /* @var $attribute Mage_Catalog_Model_Entity_Attribute */
  65. $enableBefore = ($attribute->getOrigData('backend_type') == 'static')
  66. || ($this->_getHelper()->isAddFilterableAttributes() && $attribute->getOrigData('is_filterable') > 0)
  67. || ($attribute->getOrigData('used_in_product_listing') == 1)
  68. || ($attribute->getOrigData('used_for_sort_by') == 1);
  69. $enableAfter = ($attribute->getData('backend_type') == 'static')
  70. || ($this->_getHelper()->isAddFilterableAttributes() && $attribute->getData('is_filterable') > 0)
  71. || ($attribute->getData('used_in_product_listing') == 1)
  72. || ($attribute->getData('used_for_sort_by') == 1);
  73. if (!$enableAfter && !$enableBefore) {
  74. return $this;
  75. }
  76. if ($enableBefore && !$enableAfter) {
  77. // delete attribute data from flat
  78. $this->_getIndexer()->prepareDataStorage();
  79. }
  80. else {
  81. $this->_getIndexer()->updateAttribute($attribute->getAttributeCode());
  82. }
  83. return $this;
  84. }
  85. /**
  86. * Catalog Product Status Update
  87. *
  88. * @param Varien_Event_Observer $observer
  89. * @return Mage_Catalog_Model_Product_Flat_Observer
  90. */
  91. public function catalogProductStatusUpdate(Varien_Event_Observer $observer)
  92. {
  93. if (!$this->_getHelper()->isBuilt()) {
  94. return $this;
  95. }
  96. $productId = $observer->getEvent()->getProductId();
  97. $status = $observer->getEvent()->getStatus();
  98. $storeId = $observer->getEvent()->getStoreId();
  99. $storeId = $storeId > 0 ? $storeId : null;
  100. $this->_getIndexer()->updateProductStatus($productId, $status, $storeId);
  101. return $this;
  102. }
  103. /**
  104. * Catalog Product Website(s) update
  105. *
  106. * @param Varien_Event_Observer $observer
  107. * @return Mage_Catalog_Model_Product_Flat_Observer
  108. */
  109. public function catalogProductWebsiteUpdate(Varien_Event_Observer $observer)
  110. {
  111. if (!$this->_getHelper()->isBuilt()) {
  112. return $this;
  113. }
  114. $websiteIds = $observer->getEvent()->getWebsiteIds();
  115. $productIds = $observer->getEvent()->getProductIds();
  116. foreach ($websiteIds as $websiteId) {
  117. $website = Mage::app()->getWebsite($websiteId);
  118. foreach ($website->getStores() as $store) {
  119. if ($observer->getEvent()->getAction() == 'remove') {
  120. $this->_getIndexer()->removeProduct($productIds, $store->getId());
  121. }
  122. else {
  123. $this->_getIndexer()->updateProduct($productIds, $store->getId());
  124. }
  125. }
  126. }
  127. return $this;
  128. }
  129. /**
  130. * Catalog Product After Save
  131. *
  132. * @param Varien_Event_Observer $observer
  133. * @return Mage_Catalog_Model_Product_Flat_Observer
  134. */
  135. public function catalogProductSaveAfter(Varien_Event_Observer $observer) {
  136. if (!$this->_getHelper()->isBuilt()) {
  137. return $this;
  138. }
  139. $product = $observer->getEvent()->getProduct();
  140. $productId = $product->getId();
  141. $this->_getIndexer()->saveProduct($productId);
  142. return $this;
  143. }
  144. /**
  145. * Add new store flat process
  146. *
  147. * @param Varien_Event_Observer $observer
  148. * @return Mage_Catalog_Model_Product_Flat_Observer
  149. */
  150. public function storeAdd(Varien_Event_Observer $observer)
  151. {
  152. if (!$this->_getHelper()->isBuilt()) {
  153. return $this;
  154. }
  155. $store = $observer->getEvent()->getStore();
  156. /* @var $store Mage_Core_Model_Store */
  157. $this->_getIndexer()->rebuild($store->getId());
  158. return $this;
  159. }
  160. /**
  161. * Store edit action, check change store group
  162. *
  163. * @param Varien_Event_Observer $observer
  164. * @return Mage_Catalog_Model_Product_Flat_Observer
  165. */
  166. public function storeEdit(Varien_Event_Observer $observer)
  167. {
  168. if (!$this->_getHelper()->isBuilt()) {
  169. return $this;
  170. }
  171. $store = $observer->getEvent()->getStore();
  172. /* @var $store Mage_Core_Model_Store */
  173. if ($store->dataHasChangedFor('group_id')) {
  174. $this->_getIndexer()->rebuild($store->getId());
  175. }
  176. return $this;
  177. }
  178. /**
  179. * Store delete after process
  180. *
  181. * @param Varien_Event_Observer $observer
  182. * @return Mage_Catalog_Model_Product_Flat_Observer
  183. */
  184. public function storeDelete(Varien_Event_Observer $observer)
  185. {
  186. if (!$this->_getHelper()->isBuilt()) {
  187. return $this;
  188. }
  189. $store = $observer->getEvent()->getStore();
  190. /* @var $store Mage_Core_Model_Store */
  191. $this->_getIndexer()->deleteStore($store->getId());
  192. return $this;
  193. }
  194. /**
  195. * Store Group Save process
  196. *
  197. * @param Varien_Event_Observer $observer
  198. * @return Mage_Catalog_Model_Product_Flat_Observer
  199. */
  200. public function storeGroupSave(Varien_Event_Observer $observer)
  201. {
  202. if (!$this->_getHelper()->isBuilt()) {
  203. return $this;
  204. }
  205. $group = $observer->getEvent()->getGroup();
  206. /* @var $group Mage_Core_Model_Store_Group */
  207. if ($group->dataHasChangedFor('website_id')) {
  208. foreach ($group->getStores() as $store) {
  209. /* @var $store Mage_Core_Model_Store */
  210. $this->_getIndexer()->rebuild($store->getId());
  211. }
  212. }
  213. return $this;
  214. }
  215. /**
  216. * Catalog Product Import After process
  217. *
  218. * @param Varien_Event_Observer $observer
  219. * @return Mage_Catalog_Model_Product_Flat_Observer
  220. */
  221. public function catalogProductImportAfter(Varien_Event_Observer $observer)
  222. {
  223. if (!$this->_getHelper()->isBuilt()) {
  224. return $this;
  225. }
  226. $this->_getIndexer()->rebuild();
  227. return $this;
  228. }
  229. /**
  230. * Customer Group save after process
  231. *
  232. * @param Varien_Event_Observer_Collection $observer
  233. * @return Mage_Catalog_Model_Product_Flat_Observer
  234. */
  235. public function customerGroupSaveAfter(Varien_Event_Observer $observer)
  236. {
  237. if (!$this->_getHelper()->isBuilt()) {
  238. return $this;
  239. }
  240. $customerGroup = $observer->getEvent()->getObject();
  241. /* @var $customerGroup Mage_Customer_Model_Group */
  242. if ($customerGroup->dataHasChangedFor($customerGroup->getIdFieldName())
  243. || $customerGroup->dataHasChangedFor('tax_class_id')) {
  244. $this->_getIndexer()->updateEventAttributes();
  245. }
  246. return $this;
  247. }
  248. /**
  249. * Update category ids in flat
  250. *
  251. * @deprecated 1.3.2.2
  252. * @param Varien_Event_Observer $observer
  253. * @return Mage_Catalog_Model_Product_Flat_Observer
  254. */
  255. public function catalogCategoryChangeProducts(Varien_Event_Observer $observer)
  256. {
  257. return $this;
  258. }
  259. }