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 class Mage_Log_Model_Visitor extends Mage_Core_Model_Abstract
00029 {
00030 const DEFAULT_ONLINE_MINUTES_INTERVAL = 15;
00031 const VISITOR_TYPE_CUSTOMER = 'c';
00032 const VISITOR_TYPE_VISITOR = 'v';
00033
00034 protected function _construct()
00035 {
00036 $this->_init('log/visitor');
00037 }
00038
00039
00040
00041
00042
00043
00044 protected function _getSession()
00045 {
00046 return Mage::getSingleton('core/session');
00047 }
00048
00049
00050
00051
00052
00053
00054 public function getResource()
00055 {
00056 return Mage::getResourceSingleton('log/visitor');
00057 }
00058
00059
00060
00061
00062
00063
00064 public function initServerData()
00065 {
00066 $s = $_SERVER;
00067 $this->addData(array(
00068 'server_addr' => empty($s['SERVER_ADDR']) ? '' : ip2long($s['SERVER_ADDR']),
00069 'remote_addr' => empty($s['REMOTE_ADDR']) ? '' : ip2long($s['REMOTE_ADDR']),
00070 'http_secure' => Mage::app()->getStore()->isCurrentlySecure(),
00071 'http_host' => empty($s['HTTP_HOST']) ? '' : $s['HTTP_HOST'],
00072 'http_user_agent' => empty($s['HTTP_USER_AGENT']) ? '' : $s['HTTP_USER_AGENT'],
00073 'http_accept_language'=> empty($s['HTTP_ACCEPT_LANGUAGE']) ? '' : $s['HTTP_ACCEPT_LANGUAGE'],
00074 'http_accept_charset'=> empty($s['HTTP_ACCEPT_CHARSET']) ? '' : $s['HTTP_ACCEPT_CHARSET'],
00075 'request_uri' => empty($s['REQUEST_URI']) ? '' : $s['REQUEST_URI'],
00076 'session_id' => $this->_getSession()->getSessionId(),
00077 'http_referer' => empty($s['HTTP_REFERER']) ? '' : $s['HTTP_REFERER'],
00078 ));
00079
00080 return $this;
00081 }
00082
00083
00084
00085
00086
00087
00088 public static function getOnlineMinutesInterval()
00089 {
00090 $configValue = Mage::getStoreConfig('customer/online_customers/online_minutes_interval');
00091 return intval($configValue) > 0
00092 ? intval($configValue)
00093 : self::DEFAULT_ONLINE_MINUTES_INTERVAL;
00094 }
00095
00096
00097
00098
00099
00100
00101 public function getUrl()
00102 {
00103 $url = 'http' . ($this->getHttpSecure() ? 's' : '') . '://';
00104 $url .= $this->getHttpHost().$this->getRequestUri();
00105 return $url;
00106 }
00107
00108 public function getFirstVisitAt()
00109 {
00110 if (!$this->hasData('first_visit_at')) {
00111 $this->setData('first_visit_at', now());
00112 }
00113 return $this->getData('first_visit_at');
00114 }
00115
00116 public function getLastVisitAt()
00117 {
00118 if (!$this->hasData('last_visit_at')) {
00119 $this->setData('last_visit_at', now());
00120 }
00121 return $this->getData('last_visit_at');
00122 }
00123
00124 public function isModuleIgnored($observer)
00125 {
00126 $ignores = Mage::getConfig()->getNode('global/ignoredModules/entities')->asArray();
00127
00128 if( is_array($ignores) && $observer) {
00129 $curModule = $observer->getEvent()->getControllerAction()->getRequest()->getRouteName();
00130 if (isset($ignores[$curModule])) {
00131 return true;
00132 }
00133 }
00134 return false;
00135 }
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145 public function initByRequest($observer)
00146 {
00147 if ($this->isModuleIgnored($observer)) {
00148 return $this;
00149 }
00150
00151 $this->setData($this->_getSession()->getVisitorData());
00152 $this->initServerData();
00153
00154 if (!$this->getId()) {
00155 $this->setFirstVisitAt(now());
00156 $this->setIsNewVisitor(true);
00157 $this->save();
00158 }
00159 return $this;
00160 }
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170 public function saveByRequest($observer)
00171 {
00172 if ($this->isModuleIgnored($observer)) {
00173 return $this;
00174 }
00175
00176 $this->setLastVisitAt(now());
00177 $this->save();
00178
00179 $this->_getSession()->setVisitorData($this->getData());
00180 return $this;
00181 }
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191 public function bindCustomerLogin($observer)
00192 {
00193 if (!$this->getCustomerId() && $customer = $observer->getEvent()->getCustomer()) {
00194 $this->setDoCustomerLogin(true);
00195 $this->setCustomerId($customer->getId());
00196 }
00197 return $this;
00198 }
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208 public function bindCustomerLogout($observer)
00209 {
00210 if ($this->getCustomerId() && $customer = $observer->getEvent()->getCustomer()) {
00211 $this->setDoCustomerLogout(true);
00212 }
00213 return $this;
00214 }
00215
00216 public function bindQuoteCreate($observer)
00217 {
00218 if ($quote = $observer->getEvent()->getQuote()) {
00219 if ($quote->getIsCheckoutCart()) {
00220 $this->setQuoteId($quote->getId());
00221 $this->setDoQuoteCreate(true);
00222 }
00223 }
00224 return $this;
00225 }
00226
00227 public function bindQuoteDestroy($observer)
00228 {
00229 if ($quote = $observer->getEvent()->getQuote()) {
00230 $this->setDoQuoteDestroy(true);
00231 }
00232 return $this;
00233 }
00234
00235
00236
00237
00238 public function addIpData($data)
00239 {
00240 $ipData = array();
00241 $data->setIpData($ipData);
00242 return $this;
00243 }
00244
00245 public function addCustomerData($data)
00246 {
00247 $customerId = $data->getCustomerId();
00248 if( intval($customerId) <= 0 ) {
00249 return $this;
00250 }
00251 $customerData = Mage::getModel('customer/customer')->load($customerId);
00252 $newCustomerData = array();
00253 foreach( $customerData->getData() as $propName => $propValue ) {
00254 $newCustomerData['customer_' . $propName] = $propValue;
00255 }
00256
00257 $data->addData($newCustomerData);
00258 return $this;
00259 }
00260
00261 public function addQuoteData($data)
00262 {
00263 $quoteId = $data->getQuoteId();
00264 if( intval($quoteId) <= 0 ) {
00265 return $this;
00266 }
00267 $data->setQuoteData(Mage::getModel('sales/quote')->load($quoteId));
00268 return $this;
00269 }
00270 }