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_CatalogSearch
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: * CatalogSearch Fulltext Observer
29: *
30: * @category Mage
31: * @package Mage_CatalogSearch
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_CatalogSearch_Model_Fulltext_Observer
35: {
36: /**
37: * Retrieve fulltext (indexer) model
38: *
39: * @return Mage_CatalogSearch_Model_Fulltext
40: */
41: protected function _getFulltextModel()
42: {
43: return Mage::getSingleton('catalogsearch/fulltext');
44: }
45:
46: /**
47: * Update product index when product data updated
48: *
49: * @param Varien_Event_Observer $observer
50: * @return Mage_CatalogSearch_Model_Fulltext_Observer
51: */
52: public function refreshProductIndex(Varien_Event_Observer $observer)
53: {
54: $product = $observer->getEvent()->getProduct();
55:
56: $this->_getFulltextModel()
57: ->rebuildIndex(null, $product->getId())
58: ->resetSearchResults();
59:
60: return $this;
61: }
62:
63: /**
64: * Clean product index when product deleted or marked as unsearchable/invisible
65: *
66: * @param Varien_Event_Observer $observer
67: * @return Mage_CatalogSearch_Model_Fulltext_Observer
68: */
69: public function cleanProductIndex(Varien_Event_Observer $observer)
70: {
71: $product = $observer->getEvent()->getProduct();
72:
73: $this->_getFulltextModel()
74: ->cleanIndex(null, $product->getId())
75: ->resetSearchResults();
76:
77: return $this;
78: }
79:
80: /**
81: * Update all attribute-dependant index
82: *
83: * @param Varien_Event_Observer $observer
84: * @return Mage_CatalogSearch_Model_Fulltext_Observer
85: */
86: public function eavAttributeChange(Varien_Event_Observer $observer)
87: {
88: $attribute = $observer->getEvent()->getAttribute();
89: /* @var $attribute Mage_Eav_Model_Entity_Attribute */
90: $entityType = Mage::getSingleton('eav/config')->getEntityType(Mage_Catalog_Model_Product::ENTITY);
91: /* @var $entityType Mage_Eav_Model_Entity_Type */
92:
93: if ($attribute->getEntityTypeId() != $entityType->getId()) {
94: return $this;
95: }
96: $delete = $observer->getEventName() == 'eav_entity_attribute_delete_after';
97:
98: if (!$delete && !$attribute->dataHasChangedFor('is_searchable')) {
99: return $this;
100: }
101:
102: $showNotice = false;
103: if ($delete) {
104: if ($attribute->getIsSearchable()) {
105: $showNotice = true;
106: }
107: }
108: elseif ($attribute->dataHasChangedFor('is_searchable')) {
109: $showNotice = true;
110: }
111:
112: if ($showNotice) {
113: $url = Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/system_cache');
114: Mage::getSingleton('adminhtml/session')->addNotice(
115: Mage::helper('catalogsearch')->__('Attribute setting change related with Search Index. Please run <a href="%s">Rebuild Search Index</a> process.', $url)
116: );
117: }
118:
119: return $this;
120: }
121:
122: /**
123: * Rebuild index after import
124: *
125: * @return Mage_CatalogSearch_Model_Fulltext_Observer
126: */
127: public function refreshIndexAfterImport()
128: {
129: $this->_getFulltextModel()
130: ->rebuildIndex();
131: return $this;
132: }
133:
134: /**
135: * Refresh fulltext index when we add new store
136: *
137: * @param Varien_Event_Observer $observer
138: * @return Mage_CatalogSearch_Model_Fulltext_Observer
139: */
140: public function refreshStoreIndex(Varien_Event_Observer $observer)
141: {
142: $storeId = $observer->getEvent()->getStore()->getId();
143: $this->_getFulltextModel()->rebuildIndex($storeId);
144: return $this;
145: }
146:
147: /**
148: * Catalog Product mass website update
149: *
150: * @param Varien_Event_Observer $observer
151: * @return Mage_CatalogSearch_Model_Fulltext_Observer
152: */
153: public function catalogProductWebsiteUpdate(Varien_Event_Observer $observer)
154: {
155: $websiteIds = $observer->getEvent()->getWebsiteIds();
156: $productIds = $observer->getEvent()->getProductIds();
157: $actionType = $observer->getEvent()->getAction();
158:
159: foreach ($websiteIds as $websiteId) {
160: foreach (Mage::app()->getWebsite($websiteId)->getStoreIds() as $storeId) {
161: if ($actionType == 'remove') {
162: $this->_getFulltextModel()
163: ->cleanIndex($storeId, $productIds)
164: ->resetSearchResults();
165: }
166: elseif ($actionType == 'add') {
167: $this->_getFulltextModel()
168: ->rebuildIndex($storeId, $productIds)
169: ->resetSearchResults();
170: }
171: }
172: }
173:
174: return $this;
175: }
176:
177: /**
178: * Store delete processing
179: *
180: * @param Varien_Event_Observer $observer
181: * @return Mage_CatalogSearch_Model_Fulltext_Observer
182: */
183: public function cleanStoreIndex(Varien_Event_Observer $observer)
184: {
185: $store = $observer->getEvent()->getStore();
186: /* @var $store Mage_Core_Model_Store */
187:
188: $this->_getFulltextModel()
189: ->cleanIndex($store->getId());
190:
191: return $this;
192: }
193: }
194: