Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Event.php
Go to the documentation of this file.
1 <?php
7 
14 {
20  protected $_scopeConfig;
21 
25  protected $_storeManager;
26 
33  public function __construct(
34  \Magento\Framework\Model\ResourceModel\Db\Context $context,
35  \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
36  \Magento\Store\Model\StoreManagerInterface $storeManager,
37  $connectionName = null
38  ) {
39  parent::__construct($context, $connectionName);
40  $this->_scopeConfig = $scopeConfig;
41  $this->_storeManager = $storeManager;
42  }
43 
49  protected function _construct()
50  {
51  $this->_init('report_event', 'event_id');
52  }
53 
64  public function updateCustomerType(\Magento\Reports\Model\Event $model, $visitorId, $customerId, $types = [])
65  {
66  if ($types) {
67  $this->getConnection()->update(
68  $this->getMainTable(),
69  ['subject_id' => (int) $customerId, 'subtype' => 0],
70  ['subject_id = ?' => (int) $visitorId, 'subtype = ?' => 1, 'event_type_id IN(?)' => $types]
71  );
72  }
73  return $this;
74  }
75 
88  public function applyLogToCollection(
89  \Magento\Framework\Data\Collection\AbstractDb $collection,
90  $eventTypeId,
91  $eventSubjectId,
92  $subtype,
93  $skipIds = []
94  ) {
95  $idFieldName = $collection->getResource()->getIdFieldName();
96 
97  $derivedSelect = $this->getConnection()
98  ->select()
99  ->from(
100  $this->getTable('report_event'),
101  ['event_id' => new \Zend_Db_Expr('MAX(event_id)'), 'object_id']
102  )
103  ->where('event_type_id = ?', (int) $eventTypeId)
104  ->where('subject_id = ?', (int) $eventSubjectId)
105  ->where('subtype = ?', (int) $subtype)
106  ->where('store_id IN(?)', $this->getCurrentStoreIds())
107  ->group('object_id');
108 
109  if ($skipIds) {
110  if (!is_array($skipIds)) {
111  $skipIds = [(int) $skipIds];
112  }
113  $derivedSelect->where('object_id NOT IN(?)', $skipIds);
114  }
115 
116  $collection->getSelect()->joinInner(
117  ['evt' => new \Zend_Db_Expr("({$derivedSelect})")],
118  "{$idFieldName} = evt.object_id",
119  []
120  )->order('evt.event_id ' . \Magento\Framework\DB\Select::SQL_DESC);
121 
122  return $this;
123  }
124 
131  public function getCurrentStoreIds(array $predefinedStoreIds = null)
132  {
133  $stores = [];
134  // get all or specified stores
135  if ($this->_storeManager->getStore()->getId() == 0) {
136  if (null !== $predefinedStoreIds) {
137  $stores = $predefinedStoreIds;
138  } else {
139  foreach ($this->_storeManager->getStores() as $store) {
140  $stores[] = $store->getId();
141  }
142  }
143  } else {
144  // get all stores, required by configuration in current store scope
145  $productsScope = $this->_scopeConfig->getValue(
146  'catalog/recently_products/scope',
147  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
148  );
149  switch ($productsScope) {
150  case 'website':
151  $resourceStore = $this->_storeManager->getStore()->getWebsite()->getStores();
152  break;
153  case 'group':
154  $resourceStore = $this->_storeManager->getStore()->getGroup()->getStores();
155  break;
156  default:
157  $resourceStore = [$this->_storeManager->getStore()];
158  break;
159  }
160 
161  foreach ($resourceStore as $store) {
162  $stores[] = $store->getId();
163  }
164  }
165  foreach ($stores as $key => $store) {
166  $stores[$key] = (int) $store;
167  }
168 
169  return $stores;
170  }
171 
179  public function clean(\Magento\Reports\Model\Event $object)
180  {
181  while (true) {
182  $select = $this->getConnection()->select()->from(
183  ['event_table' => $this->getMainTable()],
184  ['event_id']
185  )->joinLeft(
186  ['visitor_table' => $this->getTable('customer_visitor')],
187  'event_table.subject_id = visitor_table.visitor_id',
188  []
189  )->where('visitor_table.visitor_id IS NULL')
190  ->where('event_table.subtype = ?', 1)
191  ->limit(1000);
192  $eventIds = $this->getConnection()->fetchCol($select);
193 
194  if (!$eventIds) {
195  break;
196  }
197 
198  $this->getConnection()->delete($this->getMainTable(), ['event_id IN(?)' => $eventIds]);
199  }
200  return $this;
201  }
202 }
$storeManager
updateCustomerType(\Magento\Reports\Model\Event $model, $visitorId, $customerId, $types=[])
Definition: Event.php:64
clean(\Magento\Reports\Model\Event $object)
Definition: Event.php:179
getCurrentStoreIds(array $predefinedStoreIds=null)
Definition: Event.php:131
applyLogToCollection(\Magento\Framework\Data\Collection\AbstractDb $collection, $eventTypeId, $eventSubjectId, $subtype, $skipIds=[])
Definition: Event.php:88
__construct(\Magento\Framework\Model\ResourceModel\Db\Context $context, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Store\Model\StoreManagerInterface $storeManager, $connectionName=null)
Definition: Event.php:33