PageRenderTime 27ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Index/Model/Event.php

https://gitlab.com/LisovyiEvhenii/ismextensions
PHP | 340 lines | 160 code | 26 blank | 154 comment | 28 complexity | 4ddf63e7fa648061409e0225cd44e404 MD5 | raw file
  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@magento.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.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Index
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Enter description here ...
  28. *
  29. * @method Mage_Index_Model_Resource_Event _getResource()
  30. * @method Mage_Index_Model_Resource_Event getResource()
  31. * @method Mage_Index_Model_Event setType(string $value)
  32. * @method Mage_Index_Model_Event setEntity(string $value)
  33. * @method int getEntityPk()
  34. * @method Mage_Index_Model_Event setEntityPk(int $value)
  35. * @method string getCreatedAt()
  36. * @method Mage_Index_Model_Event setCreatedAt(string $value)
  37. * @method Mage_Index_Model_Event setOldData(string $value)
  38. * @method Mage_Index_Model_Event setNewData(string $value)
  39. * @method Varien_Object getDataObject()
  40. *
  41. * @category Mage
  42. * @package Mage_Index
  43. * @author Magento Core Team <core@magentocommerce.com>
  44. */
  45. class Mage_Index_Model_Event extends Mage_Core_Model_Abstract
  46. {
  47. /**
  48. * Predefined event types
  49. */
  50. const TYPE_SAVE = 'save';
  51. const TYPE_DELETE = 'delete';
  52. const TYPE_MASS_ACTION = 'mass_action';
  53. const TYPE_REINDEX = 'reindex';
  54. /**
  55. * Array of related processes ids
  56. * @var array
  57. */
  58. protected $_processIds = null;
  59. /**
  60. * New and old data namespace. Used for separate processes data
  61. *
  62. * @var string
  63. */
  64. protected $_dataNamespace = null;
  65. /**
  66. * Process object which currently working with event
  67. */
  68. protected $_process = null;
  69. /**
  70. * Initialize resource
  71. */
  72. protected function _construct()
  73. {
  74. $this->_init('index/event');
  75. }
  76. /**
  77. * Specify process object
  78. *
  79. * @param null|Mage_Index_Model_Process $process
  80. */
  81. public function setProcess($process)
  82. {
  83. $this->_process = $process;
  84. return $this;
  85. }
  86. /**
  87. * Get related process object
  88. *
  89. * @return Mage_Index_Model_Process | null
  90. */
  91. public function getProcess()
  92. {
  93. return $this->_process;
  94. }
  95. /**
  96. * Specify namespace for old and new data
  97. */
  98. public function setDataNamespace($namespace)
  99. {
  100. $this->_dataNamespace = $namespace;
  101. return $this;
  102. }
  103. /**
  104. * Reset old and new data arrays
  105. *
  106. * @return Mage_Index_Model_Event
  107. */
  108. public function resetData()
  109. {
  110. if ($this->_dataNamespace) {
  111. $data = $this->getNewData(false);
  112. $data[$this->_dataNamespace] = null;
  113. $this->setNewData($data);
  114. } else {
  115. $this->setNewData(array());
  116. }
  117. return $this;
  118. }
  119. /**
  120. * Add process id to event object
  121. *
  122. * @param $processId
  123. * @return Mage_Index_Model_Event
  124. */
  125. public function addProcessId($processId, $status=Mage_Index_Model_Process::EVENT_STATUS_NEW)
  126. {
  127. $this->_processIds[$processId] = $status;
  128. return $this;
  129. }
  130. /**
  131. * Get event process ids
  132. *
  133. * @return array
  134. */
  135. public function getProcessIds()
  136. {
  137. return $this->_processIds;
  138. }
  139. /**
  140. * Merge new data
  141. *
  142. * @param array $previous
  143. * @param mixed $current
  144. * @return array
  145. */
  146. protected function _mergeNewDataRecursive($previous, $current)
  147. {
  148. if (!is_array($current)) {
  149. if (!is_null($current)) {
  150. $previous[] = $current;
  151. }
  152. return $previous;
  153. }
  154. foreach ($previous as $key => $value) {
  155. if (array_key_exists($key, $current) && !is_null($current[$key]) && is_array($previous[$key])) {
  156. if (!is_string($key) || is_array($current[$key])) {
  157. $current[$key] = $this->_mergeNewDataRecursive($previous[$key], $current[$key]);
  158. }
  159. } elseif (!array_key_exists($key, $current) || is_null($current[$key])) {
  160. $current[$key] = $previous[$key];
  161. } elseif (!is_array($previous[$key]) && !is_string($key)) {
  162. $current[] = $previous[$key];
  163. }
  164. }
  165. return $current;
  166. }
  167. /**
  168. * Merge previous event data to object.
  169. * Used for events duplicated protection
  170. *
  171. * @param array $data
  172. * @return Mage_Index_Model_Event
  173. */
  174. public function mergePreviousData($data)
  175. {
  176. if (!empty($data['event_id'])) {
  177. $this->setId($data['event_id']);
  178. $this->setCreatedAt($data['created_at']);
  179. }
  180. if (!empty($data['new_data'])) {
  181. $previousNewData = unserialize($data['new_data']);
  182. $currentNewData = $this->getNewData(false);
  183. $currentNewData = $this->_mergeNewDataRecursive($previousNewData, $currentNewData);
  184. $this->setNewData(serialize($currentNewData));
  185. }
  186. return $this;
  187. }
  188. /**
  189. * Clean new data, unset data for done processes
  190. *
  191. * @return Mage_Index_Model_Event
  192. */
  193. public function cleanNewData()
  194. {
  195. $processIds = $this->getProcessIds();
  196. if (!is_array($processIds) || empty($processIds)) {
  197. return $this;
  198. }
  199. $newData = $this->getNewData(false);
  200. foreach ($processIds as $processId => $processStatus) {
  201. if ($processStatus == Mage_Index_Model_Process::EVENT_STATUS_DONE) {
  202. $process = Mage::getSingleton('index/indexer')->getProcessById($processId);
  203. if ($process) {
  204. $namespace = get_class($process->getIndexer());
  205. if (array_key_exists($namespace, $newData)) {
  206. unset($newData[$namespace]);
  207. }
  208. }
  209. }
  210. }
  211. $this->setNewData(serialize($newData));
  212. return $this;
  213. }
  214. /**
  215. * Get event old data array
  216. *
  217. * @deprecated since 1.6.2.0
  218. * @param bool $useNamespace
  219. * @return array
  220. */
  221. public function getOldData($useNamespace = true)
  222. {
  223. return array();
  224. }
  225. /**
  226. * Get event new data array
  227. *
  228. * @param bool $useNamespace
  229. * @return array
  230. */
  231. public function getNewData($useNamespace = true)
  232. {
  233. $data = $this->_getData('new_data');
  234. if (is_string($data)) {
  235. $data = unserialize($data);
  236. } elseif (empty($data) || !is_array($data)) {
  237. $data = array();
  238. }
  239. if ($useNamespace && $this->_dataNamespace) {
  240. return isset($data[$this->_dataNamespace]) ? $data[$this->_dataNamespace] : array();
  241. }
  242. return $data;
  243. }
  244. /**
  245. * Add new values to old data array (overwrite if value with same key exist)
  246. *
  247. * @deprecated since 1.6.2.0
  248. * @param array | string $data
  249. * @param null | mixed $value
  250. * @return Mage_Index_Model_Event
  251. */
  252. public function addOldData($key, $value=null)
  253. {
  254. return $this;
  255. }
  256. /**
  257. * Add new values to new data array (overwrite if value with same key exist)
  258. *
  259. * @param array | string $data
  260. * @param null | mixed $value
  261. * @return Mage_Index_Model_Event
  262. */
  263. public function addNewData($key, $value=null)
  264. {
  265. $newData = $this->getNewData(false);
  266. if (!is_array($key)) {
  267. $key = array($key => $value);
  268. }
  269. if ($this->_dataNamespace) {
  270. if (!isset($newData[$this->_dataNamespace])) {
  271. $newData[$this->_dataNamespace] = array();
  272. }
  273. $newData[$this->_dataNamespace] = array_merge($newData[$this->_dataNamespace], $key);
  274. } else {
  275. $newData = array_merge($newData, $key);
  276. }
  277. $this->setNewData($newData);
  278. return $this;
  279. }
  280. /**
  281. * Get event entity code.
  282. * Entity code declare what kind of data object related with event (product, category etc.)
  283. *
  284. * @return string
  285. */
  286. public function getEntity()
  287. {
  288. return $this->_getData('entity');
  289. }
  290. /**
  291. * Get event action type.
  292. * Data related on self::TYPE_* constants
  293. *
  294. * @return string
  295. */
  296. public function getType()
  297. {
  298. return $this->_getData('type');
  299. }
  300. /**
  301. * Serelaize old and new data arrays before saving
  302. *
  303. * @return Mage_Index_Model_Event
  304. */
  305. protected function _beforeSave()
  306. {
  307. $newData = $this->getNewData(false);
  308. $this->setNewData(serialize($newData));
  309. if (!$this->hasCreatedAt()) {
  310. $this->setCreatedAt($this->_getResource()->formatDate(time(), true));
  311. }
  312. return parent::_beforeSave();
  313. }
  314. }