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_Index
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: * Abstract index process class
29: * Predefine list of methods required by indexer
30: */
31: abstract class Mage_Index_Model_Indexer_Abstract extends Mage_Core_Model_Abstract
32: {
33: protected $_matchedEntities = array();
34:
35: /**
36: * Whether table changes are allowed
37: *
38: * @deprecated after 1.6.1.0
39: * @var bool
40: */
41: protected $_allowTableChanges = true;
42:
43: /**
44: * Whether the indexer should be displayed on process/list page
45: *
46: * @var bool
47: */
48: protected $_isVisible = true;
49:
50: /**
51: * Get Indexer name
52: *
53: * @return string
54: */
55: abstract public function getName();
56:
57: /**
58: * Get Indexer description
59: *
60: * @return string
61: */
62: public function getDescription()
63: {
64: return '';
65: }
66:
67: /**
68: * Register indexer required data inside event object
69: *
70: * @param Mage_Index_Model_Event $event
71: */
72: abstract protected function _registerEvent(Mage_Index_Model_Event $event);
73:
74: /**
75: * Process event based on event state data
76: *
77: * @param Mage_Index_Model_Event $event
78: */
79: abstract protected function _processEvent(Mage_Index_Model_Event $event);
80:
81: /**
82: * Register data required by process in event object
83: *
84: * @param Mage_Index_Model_Event $event
85: */
86: public function register(Mage_Index_Model_Event $event)
87: {
88: if ($this->matchEvent($event)) {
89: $this->_registerEvent($event);
90: }
91: return $this;
92: }
93:
94: /**
95: * Process event
96: *
97: * @param Mage_Index_Model_Event $event
98: * @return Mage_Index_Model_Indexer_Abstract
99: */
100: public function processEvent(Mage_Index_Model_Event $event)
101: {
102: if ($this->matchEvent($event)) {
103: $this->_processEvent($event);
104: }
105: return $this;
106: }
107:
108: /**
109: * Check if event can be matched by process
110: *
111: * @param Mage_Index_Model_Event $event
112: * @return bool
113: */
114: public function matchEvent(Mage_Index_Model_Event $event)
115: {
116: $entity = $event->getEntity();
117: $type = $event->getType();
118: return $this->matchEntityAndType($entity, $type);
119: }
120:
121: /**
122: * Check if indexer matched specific entity and action type
123: *
124: * @param string $entity
125: * @param string $type
126: * @return bool
127: */
128: public function matchEntityAndType($entity, $type)
129: {
130: if (isset($this->_matchedEntities[$entity])) {
131: if (in_array($type, $this->_matchedEntities[$entity])) {
132: return true;
133: }
134: }
135: return false;
136: }
137:
138: /**
139: * Rebuild all index data
140: */
141: public function reindexAll()
142: {
143: $this->_getResource()->reindexAll();
144: }
145:
146: /**
147: * Try dynamicly detect and call event hanler from resource model.
148: * Handler name will be generated from event entity and type code
149: *
150: * @param Mage_Index_Model_Event $event
151: * @return Mage_Index_Model_Indexer_Abstract
152: */
153: public function callEventHandler(Mage_Index_Model_Event $event)
154: {
155: if ($event->getEntity()) {
156: $method = $this->_camelize($event->getEntity().'_'.$event->getType());
157: } else {
158: $method = $this->_camelize($event->getType());
159: }
160:
161: $resourceModel = $this->_getResource();
162: if (method_exists($resourceModel, $method)) {
163: $resourceModel->$method($event);
164: }
165: return $this;
166: }
167:
168: /**
169: * Set whether table changes are allowed
170: *
171: * @deprecated after 1.6.1.0
172: * @param bool $value
173: * @return Mage_Index_Model_Indexer_Abstract
174: */
175: public function setAllowTableChanges($value = true)
176: {
177: $this->_allowTableChanges = $value;
178: return $this;
179: }
180:
181: /**
182: * Disable resource table keys
183: *
184: * @return Mage_Index_Model_Indexer_Abstract
185: */
186: public function disableKeys()
187: {
188: if (empty($this->_resourceName)) {
189: return $this;
190: }
191:
192: $resourceModel = $this->getResource();
193: if ($resourceModel instanceof Mage_Index_Model_Resource_Abstract) {
194: $resourceModel->useDisableKeys(true);
195: $resourceModel->disableTableKeys();
196: }
197:
198: return $this;
199: }
200:
201: /**
202: * Enable resource table keys
203: *
204: * @return Mage_Index_Model_Indexer_Abstract
205: */
206: public function enableKeys()
207: {
208: if (empty($this->_resourceName)) {
209: return $this;
210: }
211:
212: $resourceModel = $this->getResource();
213: if ($resourceModel instanceof Mage_Index_Model_Resource_Abstract) {
214: $resourceModel->useDisableKeys(true);
215: $resourceModel->enableTableKeys();
216: }
217:
218: return $this;
219: }
220:
221: /**
222: * Whether the indexer should be displayed on process/list page
223: *
224: * @return bool
225: */
226: public function isVisible()
227: {
228: return $this->_isVisible;
229: }
230: }
231: