1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Rule\Model\ResourceModel\Rule\Collection;
/**
* Abstract Rule entity resource collection model
*
* @api
* @since 100.0.2
*/
abstract class AbstractCollection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
/**
* Store associated with rule entities information map
*
* Example:
* array(
* 'entity_type1' => array(
* 'associations_table' => 'table_name',
* 'rule_id_field' => 'rule_id',
* 'entity_id_field' => 'entity_id'
* ),
* 'entity_type2' => array(
* 'associations_table' => 'table_name',
* 'rule_id_field' => 'rule_id',
* 'entity_id_field' => 'entity_id'
* )
* ....
* )
*
* @var array
*/
protected $_associatedEntitiesMap = [];
/**
* Add website ids to rules data
*
* @return $this
*/
protected function _afterLoad()
{
parent::_afterLoad();
if ($this->getFlag('add_websites_to_result') && $this->_items) {
/** @var \Magento\Rule\Model\AbstractModel $item */
foreach ($this->_items as $item) {
$item->afterLoad();
}
}
return $this;
}
/**
* Init flag for adding rule website ids to collection result
*
* @param bool|null $flag
* @return $this
*/
public function addWebsitesToResult($flag = null)
{
$flag = $flag === null ? true : $flag;
$this->setFlag('add_websites_to_result', $flag);
return $this;
}
/**
* Limit rules collection by specific websites
*
* @param int|int[]|\Magento\Store\Model\Website $websiteId
* @return $this
*/
public function addWebsiteFilter($websiteId)
{
if (!$this->getFlag('is_website_table_joined')) {
$websiteIds = is_array($websiteId) ? $websiteId : [$websiteId];
$entityInfo = $this->_getAssociatedEntityInfo('website');
$this->setFlag('is_website_table_joined', true);
foreach ($websiteIds as $index => $website) {
if ($website instanceof \Magento\Store\Model\Website) {
$websiteIds[$index] = $website->getId();
}
}
$this->getSelect()->join(
['website' => $this->getTable($entityInfo['associations_table'])],
$this->getConnection()->quoteInto('website.' . $entityInfo['entity_id_field'] . ' IN (?)', $websiteIds)
. ' AND main_table.' . $entityInfo['rule_id_field'] . ' = website.' . $entityInfo['rule_id_field'],
[]
);
}
return $this;
}
/**
* Provide support for website id filter
*
* @param string $field
* @param null|string|array $condition
* @return $this
*/
public function addFieldToFilter($field, $condition = null)
{
if ($field == 'website_ids') {
return $this->addWebsiteFilter($condition);
}
parent::addFieldToFilter($field, $condition);
return $this;
}
/**
* Filter collection to only active or inactive rules
*
* @param int $isActive
* @return $this
*/
public function addIsActiveFilter($isActive = 1)
{
if (!$this->getFlag('is_active_filter')) {
$this->addFieldToFilter('is_active', (int)$isActive ? 1 : 0);
$this->setFlag('is_active_filter', true);
}
return $this;
}
/**
* Retrieve correspondent entity information (associations table name, columns names)
* of rule's associated entity by specified entity type
*
* @param string $entityType
*
* @throws \Magento\Framework\Exception\LocalizedException
* @return array
*/
protected function _getAssociatedEntityInfo($entityType)
{
if (isset($this->_associatedEntitiesMap[$entityType])) {
return $this->_associatedEntitiesMap[$entityType];
}
throw new \Magento\Framework\Exception\LocalizedException(
__('There is no information about associated entity type "%1".', $entityType)
);
}
}