1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45:
46: class Mage_Index_Model_Event extends Mage_Core_Model_Abstract
47: {
48: 49: 50:
51: const TYPE_SAVE = 'save';
52: const TYPE_DELETE = 'delete';
53: const TYPE_MASS_ACTION = 'mass_action';
54: const TYPE_REINDEX = 'reindex';
55:
56: 57: 58: 59:
60: protected $_processIds = null;
61:
62: 63: 64: 65: 66:
67: protected $_dataNamespace = null;
68:
69: 70: 71:
72: protected $_process = null;
73:
74: 75: 76:
77: protected function _construct()
78: {
79: $this->_init('index/event');
80: }
81:
82: 83: 84: 85: 86:
87: public function setProcess($process)
88: {
89: $this->_process = $process;
90: return $this;
91: }
92:
93: 94: 95: 96: 97:
98: public function getProcess()
99: {
100: return $this->_process;
101: }
102:
103: 104: 105:
106: public function setDataNamespace($namespace)
107: {
108: $this->_dataNamespace = $namespace;
109: return $this;
110: }
111:
112: 113: 114: 115: 116:
117: public function resetData()
118: {
119: if ($this->_dataNamespace) {
120: $data = $this->getNewData(false);
121: $data[$this->_dataNamespace] = null;
122: $this->setNewData($data);
123: } else {
124: $this->setNewData(array());
125: }
126: return $this;
127: }
128:
129: 130: 131: 132: 133: 134:
135: public function addProcessId($processId, $status=Mage_Index_Model_Process::EVENT_STATUS_NEW)
136: {
137: $this->_processIds[$processId] = $status;
138: return $this;
139: }
140:
141: 142: 143: 144: 145:
146: public function getProcessIds()
147: {
148: return $this->_processIds;
149: }
150:
151: 152: 153: 154: 155: 156: 157:
158: protected function _mergeNewDataRecursive($previous, $current)
159: {
160: if (!is_array($current)) {
161: if (!is_null($current)) {
162: $previous[] = $current;
163: }
164: return $previous;
165: }
166:
167: foreach ($previous as $key => $value) {
168: if (array_key_exists($key, $current) && !is_null($current[$key]) && is_array($previous[$key])) {
169: if (!is_string($key) || is_array($current[$key])) {
170: $current[$key] = $this->_mergeNewDataRecursive($previous[$key], $current[$key]);
171: }
172: } elseif (!array_key_exists($key, $current) || is_null($current[$key])) {
173: $current[$key] = $previous[$key];
174: } elseif (!is_array($previous[$key]) && !is_string($key)) {
175: $current[] = $previous[$key];
176: }
177: }
178:
179: return $current;
180: }
181:
182: 183: 184: 185: 186: 187: 188:
189: public function mergePreviousData($data)
190: {
191: if (!empty($data['event_id'])) {
192: $this->setId($data['event_id']);
193: $this->setCreatedAt($data['created_at']);
194: }
195:
196: if (!empty($data['new_data'])) {
197: $previousNewData = unserialize($data['new_data']);
198: $currentNewData = $this->getNewData(false);
199: $currentNewData = $this->_mergeNewDataRecursive($previousNewData, $currentNewData);
200: $this->setNewData(serialize($currentNewData));
201: }
202: return $this;
203: }
204:
205: 206: 207: 208: 209:
210: public function cleanNewData()
211: {
212: $processIds = $this->getProcessIds();
213: if (!is_array($processIds) || empty($processIds)) {
214: return $this;
215: }
216:
217: $newData = $this->getNewData(false);
218: foreach ($processIds as $processId => $processStatus) {
219: if ($processStatus == Mage_Index_Model_Process::EVENT_STATUS_DONE) {
220: $process = Mage::getSingleton('index/indexer')->getProcessById($processId);
221: if ($process) {
222: $namespace = get_class($process->getIndexer());
223: if (array_key_exists($namespace, $newData)) {
224: unset($newData[$namespace]);
225: }
226: }
227: }
228: }
229: $this->setNewData(serialize($newData));
230:
231: return $this;
232: }
233:
234: 235: 236: 237: 238: 239: 240:
241: public function getOldData($useNamespace = true)
242: {
243: return array();
244: }
245:
246: 247: 248: 249: 250: 251:
252: public function getNewData($useNamespace = true)
253: {
254: $data = $this->_getData('new_data');
255: if (is_string($data)) {
256: $data = unserialize($data);
257: } elseif (empty($data) || !is_array($data)) {
258: $data = array();
259: }
260: if ($useNamespace && $this->_dataNamespace) {
261: return isset($data[$this->_dataNamespace]) ? $data[$this->_dataNamespace] : array();
262: }
263: return $data;
264: }
265:
266: 267: 268: 269: 270: 271: 272: 273:
274: public function addOldData($key, $value=null)
275: {
276: return $this;
277: }
278:
279: 280: 281: 282: 283: 284: 285:
286: public function addNewData($key, $value=null)
287: {
288: $newData = $this->getNewData(false);
289: if (!is_array($key)) {
290: $key = array($key => $value);
291: }
292: if ($this->_dataNamespace) {
293: if (!isset($newData[$this->_dataNamespace])) {
294: $newData[$this->_dataNamespace] = array();
295: }
296: $newData[$this->_dataNamespace] = array_merge($newData[$this->_dataNamespace], $key);
297: } else {
298: $newData = array_merge($newData, $key);
299: }
300: $this->setNewData($newData);
301: return $this;
302: }
303:
304: 305: 306: 307: 308: 309:
310: public function getEntity()
311: {
312: return $this->_getData('entity');
313: }
314:
315: 316: 317: 318: 319: 320:
321: public function getType()
322: {
323: return $this->_getData('type');
324: }
325:
326: 327: 328: 329: 330:
331: protected function _beforeSave()
332: {
333: $newData = $this->getNewData(false);
334: $this->setNewData(serialize($newData));
335: if (!$this->hasCreatedAt()) {
336: $this->setCreatedAt($this->_getResource()->formatDate(time(), true));
337: }
338: return parent::_beforeSave();
339: }
340: }
341: