PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/jit_bec/shopifine
PHP | 282 lines | 155 code | 32 blank | 95 comment | 13 complexity | 948c287787bf6a1ce0d45c68187c9328 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. * 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_Resource_Visitor extends Mage_Core_Model_Resource_Db_Abstract
  34. {
  35. /**
  36. * Define main table
  37. *
  38. */
  39. protected function _construct()
  40. {
  41. $this->_init('log/visitor', 'visitor_id');
  42. }
  43. /**
  44. * Prepare data for save
  45. *
  46. * @param Mage_Core_Model_Abstract $visitor
  47. * @return array
  48. */
  49. protected function _prepareDataForSave(Mage_Core_Model_Abstract $visitor)
  50. {
  51. return array(
  52. 'session_id' => $visitor->getSessionId(),
  53. 'first_visit_at' => $visitor->getFirstVisitAt(),
  54. 'last_visit_at' => $visitor->getLastVisitAt(),
  55. 'last_url_id' => $visitor->getLastUrlId() ? $visitor->getLastUrlId() : 0,
  56. 'store_id' => Mage::app()->getStore()->getId(),
  57. );
  58. }
  59. /**
  60. * Saving information about url
  61. *
  62. * @param Mage_Log_Model_Visitor $visitor
  63. * @return Mage_Log_Model_Resource_Visitor
  64. */
  65. protected function _saveUrlInfo($visitor)
  66. {
  67. $adapter = $this->_getWriteAdapter();
  68. $data = new Varien_Object(array(
  69. 'url' => Mage::helper('core/string')->substr($visitor->getUrl(), 0, 250),
  70. 'referer'=> Mage::helper('core/string')->substr($visitor->getHttpReferer(), 0, 250)
  71. ));
  72. $bind = $this->_prepareDataForTable($data, $this->getTable('log/url_info_table'));
  73. $adapter->insert($this->getTable('log/url_info_table'), $bind);
  74. $visitor->setLastUrlId($adapter->lastInsertId($this->getTable('log/url_info_table')));
  75. return $this;
  76. }
  77. /**
  78. * Save url info before save
  79. *
  80. * @param Mage_Core_Model_Abstract $visitor
  81. * @return Mage_Log_Model_Resource_Visitor
  82. */
  83. protected function _beforeSave(Mage_Core_Model_Abstract $visitor)
  84. {
  85. if (!$visitor->getIsNewVisitor()) {
  86. $this->_saveUrlInfo($visitor);
  87. }
  88. return $this;
  89. }
  90. /**
  91. * Actions after save
  92. *
  93. * @param Mage_Core_Model_Abstract $visitor
  94. * @return Mage_Log_Model_Resource_Visitor
  95. */
  96. protected function _afterSave(Mage_Core_Model_Abstract $visitor)
  97. {
  98. if ($visitor->getIsNewVisitor()) {
  99. $this->_saveVisitorInfo($visitor);
  100. $visitor->setIsNewVisitor(false);
  101. } else {
  102. $this->_saveVisitorUrl($visitor);
  103. if ($visitor->getDoCustomerLogin() || $visitor->getDoCustomerLogout()) {
  104. $this->_saveCustomerInfo($visitor);
  105. }
  106. if ($visitor->getDoQuoteCreate() || $visitor->getDoQuoteDestroy()) {
  107. $this->_saveQuoteInfo($visitor);
  108. }
  109. }
  110. return $this;
  111. }
  112. /**
  113. * Perform actions after object load
  114. *
  115. * @param Varien_Object $object
  116. * @return Mage_Core_Model_Resource_Db_Abstract
  117. */
  118. protected function _afterLoad(Mage_Core_Model_Abstract $object)
  119. {
  120. parent::_afterLoad($object);
  121. // Add information about quote to visitor
  122. $adapter = $this->_getReadAdapter();
  123. $select = $adapter->select()->from($this->getTable('log/quote_table'), 'quote_id')
  124. ->where('visitor_id = ?', $object->getId())->limit(1);
  125. $result = $adapter->query($select)->fetch();
  126. if (isset($result['quote_id'])) {
  127. $object->setQuoteId((int) $result['quote_id']);
  128. }
  129. return $this;
  130. }
  131. /**
  132. * Saving visitor information
  133. *
  134. * @param Mage_Log_Model_Visitor $visitor
  135. * @return Mage_Log_Model_Resource_Visitor
  136. */
  137. protected function _saveVisitorInfo($visitor)
  138. {
  139. /* @var $stringHelper Mage_Core_Helper_String */
  140. $stringHelper = Mage::helper('core/string');
  141. $referer = $stringHelper->cleanString($visitor->getHttpReferer());
  142. $referer = $stringHelper->substr($referer, 0, 255);
  143. $userAgent = $stringHelper->cleanString($visitor->getHttpUserAgent());
  144. $userAgent = $stringHelper->substr($userAgent, 0, 255);
  145. $charset = $stringHelper->cleanString($visitor->getHttpAcceptCharset());
  146. $charset = $stringHelper->substr($charset, 0, 255);
  147. $language = $stringHelper->cleanString($visitor->getHttpAcceptLanguage());
  148. $language = $stringHelper->substr($language, 0, 255);
  149. $adapter = $this->_getWriteAdapter();
  150. $data = new Varien_Object(array(
  151. 'visitor_id' => $visitor->getId(),
  152. 'http_referer' => $referer,
  153. 'http_user_agent' => $userAgent,
  154. 'http_accept_charset' => $charset,
  155. 'http_accept_language' => $language,
  156. 'server_addr' => $visitor->getServerAddr(),
  157. 'remote_addr' => $visitor->getRemoteAddr(),
  158. ));
  159. $bind = $this->_prepareDataForTable($data, $this->getTable('log/visitor_info'));
  160. $adapter->insert($this->getTable('log/visitor_info'), $bind);
  161. return $this;
  162. }
  163. /**
  164. * Saving visitor and url relation
  165. *
  166. * @param Mage_Log_Model_Visitor $visitor
  167. * @return Mage_Log_Model_Resource_Visitor
  168. */
  169. protected function _saveVisitorUrl($visitor)
  170. {
  171. $data = new Varien_Object(array(
  172. 'url_id' => $visitor->getLastUrlId(),
  173. 'visitor_id' => $visitor->getId(),
  174. 'visit_time' => Mage::getSingleton('core/date')->gmtDate()
  175. ));
  176. $bind = $this->_prepareDataForTable($data, $this->getTable('log/url_table'));
  177. $this->_getWriteAdapter()->insert($this->getTable('log/url_table'), $bind);
  178. return $this;
  179. }
  180. /**
  181. * Saving information about customer
  182. *
  183. * @param Mage_Log_Model_Visitor $visitor
  184. * @return Mage_Log_Model_Resource_Visitor
  185. */
  186. protected function _saveCustomerInfo($visitor)
  187. {
  188. $adapter = $this->_getWriteAdapter();
  189. if ($visitor->getDoCustomerLogin()) {
  190. $data = new Varien_Object(array(
  191. 'visitor_id' => $visitor->getVisitorId(),
  192. 'customer_id' => $visitor->getCustomerId(),
  193. 'login_at' => Mage::getSingleton('core/date')->gmtDate(),
  194. 'store_id' => Mage::app()->getStore()->getId()
  195. ));
  196. $bind = $this->_prepareDataForTable($data, $this->getTable('log/customer'));
  197. $adapter->insert($this->getTable('log/customer'), $bind);
  198. $visitor->setCustomerLogId($adapter->lastInsertId($this->getTable('log/customer')));
  199. $visitor->setDoCustomerLogin(false);
  200. }
  201. if ($visitor->getDoCustomerLogout() && $logId = $visitor->getCustomerLogId()) {
  202. $data = new Varien_Object(array(
  203. 'logout_at' => Mage::getSingleton('core/date')->gmtDate(),
  204. 'store_id' => (int)Mage::app()->getStore()->getId(),
  205. ));
  206. $bind = $this->_prepareDataForTable($data, $this->getTable('log/customer'));
  207. $condition = array(
  208. 'log_id = ?' => (int) $logId,
  209. );
  210. $adapter->update($this->getTable('log/customer'), $bind, $condition);
  211. $visitor->setDoCustomerLogout(false);
  212. $visitor->setCustomerId(null);
  213. $visitor->setCustomerLogId(null);
  214. }
  215. return $this;
  216. }
  217. /**
  218. * Saving information about quote
  219. *
  220. * @param Mage_Log_Model_Visitor $visitor
  221. * @return Mage_Log_Model_Resource_Visitor
  222. */
  223. protected function _saveQuoteInfo($visitor)
  224. {
  225. $adapter = $this->_getWriteAdapter();
  226. if ($visitor->getDoQuoteCreate()) {
  227. $data = new Varien_Object(array(
  228. 'quote_id' => (int) $visitor->getQuoteId(),
  229. 'visitor_id' => (int) $visitor->getId(),
  230. 'created_at' => Mage::getSingleton('core/date')->gmtDate()
  231. ));
  232. $bind = $this->_prepareDataForTable($data, $this->getTable('log/quote_table'));
  233. $adapter->insert($this->getTable('log/quote_table'), $bind);
  234. $visitor->setDoQuoteCreate(false);
  235. }
  236. if ($visitor->getDoQuoteDestroy()) {
  237. /**
  238. * We have delete quote from log because if original quote was
  239. * deleted and Mysql restarted we will get key duplication error
  240. */
  241. $condition = array(
  242. 'quote_id = ?' => (int) $visitor->getQuoteId(),
  243. );
  244. $adapter->delete($this->getTable('log/quote_table'), $condition);
  245. $visitor->setDoQuoteDestroy(false);
  246. $visitor->setQuoteId(null);
  247. }
  248. return $this;
  249. }
  250. }