PageRenderTime 57ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/magento/app/code/core/Mage/Reports/Model/Resource/Event.php

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