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_Reports_Model_Resource_Event extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: 38: 39: 40: 41: 42:
43: protected function _construct()
44: {
45: $this->_init('reports/event', 'event_id');
46: }
47:
48: 49: 50: 51: 52: 53: 54: 55: 56:
57: public function updateCustomerType(Mage_Reports_Model_Event $model, $visitorId, $customerId, $types = array())
58: {
59: if ($types) {
60: $this->_getWriteAdapter()->update($this->getMainTable(),
61: array('subject_id' => (int)$customerId, 'subtype' => 0),
62: array(
63: 'subject_id = ?' => (int)$visitorId,
64: 'subtype = ?' => 1,
65: 'event_type_id IN(?)' => $types
66: )
67: );
68: }
69: return $this;
70: }
71:
72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83:
84: public function applyLogToCollection(Varien_Data_Collection_Db $collection, $eventTypeId, $eventSubjectId, $subtype,
85: $skipIds = array())
86: {
87: $idFieldName = $collection->getResource()->getIdFieldName();
88:
89: $derivedSelect = $this->getReadConnection()->select()
90: ->from(
91: $this->getTable('reports/event'),
92: array('event_id' => new Zend_Db_Expr('MAX(event_id)'), 'object_id'))
93: ->where('event_type_id = ?', (int)$eventTypeId)
94: ->where('subject_id = ?', (int)$eventSubjectId)
95: ->where('subtype = ?', (int)$subtype)
96: ->where('store_id IN(?)', $this->getCurrentStoreIds())
97: ->group('object_id');
98:
99: if ($skipIds) {
100: if (!is_array($skipIds)) {
101: $skipIds = array((int)$skipIds);
102: }
103: $derivedSelect->where('object_id NOT IN(?)', $skipIds);
104: }
105:
106: $collection->getSelect()
107: ->joinInner(
108: array('evt' => new Zend_Db_Expr("({$derivedSelect})")),
109: "{$idFieldName} = evt.object_id",
110: array())
111: ->order('evt.event_id ' . Varien_Db_Select::SQL_DESC);
112:
113: return $this;
114: }
115:
116: 117: 118: 119: 120: 121:
122: public function getCurrentStoreIds(array $predefinedStoreIds = null)
123: {
124: $stores = array();
125:
126: if (Mage::app()->getStore()->getId() == 0) {
127: if (null !== $predefinedStoreIds) {
128: $stores = $predefinedStoreIds;
129: } else {
130: foreach (Mage::app()->getStores() as $store) {
131: $stores[] = $store->getId();
132: }
133: }
134: } else {
135: switch (Mage::getStoreConfig('catalog/recently_products/scope')) {
136: case 'website':
137: $resourceStore = Mage::app()->getStore()->getWebsite()->getStores();
138: break;
139: case 'group':
140: $resourceStore = Mage::app()->getStore()->getGroup()->getStores();
141: break;
142: default:
143: $resourceStore = array(Mage::app()->getStore());
144: break;
145: }
146:
147: foreach ($resourceStore as $store) {
148: $stores[] = $store->getId();
149: }
150: }
151: foreach ($stores as $key => $store) {
152: $stores[$key] = (int)$store;
153: }
154:
155: return $stores;
156: }
157:
158: 159: 160: 161: 162: 163:
164: public function clean(Mage_Reports_Model_Event $object)
165: {
166: while (true) {
167: $select = $this->_getReadAdapter()->select()
168: ->from(array('event_table' => $this->getMainTable()), array('event_id'))
169: ->joinLeft(
170: array('visitor_table' => $this->getTable('log/visitor')),
171: 'event_table.subject_id = visitor_table.visitor_id',
172: array())
173: ->where('visitor_table.visitor_id IS NULL')
174: ->where('event_table.subtype = ?', 1)
175: ->limit(1000);
176: $eventIds = $this->_getReadAdapter()->fetchCol($select);
177:
178: if (!$eventIds) {
179: break;
180: }
181:
182: $this->_getWriteAdapter()->delete($this->getMainTable(), array('event_id IN(?)' => $eventIds));
183: }
184: return $this;
185: }
186: }
187:
188: