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_Online extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: 38: 39: 40:
41: protected function _construct()
42: {
43: $this->_init('log/visitor_online', 'visitor_id');
44: }
45:
46: 47: 48: 49: 50: 51:
52: public function prepare(Mage_Log_Model_Visitor_Online $object)
53: {
54: if (($object->getUpdateFrequency() + $object->getPrepareAt()) > time()) {
55: return $this;
56: }
57:
58: $readAdapter = $this->_getReadAdapter();
59: $writeAdapter = $this->_getWriteAdapter();
60:
61: $writeAdapter->beginTransaction();
62:
63: try{
64: $writeAdapter->delete($this->getMainTable());
65:
66: $visitors = array();
67: $lastUrls = array();
68:
69:
70:
71: $lastDate = Mage::getModel('core/date')->gmtTimestamp() - $object->getOnlineInterval() * 60;
72:
73: $select = $readAdapter->select()
74: ->from(
75: $this->getTable('log/visitor'),
76: array('visitor_id', 'first_visit_at', 'last_visit_at', 'last_url_id'))
77: ->where('last_visit_at >= ?', $readAdapter->formatDate($lastDate));
78:
79: $query = $readAdapter->query($select);
80: while ($row = $query->fetch()) {
81: $visitors[$row['visitor_id']] = $row;
82: $lastUrls[$row['last_url_id']] = $row['visitor_id'];
83: $visitors[$row['visitor_id']]['visitor_type'] = Mage_Log_Model_Visitor::VISITOR_TYPE_VISITOR;
84: $visitors[$row['visitor_id']]['customer_id'] = null;
85: }
86:
87: if (!$visitors) {
88: $this->commit();
89: return $this;
90: }
91:
92:
93: $select = $readAdapter->select()
94: ->from(
95: $this->getTable('log/visitor_info'),
96: array('visitor_id', 'remote_addr'))
97: ->where('visitor_id IN(?)', array_keys($visitors));
98:
99: $query = $readAdapter->query($select);
100: while ($row = $query->fetch()) {
101: $visitors[$row['visitor_id']]['remote_addr'] = $row['remote_addr'];
102: }
103:
104:
105: $select = $readAdapter->select()
106: ->from(
107: $this->getTable('log/url_info_table'),
108: array('url_id', 'url'))
109: ->where('url_id IN(?)', array_keys($lastUrls));
110:
111: $query = $readAdapter->query($select);
112: while ($row = $query->fetch()) {
113: $visitorId = $lastUrls[$row['url_id']];
114: $visitors[$visitorId]['last_url'] = $row['url'];
115: }
116:
117:
118: $select = $readAdapter->select()
119: ->from(
120: $this->getTable('log/customer'),
121: array('visitor_id', 'customer_id'))
122: ->where('visitor_id IN(?)', array_keys($visitors));
123:
124: $query = $readAdapter->query($select);
125: while ($row = $query->fetch()) {
126: $visitors[$row['visitor_id']]['visitor_type'] = Mage_Log_Model_Visitor::VISITOR_TYPE_CUSTOMER;
127: $visitors[$row['visitor_id']]['customer_id'] = $row['customer_id'];
128: }
129:
130: foreach ($visitors as $visitorData) {
131: unset($visitorData['last_url_id']);
132:
133: $writeAdapter->insertForce($this->getMainTable(), $visitorData);
134: }
135:
136: $writeAdapter->commit();
137: } catch (Exception $e) {
138: $writeAdapter->rollBack();
139: throw $e;
140: }
141:
142: $object->setPrepareAt();
143:
144: return $this;
145: }
146: }
147: