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 Observer
30: *
31: * @category Mage
32: * @package Mage_Catalog
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_Catalog_Model_Observer
36: {
37: /**
38: * Process catalog ata related with store data changes
39: *
40: * @param Varien_Event_Observer $observer
41: * @return Mage_Catalog_Model_Observer
42: */
43: public function storeEdit(Varien_Event_Observer $observer)
44: {
45: $store = $observer->getEvent()->getStore();
46: /* @var $store Mage_Core_Model_Store */
47: if ($store->dataHasChangedFor('group_id')) {
48: Mage::app()->reinitStores();
49: /**
50: * @see Mage_Catalog_Model_Indexer_Url
51: */
52: //Mage::getModel('catalog/url')->refreshRewrites($store->getId());
53:
54: /**
55: * @see Mage_Catalog_Model_Category_Indexer_Product
56: */
57: /*Mage::getResourceModel('catalog/category')->refreshProductIndex(
58: array(),
59: array(),
60: array($store->getId())
61: );*/
62: if (Mage::helper('catalog/category_flat')->isEnabled(true)) {
63: Mage::getResourceModel('catalog/category_flat')->synchronize(null, array($store->getId()));
64: }
65: Mage::getResourceSingleton('catalog/product')->refreshEnabledIndex($store);
66: }
67: return $this;
68: }
69:
70: /**
71: * Process catalog data related with new store
72: *
73: * @param Varien_Event_Observer $observer
74: * @return Mage_Catalog_Model_Observer
75: */
76: public function storeAdd(Varien_Event_Observer $observer)
77: {
78: $store = $observer->getEvent()->getStore();
79: /* @var $store Mage_Core_Model_Store */
80: Mage::app()->reinitStores();
81: Mage::getConfig()->reinit();
82:
83: /**
84: * @see Mage_Catalog_Model_Indexer_Url
85: */
86: //Mage::getModel('catalog/url')->refreshRewrites($store->getId());
87: /**
88: * @see Mage_Catalog_Model_Category_Indexer_Product
89: */
90: /*Mage::getResourceSingleton('catalog/category')->refreshProductIndex(
91: array(),
92: array(),
93: array($store->getId())
94: );*/
95: if (Mage::helper('catalog/category_flat')->isEnabled(true)) {
96: Mage::getResourceModel('catalog/category_flat')
97: ->synchronize(null, array($store->getId()));
98: }
99: Mage::getResourceModel('catalog/product')->refreshEnabledIndex($store);
100: return $this;
101: }
102:
103: /**
104: * Process catalog data related with store group root category
105: *
106: * @param Varien_Event_Observer $observer
107: * @return Mage_Catalog_Model_Observer
108: */
109: public function storeGroupSave(Varien_Event_Observer $observer)
110: {
111: $group = $observer->getEvent()->getGroup();
112: /* @var $group Mage_Core_Model_Store_Group */
113: if ($group->dataHasChangedFor('root_category_id') || $group->dataHasChangedFor('website_id')) {
114: Mage::app()->reinitStores();
115: foreach ($group->getStores() as $store) {
116: /**
117: * @see Mage_Catalog_Model_Indexer_Url
118: */
119: //Mage::getModel('catalog/url')->refreshRewrites($store->getId());
120: /**
121: * @see Mage_Catalog_Model_Category_Indexer_Product
122: */
123: /*Mage::getResourceSingleton('catalog/category')->refreshProductIndex(
124: array(),
125: array(),
126: array($store->getId())
127: );*/
128: if (Mage::helper('catalog/category_flat')->isEnabled(true)) {
129: Mage::getResourceModel('catalog/category_flat')
130: ->synchronize(null, array($store->getId()));
131: }
132: }
133: }
134: return $this;
135: }
136:
137: /**
138: * Process delete of store
139: *
140: * @param Varien_Event_Observer $observer
141: * @return Mage_Catalog_Model_Observer
142: */
143: public function storeDelete(Varien_Event_Observer $observer)
144: {
145: if (Mage::helper('catalog/category_flat')->isEnabled(true)) {
146: $store = $observer->getEvent()->getStore();
147: Mage::getResourceModel('catalog/category_flat')->deleteStores($store->getId());
148: }
149: return $this;
150: }
151:
152: /**
153: * Process catalog data after category move
154: *
155: * @param Varien_Event_Observer $observer
156: * @return Mage_Catalog_Model_Observer
157: */
158: public function categoryMove(Varien_Event_Observer $observer)
159: {
160: $categoryId = $observer->getEvent()->getCategoryId();
161: $prevParentId = $observer->getEvent()->getPrevParentId();
162: $parentId = $observer->getEvent()->getParentId();
163: /**
164: * @see Mage_Catalog_Model_Indexer_Url
165: */
166: //Mage::getModel('catalog/url')->refreshCategoryRewrite($categoryId);
167: /**
168: * @see Mage_Catalog_Model_Category_Indexer_Product
169: */
170: /*Mage::getResourceSingleton('catalog/category')->refreshProductIndex(array(
171: $categoryId, $prevParentId, $parentId
172: ));*/
173: //Mage::getModel('catalog/category')->load($prevParentId)->save();
174: //Mage::getModel('catalog/category')->load($parentId)->save();
175: if (Mage::helper('catalog/category_flat')->isEnabled(true)) {
176: Mage::getResourceModel('catalog/category_flat')
177: ->move($categoryId, $prevParentId, $parentId);
178: }
179: return $this;
180: }
181:
182: /**
183: * Process catalog data after products import
184: *
185: * @param Varien_Event_Observer $observer
186: * @return Mage_Catalog_Model_Observer
187: */
188: public function catalogProductImportAfter(Varien_Event_Observer $observer)
189: {
190: Mage::getModel('catalog/url')->refreshRewrites();
191: Mage::getResourceSingleton('catalog/category')->refreshProductIndex();
192: return $this;
193: }
194:
195: /**
196: * Catalog Product Compare Items Clean
197: *
198: * @param Varien_Event_Observer $observer
199: * @return Mage_Catalog_Model_Observer
200: */
201: public function catalogProductCompareClean(Varien_Event_Observer $observer)
202: {
203: Mage::getModel('catalog/product_compare_item')->clean();
204: return $this;
205: }
206:
207: /**
208: * After save event of category
209: *
210: * @param Varien_Event_Observer $observer
211: * @return Mage_Catalog_Model_Observer
212: */
213: public function categorySaveAfter(Varien_Event_Observer $observer)
214: {
215: if (Mage::helper('catalog/category_flat')->isEnabled(true)) {
216: $category = $observer->getEvent()->getCategory();
217: Mage::getResourceModel('catalog/category_flat')->synchronize($category);
218: }
219: return $this;
220: }
221:
222: /**
223: * Checking whether the using static urls in WYSIWYG allowed event
224: *
225: * @param Varien_Event_Observer $observer
226: * @return Mage_Catalog_Model_Observer
227: */
228: public function catalogCheckIsUsingStaticUrlsAllowed(Varien_Event_Observer $observer)
229: {
230: $storeId = $observer->getEvent()->getData('store_id');
231: $result = $observer->getEvent()->getData('result');
232: $result->isAllowed = Mage::helper('catalog')->setStoreId($storeId)->isUsingStaticUrlsAllowed();
233: }
234:
235: /**
236: * Cron job method for product prices to reindex
237: *
238: * @param Mage_Cron_Model_Schedule $schedule
239: */
240: public function reindexProductPrices(Mage_Cron_Model_Schedule $schedule)
241: {
242: $indexProcess = Mage::getSingleton('index/indexer')->getProcessByCode('catalog_product_price');
243: if ($indexProcess) {
244: $indexProcess->reindexAll();
245: }
246: }
247:
248: /**
249: * Adds catalog categories to top menu
250: *
251: * @param Varien_Event_Observer $observer
252: */
253: public function addCatalogToTopmenuItems(Varien_Event_Observer $observer)
254: {
255: $this->_addCategoriesToMenu(Mage::helper('catalog/category')->getStoreCategories(), $observer->getMenu());
256: }
257:
258: /**
259: * Recursively adds categories to top menu
260: *
261: * @param Varien_Data_Tree_Node_Collection|array $categories
262: * @param Varien_Data_Tree_Node $parentCategoryNode
263: */
264: protected function _addCategoriesToMenu($categories, $parentCategoryNode)
265: {
266: foreach ($categories as $category) {
267: if (!$category->getIsActive()) {
268: continue;
269: }
270:
271: $nodeId = 'category-node-' . $category->getId();
272:
273: $tree = $parentCategoryNode->getTree();
274: $categoryData = array(
275: 'name' => $category->getName(),
276: 'id' => $nodeId,
277: 'url' => Mage::helper('catalog/category')->getCategoryUrl($category),
278: 'is_active' => $this->_isActiveMenuCategory($category)
279: );
280: $categoryNode = new Varien_Data_Tree_Node($categoryData, 'id', $tree, $parentCategoryNode);
281: $parentCategoryNode->addChild($categoryNode);
282:
283: if (Mage::helper('catalog/category_flat')->isEnabled()) {
284: $subcategories = (array)$category->getChildrenNodes();
285: } else {
286: $subcategories = $category->getChildren();
287: }
288:
289: $this->_addCategoriesToMenu($subcategories, $categoryNode);
290: }
291: }
292:
293: /**
294: * Checks whether category belongs to active category's path
295: *
296: * @param Varien_Data_Tree_Node $category
297: * @return bool
298: */
299: protected function _isActiveMenuCategory($category)
300: {
301: $catalogLayer = Mage::getSingleton('catalog/layer');
302: if (!$catalogLayer) {
303: return false;
304: }
305:
306: $currentCategory = $catalogLayer->getCurrentCategory();
307: if (!$currentCategory) {
308: return false;
309: }
310:
311: $categoryPathIds = explode(',', $currentCategory->getPathInStore());
312: return in_array($category->getId(), $categoryPathIds);
313: }
314: }
315: