CCrmProductPropertyRestProxy::innerAdd

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. CCrmProductPropertyRestProxy
  4. innerAdd
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/classes/general/restservice.php
  • Класс: \CCrmProductPropertyRestProxy
  • Вызов: CCrmProductPropertyRestProxy::innerAdd
protected function innerAdd(&$fields, &$errors, array $params = null)
{
	/** @global CMain $APPLICATION */
	global $APPLICATION;

	if(!Loader::includeModule('iblock'))
	{
		throw new RestException('Could not load iblock module.');
	}

	$userPerms = CCrmPerms::GetCurrentUserPermissions();
	if (!$userPerms->HavePerm('CONFIG', BX_CRM_PERM_CONFIG, 'WRITE'))
	{
		$errors[] = 'Access denied.';
		return false;
	}

	$iblockId = (int)CCrmCatalog::EnsureDefaultExists();

	if (!isset($fields['PROPERTY_TYPE']))
	{
		$fields['PROPERTY_TYPE'] = Iblock\PropertyTable::TYPE_STRING;
	}

	$userTypeSettings = [];
	if (isset($fields['USER_TYPE_SETTINGS']) && is_array($fields['USER_TYPE_SETTINGS']))
		foreach ($fields['USER_TYPE_SETTINGS'] as $key => $value)
			$userTypeSettings[mb_strtolower($key)] = $value;

	$arFields = [
		'ACTIVE' => isset($fields['ACTIVE']) ? ($fields['ACTIVE'] === 'Y' ? 'Y' : 'N') : 'Y',
		'IBLOCK_ID' => $iblockId,
		'PROPERTY_TYPE' => $fields['PROPERTY_TYPE'],
		'USER_TYPE' => $fields['USER_TYPE'] ?? '',
		'LINK_IBLOCK_ID' => $fields['PROPERTY_TYPE'] === Iblock\PropertyTable::TYPE_ELEMENT ? $iblockId : 0,
		'NAME' => $fields['NAME'],
		'SORT' => (int)$fields['SORT'] ?? 500,
		'CODE' => $fields['CODE'] ?? '',
		'MULTIPLE' => isset($fields['MULTIPLE']) ? ($fields['MULTIPLE'] === 'Y' ? 'Y' : 'N') : 'N',
		'IS_REQUIRED' => isset($fields['IS_REQUIRED']) ? ($fields['IS_REQUIRED'] === 'Y' ? 'Y' : 'N') : 'N',
		'SEARCHABLE' => 'N',
		'FILTRABLE' => 'N',
		'WITH_DESCRIPTION' => '',
		'MULTIPLE_CNT' => $fields['MULTIPLE_CNT'] ?? 0,
		'HINT' => '',
		'ROW_COUNT' => $fields['ROW_COUNT'] ?? 1,
		'COL_COUNT' => $fields['COL_COUNT'] ?? 30,
		'DEFAULT_VALUE' => $fields['DEFAULT_VALUE'] ?? null,
		'LIST_TYPE' => 'L',
		'USER_TYPE_SETTINGS' => $userTypeSettings,
		'FILE_TYPE' => $fields['FILE_TYPE'] ?? '',
		'XML_ID' => $fields['XML_ID'] ?? '',
	];

	if ($arFields['PROPERTY_TYPE'].':'.$arFields['USER_TYPE'] === 'S:map_yandex')
	{
		$arFields['MULTIPLE'] = 'N';
	}

	if (
		$fields['PROPERTY_TYPE'] === Iblock\PropertyTable::TYPE_LIST
		&& isset($fields['VALUES'])
		&& is_array($fields['VALUES'])
	)
	{
		$values = array();

		$newKey = 0;
		foreach ($fields['VALUES'] as $key => $value)
		{
			if (!is_array($value) || !isset($value['VALUE']) || trim($value['VALUE']) === '')
			{
				continue;
			}
			$valueId = (int)$key;
			if ($valueId <= 0)
			{
				$valueId = 'n'.$newKey;
				$newKey++;
			}
			$values[$valueId] = [
				'ID' => $valueId,
				'VALUE' => (string)$value['VALUE'],
				'XML_ID' => $value['XML_ID'] ?? '',
				'SORT' => (int)$value['SORT'] ?? 500,
				'DEF' => (isset($value['DEF']) ? ($value['DEF'] === 'Y' ? 'Y' : 'N') : 'N'),
			];
		}

		$arFields['VALUES'] = $values;
	}

	$property = new CIBlockProperty;
	$result = $property->Add($arFields);

	if ((int)$result <= 0)
	{
		if (!empty($property->LAST_ERROR))
		{
			$errors[] = $property->LAST_ERROR;
		}
		else if ($e = $APPLICATION->GetException())
		{
			$errors[] = $e->GetString();
		}
	}

	return $result;
}

Добавить комментарий