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_Log_Model_Resource_Visitor extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: 38: 39: 40:
41: protected function _construct()
42: {
43: $this->_init('log/visitor', 'visitor_id');
44: }
45:
46: 47: 48: 49: 50: 51:
52: protected function _prepareDataForSave(Mage_Core_Model_Abstract $visitor)
53: {
54: return array(
55: 'session_id' => $visitor->getSessionId(),
56: 'first_visit_at' => $visitor->getFirstVisitAt(),
57: 'last_visit_at' => $visitor->getLastVisitAt(),
58: 'last_url_id' => $visitor->getLastUrlId() ? $visitor->getLastUrlId() : 0,
59: 'store_id' => Mage::app()->getStore()->getId(),
60: );
61: }
62:
63: 64: 65: 66: 67: 68:
69: protected function _saveUrlInfo($visitor)
70: {
71: $adapter = $this->_getWriteAdapter();
72: $data = new Varien_Object(array(
73: 'url' => Mage::helper('core/string')->substr($visitor->getUrl(), 0, 250),
74: 'referer'=> Mage::helper('core/string')->substr($visitor->getHttpReferer(), 0, 250)
75: ));
76: $bind = $this->_prepareDataForTable($data, $this->getTable('log/url_info_table'));
77:
78: $adapter->insert($this->getTable('log/url_info_table'), $bind);
79:
80: $visitor->setLastUrlId($adapter->lastInsertId($this->getTable('log/url_info_table')));
81:
82: return $this;
83: }
84:
85: 86: 87: 88: 89: 90:
91: protected function _beforeSave(Mage_Core_Model_Abstract $visitor)
92: {
93: if (!$visitor->getIsNewVisitor()) {
94: $this->_saveUrlInfo($visitor);
95: }
96: return $this;
97: }
98:
99: 100: 101: 102: 103: 104:
105: protected function _afterSave(Mage_Core_Model_Abstract $visitor)
106: {
107: if ($visitor->getIsNewVisitor()) {
108: $this->_saveVisitorInfo($visitor);
109: $visitor->setIsNewVisitor(false);
110: } else {
111: $this->_saveVisitorUrl($visitor);
112: if ($visitor->getDoCustomerLogin() || $visitor->getDoCustomerLogout()) {
113: $this->_saveCustomerInfo($visitor);
114: }
115: if ($visitor->getDoQuoteCreate() || $visitor->getDoQuoteDestroy()) {
116: $this->_saveQuoteInfo($visitor);
117: }
118: }
119: return $this;
120: }
121:
122: 123: 124: 125: 126: 127:
128: protected function _afterLoad(Mage_Core_Model_Abstract $object)
129: {
130: parent::_afterLoad($object);
131:
132: $adapter = $this->_getReadAdapter();
133: $select = $adapter->select()->from($this->getTable('log/quote_table'), 'quote_id')
134: ->where('visitor_id = ?', $object->getId())->limit(1);
135: $result = $adapter->query($select)->fetch();
136: if (isset($result['quote_id'])) {
137: $object->setQuoteId((int) $result['quote_id']);
138: }
139: return $this;
140: }
141:
142: 143: 144: 145: 146: 147:
148: protected function _saveVisitorInfo($visitor)
149: {
150:
151: $stringHelper = Mage::helper('core/string');
152:
153: $referer = $stringHelper->cleanString($visitor->getHttpReferer());
154: $referer = $stringHelper->substr($referer, 0, 255);
155: $userAgent = $stringHelper->cleanString($visitor->getHttpUserAgent());
156: $userAgent = $stringHelper->substr($userAgent, 0, 255);
157: $charset = $stringHelper->cleanString($visitor->getHttpAcceptCharset());
158: $charset = $stringHelper->substr($charset, 0, 255);
159: $language = $stringHelper->cleanString($visitor->getHttpAcceptLanguage());
160: $language = $stringHelper->substr($language, 0, 255);
161:
162: $adapter = $this->_getWriteAdapter();
163: $data = new Varien_Object(array(
164: 'visitor_id' => $visitor->getId(),
165: 'http_referer' => $referer,
166: 'http_user_agent' => $userAgent,
167: 'http_accept_charset' => $charset,
168: 'http_accept_language' => $language,
169: 'server_addr' => $visitor->getServerAddr(),
170: 'remote_addr' => $visitor->getRemoteAddr(),
171: ));
172: $bind = $this->_prepareDataForTable($data, $this->getTable('log/visitor_info'));
173:
174: $adapter->insert($this->getTable('log/visitor_info'), $bind);
175: return $this;
176: }
177:
178: 179: 180: 181: 182: 183:
184: protected function _saveVisitorUrl($visitor)
185: {
186: $data = new Varien_Object(array(
187: 'url_id' => $visitor->getLastUrlId(),
188: 'visitor_id' => $visitor->getId(),
189: 'visit_time' => Mage::getSingleton('core/date')->gmtDate()
190: ));
191: $bind = $this->_prepareDataForTable($data, $this->getTable('log/url_table'));
192:
193: $this->_getWriteAdapter()->insert($this->getTable('log/url_table'), $bind);
194: return $this;
195: }
196:
197: 198: 199: 200: 201: 202:
203: protected function _saveCustomerInfo($visitor)
204: {
205: $adapter = $this->_getWriteAdapter();
206:
207: if ($visitor->getDoCustomerLogin()) {
208: $data = new Varien_Object(array(
209: 'visitor_id' => $visitor->getVisitorId(),
210: 'customer_id' => $visitor->getCustomerId(),
211: 'login_at' => Mage::getSingleton('core/date')->gmtDate(),
212: 'store_id' => Mage::app()->getStore()->getId()
213: ));
214: $bind = $this->_prepareDataForTable($data, $this->getTable('log/customer'));
215:
216: $adapter->insert($this->getTable('log/customer'), $bind);
217: $visitor->setCustomerLogId($adapter->lastInsertId($this->getTable('log/customer')));
218: $visitor->setDoCustomerLogin(false);
219: }
220:
221: if ($visitor->getDoCustomerLogout() && $logId = $visitor->getCustomerLogId()) {
222: $data = new Varien_Object(array(
223: 'logout_at' => Mage::getSingleton('core/date')->gmtDate(),
224: 'store_id' => (int)Mage::app()->getStore()->getId(),
225: ));
226:
227: $bind = $this->_prepareDataForTable($data, $this->getTable('log/customer'));
228:
229: $condition = array(
230: 'log_id = ?' => (int) $logId,
231: );
232:
233: $adapter->update($this->getTable('log/customer'), $bind, $condition);
234:
235: $visitor->setDoCustomerLogout(false);
236: $visitor->setCustomerId(null);
237: $visitor->setCustomerLogId(null);
238: }
239:
240: return $this;
241: }
242:
243: 244: 245: 246: 247: 248:
249: protected function _saveQuoteInfo($visitor)
250: {
251: $adapter = $this->_getWriteAdapter();
252: if ($visitor->getDoQuoteCreate()) {
253: $data = new Varien_Object(array(
254: 'quote_id' => (int) $visitor->getQuoteId(),
255: 'visitor_id' => (int) $visitor->getId(),
256: 'created_at' => Mage::getSingleton('core/date')->gmtDate()
257: ));
258:
259: $bind = $this->_prepareDataForTable($data, $this->getTable('log/quote_table'));
260:
261: $adapter->insert($this->getTable('log/quote_table'), $bind);
262:
263: $visitor->setDoQuoteCreate(false);
264: }
265:
266: if ($visitor->getDoQuoteDestroy()) {
267: 268: 269: 270:
271: $condition = array(
272: 'quote_id = ?' => (int) $visitor->getQuoteId(),
273: );
274:
275: $adapter->delete($this->getTable('log/quote_table'), $condition);
276:
277: $visitor->setDoQuoteDestroy(false);
278: $visitor->setQuoteId(null);
279: }
280: return $this;
281: }
282: }
283: