PageRenderTime 53ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/magento/app/code/core/Mage/Log/Model/Visitor.php

https://bitbucket.org/jit_bec/shopifine
PHP | 297 lines | 169 code | 27 blank | 101 comment | 21 complexity | 20ac422f0beed7426029a100a5f76c34 MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Log
  23. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Enter description here ...
  28. *
  29. * @method Mage_Log_Model_Resource_Visitor _getResource()
  30. * @method Mage_Log_Model_Resource_Visitor getResource()
  31. * @method string getSessionId()
  32. * @method Mage_Log_Model_Visitor setSessionId(string $value)
  33. * @method Mage_Log_Model_Visitor setFirstVisitAt(string $value)
  34. * @method Mage_Log_Model_Visitor setLastVisitAt(string $value)
  35. * @method int getLastUrlId()
  36. * @method Mage_Log_Model_Visitor setLastUrlId(int $value)
  37. * @method int getStoreId()
  38. * @method Mage_Log_Model_Visitor setStoreId(int $value)
  39. *
  40. * @category Mage
  41. * @package Mage_Log
  42. * @author Magento Core Team <core@magentocommerce.com>
  43. */
  44. class Mage_Log_Model_Visitor extends Mage_Core_Model_Abstract
  45. {
  46. const DEFAULT_ONLINE_MINUTES_INTERVAL = 15;
  47. const VISITOR_TYPE_CUSTOMER = 'c';
  48. const VISITOR_TYPE_VISITOR = 'v';
  49. protected $_skipRequestLogging = false;
  50. /**
  51. * Onject initialization
  52. */
  53. protected function _construct()
  54. {
  55. $this->_init('log/visitor');
  56. $userAgent = Mage::helper('core/http')->getHttpUserAgent();
  57. $ignoreAgents = Mage::getConfig()->getNode('global/ignore_user_agents');
  58. if ($ignoreAgents) {
  59. $ignoreAgents = $ignoreAgents->asArray();
  60. if (in_array($userAgent, $ignoreAgents)) {
  61. $this->_skipRequestLogging = true;
  62. }
  63. }
  64. }
  65. /**
  66. * Retrieve session object
  67. *
  68. * @return Mage_Core_Model_Session_Abstract
  69. */
  70. protected function _getSession()
  71. {
  72. return Mage::getSingleton('core/session');
  73. }
  74. /**
  75. * Initialize visitor information from server data
  76. *
  77. * @return Mage_Log_Model_Visitor
  78. */
  79. public function initServerData()
  80. {
  81. /* @var $helper Mage_Core_Helper_Http */
  82. $helper = Mage::helper('core/http');
  83. $this->addData(array(
  84. 'server_addr' => $helper->getServerAddr(true),
  85. 'remote_addr' => $helper->getRemoteAddr(true),
  86. 'http_secure' => Mage::app()->getStore()->isCurrentlySecure(),
  87. 'http_host' => $helper->getHttpHost(true),
  88. 'http_user_agent' => $helper->getHttpUserAgent(true),
  89. 'http_accept_language' => $helper->getHttpAcceptLanguage(true),
  90. 'http_accept_charset' => $helper->getHttpAcceptCharset(true),
  91. 'request_uri' => $helper->getRequestUri(true),
  92. 'session_id' => $this->_getSession()->getSessionId(),
  93. 'http_referer' => $helper->getHttpReferer(true),
  94. ));
  95. return $this;
  96. }
  97. /**
  98. * Return Online Minutes Interval
  99. *
  100. * @return int Minutes Interval
  101. */
  102. public static function getOnlineMinutesInterval()
  103. {
  104. $configValue = Mage::getStoreConfig('customer/online_customers/online_minutes_interval');
  105. return intval($configValue) > 0
  106. ? intval($configValue)
  107. : self::DEFAULT_ONLINE_MINUTES_INTERVAL;
  108. }
  109. /**
  110. * Retrieve url from model data
  111. *
  112. * @return string
  113. */
  114. public function getUrl()
  115. {
  116. $url = 'http' . ($this->getHttpSecure() ? 's' : '') . '://';
  117. $url .= $this->getHttpHost().$this->getRequestUri();
  118. return $url;
  119. }
  120. public function getFirstVisitAt()
  121. {
  122. if (!$this->hasData('first_visit_at')) {
  123. $this->setData('first_visit_at', now());
  124. }
  125. return $this->getData('first_visit_at');
  126. }
  127. public function getLastVisitAt()
  128. {
  129. if (!$this->hasData('last_visit_at')) {
  130. $this->setData('last_visit_at', now());
  131. }
  132. return $this->getData('last_visit_at');
  133. }
  134. /**
  135. * Initialization visitor information by request
  136. *
  137. * Used in event "controller_action_predispatch"
  138. *
  139. * @param Varien_Event_Observer $observer
  140. * @return Mage_Log_Model_Visitor
  141. */
  142. public function initByRequest($observer)
  143. {
  144. if ($this->_skipRequestLogging || $this->isModuleIgnored($observer)) {
  145. return $this;
  146. }
  147. $this->setData($this->_getSession()->getVisitorData());
  148. $this->initServerData();
  149. if (!$this->getId()) {
  150. $this->setFirstVisitAt(now());
  151. $this->setIsNewVisitor(true);
  152. $this->save();
  153. Mage::dispatchEvent('visitor_init', array('visitor' => $this));
  154. }
  155. return $this;
  156. }
  157. /**
  158. * Saving visitor information by request
  159. *
  160. * Used in event "controller_action_postdispatch"
  161. *
  162. * @param Varien_Event_Observer $observer
  163. * @return Mage_Log_Model_Visitor
  164. */
  165. public function saveByRequest($observer)
  166. {
  167. if ($this->_skipRequestLogging || $this->isModuleIgnored($observer)) {
  168. return $this;
  169. }
  170. try {
  171. $this->setLastVisitAt(now());
  172. $this->save();
  173. $this->_getSession()->setVisitorData($this->getData());
  174. } catch (Exception $e) {
  175. Mage::logException($e);
  176. }
  177. return $this;
  178. }
  179. /**
  180. * Bind customer data when customer login
  181. *
  182. * Used in event "customer_login"
  183. *
  184. * @param Varien_Event_Observer $observer
  185. * @return Mage_Log_Model_Visitor
  186. */
  187. public function bindCustomerLogin($observer)
  188. {
  189. if (!$this->getCustomerId() && $customer = $observer->getEvent()->getCustomer()) {
  190. $this->setDoCustomerLogin(true);
  191. $this->setCustomerId($customer->getId());
  192. }
  193. return $this;
  194. }
  195. /**
  196. * Bind customer data when customer logout
  197. *
  198. * Used in event "customer_logout"
  199. *
  200. * @param Varien_Event_Observer $observer
  201. * @return Mage_Log_Model_Visitor
  202. */
  203. public function bindCustomerLogout($observer)
  204. {
  205. if ($this->getCustomerId() && $customer = $observer->getEvent()->getCustomer()) {
  206. $this->setDoCustomerLogout(true);
  207. }
  208. return $this;
  209. }
  210. public function bindQuoteCreate($observer)
  211. {
  212. if ($quote = $observer->getEvent()->getQuote()) {
  213. if ($quote->getIsCheckoutCart()) {
  214. $this->setQuoteId($quote->getId());
  215. $this->setDoQuoteCreate(true);
  216. }
  217. }
  218. return $this;
  219. }
  220. public function bindQuoteDestroy($observer)
  221. {
  222. if ($quote = $observer->getEvent()->getQuote()) {
  223. $this->setDoQuoteDestroy(true);
  224. }
  225. return $this;
  226. }
  227. /**
  228. * Methods for research (depends from customer online admin section)
  229. */
  230. public function addIpData($data)
  231. {
  232. $ipData = array();
  233. $data->setIpData($ipData);
  234. return $this;
  235. }
  236. public function addCustomerData($data)
  237. {
  238. $customerId = $data->getCustomerId();
  239. if( intval($customerId) <= 0 ) {
  240. return $this;
  241. }
  242. $customerData = Mage::getModel('customer/customer')->load($customerId);
  243. $newCustomerData = array();
  244. foreach( $customerData->getData() as $propName => $propValue ) {
  245. $newCustomerData['customer_' . $propName] = $propValue;
  246. }
  247. $data->addData($newCustomerData);
  248. return $this;
  249. }
  250. public function addQuoteData($data)
  251. {
  252. $quoteId = $data->getQuoteId();
  253. if( intval($quoteId) <= 0 ) {
  254. return $this;
  255. }
  256. $data->setQuoteData(Mage::getModel('sales/quote')->load($quoteId));
  257. return $this;
  258. }
  259. public function isModuleIgnored($observer)
  260. {
  261. $ignores = Mage::getConfig()->getNode('global/ignoredModules/entities')->asArray();
  262. if( is_array($ignores) && $observer) {
  263. $curModule = $observer->getEvent()->getControllerAction()->getRequest()->getRouteName();
  264. if (isset($ignores[$curModule])) {
  265. return true;
  266. }
  267. }
  268. return false;
  269. }
  270. }