PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/MXWest/magento-ce-1.5.1.0
PHP | 317 lines | 163 code | 33 blank | 121 comment | 12 complexity | 7f479d0957c551a21828e0568cc1f848 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. * Mage_Log_Model_Mysql4_Customers_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_Collection extends Varien_Data_Collection_Db
  34. {
  35. /**
  36. * Visitor data table name
  37. *
  38. * @var string
  39. */
  40. protected $_visitorTable;
  41. /**
  42. * Visitor data info table name
  43. *
  44. * @var string
  45. */
  46. protected $_visitorInfoTable;
  47. /**
  48. * Customer data table
  49. *
  50. * @var string
  51. */
  52. protected $_customerTable;
  53. /**
  54. * Log URL data table name.
  55. *
  56. * @var string
  57. */
  58. protected $_urlTable;
  59. /**
  60. * Log URL expanded data table name.
  61. *
  62. * @var string
  63. */
  64. protected $_urlInfoTable;
  65. /**
  66. * Aggregator data table.
  67. *
  68. * @var string
  69. */
  70. protected $_summaryTable;
  71. /**
  72. * Aggregator type data table.
  73. *
  74. * @var string
  75. */
  76. protected $_summaryTypeTable;
  77. /**
  78. * Quote data table.
  79. *
  80. * @var string
  81. */
  82. protected $_quoteTable;
  83. protected $_isOnlineFilterUsed = false;
  84. protected $_fieldMap = array(
  85. 'customer_firstname' => 'customer_firstname_table.value',
  86. 'customer_lastname' => 'customer_lastname_table.value',
  87. 'customer_email' => 'customer_email_table.email',
  88. 'customer_id' => 'customer_table.customer_id',
  89. 'url' => 'url_info_table.url'
  90. );
  91. /**
  92. * Construct
  93. *
  94. */
  95. function __construct()
  96. {
  97. $resource = Mage::getSingleton('core/resource');
  98. parent::__construct($resource->getConnection('log_read'));
  99. $this->_visitorTable = $resource->getTableName('log/visitor');
  100. $this->_visitorInfoTable = $resource->getTableName('log/visitor_info');
  101. $this->_urlTable = $resource->getTableName('log/url_table');
  102. $this->_urlInfoTable = $resource->getTableName('log/url_info_table');
  103. $this->_customerTable = $resource->getTableName('log/customer');
  104. $this->_summaryTable = $resource->getTableName('log/summary_table');
  105. $this->_summaryTypeTable = $resource->getTableName('log/summary_type_table');
  106. $this->_quoteTable = $resource->getTableName('log/quote_table');
  107. $this->setItemObjectClass(Mage::getConfig()->getModelClassName('log/visitor'));
  108. }
  109. /**
  110. * Enables online only select
  111. *
  112. * @param int $minutes
  113. * @return object
  114. */
  115. public function useOnlineFilter($minutes=null)
  116. {
  117. if (is_null($minutes)) {
  118. $minutes = Mage_Log_Model_Visitor::getOnlineMinutesInterval();
  119. }
  120. $this->_select->from(array('visitor_table'=>$this->_visitorTable))
  121. //->joinLeft(array('url_table'=>$this->_urlTable), 'visitor_table.last_url_id=url_table.url_id')
  122. ->joinLeft(array('info_table'=>$this->_visitorInfoTable), 'info_table.visitor_id=visitor_table.visitor_id')
  123. ->joinLeft(array('customer_table'=>$this->_customerTable),
  124. 'customer_table.visitor_id = visitor_table.visitor_id AND customer_table.logout_at IS NULL',
  125. array('log_id', 'customer_id', 'login_at', 'logout_at'))
  126. ->joinLeft(array('url_info_table'=>$this->_urlInfoTable),
  127. 'url_info_table.url_id = visitor_table.last_url_id')
  128. //->joinLeft(array('quote_table'=>$this->_quoteTable), 'quote_table.visitor_id=visitor_table.visitor_id')
  129. ->where( 'visitor_table.last_visit_at >= ( ? - INTERVAL '.$minutes.' MINUTE)', now() );
  130. $customersCollection = Mage::getModel('customer/customer')->getCollection();
  131. /* @var $customersCollection Mage_Customer_Model_Entity_Customer_Collection */
  132. $firstname = $customersCollection->getAttribute('firstname');
  133. $lastname = $customersCollection->getAttribute('lastname');
  134. $email = $customersCollection->getAttribute('email');
  135. $this->_select
  136. ->columns(array('type' => 'IF(customer_id, \''.Mage_Log_Model_Visitor::VISITOR_TYPE_CUSTOMER.'\', \''.Mage_Log_Model_Visitor::VISITOR_TYPE_VISITOR.'\')'))
  137. ->joinLeft(
  138. array('customer_lastname_table'=>$lastname->getBackend()->getTable()),
  139. 'customer_lastname_table.entity_id=customer_table.customer_id
  140. AND customer_lastname_table.attribute_id = '.(int) $lastname->getAttributeId() . '
  141. ',
  142. array('customer_lastname'=>'value')
  143. )
  144. ->joinLeft(
  145. array('customer_firstname_table'=>$firstname->getBackend()->getTable()),
  146. 'customer_firstname_table.entity_id=customer_table.customer_id
  147. AND customer_firstname_table.attribute_id = '.(int) $firstname->getAttributeId() . '
  148. ',
  149. array('customer_firstname'=>'value')
  150. )
  151. ->joinLeft(
  152. array('customer_email_table'=>$email->getBackend()->getTable()),
  153. 'customer_email_table.entity_id=customer_table.customer_id',
  154. array('customer_email'=>'email')
  155. );
  156. $this->_isOnlineFilterUsed = true;
  157. return $this;
  158. }
  159. public function showCustomersOnly()
  160. {
  161. $this->_select->where('customer_table.customer_id > 0')
  162. ->group('customer_table.customer_id');
  163. return $this;
  164. }
  165. public function getAggregatedData($period=720, $type_code=null, $customFrom=null, $customTo=null)
  166. {
  167. /**
  168. * @todo : need remove agregation logic
  169. */
  170. $timeZoneOffset = Mage::getModel('core/date')->getGmtOffset();//Mage::app()->getLocale()->date()->getGmtOffset();
  171. $this->_itemObjectClass = 'Varien_Object';
  172. $this->_setIdFieldName('summary_id');
  173. /*
  174. $this->_select->from(array('summary'=>$this->_summaryTable), array('summary_id','customer_count','visitor_count','add_date'=>"DATE_SUB(summary.add_date, INTERVAL $timeZoneOffset SECOND)"))
  175. ->join(array('type'=>$this->_summaryTypeTable), 'type.type_id=summary.type_id', array());
  176. if (is_null($customFrom) && is_null($customTo)) {
  177. $this->_select->where( "DATE_SUB(summary.add_date, INTERVAL $timeZoneOffset SECOND) >= ( DATE_SUB(?, INTERVAL $timeZoneOffset SECOND) - INTERVAL {$period} {$this->_getRangeByType($type_code)} )", now() );
  178. } else {
  179. if($customFrom) {
  180. $this->_select->where( "DATE_SUB(summary.add_date, INTERVAL $timeZoneOffset SECOND) >= ", $this->_read->convertDate($customFrom));
  181. }
  182. if($customTo) {
  183. $this->_select->where( "DATE_SUB(summary.add_date, INTERVAL $timeZoneOffset SECOND) <= ", $this->_read->convertDate($customTo));
  184. }
  185. }
  186. if( is_null($type_code) ) {
  187. $this->_select->where("summary.type_id IS NULL");
  188. } else {
  189. $this->_select->where("type.type_code = ? ", $type_code);
  190. }
  191. */
  192. $this->_select->from(array('summary'=>$this->_summaryTable),
  193. array('summary_id',
  194. 'customer_count'=>'SUM(customer_count)',
  195. 'visitor_count'=>'SUM(visitor_count)',
  196. 'add_date'=>"DATE_ADD(summary.add_date, INTERVAL $timeZoneOffset SECOND)"
  197. ));
  198. $this->_select->where("DATE_SUB(summary.add_date, INTERVAL $timeZoneOffset SECOND) >= ( DATE_SUB(?, INTERVAL $timeZoneOffset SECOND) - INTERVAL {$period} {$this->_getRangeByType($type_code)} )", now() );
  199. $this->_select->group('DATE_FORMAT(add_date, \''.$this->_getGroupByDateFormat($type_code).'\')');
  200. $this->_select->order('add_date ASC');
  201. return $this;
  202. }
  203. protected function _getGroupByDateFormat($type)
  204. {
  205. switch ($type) {
  206. case 'day':
  207. $format = '%Y-%m-%d';
  208. break;
  209. default:
  210. case 'hour':
  211. $format = '%Y-%m-%d %H';
  212. break;
  213. }
  214. return $format;
  215. }
  216. protected function _getRangeByType($type_code)
  217. {
  218. switch ($type_code)
  219. {
  220. case 'day':
  221. $range = 'DAY';
  222. break;
  223. case 'hour':
  224. $range = 'HOUR';
  225. break;
  226. case 'minute':
  227. default:
  228. $range = 'MINUTE';
  229. break;
  230. }
  231. return $range;
  232. }
  233. /**
  234. * Filter by customer ID, as 'type' field does not exist
  235. *
  236. * @param string $fieldName
  237. * @param array $condition
  238. * @return Mage_Log_Model_Mysql4_Visitor_Collection
  239. */
  240. public function addFieldToFilter($fieldName, $condition=null)
  241. {
  242. if ($fieldName == 'type' && is_array($condition) && isset($condition['eq'])) {
  243. $fieldName = 'customer_id';
  244. if ($condition['eq'] === Mage_Log_Model_Visitor::VISITOR_TYPE_VISITOR) {
  245. $condition = array('null' => 1);
  246. } else {
  247. $condition = array('moreq' => 1);
  248. }
  249. }
  250. return parent::addFieldToFilter($this->_getFieldMap($fieldName), $condition);
  251. }
  252. protected function _getFieldMap($fieldName)
  253. {
  254. if(isset($this->_fieldMap[$fieldName])) {
  255. return $this->_fieldMap[$fieldName];
  256. } else {
  257. return 'visitor_table.' . $fieldName;
  258. }
  259. }
  260. public function load($printQuery = false, $logQuery = false)
  261. {
  262. if ($this->isLoaded()) {
  263. return $this;
  264. }
  265. Mage::dispatchEvent('log_visitor_collection_load_before', array('collection' => $this));
  266. return parent::load($printQuery, $logQuery);
  267. }
  268. public function getIsOnlineFilterUsed()
  269. {
  270. return $this->_isOnlineFilterUsed;
  271. }
  272. /**
  273. * Filter visitors by specified store ids
  274. *
  275. * @param array|int $storeIds
  276. */
  277. public function addVisitorStoreFilter($storeIds)
  278. {
  279. $this->_select->where('visitor_table.store_id IN (?)', $storeIds);
  280. }
  281. }