00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_Log_Model_Mysql4_Visitor_Online extends Mage_Core_Model_Mysql4_Abstract
00036 {
00037
00038
00039
00040
00041 protected function _construct()
00042 {
00043 $this->_init('log/visitor_online', 'visitor_id');
00044 }
00045
00046
00047
00048
00049
00050
00051
00052 public function prepare(Mage_Log_Model_Visitor_Online $object)
00053 {
00054 if (($object->getUpdateFrequency() + $object->getPrepareAt()) > time()) {
00055 return $this;
00056 }
00057
00058 $this->_getWriteAdapter()->beginTransaction();
00059 $this->_getWriteAdapter()->truncate($this->getMainTable());
00060
00061 $visitors = array();
00062 $lastUrls = array();
00063
00064
00065 $whereCond = sprintf('last_visit_at >= (? - INTERVAL %d MINUTE)',
00066 $object->getOnlineInterval());
00067 $select = $this->_getReadAdapter()->select()
00068 ->from(
00069 $this->getTable('log/visitor'),
00070 array('visitor_id', 'first_visit_at', 'last_visit_at', 'last_url_id'))
00071 ->where($whereCond, now());
00072
00073 $query = $this->_getReadAdapter()->query($select);
00074 while ($row = $query->fetch()) {
00075 $visitors[$row['visitor_id']] = $row;
00076 $lastUrls[$row['last_url_id']] = $row['visitor_id'];
00077 $visitors[$row['visitor_id']]['visitor_type'] = Mage_Log_Model_Visitor::VISITOR_TYPE_VISITOR;
00078 $visitors[$row['visitor_id']]['customer_id'] = null;
00079 }
00080
00081 if (!$visitors) {
00082 $this->commit();
00083 return $this;
00084 }
00085
00086
00087 $select = $this->_getReadAdapter()->select()
00088 ->from(
00089 $this->getTable('log/visitor_info'),
00090 array('visitor_id', 'remote_addr'))
00091 ->where('visitor_id IN(?)', array_keys($visitors));
00092 $query = $this->_getReadAdapter()->query($select);
00093 while ($row = $query->fetch()) {
00094 $visitors[$row['visitor_id']]['remote_addr'] = $row['remote_addr'];
00095 }
00096
00097
00098 $select = $this->_getReadAdapter()->select()
00099 ->from(
00100 $this->getTable('log/url_info_table'),
00101 array('url_id', 'url'))
00102 ->where('url_id IN(?)', array_keys($lastUrls));
00103 $query = $this->_getReadAdapter()->query($select);
00104 while ($row = $query->fetch()) {
00105 $visitorId = $lastUrls[$row['url_id']];
00106 $visitors[$visitorId]['last_url'] = $row['url'];
00107 }
00108
00109
00110 $select = $this->_getReadAdapter()->select()
00111 ->from(
00112 $this->getTable('log/customer'),
00113 array('visitor_id', 'customer_id'))
00114 ->where('visitor_id IN(?)', array_keys($visitors));
00115 $query = $this->_getReadAdapter()->query($select);
00116 while ($row = $query->fetch()) {
00117 $visitors[$row['visitor_id']]['visitor_type'] = Mage_Log_Model_Visitor::VISITOR_TYPE_CUSTOMER;
00118 $visitors[$row['visitor_id']]['customer_id'] = $row['customer_id'];
00119 }
00120
00121 foreach ($visitors as $visitorData) {
00122 unset($visitorData['last_url_id']);
00123 $this->_getWriteAdapter()->insert($this->getMainTable(), $visitorData);
00124 }
00125
00126 $this->_getWriteAdapter()->commit();
00127
00128 $object->setPrepareAt();
00129
00130 return $this;
00131 }
00132 }