20 parent::checkRequiredModules();
21 if (self::$highloadInclude ===
null)
23 self::$highloadInclude = Loader::includeModule(
'highloadblock');
24 if (!self::$highloadInclude)
33 $storage = static::createStorage($config[
'HIGHLOADBLOCK']);
34 if (!static::isSuccess())
36 return static::getErrorResult();
39 static::transformExistValues($storage);
40 if (!static::isSuccess())
42 return static::getErrorResult();
45 static::fillValues($storage);
46 if (!static::isSuccess())
48 return static::getErrorResult();
51 $config[
'HIGHLOADBLOCK'] = $storage;
52 $config = static::createLink($config);
53 if (!static::isSuccess())
55 return static::getErrorResult();
59 $result->setData([
'ID' => $config[
'FIELD'][
'ID']]);
66 return parent::verifyConfig($config);
75 return self::TABLE_NAME_PREFIX.mb_strtolower($code);
82 public static function getName(
string $code): string
84 return Text\StringHelper::snake2camel(self::NAME_PREFIX.$code);
89 if (!static::isAllowed())
94 return \CUserTypeHlblock::USER_TYPE_ID;
99 static::checkRequiredModules();
106 if (!static::isAllowed())
112 'DEFAULT_VALUE' =>
'',
113 'DISPLAY' => \CUserTypeHlblock::DISPLAY_LIST,
120 if (!static::isAllowed())
130 if (static::isBitrix24())
132 if (Loader::includeModule(
'crm'))
134 $crmAdminGroupId = \CCrmSaleHelper::getShopGroupIdByType(\CCrmSaleHelper::GROUP_CRM_ADMIN);
135 if ($crmAdminGroupId !==
null)
137 $result[
'G'.$crmAdminGroupId] =
'W';
139 $crmManagerGroupId = \CCrmSaleHelper::getShopGroupIdByType(\CCrmSaleHelper::GROUP_CRM_MANAGER);
140 if ($crmManagerGroupId)
142 $result[
'G'.$crmManagerGroupId] =
'R';
156 $storage = static::createStorageTable(
158 'NAME' => $block[
'NAME'],
159 'TABLE_NAME' => $block[
'TABLE_NAME'],
162 'ALLOW_UPDATE' =>
true,
169 $block[
'ID'] = $storage[
'ID'];
172 static::setStorageTitle($block);
173 if (!static::isSuccess())
178 static::setStorageRights($block);
179 if (!static::isSuccess())
184 return static::createStorageFields($block);
194 $row = static::getStorageTable($block);
197 if (isset($options[
'ALLOW_UPDATE']) && $options[
'ALLOW_UPDATE'] ===
true)
199 $block[
'ID'] = $row[
'ID'];
204 'BX_CATALOG_PRODUCT_SYSTEMFIELD_HIGHLOADBLOCK_ERR_EXIST_HIGHLOADBLOCK',
205 [
'#NAME#' => $block[
'NAME']]
213 $internalResult = Highload\HighloadBlockTable::add([
214 'NAME' => $block[
'NAME'],
215 'TABLE_NAME' => $block[
'TABLE_NAME'],
217 if (!$internalResult->isSuccess())
220 'BX_CATALOG_PRODUCT_SYSTEMFIELD_HIGHLOADBLOCK_ERR_CREATE_HIGHLOADBLOCK',
222 '#NAME#' => $block[
'NAME'],
223 '#ERROR#' => implode(
'; ', $internalResult->getErrorMessages()),
229 $block[
'ID'] = (int)$internalResult->getId();
237 $iterator = Highload\HighloadBlockTable::getList([
244 '=NAME' => $block[
'NAME'],
245 '=TABLE_NAME' => $block[
'TABLE_NAME'],
248 $row = $iterator->fetch();
252 $row[
'ID'] = (int)$row[
'ID'];
265 if (!isset($block[
'ID']))
267 static::addError(
Loc::getMessage(
'BX_CATALOG_PRODUCT_SYSTEMFIELD_HIGHLOADBLOCK_ERR_HIGHLOADBLOCK_ID_ABSENT'));
272 if (!empty($block[
'TITLES']) && is_array($block[
'TITLES']))
274 self::deleteStorageTitle($block);
275 foreach ($block[
'TITLES'] as $languageId => $title)
277 Highload\HighloadBlockLangTable::add([
278 'ID' => $block[
'ID'],
279 'LID' => $languageId,
286 private static function deleteStorageTitle(array $block): void
289 $entity = Highload\HighloadBlockLangTable::getEntity();
290 if (in_array(
'LID', $entity->getPrimaryArray(),
true))
292 foreach (array_keys($block[
'TITLES']) as $languageId)
294 Highload\HighloadBlockLangTable::delete([
295 'ID' => $block[
'ID'],
296 'LID' => $languageId,
302 Highload\HighloadBlockLangTable::delete([
303 'ID' => $block[
'ID'],
315 if (!isset($block[
'ID']))
317 static::addError(
Loc::getMessage(
'BX_CATALOG_PRODUCT_SYSTEMFIELD_HIGHLOADBLOCK_ERR_HIGHLOADBLOCK_ID_ABSENT'));
322 if (!empty($block[
'RIGHTS']) && is_array($block[
'RIGHTS']))
324 $tasks = static::getModuleTasks(
'highloadblock');
325 foreach ($block[
'RIGHTS'] as $accessCode => $role)
327 if (!isset($tasks[$role]))
332 'HL_ID' => $block[
'ID'],
333 'ACCESS_CODE' => $accessCode,
334 'TASK_ID' => $tasks[$role],
336 $iterator = Highload\HighloadBlockRightsTable::getList([
339 '=HL_ID' => $access[
'HL_ID'],
340 '=ACCESS_CODE' => $access[
'ACCESS_CODE'],
343 $row = $iterator->fetch();
346 Highload\HighloadBlockRightsTable::update($row[
'ID'], $access);
350 Highload\HighloadBlockRightsTable::add($access);
362 if (!isset($block[
'ID']))
364 static::addError(
Loc::getMessage(
'BX_CATALOG_PRODUCT_SYSTEMFIELD_HIGHLOADBLOCK_ERR_HIGHLOADBLOCK_ID_ABSENT'));
369 if (!empty($block[
'FIELDS']) && is_array($block[
'FIELDS']))
371 $entityId = Highload\HighloadBlockTable::compileEntityId($block[
'ID']);
373 foreach (array_keys($block[
'FIELDS']) as $index)
375 $block[
'FIELDS'][$index][
'ENTITY_ID'] = $entityId;
377 $internalResult = static::createUserField($block[
'FIELDS'][$index]);
378 if (!$internalResult->isSuccess())
381 'BX_CATALOG_PRODUCT_SYSTEMFIELD_HIGHLOADBLOCK_ERR_HIGHLOADBLOCK_CREATE_FIELD',
383 '#FIELD#' => $block[
'FIELDS'][$index][
'FIELD_NAME'],
384 '#ERROR#' => implode(
'; ', $internalResult->getErrorMessages()),
390 $data = $internalResult->getData();
391 $block[
'FIELDS'][$index][
'ID'] = (int)$data[
'ID'];
400 if (!empty($block[
'TRANSFORM_VALUES']) && is_array($block[
'TRANSFORM_VALUES']))
402 $entity = Highload\HighloadBlockTable::compileEntity($block);
403 $entityDataClass = $entity->getDataClass();
405 $dictionary = $block[
'TITLES'][LANGUAGE_ID] ?? $block[
'NAME'];
407 foreach ($block[
'TRANSFORM_VALUES'] as $group)
409 $iterator = $entityDataClass::getList([
411 'filter' => [
'=UF_XML_ID' => $group[
'OLD_XML_ID']],
413 $row = $iterator->fetch();
416 $internalResult = $entityDataClass::update($row[
'ID'], [
'UF_XML_ID' => $group[
'NEW_XML_ID']]);
417 if (!$internalResult->isSuccess())
420 'BX_CATALOG_PRODUCT_SYSTEMFIELD_HIGHLOADBLOCK_ERR_TRANFORM_VALUE',
422 '#DICTIONARY#' => $dictionary,
423 '#ERROR#' => implode(
'; ', $internalResult->getErrorMessages()),
434 if (!empty($block[
'VALUES']) && is_array($block[
'VALUES']))
436 $entity = Highload\HighloadBlockTable::compileEntity($block);
437 $entityDataClass = $entity->getDataClass();
439 $dictionary = $block[
'TITLES'][LANGUAGE_ID] ?? $block[
'NAME'];
441 foreach ($block[
'VALUES'] as $group)
443 $iterator = $entityDataClass::getList([
445 'filter' => [
'=UF_XML_ID' => $group[
'UF_XML_ID']]
447 $row = $iterator->fetch();
448 $found = !empty($row);
451 $iterator = $entityDataClass::getList([
453 'filter' => [
'=UF_NAME' => $group[
'UF_NAME']]
455 $row = $iterator->fetch();
456 $found = !empty($row);
460 $internalResult = $entityDataClass::update($row[
'ID'], $group);
464 $internalResult = $entityDataClass::add($group);
466 if (!$internalResult->isSuccess())
468 static::addError(
new Main\
Error(
470 'BX_CATALOG_PRODUCT_SYSTEMFIELD_HIGHLOADBLOCK_ERR_ITEM_CREATE',
472 '#DICTIONARY#' => $dictionary,
473 '#CODE#' =>
'['.$group[
'UF_XML_ID'].
'] '.$group[
'UF_NAME'],
484 $field = static::getPrepareLinkSettings($config);
485 $internalResult = static::createUserField($field);
486 if (!$internalResult->isSuccess())
488 static::addError(implode(
'; ', $internalResult->getErrorMessages()));
494 $data = $internalResult->getData();
495 $field[
'ID'] = (int)$data[
'ID'];
496 $config[
'FIELD'] = $field;
505 foreach ($fields as $item)
507 if ($item[
'FIELD_NAME'] === $name)
509 $result = $item[
'ID'];
518 $field = $config[
'FIELD'];
519 $field[
'SETTINGS'][
'HLBLOCK_ID'] = $config[
'HIGHLOADBLOCK'][
'ID'];
521 if (isset($config[
'FIELD_CONFIG'][
'HLFIELD_ID']))
523 $showedField = (string)static::getFieldIdbyName(
524 $config[
'HIGHLOADBLOCK'][
'FIELDS'],
525 $config[
'FIELD_CONFIG'][
'HLFIELD_ID']
528 $field[
'SETTINGS'][
'HLFIELD_ID'] = $showedField;
530 if (!empty($field[
'SETTINGS'][
'DEFAULT_VALUE']))
532 $entity = Highload\HighloadBlockTable::compileEntity($config[
'HIGHLOADBLOCK'][
'ID']);
533 $entityDataClass = $entity->getDataClass();
535 $rawValues = $field[
'SETTINGS'][
'DEFAULT_VALUE'];
537 $iterator = $entityDataClass::getList([
542 'filter' => [
'@UF_XML_ID' => $rawValues],
544 while ($row = $iterator->fetch())
546 $map[$row[
'UF_XML_ID']] = (int)$row[
'ID'];
548 unset($row, $iterator);
550 if (is_array($rawValues))
553 foreach ($rawValues as $id)
555 if (isset($map[$id]))
557 $values[] = $map[$id];
560 $field[
'SETTINGS'][
'DEFAULT_VALUE'] = $values;
565 $field[
'SETTINGS'][
'DEFAULT_VALUE'] = ($map[$rawValues] ?? 0);
575 if (empty($config[
'USER_FIELD']) || !is_array($config[
'USER_FIELD']))
579 $userField = $config[
'USER_FIELD'];
583 'NAME_WITH_ID' =>
'Y',
587 !empty($config[
'ADDITIONAL_VALUES'])
588 && is_array($config[
'ADDITIONAL_VALUES'])
591 $itemsConfig[
'ADDITIONAL_VALUES'] = $config[
'ADDITIONAL_VALUES'];
594 $list = static::getItems($userField, $itemsConfig);
601 $userField[
'MULTIPLE'] ===
'Y'
602 ?
Loc::getMessage(
'BX_CATALOG_PRODUCT_SYSTEMFIELD_HIGHLOADBLOCK_MESS_EMPTY_VALUE')
608 'ACTION' => Main\Grid\Panel\Actions::RESET_CONTROLS
611 'ACTION' => Main\Grid\Panel\Actions::CREATE,
614 'TYPE' => Main\Grid\Panel\Types::DROPDOWN,
615 'ID' => $config[
'VISUAL'][
'LIST'][
'ID'],
616 'NAME' => $config[
'VISUAL'][
'LIST'][
'NAME'],
618 'MULTIPLE' => $userField[
'MULTIPLE'],
619 'EMPTY_TEXT' => $emptyText,
625 'NAME' => $userField[
'EDIT_FORM_LABEL'] ?? $userField[
'FIELD_NAME'],
626 'VALUE' => $userField[
'FIELD_NAME'],
627 'ONCHANGE' => $action
629 unset($action, $list);
634 public static function getIdByXmlId(
int $hlblockId, array $xmlIds): array
637 $xmlIds = array_filter($xmlIds);
642 $hlblock = Highload\HighloadBlockTable::resolveHighloadblock($hlblockId);
643 if ($hlblock ===
null)
647 $entity = Highload\HighloadBlockTable::compileEntity($hlblock);
648 $fieldsList = $entity->getFields();
649 if (isset($fieldsList[
'ID']) && isset($fieldsList[
'UF_XML_ID']))
651 $entityDataClass = $entity->getDataClass();
652 $iterator = $entityDataClass::getList([
658 '@UF_XML_ID' => $xmlIds,
661 while ($value = $iterator->fetch())
663 $result[$value[
'UF_XML_ID']] = (int)$value[
'ID'];
665 unset($value, $iterator);
666 unset($entityDataClass);
668 unset($fieldsList, $entity);
676 Main\Type\Collection::normalizeArrayValuesByInt($ids);
681 $hlblock = Highload\HighloadBlockTable::resolveHighloadblock($hlblockId);
682 if ($hlblock ===
null)
686 $entity = Highload\HighloadBlockTable::compileEntity($hlblock);
687 $fieldsList = $entity->getFields();
688 if (isset($fieldsList[
'ID']) && isset($fieldsList[
'UF_XML_ID']))
690 $entityDataClass = $entity->getDataClass();
691 $iterator = $entityDataClass::getList([
700 while ($value = $iterator->fetch())
702 $result[$value[
'ID']] = $value[
'UF_XML_ID'];
704 unset($value, $iterator);
705 unset($entityDataClass);
707 unset($fieldsList, $entity);
712 public static function getItems(array $userField, array $config = []): ?array
714 if (empty($userField[
'SETTINGS']) || !is_array($userField[
'SETTINGS']))
718 if (!isset($userField[
'SETTINGS'][
'HLBLOCK_ID']))
723 $hlblock = Highload\HighloadBlockTable::resolveHighloadblock($userField[
'SETTINGS'][
'HLBLOCK_ID']);
724 if ($hlblock ===
null)
728 $entity = Highload\HighloadBlockTable::compileEntity($hlblock);
729 $fieldsList = $entity->getFields();
730 if (!isset($fieldsList[
'ID']) || !isset($fieldsList[
'UF_NAME']))
737 if (!empty($config[
'RESULT']) && is_array($config[
'RESULT']))
739 $useIdKey = isset($config[
'RESULT'][
'RETURN_FIELD_ID']) && $config[
'RESULT'][
'RETURN_FIELD_ID'] ===
'Y';
740 $nameWithId = isset($config[
'RESULT'][
'NAME_WITH_ID']) && $config[
'RESULT'][
'NAME_WITH_ID'] ===
'Y';
745 $userField[
'MANDATORY'] ===
'N'
746 && $userField[
'MULTIPLE'] ===
'N'
751 'NAME' =>
Loc::getMessage(
'BX_CATALOG_PRODUCT_SYSTEMFIELD_HIGHLOADBLOCK_MESS_EMPTY_VALUE'),
761 !empty($config[
'ADDITIONAL_ITEMS'][
'LIST'])
762 && is_array($config[
'ADDITIONAL_ITEMS'][
'LIST'])
765 $items = array_merge(
767 $config[
'ADDITIONAL_ITEMS'][
'LIST']
771 $entityDataClass = $entity->getDataClass();
772 $iterator = $entityDataClass::getList([
781 while ($value = $iterator->fetch())
784 'ID' => $value[
'ID'],
785 'VALUE' => $value[
'ID'],
786 'NAME' => ($nameWithId
787 ? $value[
'UF_NAME'] .
' [' .$value[
'ID'] .
']'
793 $row[
'ID'] = $value[
'ID'];
797 unset($value, $iterator);
798 unset($entityDataClass, $entity);
static addError(string $error)
static getName(string $code)
static getDefaultSettings()
static getIdByXmlId(int $hlblockId, array $xmlIds)
static bool $highloadInclude
static createStorageFields(array $block)
static verifyConfig(array $config)
static internalCreate(array $config)
static setStorageRights(array $block)
static getPrepareLinkSettings(array $config)
static getStorageTable(array $block)
static setStorageTitle(array $block)
static getItems(array $userField, array $config=[])
static createLink(array $config)
static getTableName(string $code)
static transformExistValues(array $block)
static createStorageTable(array $block, array $options=[])
static createStorage(array $block)
static fillValues(array $block)
static internalGridAction(array $config)
static getFieldIdbyName(array $fields, string $name)
static checkRequiredModules()
static getXmlIdById(int $hlblockId, array $ids)
static getDefaultRights()
static getMessage($code, $replace=null, $language=null)