PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/jit_bec/shopifine
PHP | 146 lines | 78 code | 23 blank | 45 comment | 6 complexity | 122c4343f69193f02a5e6077054f82ad 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. * Log Prepare Online visitors 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_Online extends Mage_Core_Model_Resource_Db_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_Resource_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. $readAdapter = $this->_getReadAdapter();
  55. $writeAdapter = $this->_getWriteAdapter();
  56. $writeAdapter->beginTransaction();
  57. try{
  58. $writeAdapter->delete($this->getMainTable());
  59. $visitors = array();
  60. $lastUrls = array();
  61. // retrieve online visitors general data
  62. $lastDate = Mage::getModel('core/date')->gmtTimestamp() - $object->getOnlineInterval() * 60;
  63. $select = $readAdapter->select()
  64. ->from(
  65. $this->getTable('log/visitor'),
  66. array('visitor_id', 'first_visit_at', 'last_visit_at', 'last_url_id'))
  67. ->where('last_visit_at >= ?', $readAdapter->formatDate($lastDate));
  68. $query = $readAdapter->query($select);
  69. while ($row = $query->fetch()) {
  70. $visitors[$row['visitor_id']] = $row;
  71. $lastUrls[$row['last_url_id']] = $row['visitor_id'];
  72. $visitors[$row['visitor_id']]['visitor_type'] = Mage_Log_Model_Visitor::VISITOR_TYPE_VISITOR;
  73. $visitors[$row['visitor_id']]['customer_id'] = null;
  74. }
  75. if (!$visitors) {
  76. $this->commit();
  77. return $this;
  78. }
  79. // retrieve visitor remote addr
  80. $select = $readAdapter->select()
  81. ->from(
  82. $this->getTable('log/visitor_info'),
  83. array('visitor_id', 'remote_addr'))
  84. ->where('visitor_id IN(?)', array_keys($visitors));
  85. $query = $readAdapter->query($select);
  86. while ($row = $query->fetch()) {
  87. $visitors[$row['visitor_id']]['remote_addr'] = $row['remote_addr'];
  88. }
  89. // retrieve visitor last URLs
  90. $select = $readAdapter->select()
  91. ->from(
  92. $this->getTable('log/url_info_table'),
  93. array('url_id', 'url'))
  94. ->where('url_id IN(?)', array_keys($lastUrls));
  95. $query = $readAdapter->query($select);
  96. while ($row = $query->fetch()) {
  97. $visitorId = $lastUrls[$row['url_id']];
  98. $visitors[$visitorId]['last_url'] = $row['url'];
  99. }
  100. // retrieve customers
  101. $select = $readAdapter->select()
  102. ->from(
  103. $this->getTable('log/customer'),
  104. array('visitor_id', 'customer_id'))
  105. ->where('visitor_id IN(?)', array_keys($visitors));
  106. $query = $readAdapter->query($select);
  107. while ($row = $query->fetch()) {
  108. $visitors[$row['visitor_id']]['visitor_type'] = Mage_Log_Model_Visitor::VISITOR_TYPE_CUSTOMER;
  109. $visitors[$row['visitor_id']]['customer_id'] = $row['customer_id'];
  110. }
  111. foreach ($visitors as $visitorData) {
  112. unset($visitorData['last_url_id']);
  113. $writeAdapter->insertForce($this->getMainTable(), $visitorData);
  114. }
  115. $writeAdapter->commit();
  116. } catch (Exception $e) {
  117. $writeAdapter->rollBack();
  118. throw $e;
  119. }
  120. $object->setPrepareAt();
  121. return $this;
  122. }
  123. }