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