Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
systemfield.php
1<?php
3
10
11final class SystemField
12{
13 public const EVENT_ID_BUILD_FIELD_LIST = 'OnProductUserFieldBuildList';
14
15 public const STATUS_CONTINUE = 'continue';
16 public const STATUS_FINAL = 'final';
17
19 public const CODE_MARKING_CODE_GROUP = Catalog\Product\SystemField\MarkingCodeGroup::FIELD_ID;
20
21 public const OPERATION_EXPORT = 'EXPORT';
22 public const OPERATION_IMPORT = 'IMPORT';
23 public const OPERATION_PROVIDER = 'PROVIDER';
24
25 public const DESCRIPTION_MODE_FIELD_NAME = 'FIELD_NAME';
26 public const DESCRIPTION_MODE_UI_LIST = 'UI_ENTITY_LIST';
27 public const DESCRIPTION_MODE_UI_FORM_EDITOR = 'UI_FORM_EDITOR';
28 public const DESCRIPTION_MODE_UI_FIELDS = 'UI_FIELDS';
29 public const DESCRIPTION_MODE_FULL = 'FULL';
30 public const DESCRIPTION_MODE_CLASSNAME = 'CLASSNAME';
31
32 private static ?array $currentFieldSet = null;
33
34 private static array $defaultFieldList = [
35 Catalog\Product\SystemField\MarkingCodeGroup::class,
36 Catalog\Product\SystemField\ProductMapping::class,
37 ];
38
42 public static function execAgent(): string
43 {
44 $result = '';
45 $createResult = self::create();
46 if (!$createResult->isSuccess())
47 {
48 $result = '\Bitrix\Catalog\Product\SystemField::execAgent();';
49 }
50 return $result;
51 }
52
56 public static function create(): Main\Result
57 {
58 $result = new Main\Result();
59
60 self::$currentFieldSet = null;
61
62 $fieldList = self::getBuildedFieldList();
63 if (empty($fieldList))
64 {
65 $result->setData(['STATUS' => self::STATUS_FINAL]);
66 return $result;
67 }
68
69 foreach ($fieldList as $field)
70 {
71 $internalResult = $field::create();
72 if (!$internalResult->isSuccess())
73 {
74 foreach ($internalResult->getErrors() as $error)
75 {
76 $result->addError($error);
77 }
78 }
79 }
80
81 $result->setData(['STATUS' => self::STATUS_FINAL]);
82
83 return $result;
84 }
85
89 public static function delete(): void
90 {
91 self::$currentFieldSet = null;
92 }
93
94 public static function getSelectFields(string $operation): array
95 {
96 $result = [];
97 foreach (self::getCurrentFieldSet($operation) as $field)
98 {
99 $result = array_merge(
100 $result,
101 $field::getOperationSelectFieldList($operation)
102 );
103 }
104
105 return $result;
106 }
107
112 private static function getCurrentFieldSet(string $operation): array
113 {
114 self::loadCurrentFieldSet($operation);
115
116 return self::$currentFieldSet[$operation] ?? [];
117 }
118
119 private static function getDefaultFieldSet(): array
120 {
121 return [
122 self::OPERATION_PROVIDER => null,
123 self::OPERATION_IMPORT => null,
124 self::OPERATION_EXPORT => null,
125 ];
126 }
127
128 private static function loadCurrentFieldSet(string $operation): void
129 {
130 if (self::$currentFieldSet === null)
131 {
132 self::$currentFieldSet = self::getDefaultFieldSet();
133 }
134 if (!array_key_exists($operation, self::$currentFieldSet))
135 {
136 return;
137 }
138 if (self::$currentFieldSet[$operation] === null)
139 {
140 self::$currentFieldSet[$operation] = [];
141
142 $fieldList = self::getBuildedFieldList();
143 if (!empty($fieldList))
144 {
145 foreach ($fieldList as $field)
146 {
147 if ($field::checkAllowedOperation($operation) && $field::isExists())
148 {
149 self::$currentFieldSet[$operation][] = $field;
150 }
151 }
152 unset($field);
153 }
154 unset($fieldList);
155 }
156 }
157
158 public static function getProviderSelectFields(): array
159 {
160 return self::getSelectFields(self::OPERATION_PROVIDER);
161 }
162
163 public static function getExportSelectFields(): array
164 {
165 return self::getSelectFields(self::OPERATION_EXPORT);
166 }
167
168 public static function getImportSelectFields(): array
169 {
170 return self::getSelectFields(self::OPERATION_IMPORT);
171 }
172
179 public static function getFieldList(): array
180 {
181 return self::getProviderSelectFields();
182 }
183
192 public static function convertRow(array &$row, string $operation = self::OPERATION_PROVIDER): void
193 {
194 self::prepareRow($row, $operation);
195 }
196
197 public static function prepareRow(array &$row, string $operation = self::OPERATION_IMPORT): void
198 {
199 foreach (self::getCurrentFieldSet($operation) as $field)
200 {
201 $row = $field::prepareValue($operation, $row);
202 }
203 unset($field);
204 }
205
210 public static function getGroupActions(ProductGroupAction $panel): ?array
211 {
212 $catalog = $panel->getCatalogConfig();
213 if (empty($catalog))
214 {
215 return null;
216 }
217
218 $fieldList = self::getBuildedFieldList();
219 if (empty($fieldList))
220 {
221 return null;
222 }
223
224 $result = [];
225 foreach ($fieldList as $field)
226 {
227 $action = $field::getGridAction($panel);
228 if (!empty($action))
229 {
230 $result[] = $action;
231 }
232 }
233 unset($action, $field, $fieldList);
234
235 return (!empty($result) ? $result : null);
236 }
237
238 public static function getByUserFieldName(string $fieldName): ?string
239 {
240 $fieldList = self::getBuildedFieldList();
241 if (empty($fieldList))
242 {
243 return null;
244 }
245
246 $result = null;
247 foreach ($fieldList as $field)
248 {
249 $baseParams = $field::getUserFieldBaseParam();
250 if ($baseParams['FIELD_NAME'] === $fieldName)
251 {
253 $result = $field;
254 break;
255 }
256 }
257 unset($baseParams, $field, $fieldList);
258
259 return $result;
260 }
261
262 public static function getFieldsByRestrictions(array $restrictions, array $config = []): array
263 {
264 $fieldList = self::getBuildedFieldList();
265 if (empty($fieldList))
266 {
267 return [];
268 }
269
270 $resultMode = self::DESCRIPTION_MODE_FULL;
271 if (isset($config['RESULT_MODE']) && is_string($config['RESULT_MODE']))
272 {
273 $resultMode = $config['RESULT_MODE'];
274 }
275
276 $result = [];
277 foreach ($fieldList as $field)
278 {
279 if (
280 $field::checkRestictions($restrictions)
281 && $field::isExists()
282 )
283 {
284 $data = $field::getUserFieldBaseParam();
285 switch ($resultMode)
286 {
287 case self::DESCRIPTION_MODE_FIELD_NAME:
288 $result[] = $data['FIELD_NAME'];
289 break;
290 case self::DESCRIPTION_MODE_UI_LIST:
291 $result[] = [
292 $data['FIELD_NAME'] => $field::getTitle(),
293 ];
294 break;
295 case self::DESCRIPTION_MODE_UI_FORM_EDITOR:
296 $result[] = [
297 'name' => $data['FIELD_NAME'],
298 ];
299 break;
300 case self::DESCRIPTION_MODE_UI_FIELDS:
301 $result[] = $field::getUiDescription($restrictions);
302 break;
303 case self::DESCRIPTION_MODE_CLASSNAME:
304 $result[$data['FIELD_NAME']] = $field;
305 break;
306 case self::DESCRIPTION_MODE_FULL:
307 default:
308 $result[$data['FIELD_NAME']] = $data;
309 break;
310 }
311 }
312 }
313 unset($field, $fieldList);
314
315 return $result;
316 }
317
318 public static function getFieldNamesByRestrictions(array $restrictions): array
319 {
320 return self::getFieldsByRestrictions(
321 $restrictions,
322 [
323 'RESULT_MODE' => self::DESCRIPTION_MODE_FIELD_NAME,
324 ]
325 );
326 }
327
328 public static function getPermissionFieldsByRestrictions(array $restrictions): array
329 {
330 $fieldList = self::getBuildedFieldList();
331 if (empty($fieldList))
332 {
333 return [];
334 }
335
336 $result = [];
337 foreach ($fieldList as $field)
338 {
339 if ($field::isExists())
340 {
341 $data = $field::getUserFieldBaseParam();
342 $result[$data['FIELD_NAME']] = $field::checkRestictions($restrictions);
343 }
344 }
345 unset($field, $fieldList);
346
347 return $result;
348 }
349
350 public static function getAllowedProductTypes(): array
351 {
352 $fieldList = self::getBuildedFieldList();
353 if (empty($fieldList))
354 {
355 return [];
356 }
357
358 $result = [];
359 foreach ($fieldList as $field)
360 {
361 $baseParams = $field::getUserFieldBaseParam();
362 $result[$baseParams['FIELD_NAME']] = $field::getAllowedProductTypeList();
363 }
364 unset($field, $fieldList);
365
366 return $result;
367 }
368
374 public static function getGroupActionRequest(ProductGroupAction $panel, string $fieldName): ?array
375 {
376 $catalog = $panel->getCatalogConfig();
377 if (empty($catalog))
378 {
379 return null;
380 }
381
383 $field = self::getByUserFieldName($fieldName);
384 if (empty($field))
385 {
386 return null;
387 }
388
389 return $field::getGroupActionRequest($panel);
390 }
391
399 public static function handlerHighloadBlockBeforeDelete(ORM\Event $event): ORM\EventResult
400 {
401 $result = new ORM\EventResult();
402
403 if (Catalog\Product\SystemField\Type\HighloadBlock::isAllowed())
404 {
405 $primary = $event->getParameter('primary');
406 if (!empty($primary))
407 {
408 $iterator = Highload\HighloadBlockTable::getList([
409 'filter' => $primary,
410 ]);
411 $row = $iterator->fetch();
412 unset($iterator);
413
414 if (!empty($row))
415 {
416 $fieldList = self::getBuildedFieldList();
417 foreach ($fieldList as $field)
418 {
419 if ($field::getTypeId() !== Catalog\Product\SystemField\Type\HighloadBlock::class)
420 {
421 continue;
422 }
423 if (!$field::isAllowed() || !$field::isExists())
424 {
425 continue;
426 }
427 $config = $field::getConfig();
428
429 if ($row['NAME'] === $config['HIGHLOADBLOCK']['NAME'])
430 {
431 $result->addError(new ORM\EntityError(
433 'BX_CATALOG_PRODUCT_SYSTEMFIELD_ERR_CANNOT_DELETE_HIGHLOADBLOCK',
434 ['#NAME#' => $row['NAME']]
435 )
436 ));
437 }
438 unset($config);
439 }
440 unset($field, $fieldList);
441 }
442 unset($row);
443 }
444 unset($primary);
445 }
446
447 return $result;
448 }
449
457 public static function handlerHighloadBlockBeforeUpdate(ORM\Event $event): ORM\EventResult
458 {
459 $result = new ORM\EventResult();
460
461 if (Catalog\Product\SystemField\Type\HighloadBlock::isAllowed())
462 {
463 $primary = $event->getParameter('primary');
464 $fields = $event->getParameter('fields');
465 if (!empty($primary))
466 {
467 $iterator = Highload\HighloadBlockTable::getList([
468 'filter' => $primary,
469 ]);
470 $row = $iterator->fetch();
471 unset($iterator);
472 if (!empty($row))
473 {
474 $fieldList = self::getBuildedFieldList();
475 foreach ($fieldList as $field)
476 {
477 if ($field::getTypeId() !== Catalog\Product\SystemField\Type\HighloadBlock::class)
478 {
479 continue;
480 }
481 if (!$field::isAllowed() || !$field::isExists())
482 {
483 continue;
484 }
485 $config = $field::getConfig();
486
487 if ($row['NAME'] === $config['HIGHLOADBLOCK']['NAME'])
488 {
489 if (
490 (isset($fields['NAME']) && $row['NAME'] != $fields['NAME'])
491 || (isset($fields['TABLE_NAME']) && $row['TABLE_NAME'] != $fields['TABLE_NAME'])
492 )
493 {
494 $result->addError(new ORM\EntityError(
496 'BX_CATALOG_PRODUCT_SYSTEMFIELD_ERR_CANNOT_UPDATE_HIGHLOADBLOCK',
497 ['#NAME#' => $row['NAME']]
498 )
499 ));
500 }
501 }
502 unset($config);
503 }
504 unset($field, $fieldList);
505 }
506 unset($row);
507 }
508 unset($primary);
509 }
510
511 return $result;
512 }
513
518 public static function handlerHighloadBlockBeforeUninstall(Main\Event $event): Main\EventResult
519 {
520 $blockNames = [];
521
522 $module = $event->getParameter('module');
523 if ($module === 'highloadblock')
524 {
525 $fieldList = self::getBuildedFieldList();
526 foreach ($fieldList as $field)
527 {
528 if ($field::getTypeId() !== Catalog\Product\SystemField\Type\HighloadBlock::class)
529 {
530 continue;
531 }
532 if (!$field::isAllowed() || !$field::isExists())
533 {
534 continue;
535 }
536 $config = $field::getConfig();
538 $fieldType = $field::getTypeId();
539 $row = $fieldType::getStorageTable($config['HIGHLOADBLOCK']);
540 if (!empty($row))
541 {
542 $blockNames[] = $config['HIGHLOADBLOCK']['NAME'];
543 }
544 unset($row, $fieldType, $config);
545 }
546 unset($fieldList);
547 }
548 unset($module);
549
550 if (empty($blockNames))
551 {
552 return new Main\EventResult(Main\EventResult::SUCCESS);
553 }
554 else
555 {
556 if (count($blockNames) === 1)
557 {
558 $error = Loc::getMessage(
559 'BX_CATALOG_PRODUCT_SYSTEMFIELD_ERR_DISALLOW_UNINSTALL_HIGHLOADBLOCK',
560 [
561 '#NAME#' => reset($blockNames),
562 ]
563 );
564 }
565 else
566 {
567 $error = Loc::getMessage(
568 'BX_CATALOG_PRODUCT_SYSTEMFIELD_ERR_DISALLOW_UNINSTALL_HIGHLOADBLOCK_LIST',
569 [
570 '#NAME#' => implode(', ', $blockNames),
571 ]
572 );
573 }
574
575 return new Main\EventResult(
576 Main\EventResult::ERROR,
577 [
578 'error' => $error,
579 ],
580 'catalog'
581 );
582 }
583 }
584
588 protected static function getBuildedFieldList(): array
589 {
590 $result = [];
591
592 $list = array_merge(
593 self::$defaultFieldList,
594 self::getExternalFieldList()
595 );
597 foreach ($list as $className)
598 {
599 if ($className::isAllowed())
600 {
601 $result[] = $className;
602 }
603 }
604
605 return $result;
606 }
607
611 protected static function getExternalFieldList(): array
612 {
613 $result = [];
614 $event = new Main\Event(
615 'catalog',
616 self::EVENT_ID_BUILD_FIELD_LIST,
617 []
618 );
619 $event->send();
620 $eventResult = $event->getResults();
621 if (!empty($eventResult) && is_array($eventResult))
622 {
623 foreach ($eventResult as $row)
624 {
625 if ($row->getType() != Main\EventResult::SUCCESS)
626 {
627 continue;
628 }
629 $classList = $row->getParameters();
630 if (empty($classList) || !is_array($classList))
631 {
632 continue;
633 }
634 foreach ($classList as $item)
635 {
636 if (!is_string($item))
637 {
638 continue;
639 }
640 $item = trim($item);
641 if (
642 $item === ''
643 || !class_exists($item)
644 || !is_a($item, Catalog\Product\SystemField\Base::class, true)
645 )
646 {
647 continue;
648 }
649 $result[] = $item;
650 }
651 }
652 }
653
654 return $result;
655 }
656
657 public static function renderAdminEditForm(array $product, array $config): ?string
658 {
659 if (!isset($product['ID']) || !isset($product['IBLOCK_ID']) || !isset($product['TYPE']))
660 {
661 return null;
662 }
663 $product['IBLOCK_ID'] = (int)$product['IBLOCK_ID'];
664 if ($product['IBLOCK_ID'] <= 0)
665 {
666 return null;
667 }
668 $product['PRODUCT_ID'] = (int)($product['PRODUCT_ID'] ?? \CIBlockElement::GetRealElement($product['ID']));
669 $product['TYPE'] = (int)$product['TYPE'];
670
671 $config['FROM_FORM'] = $config['FROM_FORM'] ?? false;
672 $config['ALLOW_EDIT'] = $config['ALLOW_EDIT'] ?? true;
673
674 $systemFields = self::getFieldsByRestrictions(
675 [
676 'TYPE' => $product['TYPE'],
677 'IBLOCK_ID' => $product['IBLOCK_ID'],
678 ],
679 [
680 'RESULT_MODE' => self::DESCRIPTION_MODE_CLASSNAME,
681 ]
682 );
683 if (empty($systemFields))
684 {
685 return '';
686 }
687
688 $userFieldManager = Main\UserField\Internal\UserFieldHelper::getInstance()->getManager();
689 $userFields = $userFieldManager->GetUserFields(
690 Catalog\ProductTable::getUfId(),
691 $product['PRODUCT_ID'],
692 LANGUAGE_ID
693 );
694 if (empty($userFields))
695 {
696 return '';
697 }
698
699 $result = '';
700
705 foreach ($systemFields as $fieldName => $className)
706 {
707 $row = $userFields[$fieldName];
708
709 $row['VALUE_ID'] = $product['PRODUCT_ID'];
710 $row['EDIT_FORM_LABEL'] = $row['EDIT_FORM_LABEL'] ?? $row['FIELD_NAME'];
711 if (!$config['ALLOW_EDIT'])
712 {
713 $row['EDIT_IN_LIST'] = 'N';
714 }
715
716 $html = $className::renderAdminFormControl($row, $product, $config);
717 if ($html !== null)
718 {
719 $result .= $html;
720 }
721 }
722 unset($row, $fieldName);
723
724 return $result;
725 }
726
727 public static function getUiDescriptions(array $restrictions): array
728 {
729 return self::getFieldsByRestrictions(
730 $restrictions,
731 [
732 'RESULT_MODE' => self::DESCRIPTION_MODE_UI_FIELDS,
733 ]
734 );
735 }
736}
static getUiDescriptions(array $restrictions)
static getFieldNamesByRestrictions(array $restrictions)
static getFieldsByRestrictions(array $restrictions, array $config=[])
static handlerHighloadBlockBeforeUpdate(ORM\Event $event)
static convertRow(array &$row, string $operation=self::OPERATION_PROVIDER)
static getSelectFields(string $operation)
static getPermissionFieldsByRestrictions(array $restrictions)
static getGroupActions(ProductGroupAction $panel)
static prepareRow(array &$row, string $operation=self::OPERATION_IMPORT)
static handlerHighloadBlockBeforeDelete(ORM\Event $event)
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29