PageRenderTime 64ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Reports/Model/Mysql4/Event.php

https://bitbucket.org/MXWest/magento-ce-1.5.1.0
PHP | 179 lines | 100 code | 10 blank | 69 comment | 11 complexity | 3e46c9f08795a3e4e43b9bbb93adb701 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_Reports
  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. * Report events resource model
  28. *
  29. * @category Mage
  30. * @package Mage_Reports
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Reports_Model_Mysql4_Event extends Mage_Core_Model_Mysql4_Abstract
  34. {
  35. /**
  36. * Initialize connection
  37. *
  38. */
  39. protected function _construct()
  40. {
  41. $this->_init('reports/event', 'event_id');
  42. }
  43. /**
  44. * Update customer type after customer login
  45. *
  46. * @param Mage_Reports_Model_Event $model
  47. * @param int $visitorId
  48. * @param int $customerId
  49. * @param array $types
  50. * @return Mage_Reports_Model_Mysql4_Event
  51. */
  52. public function updateCustomerType(Mage_Reports_Model_Event $model, $visitorId, $customerId, $types = array())
  53. {
  54. if ($types) {
  55. $this->_getWriteAdapter()->update($this->getMainTable(),
  56. array('subject_id' => $customerId, 'subtype' => 0),
  57. array(
  58. $this->_getWriteAdapter()->quoteInto('subject_id=?', $visitorId),
  59. $this->_getWriteAdapter()->quoteInto('subtype=?', 1),
  60. $this->_getWriteAdapter()->quoteInto('event_type_id IN(?)', $types)
  61. )
  62. );
  63. }
  64. return $this;
  65. }
  66. /**
  67. * Add events log to a collection
  68. * The collection id field is used without corellation, so it must be unique.
  69. * DESC ordering by event will be added to the collection
  70. *
  71. * @param Varien_Data_Collection_Db $collection
  72. * @param int $eventTypeId
  73. * @param int $eventSubjectId
  74. * @param int $subtype
  75. * @param array $skipIds
  76. */
  77. public function applyLogToCollection(Varien_Data_Collection_Db $collection, $eventTypeId, $eventSubjectId, $subtype, $skipIds = array())
  78. {
  79. $idFieldName = $collection->getResource()->getIdFieldName();
  80. $derivedSelect = $this->getReadConnection()->select()
  81. ->from($this->getTable('reports/event'), array('event_id' => new Zend_Db_Expr('MAX(event_id)'), 'object_id'))
  82. ->where('event_type_id=?', (int)$eventTypeId)
  83. ->where('subject_id=?', (int)$eventSubjectId)
  84. ->where('subtype=?', (int)$subtype)
  85. ->where('store_id IN(?)', $this->getCurrentStoreIds())
  86. ->group('object_id')
  87. ;
  88. if ($skipIds) {
  89. if (!is_array($skipIds)) {
  90. $skipIds = array((int)$skipIds);
  91. }
  92. $derivedSelect->where('object_id NOT IN(?)', $skipIds);
  93. }
  94. $collection->getSelect()
  95. ->joinInner(array('evt' => new Zend_Db_Expr("({$derivedSelect})")), "`{$idFieldName}`=evt.object_id", array())
  96. ->order('evt.event_id DESC');
  97. }
  98. /**
  99. * Obtain all current store ids, depending on configuration
  100. *
  101. * @param array $predefinedStoreIds
  102. * @return array
  103. */
  104. public function getCurrentStoreIds(array $predefinedStoreIds = null)
  105. {
  106. $stores = array();
  107. // get all or specified stores
  108. if (Mage::app()->getStore()->getId() == 0) {
  109. if (null !== $predefinedStoreIds) {
  110. $stores = $predefinedStoreIds;
  111. }
  112. else {
  113. foreach (Mage::app()->getStores() as $store) {
  114. $stores[] = $store->getId();
  115. }
  116. }
  117. }
  118. // get all stores, required by configuration in current store scope
  119. else {
  120. switch (Mage::getStoreConfig('catalog/recently_products/scope')) {
  121. case 'website':
  122. $resourceStore = Mage::app()->getStore()->getWebsite()->getStores();
  123. break;
  124. case 'group':
  125. $resourceStore = Mage::app()->getStore()->getGroup()->getStores();
  126. break;
  127. default:
  128. $resourceStore = array(Mage::app()->getStore());
  129. break;
  130. }
  131. foreach ($resourceStore as $store) {
  132. $stores[] = $store->getId();
  133. }
  134. }
  135. foreach ($stores as $key => $store) {
  136. $stores[$key] = (int)$store;
  137. }
  138. return $stores;
  139. }
  140. /**
  141. * Clean report event table
  142. *
  143. * @param Mage_Reports_Model_Event $object
  144. * @return Mage_Reports_Model_Mysql4_Event
  145. */
  146. public function clean(Mage_Reports_Model_Event $object)
  147. {
  148. while (true) {
  149. $select = $this->_getReadAdapter()->select()
  150. ->from(array('event_table' => $this->getMainTable()), array('event_id'))
  151. ->joinLeft(
  152. array('visitor_table' => $this->getTable('log/visitor')),
  153. 'event_table.subject_id = visitor_table.visitor_id',
  154. array())
  155. ->where('visitor_table.visitor_id IS NULL')
  156. ->where('event_table.subtype=?', 1)
  157. ->limit(1000);
  158. $eventIds = $this->_getReadAdapter()->fetchCol($select);
  159. if (!$eventIds) {
  160. break;
  161. }
  162. $this->_getWriteAdapter()->delete(
  163. $this->getMainTable(),
  164. $this->_getWriteAdapter()->quoteInto('event_id IN(?)', $eventIds)
  165. );
  166. }
  167. return $this;
  168. }
  169. }