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 class Mage_Log_Model_Mysql4_Visitor extends Mage_Core_Model_Mysql4_Abstract
00035 {
00036 protected function _construct()
00037 {
00038 $this->_init('log/visitor', 'visitor_id');
00039 }
00040
00041 protected function _prepareDataForSave(Mage_Core_Model_Abstract $visitor)
00042 {
00043 return array(
00044 'session_id' => $visitor->getSessionId(),
00045 'first_visit_at'=> $visitor->getFirstVisitAt(),
00046 'last_visit_at' => $visitor->getLastVisitAt(),
00047 'last_url_id' => $visitor->getLastUrlId() ? $visitor->getLastUrlId() : 0,
00048 'store_id' => Mage::app()->getStore()->getId(),
00049 );
00050 }
00051
00052
00053
00054
00055
00056
00057
00058 protected function _saveUrlInfo($visitor)
00059 {
00060 $this->_getWriteAdapter()->insert($this->getTable('log/url_info_table'), array(
00061 'url' => Mage::helper('core/string')->substr($visitor->getUrl(), 0, 250),
00062 'referer'=> Mage::helper('core/string')->substr($visitor->getHttpReferer(), 0, 250)
00063 ));
00064 $visitor->setLastUrlId($this->_getWriteAdapter()->lastInsertId());
00065 return $this;
00066 }
00067
00068 protected function _beforeSave(Mage_Core_Model_Abstract $visitor)
00069 {
00070 if (!$visitor->getIsNewVisitor()) {
00071 $this->_saveUrlInfo($visitor);
00072 }
00073 return $this;
00074 }
00075
00076 protected function _afterSave(Mage_Core_Model_Abstract $visitor)
00077 {
00078 if ($visitor->getIsNewVisitor()) {
00079 $this->_saveVisitorInfo($visitor);
00080 $visitor->setIsNewVisitor(false);
00081 }
00082 else {
00083 $this->_saveVisitorUrl($visitor);
00084 if ($visitor->getDoCustomerLogin() || $visitor->getDoCustomerLogout()) {
00085 $this->_saveCustomerInfo($visitor);
00086 }
00087 if ($visitor->getDoQuoteCreate() || $visitor->getDoQuoteDestroy()) {
00088 $this->_saveQuoteInfo($visitor);
00089 }
00090 }
00091 return $this;
00092 }
00093
00094
00095
00096
00097
00098
00099
00100 protected function _saveVisitorInfo($visitor)
00101 {
00102 $write = $this->_getWriteAdapter();
00103 $data = array(
00104 'visitor_id' => $visitor->getId(),
00105 'http_referer' => Mage::helper('core/string')->substr($visitor->getHttpReferer(), 0, 250),
00106 'http_user_agent' => $visitor->getHttpUserAgent(),
00107 'http_accept_charset'=>$visitor->getHttpAcceptCharset(),
00108 'http_accept_language'=>$visitor->getHttpAcceptLanguage(),
00109 'server_addr' => $visitor->getServerAddr(),
00110 'remote_addr' => $visitor->getRemoteAddr(),
00111 );
00112
00113 $write->insert($this->getTable('log/visitor_info'), $data);
00114 return $this;
00115 }
00116
00117
00118
00119
00120
00121
00122
00123 protected function _saveVisitorUrl($visitor)
00124 {
00125 $write = $this->_getWriteAdapter();
00126 $write->insert($this->getTable('log/url_table'), array(
00127 'url_id' => $visitor->getLastUrlId(),
00128 'visitor_id'=> $visitor->getId(),
00129 'visit_time'=> now(),
00130 ));
00131 return $this;
00132 }
00133
00134
00135
00136
00137
00138
00139
00140 protected function _saveCustomerInfo($visitor)
00141 {
00142 $write = $this->_getWriteAdapter();
00143
00144 if ($visitor->getDoCustomerLogin()) {
00145 $write->insert($this->getTable('log/customer'), array(
00146 'visitor_id' => $visitor->getVisitorId(),
00147 'customer_id' => $visitor->getCustomerId(),
00148 'login_at' => now(),
00149 'store_id' => Mage::app()->getStore()->getId(),
00150 ));
00151 $visitor->setCustomerLogId($write->lastInsertId());
00152 $visitor->setDoCustomerLogin(false);
00153 }
00154
00155 if ($visitor->getDoCustomerLogout() && $logId = $visitor->getCustomerLogId()) {
00156 $write->update($this->getTable('log/customer'),
00157 array(
00158 'logout_at' => now(),
00159 'store_id' => Mage::app()->getStore()->getId(),
00160 ),
00161 $write->quoteInto('log_id=?', $logId)
00162 );
00163 $visitor->setDoCustomerLogout(false);
00164 $visitor->setCustomerId(null);
00165 $visitor->setCustomerLogId(null);
00166 }
00167 return $this;
00168 }
00169
00170
00171
00172
00173
00174
00175
00176 protected function _saveQuoteInfo($visitor)
00177 {
00178 $write = $this->_getWriteAdapter();
00179 if ($visitor->getDoQuoteCreate()) {
00180 $write->insert($this->getTable('log/quote_table'), array(
00181 'quote_id' => $visitor->getQuoteId(),
00182 'visitor_id'=> $visitor->getId(),
00183 'created_at'=> now()
00184 ));
00185 $visitor->setDoQuoteCreate(false);
00186 }
00187
00188 if ($visitor->getDoQuoteDestroy()) {
00189
00190
00191
00192
00193 $write->delete($this->getTable('log/quote_table'),
00194 $write->quoteInto('quote_id=?', $visitor->getQuoteId())
00195 );
00196
00197
00198
00199
00200 $visitor->setDoQuoteDestroy(false);
00201 $visitor->setQuoteId(null);
00202 }
00203 return $this;
00204 }
00205 }