PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/jit_bec/shopifine
PHP | 153 lines | 71 code | 16 blank | 66 comment | 5 complexity | 1b6e3bd6fe2081f628dfae7386ad9378 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 aggregation resource model
  28. *
  29. * @category Mage
  30. * @package Mage_Log
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Log_Model_Resource_Aggregation extends Mage_Core_Model_Resource_Db_Abstract
  34. {
  35. /**
  36. * Resource initialization
  37. *
  38. */
  39. protected function _construct()
  40. {
  41. $this->_init('log/summary_table', 'log_summary_id');
  42. }
  43. /**
  44. * Retrieve last added record
  45. *
  46. * @return string
  47. */
  48. public function getLastRecordDate()
  49. {
  50. $adapter = $this->_getReadAdapter();
  51. $select = $adapter->select()
  52. ->from($this->getTable('log/summary_table'),
  53. array($adapter->quoteIdentifier('date')=>'MAX(add_date)'));
  54. return $adapter->fetchOne($select);
  55. }
  56. /**
  57. * Retrieve count of visitors, customers
  58. *
  59. * @param string $from
  60. * @param string $to
  61. * @param int $store
  62. * @return array
  63. */
  64. public function getCounts($from, $to, $store)
  65. {
  66. $adapter = $this->_getReadAdapter();
  67. $result = array('customers'=>0, 'visitors'=>0);
  68. $select = $adapter->select()
  69. ->from($this->getTable('log/customer'), 'visitor_id')
  70. ->where('login_at >= ?', $from)
  71. ->where('login_at <= ?', $to);
  72. if ($store) {
  73. $select->where('store_id = ?', $store);
  74. }
  75. $customers = $adapter->fetchCol($select);
  76. $result['customers'] = count($customers);
  77. $select = $adapter->select();
  78. $select->from($this->getTable('log/visitor'), 'COUNT(*)')
  79. ->where('first_visit_at >= ?', $from)
  80. ->where('first_visit_at <= ?', $to);
  81. if ($store) {
  82. $select->where('store_id = ?', $store);
  83. }
  84. if ($result['customers']) {
  85. $select->where('visitor_id NOT IN(?)', $customers);
  86. }
  87. $result['visitors'] = $adapter->fetchOne($select);
  88. return $result;
  89. }
  90. /**
  91. * Save log
  92. *
  93. * @param array $data
  94. * @param int $id
  95. */
  96. public function saveLog($data, $id = null)
  97. {
  98. $adapter = $this->_getWriteAdapter();
  99. if (is_null($id)) {
  100. $adapter->insert($this->getTable('log/summary_table'), $data);
  101. } else {
  102. $condition = $adapter->quoteInto('summary_id = ?', $id);
  103. $adapter->update($this->getTable('log/summary_table'), $data, $condition);
  104. }
  105. }
  106. /**
  107. * Remove empty records
  108. *
  109. * @param string $date
  110. */
  111. public function removeEmpty($date)
  112. {
  113. $adapter = $this->_getWriteAdapter();
  114. $condition = array(
  115. 'add_date < ?' => $date,
  116. 'customer_count = 0',
  117. 'visitor_count = 0'
  118. );
  119. $adapter->delete($this->getTable('log/summary_table'), $condition);
  120. }
  121. /**
  122. * Retrieve log id
  123. *
  124. * @param string $from
  125. * @param string $to
  126. * @return string
  127. */
  128. public function getLogId($from, $to)
  129. {
  130. $adapter = $this->_getReadAdapter();
  131. $select = $adapter->select()
  132. ->from($this->getTable('log/summary_table'), 'summary_id')
  133. ->where('add_date >= ?', $from)
  134. ->where('add_date <= ?', $to);
  135. return $adapter->fetchOne($select);
  136. }
  137. }