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_Event_Observer
36: {
37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47:
48: protected function _event($eventTypeId, $objectId, $subjectId = null, $subtype = 0)
49: {
50: if (is_null($subjectId)) {
51: if (Mage::getSingleton('customer/session')->isLoggedIn()) {
52: $customer = Mage::getSingleton('customer/session')->getCustomer();
53: $subjectId = $customer->getId();
54: }
55: else {
56: $subjectId = Mage::getSingleton('log/visitor')->getId();
57: $subtype = 1;
58: }
59: }
60:
61: $eventModel = Mage::getModel('reports/event');
62: $storeId = Mage::app()->getStore()->getId();
63: $eventModel
64: ->setEventTypeId($eventTypeId)
65: ->setObjectId($objectId)
66: ->setSubjectId($subjectId)
67: ->setSubtype($subtype)
68: ->setStoreId($storeId);
69: $eventModel->save();
70:
71: return $this;
72: }
73:
74: 75: 76: 77: 78: 79:
80: public function customerLogin(Varien_Event_Observer $observer)
81: {
82: if (!Mage::getSingleton('customer/session')->isLoggedIn()) {
83: return $this;
84: }
85:
86: $visitorId = Mage::getSingleton('log/visitor')->getId();
87: $customerId = Mage::getSingleton('customer/session')->getCustomerId();
88: $eventModel = Mage::getModel('reports/event');
89: $eventModel->updateCustomerType($visitorId, $customerId);
90:
91: Mage::getModel('reports/product_index_compared')
92: ->updateCustomerFromVisitor()
93: ->calculate();
94: Mage::getModel('reports/product_index_viewed')
95: ->updateCustomerFromVisitor()
96: ->calculate();
97:
98: return $this;
99: }
100:
101: 102: 103: 104: 105: 106:
107: public function customerLogout(Varien_Event_Observer $observer)
108: {
109: Mage::getModel('reports/product_index_compared')
110: ->purgeVisitorByCustomer()
111: ->calculate();
112: Mage::getModel('reports/product_index_viewed')
113: ->purgeVisitorByCustomer()
114: ->calculate();
115: return $this;
116: }
117:
118: 119: 120: 121: 122: 123:
124: public function catalogProductView(Varien_Event_Observer $observer)
125: {
126: $productId = $observer->getEvent()->getProduct()->getId();
127:
128: Mage::getModel('reports/product_index_viewed')
129: ->setProductId($productId)
130: ->save()
131: ->calculate();
132:
133: return $this->_event(Mage_Reports_Model_Event::EVENT_PRODUCT_VIEW, $productId);
134: }
135:
136: 137: 138: 139: 140: 141:
142: public function sendfriendProduct(Varien_Event_Observer $observer)
143: {
144: return $this->_event(Mage_Reports_Model_Event::EVENT_PRODUCT_SEND,
145: $observer->getEvent()->getProduct()->getId()
146: );
147: }
148:
149: 150: 151: 152: 153: 154: 155: 156:
157: public function catalogProductCompareRemoveProduct(Varien_Event_Observer $observer)
158: {
159: Mage::getModel('reports/product_index_compared')->calculate();
160:
161: return $this;
162: }
163:
164: 165: 166: 167: 168: 169: 170: 171:
172: public function catalogProductCompareClear(Varien_Event_Observer $observer)
173: {
174: Mage::getModel('reports/product_index_compared')->calculate();
175:
176: return $this;
177: }
178:
179: 180: 181: 182: 183: 184: 185: 186:
187: public function catalogProductCompareAddProduct(Varien_Event_Observer $observer)
188: {
189: $productId = $observer->getEvent()->getProduct()->getId();
190:
191: Mage::getModel('reports/product_index_compared')
192: ->setProductId($productId)
193: ->save()
194: ->calculate();
195:
196: return $this->_event(Mage_Reports_Model_Event::EVENT_PRODUCT_COMPARE, $productId);
197: }
198:
199: 200: 201: 202: 203: 204:
205: public function checkoutCartAddProduct(Varien_Event_Observer $observer)
206: {
207: $quoteItem = $observer->getEvent()->getItem();
208: if (!$quoteItem->getId() && !$quoteItem->getParentItem()) {
209: $productId = $quoteItem->getProductId();
210: $this->_event(Mage_Reports_Model_Event::EVENT_PRODUCT_TO_CART, $productId);
211: }
212: return $this;
213: }
214:
215: 216: 217: 218: 219: 220:
221: public function wishlistAddProduct(Varien_Event_Observer $observer)
222: {
223: return $this->_event(Mage_Reports_Model_Event::EVENT_PRODUCT_TO_WISHLIST,
224: $observer->getEvent()->getProduct()->getId()
225: );
226: }
227:
228: 229: 230: 231: 232: 233:
234: public function wishlistShare(Varien_Event_Observer $observer)
235: {
236: return $this->_event(Mage_Reports_Model_Event::EVENT_WISHLIST_SHARE,
237: $observer->getEvent()->getWishlist()->getId()
238: );
239: }
240:
241: 242: 243: 244: 245: 246: 247: 248:
249: public function eventClean(Varien_Event_Observer $observer)
250: {
251:
252: $event = Mage::getModel('reports/event');
253: $event->clean();
254:
255: Mage::getModel('reports/product_index_compared')->clean();
256: Mage::getModel('reports/product_index_viewed')->clean();
257:
258: return $this;
259: }
260: }
261: