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
149
150
151
152
153
154
155
156
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\CatalogRule\Model\ResourceModel\Rule;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Framework\App\ObjectManager;
class Collection extends \Magento\Rule\Model\ResourceModel\Rule\Collection\AbstractCollection
{
/**
* Store associated with rule entities information map
*
* @var array
*/
protected $_associatedEntitiesMap;
/**
* @var Json
*/
protected $serializer;
/**
* Collection constructor.
* @param \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory
* @param \Psr\Log\LoggerInterface $logger
* @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
* @param \Magento\Framework\Event\ManagerInterface $eventManager
* @param \Magento\Framework\DB\Adapter\AdapterInterface $connection
* @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
* @param Json|null $serializer
*/
public function __construct(
\Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
\Psr\Log\LoggerInterface $logger,
\Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
\Magento\Framework\Event\ManagerInterface $eventManager,
\Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
\Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null,
Json $serializer = null
) {
parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
$this->_associatedEntitiesMap = $this->getAssociatedEntitiesMap();
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
}
/**
* Set resource model
*
* @return void
* @codeCoverageIgnore
*/
protected function _construct()
{
$this->_init(\Magento\CatalogRule\Model\Rule::class, \Magento\CatalogRule\Model\ResourceModel\Rule::class);
}
/**
* Find product attribute in conditions or actions
*
* @param string $attributeCode
* @return $this
* @api
*/
public function addAttributeInConditionFilter($attributeCode)
{
$match = sprintf('%%%s%%', substr($this->serializer->serialize(['attribute' => $attributeCode]), 1, -1));
$this->addFieldToFilter('conditions_serialized', ['like' => $match]);
return $this;
}
/**
* @param string $entityType
* @param string $objectField
* @throws \Magento\Framework\Exception\LocalizedException
* @return void
*/
protected function mapAssociatedEntities($entityType, $objectField)
{
if (!$this->_items) {
return;
}
$entityInfo = $this->_getAssociatedEntityInfo($entityType);
$ruleIdField = $entityInfo['rule_id_field'];
$entityIds = $this->getColumnValues($ruleIdField);
$select = $this->getConnection()->select()->from(
$this->getTable($entityInfo['associations_table'])
)->where(
$ruleIdField . ' IN (?)',
$entityIds
);
$associatedEntities = $this->getConnection()->fetchAll($select);
array_map(function ($associatedEntity) use ($entityInfo, $ruleIdField, $objectField) {
$item = $this->getItemByColumnValue($ruleIdField, $associatedEntity[$ruleIdField]);
$itemAssociatedValue = $item->getData($objectField) === null ? [] : $item->getData($objectField);
$itemAssociatedValue[] = $associatedEntity[$entityInfo['entity_id_field']];
$item->setData($objectField, $itemAssociatedValue);
}, $associatedEntities);
}
/**
* @return $this
*/
protected function _afterLoad()
{
$this->mapAssociatedEntities('website', 'website_ids');
$this->mapAssociatedEntities('customer_group', 'customer_group_ids');
$this->setFlag('add_websites_to_result', false);
return parent::_afterLoad();
}
/**
* Limit rules collection by specific customer group
*
* @param int $customerGroupId
* @return $this
*/
public function addCustomerGroupFilter($customerGroupId)
{
$entityInfo = $this->_getAssociatedEntityInfo('customer_group');
if (!$this->getFlag('is_customer_group_joined')) {
$this->setFlag('is_customer_group_joined', true);
$this->getSelect()->join(
['customer_group' => $this->getTable($entityInfo['associations_table'])],
$this->getConnection()
->quoteInto('customer_group.' . $entityInfo['entity_id_field'] . ' = ?', $customerGroupId)
. ' AND main_table.' . $entityInfo['rule_id_field'] . ' = customer_group.'
. $entityInfo['rule_id_field'],
[]
);
}
return $this;
}
/**
* @return array
* @deprecated 100.1.0
*/
private function getAssociatedEntitiesMap()
{
if (!$this->_associatedEntitiesMap) {
$this->_associatedEntitiesMap = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\CatalogRule\Model\ResourceModel\Rule\AssociatedEntityMap::class)
->getData();
}
return $this->_associatedEntitiesMap;
}
}