Newer
Older
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
namespace NicolasBejean\CategoryWidget\Model\ResourceModel;
use \Magento\Framework\Data\Collection\Db\FetchStrategyInterface;
use \Magento\Framework\Data\Collection\EntityFactoryInterface;
use \Magento\Framework\DB\Adapter\AdapterInterface;
use \Magento\Framework\DB\Select;
use \Magento\Framework\EntityManager\MetadataPool;
use \Magento\Framework\Event\ManagerInterface;
use \Magento\Framework\Exception\NoSuchEntityException;
use \Magento\Framework\Model\ResourceModel\Db\AbstractDb;
use \Magento\Store\Model\Store;
use \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection as AbstractCollectionExtends;
use \Magento\Store\Model\StoreManagerInterface;
use \Psr\Log\LoggerInterface;
/**
* Class AbstractCollection
*
* @category PHP
* @package NicolasBejean\CategoryWidget\Model\ResourceModel
* @author Nicolas Béjean <nicolas@bejean.eu>
* @license https://github.com/nicolasbejean/category-widget/blob/master/licence.txt BSD Licence
* @link https://www.bejean.eu
*/
abstract class AbstractCollection extends AbstractCollectionExtends
{
/**
* Store manager
*
* @var StoreManagerInterface
*/
protected $storeManager;
/**
* @var MetadataPool
*/
protected $metadataPool;
/**
* @param EntityFactoryInterface $entityFactory
* @param LoggerInterface $logger
* @param FetchStrategyInterface $fetchStrategy
* @param ManagerInterface $eventManager
* @param StoreManagerInterface $storeManager
* @param MetadataPool $metadataPool
* @param AdapterInterface|null $connection
* @param AbstractDb|null $resource
*/
public function __construct(
EntityFactoryInterface $entityFactory,
LoggerInterface $logger,
FetchStrategyInterface $fetchStrategy,
ManagerInterface $eventManager,
StoreManagerInterface $storeManager,
MetadataPool $metadataPool,
AdapterInterface $connection = null,
AbstractDb $resource = null
) {
$this->storeManager = $storeManager;
$this->metadataPool = $metadataPool;
parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
}
/**
* Perform operations after collection load
*
* @param string $tableName
* @param string|null $linkField
* @return void
* @throws NoSuchEntityException
*/
protected function performAfterLoad($tableName, $linkField)
{
$linkedIds = $this->getColumnValues($linkField);
if (count($linkedIds)) {
$connection = $this->getConnection();
$select = $connection->select()->from(['cms_entity_store' => $this->getTable($tableName)])
->where('cms_entity_store.' . $linkField . ' IN (?)', $linkedIds);
$result = $connection->fetchAll($select);
if ($result) {
$storesData = [];
foreach ($result as $storeData) {
$storesData[$storeData[$linkField]][] = $storeData['store_id'];
}
foreach ($this as $item) {
$linkedId = $item->getData($linkField);
if (!isset($storesData[$linkedId])) {
continue;
}
$storeIdKey = array_search(Store::DEFAULT_STORE_ID, $storesData[$linkedId], true);
if ($storeIdKey !== false) {
$stores = $this->storeManager->getStores(false, true);
$storeId = current($stores)->getId();
$storeCode = key($stores);
} else {
$storeId = current($storesData[$linkedId]);
$storeCode = $this->storeManager->getStore($storeId)->getCode();
}
$item->setData('_first_store_id', $storeId);
$item->setData('store_code', $storeCode);
$item->setData('store_id', $storesData[$linkedId]);
}
}
}
}
/**
* Add field filter to collection
*
* @param array|string $field
* @param string|int|array|null $condition
* @return AbstractCollectionExtends
*/
public function addFieldToFilter($field, $condition = null)
{
if ($field === 'store_id') {
return $this->addStoreFilter($condition, false);
}
return parent::addFieldToFilter($field, $condition);
}
/**
* Add filter by store
*
* @param int|array|Store $store
* @param bool $withAdmin
* @return $this
*/
abstract public function addStoreFilter($store, $withAdmin = true);
/**
* Perform adding filter by store
*
* @param int|array|Store $store
* @param bool $withAdmin
* @return void
*/
protected function performAddStoreFilter($store, $withAdmin = true)
{
if ($store instanceof Store) {
$store = [$store->getId()];
}
if (!is_array($store)) {
$store = [$store];
}
if ($withAdmin) {
$store[] = Store::DEFAULT_STORE_ID;
}
$this->addFilter('store', ['in' => $store], 'public');
}
/**
* Join store relation table if there is store filter
*
* @param string $tableName
* @param string|null $linkField
* @return void
*/
protected function joinStoreRelationTable($tableName, $linkField)
{
if ($this->getFilter('store')) {
$this->getSelect()->join(
['store_table' => $this->getTable($tableName)],
'main_table.' . $linkField . ' = store_table.' . $linkField,
[]
)->group(
'main_table.' . $linkField
);
}
parent::_renderFiltersBefore();
}
/**
* Get SQL for get record count
*
* Extra GROUP BY strip added.
*
* @return Select
*/
public function getSelectCountSql()
{
$countSelect = parent::getSelectCountSql();
$countSelect->reset(Select::GROUP);
return $countSelect;
}
/**
* Returns pairs identifier - title for unique identifiers
* and pairs identifier|entity_id - title for non-unique after first
*
* @return array
*/
public function toOptionIdArray()
{
$res = [];
$existingIdentifiers = [];
foreach ($this as $item) {
$identifier = $item->getData('identifier');
$data['value'] = $identifier;
$data['label'] = $item->getData('title');
if (in_array($identifier, $existingIdentifiers)) {
$data['value'] .= '|' . $item->getData($this->getIdFieldName());
} else {
$existingIdentifiers[] = $identifier;
}
$res[] = $data;
}
return $res;
}
}