Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
markingcodegroup.php
1<?php
2
4
11use Bitrix\UI;
12
14{
15 public const FIELD_ID = 'MARKING_CODE_GROUP';
16
17 protected const SHORT_FIELD_ID = 'PRODUCT_GROUP';
18
19 public const TYPE_ID = Catalog\Product\SystemField\Type\HighloadBlock::class;
20
21 protected const VALUE_NAME_PREFIX = 'MARKING_CODE_GROUP_TYPE_';
22
23 protected const USE_PARENT_PRODUCT_VALUE = -1;
24 protected const USE_PARENT_PRODUCT_XML_VALUE = 'PARENT_MARKING_GROUP';
25
26 public static function getConfig(): ?array
27 {
28 if (!static::isAllowed())
29 {
30 return null;
31 }
32
34 $className = static::getTypeId();
35 $fieldId = static::getFieldId();
36
37 $result = [
38 'HIGHLOADBLOCK' => [
39 'TABLE_NAME' => $className::getTableName($fieldId),
40 'NAME' => $className::getName($fieldId),
41 'FIELDS' => static::getHighloadblockFields(),
42 'RIGHTS' => $className::getDefaultRights(),
43 'TRANSFORM_VALUES' => static::getHighloadblockTransformValues(),
44 'VALUES' => static::getHighloadblockValues(),
45 ],
46 'FIELD' => self::getUserFieldBaseParam() + [
47 'SORT' => 100,
48 'SHOW_FILTER' => 'S',
49 'SHOW_IN_LIST' => 'Y',
50 'EDIT_IN_LIST' => 'Y',
51 'IS_SEARCHABLE' => 'N',
52 'SETTINGS' => $className::getDefaultSettings(),
53 ],
54 'FIELD_CONFIG' => [
55 'HLFIELD_ID' => 'UF_NAME',
56 ],
57 ];
58
59 $titles = static::getMessages(
60 __FILE__,
61 ['TITLES' => 'MARKING_CODE_GROUP_STORAGE_TITLE',]
62 );
63
64 $result['HIGHLOADBLOCK'] = $result['HIGHLOADBLOCK'] + $titles;
65
66 $result['FIELD'] += static::getMessages(
67 __FILE__,
68 [
69 'EDIT_FORM_LABEL' => 'MARKING_CODE_GROUP_FIELD_TITLE',
70 'LIST_COLUMN_LABEL' => 'MARKING_CODE_GROUP_FIELD_TITLE',
71 'LIST_FILTER_LABEL' => 'MARKING_CODE_GROUP_FIELD_TITLE',
72 ]
73 );
74
75 return $result;
76 }
77
78 public static function isAllowed(): bool
79 {
81 $className = static::getTypeId();
82
83 if (!$className::isAllowed())
84 {
85 return false;
86 }
87 if (!static::isBitrix24())
88 {
89 $iterator = LanguageTable::getList([
90 'select' => ['ID'],
91 'filter' => [
92 '=ID' => 'ru',
93 '=ACTIVE' => 'Y',
94 ],
95 ]);
96 $row = $iterator->fetch();
97 unset($iterator);
98 if (empty($row))
99 {
100 return false;
101 }
102 $iterator = LanguageTable::getList([
103 'select' => ['ID'],
104 'filter' => [
105 '@ID' => ['ua', 'by', 'kz'],
106 '=ACTIVE' => 'Y',
107 ],
108 'limit' => 1,
109 ]);
110 $row = $iterator->fetch();
111 unset($iterator);
112 if (!empty($row))
113 {
114 return false;
115 }
116 return true;
117 }
118 else
119 {
120 return (\CBitrix24::getPortalZone() === 'ru');
121 }
122 }
123
124 protected static function getTitleInternal(): ?string
125 {
126 return Loc::getMessage('MARKING_CODE_GROUP_FIELD_TITLE');
127 }
128
129 public static function getUserFieldBaseParam(): array
130 {
132 $className = static::getTypeId();
133
134 return [
135 'ENTITY_ID' => Catalog\ProductTable::getUfId(),
136 'FIELD_NAME' => static::getUserFieldName(self::SHORT_FIELD_ID),
137 'USER_TYPE_ID' => $className::getUserTypeId(),
138 'XML_ID' => static::getFieldId(),
139 'MULTIPLE' => 'N',
140 'MANDATORY' => 'N',
141 ];
142 }
143
147 protected static function getHighloadblockFields(): array
148 {
149 $result = [];
150
151 $fieldSettings = [
152 'XML_ID' => [
153 'DEFAULT_VALUE' => '',
154 'SIZE' => 16,
155 'ROWS' => 1,
156 'MIN_LENGTH' => 0,
157 'MAX_LENGTH' => 0,
158 'REGEXP' => '/^[0-9]{1,16}$/'
159 ],
160 'NAME' => [
161 'DEFAULT_VALUE' => '',
162 'SIZE' => 100,
163 'ROWS' => 1,
164 'MIN_LENGTH' => 1,
165 'MAX_LENGTH' => 255,
166 'REGEXP' => ''
167 ]
168 ];
169
170 $sort = 100;
171 foreach (array_keys($fieldSettings) as $fieldId)
172 {
173 $messageList = static::getMessages(
174 __FILE__,
175 [
176 'EDIT_FORM_LABEL' => 'MARKING_CODE_GROUP_UF_FIELD_'.$fieldId,
177 'LIST_COLUMN_LABEL' => 'MARKING_CODE_GROUP_UF_FIELD_'.$fieldId,
178 'LIST_FILTER_LABEL' => 'MARKING_CODE_GROUP_UF_FIELD_'.$fieldId
179 ]
180 );
181
182 $result[] = [
183 'FIELD_NAME' => static::getUserFieldName($fieldId),
184 'USER_TYPE_ID' => Main\UserField\Types\StringType::USER_TYPE_ID,
185 'XML_ID' => $fieldId,
186 'SORT' => $sort,
187 'MULTIPLE' => 'N',
188 'MANDATORY' => 'Y',
189 'SHOW_FILTER' => 'S',
190 'SHOW_IN_LIST' => 'Y',
191 'EDIT_IN_LIST' => 'Y',
192 'IS_SEARCHABLE' => 'N',
193 'SETTINGS' => $fieldSettings[$fieldId],
194 ] + $messageList;
195 $sort += 100;
196 }
197
198 return $result;
199 }
200
201 protected static function getHighloadblockValues(): array
202 {
203 $groupCodes = [
204 '02',
205 '03',
206 '05',
207 '17485',
208 '8258',
209 '8721',
210 '9840',
211 '06',
212 '5010',
213 '5137',
214 '5139',
215 '5140',
216 ];
217 $groupTitles = Loc::loadLanguageFile(
218 $_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/catalog/regionalsystemfields/markingcodegroup.php',
219 'ru'
220 );
221
222 $result = [];
223 foreach ($groupCodes as $id)
224 {
225 $result[] = [
226 'UF_XML_ID' => $id,
227 'UF_NAME' => $groupTitles[self::VALUE_NAME_PREFIX.$id]
228 ];
229 }
230
231 return $result;
232 }
233
234 protected static function getHighloadblockTransformValues(): array
235 {
236 return [
237 [
238 'OLD_XML_ID' => '5048',
239 'NEW_XML_ID' => '17485',
240 ],
241 [
242 'OLD_XML_ID' => '5408',
243 'NEW_XML_ID' => '17485',
244 ],
245 ];
246 }
247
248 protected static function getGridActionConfig(ProductGroupAction $panel): ?array
249 {
250 $catalog = $panel->getCatalogConfig();
251 if (empty($catalog))
252 {
253 return null;
254 }
255 $allowForOffers =
256 $catalog['CATALOG_TYPE'] === \CCatalogSku::TYPE_OFFERS
257 && self::isUsedMarkingOffer()
258 ;
259 if (
260 $catalog['CATALOG_TYPE'] !== \CCatalogSku::TYPE_CATALOG
261 && $catalog['CATALOG_TYPE'] !== \CCatalogSku::TYPE_FULL
262 && $catalog['CATALOG_TYPE'] !== \CCatalogSku::TYPE_PRODUCT
263 && !$allowForOffers
264 )
265 {
266 return null;
267 }
268
269 $field = static::load();
270 if (empty($field))
271 {
272 return null;
273 }
274
275 $config = [
276 'USER_FIELD' => $field,
277 'VISUAL' => [
278 'LIST' => [
279 'ID' => $panel->getFormRowFieldId($field['FIELD_NAME']),
280 'NAME' => $panel->getFormRowFieldName($field['FIELD_NAME']),
281 ],
282 ],
283 ];
284
285 if ($allowForOffers)
286 {
287 $config['ADDITIONAL_ITEMS'] = [
288 'LIST' => [
289 [
291 'NAME' => Loc::getMessage('MARKING_CODE_GROUP_MESS_USE_PARENT_PRODUCT_VALUE'),
292 ],
293 ]
294 ];
295 }
296
297 return $config;
298 }
299
300 public static function getAllowedProductTypeList(): array
301 {
302 $result = [
303 Catalog\ProductTable::TYPE_PRODUCT,
304 Catalog\ProductTable::TYPE_SKU,
305 ];
306 if (self::isUsedMarkingOffer())
307 {
308 $result[] = Catalog\ProductTable::TYPE_OFFER;
309 }
310
311 return $result;
312 }
313
314 public static function checkRestictions(array $restrictions): bool
315 {
316 if (!parent::checkRestictions($restrictions))
317 {
318 return false;
319 }
320
321 if (isset($restrictions['IBLOCK_ID']) && !isset($restrictions['CATALOG']))
322 {
323 $iterator = Catalog\CatalogIblockTable::getList([
324 'select' => [
325 '*',
326 ],
327 'filter' => [
328 '=IBLOCK_ID' => $restrictions['IBLOCK_ID'],
329 ],
330 'cache' => [
331 'ttl' => 86400,
332 ]
333 ]);
334 $row = $iterator->fetch();
335 if (!empty($row))
336 {
337 $restrictions['CATALOG'] = $row;
338 }
339 unset($row, $iterator);
340 }
341
342 if (!empty($restrictions['CATALOG']) && is_array($restrictions['CATALOG']))
343 {
344 if ($restrictions['CATALOG']['SUBSCRIPTION'] === 'Y')
345 {
346 return false;
347 }
348 }
349
350 return true;
351 }
352
353 public static function getAllowedOperations(): array
354 {
355 return [
356 Catalog\Product\SystemField::OPERATION_PROVIDER,
357 Catalog\Product\SystemField::OPERATION_EXPORT,
358 Catalog\Product\SystemField::OPERATION_IMPORT,
359 ];
360 }
361
362 public static function getOperationSelectFieldList(string $operation): array
363 {
364 if (!static::isAllowed())
365 {
366 return [];
367 }
368
369 $fields = static::getUserFieldBaseParam();
370 switch($operation)
371 {
372 case Catalog\Product\SystemField::OPERATION_PROVIDER:
373 case Catalog\Product\SystemField::OPERATION_EXPORT:
374 case Catalog\Product\SystemField::OPERATION_IMPORT:
375 $result = [
376 $fields['XML_ID'] => $fields['FIELD_NAME'],
377 ];
378 break;
379 default:
380 $result = [];
381 break;
382 }
383
384 return $result;
385 }
386
387 public static function prepareValue(string $operation, array $productRow): array
388 {
389 $field = static::load();
390 if ($field === null)
391 {
392 return $productRow;
393 }
394 if (!array_key_exists($field['XML_ID'], $productRow))
395 {
396 return $productRow;
397 }
398
399 switch ($operation)
400 {
401 case Catalog\Product\SystemField::OPERATION_PROVIDER:
402 $productRow = self::prepareValueForProvider($field, $productRow);
403 break;
404 case Catalog\Product\SystemField::OPERATION_IMPORT:
405 $productRow = self::prepareValueForImport($field, $productRow);
406 break;
407 case Catalog\Product\SystemField::OPERATION_EXPORT:
408 $productRow = self::prepareValueForExport($field, $productRow);
409 break;
410 }
411
412 return $productRow;
413 }
414
415 private static function prepareValueForProvider(array $field, array $productRow): array
416 {
417 $value = $productRow[$field['XML_ID']];
418 if ($value !== null)
419 {
420 $value = (int)$value;
421 }
422 if (self::isNeedParent($productRow))
423 {
424 if (
425 !self::isUsedMarkingOffer()
427 )
428 {
429 $productValue = self::getParentProductValue($productRow['ID'], $field);
430 if ($value === self::USE_PARENT_PRODUCT_VALUE)
431 {
432 $value = $productValue;
433 }
434 else
435 {
436 $value = $productValue ?? $value;
437 }
438 }
439 }
440
441 $productRow[$field['XML_ID']] = ($value !== null
442 ? self::getXmlIdById($field['SETTINGS']['HLBLOCK_ID'], (int)$value)
443 : null
444 );
445
446 return $productRow;
447 }
448
449 private static function prepareValueForImport(array $field, array $productRow): array
450 {
451 $value = $productRow[$field['XML_ID']];
452 if ($value === self::USE_PARENT_PRODUCT_XML_VALUE)
453 {
454 $productRow[$field['FIELD_NAME']] = self::USE_PARENT_PRODUCT_VALUE;
455 }
456 else
457 {
458 $productRow[$field['FIELD_NAME']] = ($value !== null
459 ? self::getIdByXmlId($field['SETTINGS']['HLBLOCK_ID'], $value)
460 : null
461 );
462 }
463 unset($productRow[$field['XML_ID']]);
464
465 return $productRow;
466 }
467
468 private static function prepareValueForExport(array $field, array $productRow): array
469 {
470 $value = $productRow[$field['XML_ID']];
471 if ($value !== null)
472 {
473 $value = (int)$value;
474 }
475 if ($value === self::USE_PARENT_PRODUCT_VALUE)
476 {
477 $productRow[$field['XML_ID']] = self::USE_PARENT_PRODUCT_XML_VALUE;
478 }
479 elseif ($value !== null)
480 {
481 $productRow[$field['XML_ID']] = self::getXmlIdById(
482 $field['SETTINGS']['HLBLOCK_ID'],
483 $value
484 );
485 }
486
487 return $productRow;
488 }
489
490 private static function isNeedParent(array $productRow): bool
491 {
492 return
493 isset($productRow['ID'])
494 && isset($productRow['TYPE'])
495 && (int)$productRow['TYPE'] === Catalog\ProductTable::TYPE_OFFER
496 ;
497 }
498
499 private static function isUsedMarkingOffer(): bool
500 {
501 return Option::get('catalog', 'use_offer_marking_code_group') === 'Y';
502 }
503
504 private static function getParentProductValue(int $id, $field): ?string
505 {
506 $result = null;
507 $parentsList = \CCatalogSku::getProductList($id);
508 if (!empty($parentsList) && isset($parentsList[$id]))
509 {
510 $row = Catalog\ProductTable::getRow([
511 'select' => [
512 'ID',
513 $field['FIELD_NAME'],
514 ],
515 'filter' => [
516 '=ID' => $parentsList[$id]['ID'],
517 ],
518 ]);
519 if ($row !== null)
520 {
521 $result = $row[$field['FIELD_NAME']];
522 }
523 }
524
525 return $result;
526 }
527
528 protected static function afterLoadInternalModify(array $row): array
529 {
530 $row = parent::afterLoadInternalModify($row);
531 if (empty($row['SETTINGS']) || !is_array($row['SETTINGS']))
532 {
533 $row['SETTINGS'] = [];
534 }
535 $row['SETTINGS']['HLBLOCK_ID'] = (int)($row['SETTINGS']['HLBLOCK_ID'] ?? 0);
536 $row['SETTINGS']['HLFIELD_ID'] = (int)($row['SETTINGS']['HLFIELD_ID'] ?? 0);
537
538 return $row;
539 }
540
541 public static function updateProductFormConfiguration(): void
542 {
543 if (!static::isAllowed())
544 {
545 return;
546 }
547 $field = static::load();
548 if ($field === null)
549 {
550 return;
551 }
552
553 Catalog\Update\UiFormConfiguration::addFormField(
554 [
555 'name' => $field['FIELD_NAME'],
556 'optionFlags' => '1',
557 'options' => [
558 'showCode' => 'true',
559 ]
560 ],
561 Catalog\Update\UiFormConfiguration::PARENT_SECTION_MAIN
562 );
563 }
564
565 public static function renderAdminFormControl(array $field, array $product, array $config): ?string
566 {
567 $result = parent::renderAdminFormControl($field, $product, $config);
568 if ($result !== null)
569 {
570 if ($product['TYPE'] === Catalog\ProductTable::TYPE_OFFER)
571 {
572 $parentSelected = (int)$field['VALUE'] === self::USE_PARENT_PRODUCT_VALUE;
573 $addOption = '<option value="' . self::USE_PARENT_PRODUCT_VALUE . '"'
574 . ($parentSelected ? ' selected' : '')
575 . '>' . htmlspecialcharsbx(Loc::getMessage('MARKING_CODE_GROUP_MESS_USE_PARENT_PRODUCT_VALUE'))
576 . '</option>'
577 ;
578 if ($parentSelected)
579 {
580 $result = str_replace('selected', '', $result);
581 }
582 $index = strpos($result, '</option>');
583 if ($index !== false)
584 {
585 $index += 9; // after first option
586 $result = substr($result, 0, $index)
587 . $addOption
588 . substr($result, $index)
589 ;
590 }
591 }
592 }
593
594 return $result;
595 }
596
597 protected static function getUiDescriptionInternal(array $description, array $userField, array $restrictions): ?array
598 {
599 $description['type'] = UI\EntityForm\Control\Type::LIST;
600
601 $config = [
602 'RESULT' => [
603 'RETURN_FIELD_ID' => 'Y',
604 ],
605 ];
606
607 if (
608 isset($restrictions['TYPE'])
609 && $restrictions['TYPE'] === Catalog\ProductTable::TYPE_OFFER
610 && self::isUsedMarkingOffer()
611 )
612 {
613 $config['ADDITIONAL_ITEMS'] = [
614 'LIST' => [
615 0 => [
616 'ID' => (string)self::USE_PARENT_PRODUCT_VALUE,
617 'VALUE' => (string)self::USE_PARENT_PRODUCT_VALUE,
618 'NAME' => Loc::getMessage('MARKING_CODE_GROUP_MESS_USE_PARENT_PRODUCT_VALUE'),
619 ]
620 ]
621 ];
622 }
623
624 $items = Type\HighloadBlock::getItems($userField, $config);
625 if ($items !== null)
626 {
627 $description['data'] += [
628 'items' => $items
629 ];
630 }
631 unset($items);
632
633 return $description;
634 }
635}
static getXmlIdById(int $hlblockId, int $id)
static getIdByXmlId(int $hlblockId, string $xmlId)
static getUiDescriptionInternal(array $description, array $userField, array $restrictions)
static prepareValue(string $operation, array $productRow)
static getGridActionConfig(ProductGroupAction $panel)
static renderAdminFormControl(array $field, array $product, array $config)
static loadLanguageFile($file, $language=null, $normalize=true)
Definition loc.php:224
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29