/
Collection.php
209 lines (193 loc) · 5.37 KB
/
Collection.php
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
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Bundle\Model\ResourceModel\Option;
/**
* Bundle Options Resource Collection
*/
class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
/**
* All item ids cache
*
* @var array
*/
protected $_itemIds;
/**
* True when selections appended
*
* @var bool
*/
protected $_selectionsAppended = false;
/**
* Init model and resource model
*
* @return void
*/
protected function _construct()
{
$this->_init('Magento\Bundle\Model\Option', 'Magento\Bundle\Model\ResourceModel\Option');
}
/**
* Joins values to options
*
* @param int $storeId
* @return $this
*/
public function joinValues($storeId)
{
$this->getSelect()->joinLeft(
['option_value_default' => $this->getTable('catalog_product_bundle_option_value')],
'main_table.option_id = option_value_default.option_id and option_value_default.store_id = 0',
[]
)->columns(
['default_title' => 'option_value_default.title']
);
$title = $this->getConnection()->getCheckSql(
'option_value.title IS NOT NULL',
'option_value.title',
'option_value_default.title'
);
if ($storeId !== null) {
$this->getSelect()->columns(
['title' => $title]
)->joinLeft(
['option_value' => $this->getTable('catalog_product_bundle_option_value')],
$this->getConnection()->quoteInto(
'main_table.option_id = option_value.option_id and option_value.store_id = ?',
$storeId
),
[]
);
}
return $this;
}
/**
* Sets product id filter
*
* @param int $productId
* @return $this
*/
public function setProductIdFilter($productId)
{
$productTable = $this->getTable('catalog_product_entity');
$linkField = $this->getConnection()->getAutoIncrementField($productTable);
$this->getSelect()->join(
['cpe' => $productTable],
'cpe.'.$linkField.' = main_table.parent_id',
[]
)->where(
"cpe.entity_id = ?",
$productId
);
return $this;
}
/**
* Set product link filter
*
* @param int $productLinkFieldValue
*
* @return $this
*/
public function setProductLinkFilter($productLinkFieldValue)
{
$this->getSelect()->where(
'main_table.parent_id = ?',
$productLinkFieldValue
);
return $this;
}
/**
* Sets order by position
*
* @return $this
*/
public function setPositionOrder()
{
$this->getSelect()->order('main_table.position asc')->order('main_table.option_id asc');
return $this;
}
/**
* Append selection to options
* stripBefore - indicates to reload
* appendAll - indicates do we need to filter by saleable and required custom options
*
* @param \Magento\Bundle\Model\ResourceModel\Selection\Collection $selectionsCollection
* @param bool $stripBefore
* @param bool $appendAll
* @return \Magento\Framework\DataObject[]
*/
public function appendSelections($selectionsCollection, $stripBefore = false, $appendAll = true)
{
if ($stripBefore) {
$this->_stripSelections();
}
if (!$this->_selectionsAppended) {
foreach ($selectionsCollection->getItems() as $key => $selection) {
$option = $this->getItemById($selection->getOptionId());
if ($option) {
if ($appendAll || $selection->isSalable() && !$selection->getRequiredOptions()) {
$selection->setOption($option);
$option->addSelection($selection);
} else {
$selectionsCollection->removeItemByKey($key);
}
}
}
$this->_selectionsAppended = true;
}
return $this->getItems();
}
/**
* Removes appended selections before
*
* @return $this
*/
protected function _stripSelections()
{
foreach ($this->getItems() as $option) {
$option->setSelections([]);
}
$this->_selectionsAppended = false;
return $this;
}
/**
* Sets filter by option id
*
* @param array|int $ids
* @return $this
*/
public function setIdFilter($ids)
{
if (is_array($ids)) {
$this->addFieldToFilter('main_table.option_id', ['in' => $ids]);
} elseif ($ids != '') {
$this->addFieldToFilter('main_table.option_id', $ids);
}
return $this;
}
/**
* Reset all item ids cache
*
* @return $this
*/
public function resetAllIds()
{
$this->_itemIds = null;
return $this;
}
/**
* Retrieve all ids for collection
*
* @return array
*/
public function getAllIds()
{
if ($this->_itemIds === null) {
$this->_itemIds = parent::getAllIds();
}
return $this->_itemIds;
}
}