Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
productdataprovider.php
1<?php
2
4
5use Bitrix\Catalog\Filter\DataProvider\Currency\CurrencyListItems;
6use Bitrix\Catalog\Filter\DataProvider\Measure\MeasureListItems;
8use Bitrix\Catalog\Filter\PrefixableDataProviderTrait;
16
17Loader::requireModule('iblock');
18
23{
24 use MeasureListItems;
25 use CurrencyListItems;
26 use PrefixableDataProviderTrait;
27
28 private const VARIATION_PREFIX = 'VARIATION_';
29
30 private ?ElementFilterFields $variationFields;
31
32 public function __construct(ProductSettings $settings)
33 {
34 parent::__construct($settings);
35
36 $variationIblockId = $settings->getVariationIblockId();
37 if ($variationIblockId > 0)
38 {
39 $this->variationFields = new ElementFilterFields($variationIblockId, false, false);
40 }
41 }
42
43 private function isWithVariations(): bool
44 {
45 return isset($this->variationFields);
46 }
47
48 public function prepareFields()
49 {
50 $result = parent::prepareFields();
51
52 $result['TYPE'] = $this->createField('TYPE', [
53 'type' => 'list',
54 'default' => false,
55 'partial' => true,
56 ]);
57 $result['AVAILABLE'] = $this->createField('AVAILABLE', [
58 'type' => 'checkbox',
59 'default' => false,
60 ]);
61 $result['QUANTITY'] = $this->createField('QUANTITY', [
62 'type' => 'number',
63 'default' => false,
64 ]);
65 $result['QUANTITY_RESERVED'] = $this->createField('QUANTITY_RESERVED', [
66 'type' => 'number',
67 'default' => false,
68 ]);
69 $result['PURCHASING_PRICE'] = $this->createField('PURCHASING_PRICE', [
70 'type' => 'number',
71 'default' => false,
72 ]);
73 $result['PURCHASING_CURRENCY'] = $this->createField('PURCHASING_CURRENCY', [
74 'type' => 'list',
75 'default' => false,
76 'partial' => true,
77 ]);
78 $result['WEIGHT'] = $this->createField('WEIGHT', [
79 'type' => 'number',
80 'default' => false,
81 ]);
82 $result['WIDTH'] = $this->createField('WIDTH', [
83 'type' => 'number',
84 'default' => false,
85 ]);
86 $result['LENGTH'] = $this->createField('LENGTH', [
87 'type' => 'number',
88 'default' => false,
89 ]);
90 $result['HEIGHT'] = $this->createField('HEIGHT', [
91 'type' => 'number',
92 'default' => false,
93 ]);
94 $result['MEASURE'] = $this->createField('MEASURE', [
95 'type' => 'list',
96 'default' => false,
97 'partial' => true,
98 ]);
99
100 if ($this->isWithVariations())
101 {
102 $result += $this->prepareVariationElementFields();
103 }
104
105 return $result;
106 }
107
113 private function prepareVariationElementFields(): array
114 {
115 $result = [];
116
117 /*
118 $fields = $this->variationFields->getElementFieldsParams();
119 foreach ($fields as $id => $params)
120 {
121 $result[$id] = $this->createField($id, $params);
122 }
123 */
124
125 $properties = $this->variationFields->getElementPropertiesParams();
126 foreach ($properties as $id => $params)
127 {
128 $result[$id] = $this->createField($id, $params);
129 }
130
131 $nameWithPrefixTemplate = Loc::getMessage('CATALOG_FILTER_PRODUCT_DATAPROVIDER_NAME_WITH_PREFIX');
132
133 return $this->prepareFieldsByPrefix(self::VARIATION_PREFIX, $result, $nameWithPrefixTemplate);
134 }
135
139 public function prepareFieldData($fieldID)
140 {
141 if ($fieldID === 'PURCHASING_CURRENCY')
142 {
143 return [
144 'items' => $this->getCurrencyListItems(),
145 ];
146 }
147 elseif ($fieldID === 'MEASURE')
148 {
149 return [
150 'items' => $this->getMeasureListItems(),
151 ];
152 }
153 elseif ($fieldID === 'TYPE')
154 {
155 return [
156 'items' => ProductTable::getProductTypes(true),
157 ];
158 }
159
160 $result = parent::prepareFieldData($fieldID);
161 if (isset($result))
162 {
163 return $result;
164 }
165
166 if ($this->isWithVariations())
167 {
168 return $this->prepareVariationFieldData($fieldID);
169 }
170
171 return null;
172 }
173
174 private function prepareVariationFieldData(string $fieldIdWithPrefix): ?array
175 {
176 $fieldID = $this->removePrefix(self::VARIATION_PREFIX, $fieldIdWithPrefix);
177
178 return $this->variationFields->getPropertyDescription($fieldID);
179 }
180
184 protected function getFieldName($fieldID)
185 {
186 return Loc::getMessage('CATALOG_FILTER_PRODUCT_DATAPROVIDER_FIELD_' . $fieldID) ?? parent::getFieldName($fieldID);
187 }
188
192 public function prepareFilterValue(array $rawFilterValue): array
193 {
194 $rawFilterValue = parent::prepareFilterValue($rawFilterValue);
195
196 if (!$this->isWithVariations() || empty($rawFilterValue))
197 {
198 return $rawFilterValue;
199 }
200
201 [$variationFilterValue, $rawFilterValue] = $this->splitPrefixFilterValues(self::VARIATION_PREFIX, $rawFilterValue);
202
203 if (!empty($variationFilterValue))
204 {
205 $variationFilterValue = $this->variationFields->prepareFilterValue($variationFilterValue);
206
207 $settings = $this->getSettings();
208 $variationFilterValue['IBLOCK_ID'] = $settings->getVariationIblockId();
209 $rawFilterValue['SUBQUERY'] = [
210 'FIELD' => 'PROPERTY_' . $settings->getLinkPropertyId(),
211 'FILTER' => $variationFilterValue,
212 ];
213 unset($settings);
214 }
215
216 return $rawFilterValue;
217 }
218}
static getProductTypes($descr=false)
Definition product.php:824
createField($fieldID, array $params=null)
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29