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: class Mage_Index_Model_Resource_Event extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: 38: 39: 40:
41: protected function _construct()
42: {
43: $this->_init('index/event', 'event_id');
44: }
45:
46: 47: 48: 49: 50: 51:
52: protected function _beforeSave(Mage_Core_Model_Abstract $object)
53: {
54: 55: 56:
57: if (!$object->getId()) {
58: $select = $this->_getReadAdapter()->select()
59: ->from($this->getMainTable())
60: ->where('type=?', $object->getType())
61: ->where('entity=?', $object->getEntity());
62: if ($object->hasEntityPk()) {
63: $select->where('entity_pk=?', $object->getEntityPk());
64: }
65: $data = $this->_getWriteAdapter()->fetchRow($select);
66: if ($data) {
67: $object->mergePreviousData($data);
68: }
69: }
70: $object->cleanNewData();
71: return parent::_beforeSave($object);
72: }
73:
74: 75: 76: 77: 78: 79:
80: protected function _afterSave(Mage_Core_Model_Abstract $object)
81: {
82: $processIds = $object->getProcessIds();
83: if (is_array($processIds)) {
84: $processTable = $this->getTable('index/process_event');
85: if (empty($processIds)) {
86: $this->_getWriteAdapter()->delete($processTable);
87: } else {
88: foreach ($processIds as $processId => $processStatus) {
89: if (is_null($processStatus) || $processStatus == Mage_Index_Model_Process::EVENT_STATUS_DONE) {
90: $this->_getWriteAdapter()->delete($processTable, array(
91: 'process_id = ?' => $processId,
92: 'event_id = ?' => $object->getId(),
93: ));
94: continue;
95: }
96: $data = array(
97: 'process_id' => $processId,
98: 'event_id' => $object->getId(),
99: 'status' => $processStatus
100: );
101: $this->_getWriteAdapter()->insertOnDuplicate($processTable, $data, array('status'));
102: }
103: }
104: }
105: return parent::_afterSave($object);
106: }
107:
108: 109: 110: 111: 112: 113: 114:
115: public function updateProcessEvents($process, $status = Mage_Index_Model_Process::EVENT_STATUS_DONE)
116: {
117: $whereCondition = '';
118: if ($process instanceof Mage_Index_Model_Process) {
119: $whereCondition = array('process_id = ?' => $process->getId());
120: } elseif (is_array($process) && !empty($process)) {
121: $whereCondition = array('process_id IN (?)' => $process);
122: } elseif (!is_array($whereCondition)) {
123: $whereCondition = array('process_id = ?' => $process);
124: }
125: $this->_getWriteAdapter()->update(
126: $this->getTable('index/process_event'),
127: array('status' => $status),
128: $whereCondition
129: );
130: return $this;
131: }
132:
133: 134: 135: 136: 137: 138:
139: public function getUnprocessedEvents($process)
140: {
141: $select = $this->_getReadAdapter()->select()
142: ->from($this->getTable('index/process_event'))
143: ->where('process_id = ?', $process->getId())
144: ->where('status = ?', Mage_Index_Model_Process::EVENT_STATUS_NEW);
145:
146: return $this->_getReadAdapter()->fetchAll($select);
147: }
148: }
149: