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: abstract class Mage_Reports_Model_Resource_Product_Index_Abstract extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: 38: 39: 40: 41:
42: protected $_fieldsForUpdate = array('store_id', 'added_at');
43:
44: 45: 46: 47: 48: 49:
50: public function updateCustomerFromVisitor(Mage_Reports_Model_Product_Index_Abstract $object)
51: {
52: 53: 54:
55: if (!$object->getCustomerId() || !$object->getVisitorId()) {
56: return $this;
57: }
58: $adapter = $this->_getWriteAdapter();
59: $select = $adapter->select()
60: ->from($this->getMainTable())
61: ->where('visitor_id = ?', $object->getVisitorId());
62:
63: $rowSet = $select->query()->fetchAll();
64: foreach ($rowSet as $row) {
65:
66: 67: 68:
69:
70: $select = $adapter->select()
71: ->from($this->getMainTable())
72: ->where('customer_id = ?', $object->getCustomerId())
73: ->where('product_id = ?', $row['product_id']);
74: $idx = $adapter->fetchRow($select);
75:
76: if ($idx) {
77: 78: 79: 80:
81: $adapter->delete($this->getMainTable(), array('index_id = ?' => $row['index_id']));
82: $where = array('index_id = ?' => $idx['index_id']);
83: $data = array(
84: 'visitor_id' => $object->getVisitorId(),
85: 'store_id' => $object->getStoreId(),
86: 'added_at' => Varien_Date::now(),
87: );
88: } else {
89: $where = array('index_id = ?' => $row['index_id']);
90: $data = array(
91: 'customer_id' => $object->getCustomerId(),
92: 'store_id' => $object->getStoreId(),
93: 'added_at' => Varien_Date::now()
94: );
95: }
96:
97: $adapter->update($this->getMainTable(), $data, $where);
98:
99: }
100: return $this;
101: }
102:
103: 104: 105: 106: 107: 108:
109: public function purgeVisitorByCustomer(Mage_Reports_Model_Product_Index_Abstract $object)
110: {
111: 112: 113:
114: if (!$object->getCustomerId()) {
115: return $this;
116: }
117:
118: $bind = array('visitor_id' => null);
119: $where = array('customer_id = ?' => (int)$object->getCustomerId());
120: $this->_getWriteAdapter()->update($this->getMainTable(), $bind, $where);
121:
122: return $this;
123: }
124:
125: 126: 127: 128: 129: 130:
131: public function save(Mage_Core_Model_Abstract $object)
132: {
133: if ($object->isDeleted()) {
134: return $this->delete($object);
135: }
136:
137: $this->_serializeFields($object);
138: $this->_beforeSave($object);
139: $this->_checkUnique($object);
140:
141: $data = $this->_prepareDataForSave($object);
142: unset($data[$this->getIdFieldName()]);
143:
144: $matchFields = array('product_id', 'store_id');
145:
146: Mage::getResourceHelper('reports')->mergeVisitorProductIndex(
147: $this->getMainTable(),
148: $data,
149: $matchFields
150: );
151:
152:
153: $this->unserializeFields($object);
154: $this->_afterSave($object);
155:
156: return $this;
157: }
158:
159:
160: 161: 162: 163: 164:
165: public function clean()
166: {
167: while (true) {
168: $select = $this->_getReadAdapter()->select()
169: ->from(array('main_table' => $this->getMainTable()), array($this->getIdFieldName()))
170: ->joinLeft(
171: array('visitor_table' => $this->getTable('log/visitor')),
172: 'main_table.visitor_id = visitor_table.visitor_id',
173: array())
174: ->where('main_table.visitor_id > ?', 0)
175: ->where('visitor_table.visitor_id IS NULL')
176: ->limit(100);
177: $indexIds = $this->_getReadAdapter()->fetchCol($select);
178:
179: if (!$indexIds) {
180: break;
181: }
182:
183: $this->_getWriteAdapter()->delete(
184: $this->getMainTable(),
185: $this->_getWriteAdapter()->quoteInto($this->getIdFieldName() . ' IN(?)', $indexIds)
186: );
187: }
188: return $this;
189: }
190:
191: 192: 193: 194: 195: 196: 197: 198:
199: public function registerIds(Varien_Object $object, $productIds)
200: {
201: $row = array(
202: 'visitor_id' => $object->getVisitorId(),
203: 'customer_id' => $object->getCustomerId(),
204: 'store_id' => $object->getStoreId(),
205: );
206: $addedAt = Varien_Date::toTimestamp(true);
207: $data = array();
208: foreach ($productIds as $productId) {
209: $productId = (int) $productId;
210: if ($productId) {
211: $row['product_id'] = $productId;
212: $row['added_at'] = Varien_Date::formatDate($addedAt);
213: $data[] = $row;
214: }
215: $addedAt -= ($addedAt > 0) ? 1 : 0;
216: }
217:
218: $matchFields = array('product_id', 'store_id');
219: foreach ($data as $row) {
220: Mage::getResourceHelper('reports')->mergeVisitorProductIndex(
221: $this->getMainTable(),
222: $row,
223: $matchFields
224: );
225: }
226: return $this;
227: }
228: }
229: