Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
setparametersgroupchild.php
1<?php
2
4
14use Bitrix\Iblock\Grid\Panel\UI\Actions\Helpers\ItemFinder;
27use CCatalogMeasure;
28use CCatalogSku;
29
30Loader::requireModule('iblock');
31
33{
34 use ItemFinder;
35
36 private Entity $productEntity;
37 private bool $isCatalogTypeProduct;
38
39 public static function getId(): string
40 {
41 return 'product_field';
42 }
43
44 public function getName(): string
45 {
46 return Loc::getMessage('CATALOG_GRID_PANEL_UI_PRODUCT_ACTION_SET_PARAMETERS_NAME');
47 }
48
49 private function isUsedInventoryManagement(): bool
50 {
51 return State::isUsedInventoryManagement();
52 }
53
54 private function canChangePurchasingPrice(): bool
55 {
56 return AccessController::getCurrent()->check(ActionDictionary::ACTION_PRODUCT_PURCHASE_INFO_VIEW);
57 }
58
59 private function isCatalogTypeProduct(): bool
60 {
61 if (!isset($this->isCatalogTypeProduct))
62 {
63 $this->isCatalogTypeProduct = false;
64
65 $info = CCatalogSku::GetInfoByIBlock($this->getIblockId());
66 if (isset($info['CATALOG_TYPE']))
67 {
68 $this->isCatalogTypeProduct = $info['CATALOG_TYPE'] === CCatalogSku::TYPE_PRODUCT;
69 }
70 }
71
72 return $this->isCatalogTypeProduct;
73 }
74
75 private function getRequestFields(HttpRequest $request): ?array
76 {
77 $controls = $request->getPost('controls');
78 if (!is_array($controls))
79 {
80 return null;
81 }
82
83 $fieldName = $controls[self::getId()] ?? null;
84 $fieldValue = $controls[$fieldName] ?? null;
85 if (empty($fieldName) || !is_string($fieldName) || !isset($fieldValue))
86 {
87 return null;
88 }
89 elseif (!$this->isAvailableField($fieldName))
90 {
91 return null;
92 }
93
94 $fields = [
95 $fieldName => $fieldValue,
96 ];
97
98 if ($fieldName !== 'PURCHASING_PRICE')
99 {
100 return $fields;
101 }
102
103 $fieldValue = $controls['PURCHASING_CURRENCY'] ?? null;
104 if (empty($fieldValue) || !is_string($fieldValue))
105 {
106 return null;
107 }
108 $fields['PURCHASING_CURRENCY'] = $fieldValue;
109
110 return $fields;
111 }
112
113 public function processRequest(HttpRequest $request, bool $isSelectedAllRows, ?Filter $filter = null): ?Result
114 {
115 $result = new Result();
116
117 $fields = $this->getRequestFields($request);
118 if ($fields === null)
119 {
120 return null;
121 }
122
123 [$elementIds, $sectionIds] = $this->prepareItemIds($request, $isSelectedAllRows, $filter);
124
125 if ($elementIds)
126 {
127 $result->addErrors(
128 ProductAction::updateElementList($this->getIblockId(), $elementIds, $fields)->getErrors()
129 );
130 }
131
132 if ($sectionIds)
133 {
134 $result->addErrors(
135 ProductAction::updateSectionList($this->getIblockId(), $sectionIds, $fields)->getErrors()
136 );
137 }
138
139 return $result;
140 }
141
142 protected function getOnchange(): Onchange
143 {
144 $confirmMessage = Loc::getMessage('CATALOG_GRID_PANEL_UI_PRODUCT_ACTION_SET_PARAMETERS_CONFIRM');
145
146 return new Onchange([
147 [
148 'ACTION' => Actions::RESET_CONTROLS,
149 ],
150 [
151 'ACTION' => Actions::CREATE,
152 'DATA' => [
153 $this->getParametersDropdownControl(),
154 (new Snippet)->getSendSelectedButton($confirmMessage),
155 ],
156 ],
157 ]);
158 }
159
160 private function getProductEntity(): Entity
161 {
162 $this->productEntity ??= ProductTable::getEntity();
163
164 return $this->productEntity;
165 }
166
167 private function getParametersDropdownControl(): array
168 {
169 $items = [];
170
171 if (!$this->isCatalogTypeProduct())
172 {
173 $items[] = $this->getInputDropdownItem('WEIGHT');
174 $items[] = $this->getSelectDropdownItem('MEASURE', $this->getMeasureDropdownItems());
175 $items[] = $this->getSelectDropdownItem('SUBSCRIBE', $this->getStatusDropdownItems(
176 Option::get('catalog', 'default_subscribe') !== 'N'
177 ));
178
179 $vatItems = $this->getVatIdDropdownItems();
180 if (!empty($vatItems))
181 {
182 $items[] = $this->getSelectDropdownItem('VAT_ID', $vatItems);
183 }
184 $items[] = $this->getSelectDropdownItem('VAT_INCLUDED', $this->getStatusDropdownItems());
185
186 if (!$this->isUsedInventoryManagement())
187 {
188 $items[] = $this->getInputDropdownItem('QUANTITY');
189 $items[] = $this->getSelectDropdownItem('QUANTITY_TRACE', $this->getStatusDropdownItems(
190 Option::get('catalog', 'default_quantity_trace') === 'Y'
191 ));
192 $items[] = $this->getSelectDropdownItem('CAN_BUY_ZERO', $this->getStatusDropdownItems(
193 Option::get('catalog', 'default_can_buy_zero') === 'Y'
194 ));
195
196 if ($this->canChangePurchasingPrice())
197 {
198 $row = $this->getPriceDropdownItem([
199 'VALUE' => 'PURCHASING_PRICE',
200 'UNIT' => 'PURCHASING_CURRENCY',
201 ]);
202 if ($row !== null)
203 {
204 $items[] = $row;
205 }
206 unset($row);
207 }
208 }
209 }
210
211 $items = $this->appendSystemFieldItems($items);
212
213 return [
214 'ID' => 'product_field',
215 'NAME' => self::getId(),
216 'TYPE' => Types::DROPDOWN,
217 'ITEMS' => $items,
218 ];
219 }
220
221 private function appendSystemFieldItems(array $items): array
222 {
223 $options = [
224 'ENTITY_ID' => '',
225 'IBLOCK_ID' => $this->getIblockId(),
226 ];
227
228 $productGroupAction = new class($options) extends ProductGroupAction
229 {
230 public function getFormRowFieldName(string $field): string
231 {
232 return $field;
233 }
234 };
235
236 $systemFieldsItems = SystemField::getGroupActions($productGroupAction);
237 if (!empty($systemFieldsItems))
238 {
239 array_push($items, ...$systemFieldsItems);
240 }
241
242 return $items;
243 }
244
245 private function getInputDropdownItem(string $fieldName): array
246 {
247 $field = $this->getProductEntity()->getField($fieldName);
248
249 return [
250 'VALUE' => $fieldName,
251 'NAME' => $field->getTitle(),
252 'ONCHANGE' => [
253 [
254 'ACTION' => Actions::RESET_CONTROLS,
255 ],
256 [
257 'ACTION' => Actions::CREATE,
258 'DATA' => [
259 [
260 'ID' => 'product_field_text_' . $fieldName,
261 'NAME' => $fieldName,
262 'TYPE' => Types::TEXT,
263 'VALUE' => '',
264 ],
265 ],
266 ],
267 ],
268 ];
269 }
270
271 private function getSelectDropdownItem(string $fieldName, array $dropdownItems): array
272 {
273 $field = $this->getProductEntity()->getField($fieldName);
274
275 return [
276 'VALUE' => $fieldName,
277 'NAME' => $field->getTitle(),
278 'ONCHANGE' => [
279 [
280 'ACTION' => Actions::RESET_CONTROLS,
281 ],
282 [
283 'ACTION' => Actions::CREATE,
284 'DATA' => [
285 [
286 'ID' => 'product_field_dropdown_' . $fieldName,
287 'NAME' => $fieldName,
288 'TYPE' => Types::DROPDOWN,
289 'VALUE' => '',
290 'ITEMS' => $dropdownItems,
291 ],
292 ],
293 ],
294 ],
295 ];
296 }
297
298 private function getPriceDropdownItem(array $fields): ?array
299 {
300 if (!isset($fields['VALUE']) || !isset($fields['UNIT']))
301 {
302 return null;
303 }
304 $fieldName = $fields['VALUE'];
305 $field = $this->getProductEntity()->getField($fieldName);
306
307 $currencyItems = [];
308 foreach (CurrencyManager::getCurrencyList() as $currencyId => $currencyName)
309 {
310 $currencyItems[] = [
311 'VALUE' => $currencyId,
312 'NAME' => $currencyName
313 ];
314 }
315
316 return [
317 'VALUE' => $fieldName,
318 'NAME' => $field->getTitle(),
319 'ONCHANGE' => [
320 [
321 'ACTION' => Actions::RESET_CONTROLS,
322 ],
323 [
324 'ACTION' => Actions::CREATE,
325 'DATA' => [
326 [
327 'ID' => 'product_field_price_' . $fieldName,
328 'NAME' => $fieldName,
329 'TYPE' => Types::TEXT,
330 'VALUE' => '',
331 ],
332 [
333 'ID' => 'product_field_currency_' . $fieldName,
334 'NAME' => $fields['UNIT'],
335 'TYPE' => Types::DROPDOWN,
336 'VALUE' => '',
337 'ITEMS' => $currencyItems,
338 ],
339 ],
340 ],
341 ],
342 ];
343 }
344
345 private function getStatusDropdownItems(?bool $default = null): array
346 {
347 $result = [];
348
349 if (isset($default))
350 {
351 $result[] = [
352 'NAME' => Loc::getMessage('CATALOG_GRID_PANEL_UI_PRODUCT_ACTION_SET_PARAMETERS_DEFAULT_VALUE', [
353 '#VALUE#' => $default ? Loc::getMessage('MAIN_YES') : Loc::getMessage('MAIN_NO'),
354 ]),
355 'VALUE' => ProductTable::STATUS_DEFAULT,
356 ];
357 }
358
359 $result[] = [
360 'NAME' => Loc::getMessage('MAIN_YES'),
361 'VALUE' => ProductTable::STATUS_YES,
362 ];
363 $result[] = [
364 'NAME' => Loc::getMessage('MAIN_NO'),
365 'VALUE' => ProductTable::STATUS_NO,
366 ];
367
368 return $result;
369 }
370
371 private function getVatIdDropdownItems(): array
372 {
373 $result = [];
374
375 $rows = VatTable::getList([
376 'select' => [
377 'ID',
378 'NAME',
379 ],
380 'filter' => [
381 '=ACTIVE' => 'Y',
382 ],
383 'order' => [
384 'SORT' => 'ASC',
385 'ID' => 'ASC',
386 ],
387 ]);
388
389 if ($rows->getSelectedRowsCount() > 0)
390 {
391 $result[] = [
392 'VALUE' => '0',
393 'NAME' => Loc::getMessage('CATALOG_GRID_PANEL_UI_PRODUCT_ACTION_SET_PARAMETERS_NOT_SELECTED'),
394 ];
395 }
396
397 foreach ($rows as $row)
398 {
399 $result[] = [
400 'VALUE' => $row['ID'],
401 'NAME' => $row['NAME'],
402 ];
403 }
404
405 return $result;
406 }
407
408 private function getMeasureDropdownItems(): array
409 {
410 $result = [];
411
412 $rows = CCatalogMeasure::getList();
413 while ($row = $rows->Fetch())
414 {
415 $result[] = [
416 'VALUE' => $row['ID'],
417 'NAME' => $row['MEASURE_TITLE'],
418 ];
419 }
420
421 return $result;
422 }
423
424 private function isAvailableField(string $fieldName): bool
425 {
426 $dropdownItems = $this->getParametersDropdownControl()['ITEMS'];
427 $dropdownItemsNames = array_column($dropdownItems, 'VALUE');
428
429 return in_array($fieldName, $dropdownItemsNames, true);
430 }
431}
processRequest(HttpRequest $request, bool $isSelectedAllRows, ?Filter $filter=null)
static updateElementList(int $iblockId, array $elementIds, array $fields)
static updateSectionList(int $iblockId, array $sections, array $fields)
static getGroupActions(ProductGroupAction $panel)
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static getList(array $parameters=array())