PageRenderTime 151ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/MXWest/magento-ce-1.5.1.0
PHP | 217 lines | 132 code | 15 blank | 70 comment | 12 complexity | 45e561733968227c07b57851dc8140f2 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  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) 2010 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. * Visitor log resource
  28. *
  29. * @category Mage
  30. * @package Mage_Log
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Log_Model_Mysql4_Visitor extends Mage_Core_Model_Mysql4_Abstract
  34. {
  35. protected function _construct()
  36. {
  37. $this->_init('log/visitor', 'visitor_id');
  38. }
  39. protected function _prepareDataForSave(Mage_Core_Model_Abstract $visitor)
  40. {
  41. return array(
  42. 'session_id' => $visitor->getSessionId(),
  43. 'first_visit_at'=> $visitor->getFirstVisitAt(),
  44. 'last_visit_at' => $visitor->getLastVisitAt(),
  45. 'last_url_id' => $visitor->getLastUrlId() ? $visitor->getLastUrlId() : 0,
  46. 'store_id' => Mage::app()->getStore()->getId(),
  47. );
  48. }
  49. /**
  50. * Saving information about url
  51. *
  52. * @param Mage_Log_Model_Visitor $visitor
  53. * @return Mage_Log_Model_Mysql4_Visitor
  54. */
  55. protected function _saveUrlInfo($visitor)
  56. {
  57. $this->_getWriteAdapter()->insert($this->getTable('log/url_info_table'), array(
  58. 'url' => Mage::helper('core/string')->substr($visitor->getUrl(), 0, 250),
  59. 'referer'=> Mage::helper('core/string')->substr($visitor->getHttpReferer(), 0, 250)
  60. ));
  61. $visitor->setLastUrlId($this->_getWriteAdapter()->lastInsertId());
  62. return $this;
  63. }
  64. protected function _beforeSave(Mage_Core_Model_Abstract $visitor)
  65. {
  66. if (!$visitor->getIsNewVisitor()) {
  67. $this->_saveUrlInfo($visitor);
  68. }
  69. return $this;
  70. }
  71. protected function _afterSave(Mage_Core_Model_Abstract $visitor)
  72. {
  73. if ($visitor->getIsNewVisitor()) {
  74. $this->_saveVisitorInfo($visitor);
  75. $visitor->setIsNewVisitor(false);
  76. }
  77. else {
  78. $this->_saveVisitorUrl($visitor);
  79. if ($visitor->getDoCustomerLogin() || $visitor->getDoCustomerLogout()) {
  80. $this->_saveCustomerInfo($visitor);
  81. }
  82. if ($visitor->getDoQuoteCreate() || $visitor->getDoQuoteDestroy()) {
  83. $this->_saveQuoteInfo($visitor);
  84. }
  85. }
  86. return $this;
  87. }
  88. /**
  89. * Saving visitor information
  90. *
  91. * @param Mage_Log_Model_Visitor $visitor
  92. * @return Mage_Log_Model_Mysql4_Visitor
  93. */
  94. protected function _saveVisitorInfo($visitor)
  95. {
  96. /* @var $stringHelper Mage_Core_Helper_String */
  97. $stringHelper = Mage::helper('core/string');
  98. $referer = $stringHelper->cleanString($visitor->getHttpReferer());
  99. $referer = $stringHelper->substr($referer, 0, 255);
  100. $userAgent = $stringHelper->cleanString($visitor->getHttpUserAgent());
  101. $userAgent = $stringHelper->substr($userAgent, 0, 255);
  102. $charset = $stringHelper->cleanString($visitor->getHttpAcceptCharset());
  103. $charset = $stringHelper->substr($charset, 0, 255);
  104. $language = $stringHelper->cleanString($visitor->getHttpAcceptLanguage());
  105. $language = $stringHelper->substr($language, 0, 255);
  106. $write = $this->_getWriteAdapter();
  107. $data = array(
  108. 'visitor_id' => $visitor->getId(),
  109. 'http_referer' => $stringHelper->substr($visitor->getHttpReferer(), 0, 255),
  110. 'http_user_agent' => $stringHelper->substr($visitor->getHttpUserAgent(), 0, 255),
  111. 'http_accept_charset' => $stringHelper->substr($visitor->getHttpAcceptCharset(), 0, 255),
  112. 'http_accept_language' => $stringHelper->substr($visitor->getHttpAcceptLanguage(), 0, 255),
  113. 'server_addr' => $visitor->getServerAddr(),
  114. 'remote_addr' => $visitor->getRemoteAddr(),
  115. );
  116. $write->insert($this->getTable('log/visitor_info'), $data);
  117. return $this;
  118. }
  119. /**
  120. * Saving visitor and url relation
  121. *
  122. * @param Mage_Log_Model_Visitor $visitor
  123. * @return Mage_Log_Model_Mysql4_Visitor
  124. */
  125. protected function _saveVisitorUrl($visitor)
  126. {
  127. $write = $this->_getWriteAdapter();
  128. $write->insert($this->getTable('log/url_table'), array(
  129. 'url_id' => $visitor->getLastUrlId(),
  130. 'visitor_id'=> $visitor->getId(),
  131. 'visit_time'=> now(),
  132. ));
  133. return $this;
  134. }
  135. /**
  136. * Saving information about customer
  137. *
  138. * @param Mage_Log_Model_Visitor $visitor
  139. * @return Mage_Log_Model_Mysql4_Visitor
  140. */
  141. protected function _saveCustomerInfo($visitor)
  142. {
  143. $write = $this->_getWriteAdapter();
  144. if ($visitor->getDoCustomerLogin()) {
  145. $write->insert($this->getTable('log/customer'), array(
  146. 'visitor_id' => $visitor->getVisitorId(),
  147. 'customer_id' => $visitor->getCustomerId(),
  148. 'login_at' => now(),
  149. 'store_id' => Mage::app()->getStore()->getId(),
  150. ));
  151. $visitor->setCustomerLogId($write->lastInsertId());
  152. $visitor->setDoCustomerLogin(false);
  153. }
  154. if ($visitor->getDoCustomerLogout() && $logId = $visitor->getCustomerLogId()) {
  155. $write->update($this->getTable('log/customer'),
  156. array(
  157. 'logout_at' => now(),
  158. 'store_id' => Mage::app()->getStore()->getId(),
  159. ),
  160. $write->quoteInto('log_id=?', $logId)
  161. );
  162. $visitor->setDoCustomerLogout(false);
  163. $visitor->setCustomerId(null);
  164. $visitor->setCustomerLogId(null);
  165. }
  166. return $this;
  167. }
  168. /**
  169. * Saving information about quote
  170. *
  171. * @param Mage_Log_Model_Visitor $visitor
  172. * @return Mage_Log_Model_Mysql4_Visitor
  173. */
  174. protected function _saveQuoteInfo($visitor)
  175. {
  176. $write = $this->_getWriteAdapter();
  177. if ($visitor->getDoQuoteCreate()) {
  178. $write->insert($this->getTable('log/quote_table'), array(
  179. 'quote_id' => $visitor->getQuoteId(),
  180. 'visitor_id'=> $visitor->getId(),
  181. 'created_at'=> now()
  182. ));
  183. $visitor->setDoQuoteCreate(false);
  184. }
  185. if ($visitor->getDoQuoteDestroy()) {
  186. /**
  187. * We have delete quote from log because if original quote was
  188. * deleted and Mysql restarted we will get key duplication error
  189. */
  190. $write->delete($this->getTable('log/quote_table'),
  191. $write->quoteInto('quote_id=?', $visitor->getQuoteId())
  192. );
  193. // $write->update($this->getTable('log/quote_table'),
  194. // array('deleted_at'=> now()),
  195. // $write->quoteInto('quote_id=?', $visitor->getQuoteId())
  196. // );
  197. $visitor->setDoQuoteDestroy(false);
  198. $visitor->setQuoteId(null);
  199. }
  200. return $this;
  201. }
  202. }