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_Catalog_Model_Resource_Product_Compare_Item extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: 38: 39: 40:
41: protected function _construct()
42: {
43: $this->_init('catalog/compare_item', 'catalog_compare_item_id');
44: }
45:
46: 47: 48: 49: 50: 51: 52:
53: public function loadByProduct(Mage_Catalog_Model_Product_Compare_Item $object, $product)
54: {
55: $read = $this->_getReadAdapter();
56: if ($product instanceof Mage_Catalog_Model_Product) {
57: $productId = $product->getId();
58: } else {
59: $productId = $product;
60: }
61: $select = $read->select()->from($this->getMainTable())
62: ->where('product_id = ?', (int)$productId);
63:
64: if ($object->getCustomerId()) {
65: $select->where('customer_id = ?', (int)$object->getCustomerId());
66: } else {
67: $select->where('visitor_id = ?', (int)$object->getVisitorId());
68: }
69:
70: $data = $read->fetchRow($select);
71:
72: if (!$data) {
73: return false;
74: }
75:
76: $object->setData($data);
77:
78: $this->_afterLoad($object);
79: return true;
80: }
81:
82: 83: 84: 85: 86: 87: 88:
89: public function getCount($customerId, $visitorId)
90: {
91: $bind = array('visitore_id' => (int)$visitorId);
92: $select = $this->_getReadAdapter()->select()->from($this->getMainTable(), 'COUNT(*)')
93: ->where('visitor_id = :visitore_id');
94: if ($customerId) {
95: $bind['customer_id'] = (int)$customerId;
96: $select->where('customer_id = :customer_id');
97: }
98: return $this->_getReadAdapter()->fetchOne($select, $bind);
99: }
100:
101: 102: 103: 104: 105:
106: public function clean()
107: {
108: while (true) {
109: $select = $this->_getReadAdapter()->select()
110: ->from(array('compare_table' => $this->getMainTable()), array('catalog_compare_item_id'))
111: ->joinLeft(
112: array('visitor_table' => $this->getTable('log/visitor')),
113: 'visitor_table.visitor_id=compare_table.visitor_id AND compare_table.customer_id IS NULL',
114: array())
115: ->where('compare_table.visitor_id > ?', 0)
116: ->where('visitor_table.visitor_id IS NULL')
117: ->limit(100);
118: $itemIds = $this->_getReadAdapter()->fetchCol($select);
119:
120: if (!$itemIds) {
121: break;
122: }
123:
124: $this->_getWriteAdapter()->delete(
125: $this->getMainTable(),
126: $this->_getWriteAdapter()->quoteInto('catalog_compare_item_id IN(?)', $itemIds)
127: );
128: }
129:
130: return $this;
131: }
132:
133: 134: 135: 136: 137: 138:
139: public function purgeVisitorByCustomer($object)
140: {
141: if (!$object->getCustomerId()) {
142: return $this;
143: }
144:
145: $where = $this->_getWriteAdapter()->quoteInto('customer_id=?', $object->getCustomerId());
146: $bind = array(
147: 'visitor_id' => 0,
148: );
149:
150: $this->_getWriteAdapter()->update($this->getMainTable(), $bind, $where);
151:
152: return $this;
153: }
154:
155: 156: 157: 158: 159: 160: 161:
162: public function updateCustomerFromVisitor($object)
163: {
164: if (!$object->getCustomerId()) {
165: return $this;
166: }
167:
168:
169: $select = $this->_getWriteAdapter()->select()
170: ->from($this->getMainTable())
171: ->where('visitor_id=?', $object->getVisitorId());
172: $visitor = $this->_getWriteAdapter()->fetchAll($select);
173:
174:
175: $select = $this->_getWriteAdapter()->select()
176: ->from($this->getMainTable())
177: ->where('customer_id = ?', $object->getCustomerId())
178: ->where('visitor_id != ?', $object->getVisitorId());
179: $customer = $this->_getWriteAdapter()->fetchAll($select);
180:
181: $products = array();
182: $delete = array();
183: $update = array();
184: foreach ($visitor as $row) {
185: $products[$row['product_id']] = array(
186: 'store_id' => $row['store_id'],
187: 'customer_id' => $object->getCustomerId(),
188: 'visitor_id' => $object->getVisitorId(),
189: 'product_id' => $row['product_id']
190: );
191: $update[$row[$this->getIdFieldName()]] = $row['product_id'];
192: }
193:
194: foreach ($customer as $row) {
195: if (isset($products[$row['product_id']])) {
196: $delete[] = $row[$this->getIdFieldName()];
197: }
198: else {
199: $products[$row['product_id']] = array(
200: 'store_id' => $row['store_id'],
201: 'customer_id' => $object->getCustomerId(),
202: 'visitor_id' => $object->getVisitorId(),
203: 'product_id' => $row['product_id']
204: );
205: }
206: }
207:
208: if ($delete) {
209: $this->_getWriteAdapter()->delete($this->getMainTable(),
210: $this->_getWriteAdapter()->quoteInto($this->getIdFieldName() . ' IN(?)', $delete));
211: }
212: if ($update) {
213: foreach ($update as $itemId => $productId) {
214: $bind = $products[$productId];
215: $this->_getWriteAdapter()->update($this->getMainTable(), $bind,
216: $this->_getWriteAdapter()->quoteInto($this->getIdFieldName() . '=?', $itemId));
217: }
218: }
219:
220: return $this;
221: }
222:
223: 224: 225: 226: 227: 228: 229:
230: public function clearItems($visitorId = null, $customerId = null)
231: {
232: $where = array();
233: if ($customerId) {
234: $customerId = (int)$customerId;
235: $where[] = $this->_getWriteAdapter()->quoteInto('customer_id = ?', $customerId);
236: }
237: if ($visitorId) {
238: $visitorId = (int)$visitorId;
239: $where[] = $this->_getWriteAdapter()->quoteInto('visitor_id = ?', $visitorId);
240: }
241: if (!$where) {
242: return $this;
243: }
244: $this->_getWriteAdapter()->delete($this->getMainTable(), $where);
245: return $this;
246: }
247: }
248: