PageRenderTime 58ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/MXWest/magento-ce-1.5.1.0
PHP | 132 lines | 72 code | 15 blank | 45 comment | 6 complexity | 6aca5c6044e4d4eebf21147161a34aa2 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. * Log Prepare Online visitors resource collection
  28. *
  29. * @category Mage
  30. * @package Mage_Log
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Log_Model_Mysql4_Visitor_Online extends Mage_Core_Model_Mysql4_Abstract
  34. {
  35. /**
  36. * Initialize connection and define resource
  37. *
  38. */
  39. protected function _construct()
  40. {
  41. $this->_init('log/visitor_online', 'visitor_id');
  42. }
  43. /**
  44. * Prepare online visitors for collection
  45. *
  46. * @param Mage_Log_Model_Visitor_Online $object
  47. * @return Mage_Log_Model_Mysql4_Visitor_Online
  48. */
  49. public function prepare(Mage_Log_Model_Visitor_Online $object)
  50. {
  51. if (($object->getUpdateFrequency() + $object->getPrepareAt()) > time()) {
  52. return $this;
  53. }
  54. $this->_getWriteAdapter()->beginTransaction();
  55. $this->_getWriteAdapter()->truncate($this->getMainTable());
  56. $visitors = array();
  57. $lastUrls = array();
  58. // retrieve online visitors general data
  59. $whereCond = sprintf('last_visit_at >= (? - INTERVAL %d MINUTE)',
  60. $object->getOnlineInterval());
  61. $select = $this->_getReadAdapter()->select()
  62. ->from(
  63. $this->getTable('log/visitor'),
  64. array('visitor_id', 'first_visit_at', 'last_visit_at', 'last_url_id'))
  65. ->where($whereCond, now());
  66. $query = $this->_getReadAdapter()->query($select);
  67. while ($row = $query->fetch()) {
  68. $visitors[$row['visitor_id']] = $row;
  69. $lastUrls[$row['last_url_id']] = $row['visitor_id'];
  70. $visitors[$row['visitor_id']]['visitor_type'] = Mage_Log_Model_Visitor::VISITOR_TYPE_VISITOR;
  71. $visitors[$row['visitor_id']]['customer_id'] = null;
  72. }
  73. if (!$visitors) {
  74. $this->commit();
  75. return $this;
  76. }
  77. // retrieve visitor remote addr
  78. $select = $this->_getReadAdapter()->select()
  79. ->from(
  80. $this->getTable('log/visitor_info'),
  81. array('visitor_id', 'remote_addr'))
  82. ->where('visitor_id IN(?)', array_keys($visitors));
  83. $query = $this->_getReadAdapter()->query($select);
  84. while ($row = $query->fetch()) {
  85. $visitors[$row['visitor_id']]['remote_addr'] = $row['remote_addr'];
  86. }
  87. // retrieve visitor last URLs
  88. $select = $this->_getReadAdapter()->select()
  89. ->from(
  90. $this->getTable('log/url_info_table'),
  91. array('url_id', 'url'))
  92. ->where('url_id IN(?)', array_keys($lastUrls));
  93. $query = $this->_getReadAdapter()->query($select);
  94. while ($row = $query->fetch()) {
  95. $visitorId = $lastUrls[$row['url_id']];
  96. $visitors[$visitorId]['last_url'] = $row['url'];
  97. }
  98. // retrieve customers
  99. $select = $this->_getReadAdapter()->select()
  100. ->from(
  101. $this->getTable('log/customer'),
  102. array('visitor_id', 'customer_id'))
  103. ->where('visitor_id IN(?)', array_keys($visitors));
  104. $query = $this->_getReadAdapter()->query($select);
  105. while ($row = $query->fetch()) {
  106. $visitors[$row['visitor_id']]['visitor_type'] = Mage_Log_Model_Visitor::VISITOR_TYPE_CUSTOMER;
  107. $visitors[$row['visitor_id']]['customer_id'] = $row['customer_id'];
  108. }
  109. foreach ($visitors as $visitorData) {
  110. unset($visitorData['last_url_id']);
  111. $this->_getWriteAdapter()->insert($this->getMainTable(), $visitorData);
  112. }
  113. $this->_getWriteAdapter()->commit();
  114. $object->setPrepareAt();
  115. return $this;
  116. }
  117. }