Event.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Reports\Model\ResourceModel;
  7. /**
  8. * Report events resource model
  9. * @api
  10. * @since 100.0.2
  11. */
  12. class Event extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  13. {
  14. /**
  15. * Core store config
  16. *
  17. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  18. */
  19. protected $_scopeConfig;
  20. /**
  21. * @var \Magento\Store\Model\StoreManagerInterface
  22. */
  23. protected $_storeManager;
  24. /**
  25. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
  26. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  27. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  28. * @param string $connectionName
  29. */
  30. public function __construct(
  31. \Magento\Framework\Model\ResourceModel\Db\Context $context,
  32. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  33. \Magento\Store\Model\StoreManagerInterface $storeManager,
  34. $connectionName = null
  35. ) {
  36. parent::__construct($context, $connectionName);
  37. $this->_scopeConfig = $scopeConfig;
  38. $this->_storeManager = $storeManager;
  39. }
  40. /**
  41. * Initialize main table and identifier field. Set main entity table name and primary key field name.
  42. *
  43. * @return void
  44. */
  45. protected function _construct()
  46. {
  47. $this->_init('report_event', 'event_id');
  48. }
  49. /**
  50. * Update customer type after customer login
  51. *
  52. * @param \Magento\Reports\Model\Event $model
  53. * @param int $visitorId
  54. * @param int $customerId
  55. * @param array $types
  56. * @return $this
  57. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  58. */
  59. public function updateCustomerType(\Magento\Reports\Model\Event $model, $visitorId, $customerId, $types = [])
  60. {
  61. if ($types) {
  62. $this->getConnection()->update(
  63. $this->getMainTable(),
  64. ['subject_id' => (int) $customerId, 'subtype' => 0],
  65. ['subject_id = ?' => (int) $visitorId, 'subtype = ?' => 1, 'event_type_id IN(?)' => $types]
  66. );
  67. }
  68. return $this;
  69. }
  70. /**
  71. * Add events log to a collection
  72. * The collection id field is used without corellation, so it must be unique.
  73. * DESC ordering by event will be added to the collection
  74. *
  75. * @param \Magento\Framework\Data\Collection\AbstractDb $collection
  76. * @param int $eventTypeId
  77. * @param int $eventSubjectId
  78. * @param int $subtype
  79. * @param array $skipIds
  80. * @return $this
  81. */
  82. public function applyLogToCollection(
  83. \Magento\Framework\Data\Collection\AbstractDb $collection,
  84. $eventTypeId,
  85. $eventSubjectId,
  86. $subtype,
  87. $skipIds = []
  88. ) {
  89. $idFieldName = $collection->getResource()->getIdFieldName();
  90. $derivedSelect = $this->getConnection()
  91. ->select()
  92. ->from(
  93. $this->getTable('report_event'),
  94. ['event_id' => new \Zend_Db_Expr('MAX(event_id)'), 'object_id']
  95. )
  96. ->where('event_type_id = ?', (int) $eventTypeId)
  97. ->where('subject_id = ?', (int) $eventSubjectId)
  98. ->where('subtype = ?', (int) $subtype)
  99. ->where('store_id IN(?)', $this->getCurrentStoreIds())
  100. ->group('object_id');
  101. if ($skipIds) {
  102. if (!is_array($skipIds)) {
  103. $skipIds = [(int) $skipIds];
  104. }
  105. $derivedSelect->where('object_id NOT IN(?)', $skipIds);
  106. }
  107. $collection->getSelect()->joinInner(
  108. ['evt' => new \Zend_Db_Expr("({$derivedSelect})")],
  109. "{$idFieldName} = evt.object_id",
  110. []
  111. )->order('evt.event_id ' . \Magento\Framework\DB\Select::SQL_DESC);
  112. return $this;
  113. }
  114. /**
  115. * Obtain all current store ids, depending on configuration
  116. *
  117. * @param null|array $predefinedStoreIds
  118. * @return array
  119. */
  120. public function getCurrentStoreIds(array $predefinedStoreIds = null)
  121. {
  122. $stores = [];
  123. // get all or specified stores
  124. if ($this->_storeManager->getStore()->getId() == 0) {
  125. if (null !== $predefinedStoreIds) {
  126. $stores = $predefinedStoreIds;
  127. } else {
  128. foreach ($this->_storeManager->getStores() as $store) {
  129. $stores[] = $store->getId();
  130. }
  131. }
  132. } else {
  133. // get all stores, required by configuration in current store scope
  134. $productsScope = $this->_scopeConfig->getValue(
  135. 'catalog/recently_products/scope',
  136. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  137. );
  138. switch ($productsScope) {
  139. case 'website':
  140. $resourceStore = $this->_storeManager->getStore()->getWebsite()->getStores();
  141. break;
  142. case 'group':
  143. $resourceStore = $this->_storeManager->getStore()->getGroup()->getStores();
  144. break;
  145. default:
  146. $resourceStore = [$this->_storeManager->getStore()];
  147. break;
  148. }
  149. foreach ($resourceStore as $store) {
  150. $stores[] = $store->getId();
  151. }
  152. }
  153. foreach ($stores as $key => $store) {
  154. $stores[$key] = (int) $store;
  155. }
  156. return $stores;
  157. }
  158. /**
  159. * Clean report event table
  160. *
  161. * @param \Magento\Reports\Model\Event $object
  162. * @return $this
  163. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  164. */
  165. public function clean(\Magento\Reports\Model\Event $object)
  166. {
  167. while (true) {
  168. $select = $this->getConnection()->select()->from(
  169. ['event_table' => $this->getMainTable()],
  170. ['event_id']
  171. )->joinLeft(
  172. ['visitor_table' => $this->getTable('customer_visitor')],
  173. 'event_table.subject_id = visitor_table.visitor_id',
  174. []
  175. )->where('visitor_table.visitor_id IS NULL')
  176. ->where('event_table.subtype = ?', 1)
  177. ->limit(1000);
  178. $eventIds = $this->getConnection()->fetchCol($select);
  179. if (!$eventIds) {
  180. break;
  181. }
  182. $this->getConnection()->delete($this->getMainTable(), ['event_id IN(?)' => $eventIds]);
  183. }
  184. return $this;
  185. }
  186. }