-
Notifications
You must be signed in to change notification settings - Fork 0
/
Event.php
executable file
·340 lines (314 loc) · 9.19 KB
/
Event.php
1
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magento.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magento.com for more information.
*
* @category Mage
* @package Mage_Index
* @copyright Copyright (c) 2006-2015 X.commerce, Inc. (http://www.magento.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
/**
* Enter description here ...
*
* @method Mage_Index_Model_Resource_Event _getResource()
* @method Mage_Index_Model_Resource_Event getResource()
* @method Mage_Index_Model_Event setType(string $value)
* @method Mage_Index_Model_Event setEntity(string $value)
* @method int getEntityPk()
* @method Mage_Index_Model_Event setEntityPk(int $value)
* @method string getCreatedAt()
* @method Mage_Index_Model_Event setCreatedAt(string $value)
* @method Mage_Index_Model_Event setOldData(string $value)
* @method Mage_Index_Model_Event setNewData(string $value)
* @method Varien_Object getDataObject()
*
* @category Mage
* @package Mage_Index
* @author Magento Core Team <core@magentocommerce.com>
*/
class Mage_Index_Model_Event extends Mage_Core_Model_Abstract
{
/**
* Predefined event types
*/
const TYPE_SAVE = 'save';
const TYPE_DELETE = 'delete';
const TYPE_MASS_ACTION = 'mass_action';
const TYPE_REINDEX = 'reindex';
/**
* Array of related processes ids
* @var array
*/
protected $_processIds = null;
/**
* New and old data namespace. Used for separate processes data
*
* @var string
*/
protected $_dataNamespace = null;
/**
* Process object which currently working with event
*/
protected $_process = null;
/**
* Initialize resource
*/
protected function _construct()
{
$this->_init('index/event');
}
/**
* Specify process object
*
* @param null|Mage_Index_Model_Process $process
*/
public function setProcess($process)
{
$this->_process = $process;
return $this;
}
/**
* Get related process object
*
* @return Mage_Index_Model_Process | null
*/
public function getProcess()
{
return $this->_process;
}
/**
* Specify namespace for old and new data
*/
public function setDataNamespace($namespace)
{
$this->_dataNamespace = $namespace;
return $this;
}
/**
* Reset old and new data arrays
*
* @return Mage_Index_Model_Event
*/
public function resetData()
{
if ($this->_dataNamespace) {
$data = $this->getNewData(false);
$data[$this->_dataNamespace] = null;
$this->setNewData($data);
} else {
$this->setNewData(array());
}
return $this;
}
/**
* Add process id to event object
*
* @param $processId
* @return Mage_Index_Model_Event
*/
public function addProcessId($processId, $status=Mage_Index_Model_Process::EVENT_STATUS_NEW)
{
$this->_processIds[$processId] = $status;
return $this;
}
/**
* Get event process ids
*
* @return array
*/
public function getProcessIds()
{
return $this->_processIds;
}
/**
* Merge new data
*
* @param array $previous
* @param mixed $current
* @return array
*/
protected function _mergeNewDataRecursive($previous, $current)
{
if (!is_array($current)) {
if (!is_null($current)) {
$previous[] = $current;
}
return $previous;
}
foreach ($previous as $key => $value) {
if (array_key_exists($key, $current) && !is_null($current[$key]) && is_array($previous[$key])) {
if (!is_string($key) || is_array($current[$key])) {
$current[$key] = $this->_mergeNewDataRecursive($previous[$key], $current[$key]);
}
} elseif (!array_key_exists($key, $current) || is_null($current[$key])) {
$current[$key] = $previous[$key];
} elseif (!is_array($previous[$key]) && !is_string($key)) {
$current[] = $previous[$key];
}
}
return $current;
}
/**
* Merge previous event data to object.
* Used for events duplicated protection
*
* @param array $data
* @return Mage_Index_Model_Event
*/
public function mergePreviousData($data)
{
if (!empty($data['event_id'])) {
$this->setId($data['event_id']);
$this->setCreatedAt($data['created_at']);
}
if (!empty($data['new_data'])) {
$previousNewData = unserialize($data['new_data']);
$currentNewData = $this->getNewData(false);
$currentNewData = $this->_mergeNewDataRecursive($previousNewData, $currentNewData);
$this->setNewData(serialize($currentNewData));
}
return $this;
}
/**
* Clean new data, unset data for done processes
*
* @return Mage_Index_Model_Event
*/
public function cleanNewData()
{
$processIds = $this->getProcessIds();
if (!is_array($processIds) || empty($processIds)) {
return $this;
}
$newData = $this->getNewData(false);
foreach ($processIds as $processId => $processStatus) {
if ($processStatus == Mage_Index_Model_Process::EVENT_STATUS_DONE) {
$process = Mage::getSingleton('index/indexer')->getProcessById($processId);
if ($process) {
$namespace = get_class($process->getIndexer());
if (array_key_exists($namespace, $newData)) {
unset($newData[$namespace]);
}
}
}
}
$this->setNewData(serialize($newData));
return $this;
}
/**
* Get event old data array
*
* @deprecated since 1.6.2.0
* @param bool $useNamespace
* @return array
*/
public function getOldData($useNamespace = true)
{
return array();
}
/**
* Get event new data array
*
* @param bool $useNamespace
* @return array
*/
public function getNewData($useNamespace = true)
{
$data = $this->_getData('new_data');
if (is_string($data)) {
$data = unserialize($data);
} elseif (empty($data) || !is_array($data)) {
$data = array();
}
if ($useNamespace && $this->_dataNamespace) {
return isset($data[$this->_dataNamespace]) ? $data[$this->_dataNamespace] : array();
}
return $data;
}
/**
* Add new values to old data array (overwrite if value with same key exist)
*
* @deprecated since 1.6.2.0
* @param array | string $data
* @param null | mixed $value
* @return Mage_Index_Model_Event
*/
public function addOldData($key, $value=null)
{
return $this;
}
/**
* Add new values to new data array (overwrite if value with same key exist)
*
* @param array | string $data
* @param null | mixed $value
* @return Mage_Index_Model_Event
*/
public function addNewData($key, $value=null)
{
$newData = $this->getNewData(false);
if (!is_array($key)) {
$key = array($key => $value);
}
if ($this->_dataNamespace) {
if (!isset($newData[$this->_dataNamespace])) {
$newData[$this->_dataNamespace] = array();
}
$newData[$this->_dataNamespace] = array_merge($newData[$this->_dataNamespace], $key);
} else {
$newData = array_merge($newData, $key);
}
$this->setNewData($newData);
return $this;
}
/**
* Get event entity code.
* Entity code declare what kind of data object related with event (product, category etc.)
*
* @return string
*/
public function getEntity()
{
return $this->_getData('entity');
}
/**
* Get event action type.
* Data related on self::TYPE_* constants
*
* @return string
*/
public function getType()
{
return $this->_getData('type');
}
/**
* Serelaize old and new data arrays before saving
*
* @return Mage_Index_Model_Event
*/
protected function _beforeSave()
{
$newData = $this->getNewData(false);
$this->setNewData(serialize($newData));
if (!$this->hasCreatedAt()) {
$this->setCreatedAt($this->_getResource()->formatDate(time(), true));
}
return parent::_beforeSave();
}
}