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

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

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

	$iblockId = intval(CCrmCatalog::EnsureDefaultExists());
	$userTypes = CCrmProductPropsHelper::GetPropsTypesByOperations(false, 'rest');
	$res = CIBlockProperty::GetByID($id, $iblockId);
	$prop = false;
	if (is_object($res))
		$prop = $res->Fetch();
	unset($res);
	if(!is_array($prop)
		|| (isset($prop['USER_TYPE']) && !empty($prop['USER_TYPE'])
			&& !array_key_exists($prop['USER_TYPE'], $userTypes)))
	{
		$errors[] = 'Not found';
		return false;
	}

	$fields['IBLOCK_ID'] = $iblockId;
	$fields['PROPERTY_TYPE'] = $prop['PROPERTY_TYPE'];
	$fields['USER_TYPE'] = $prop['USER_TYPE'];

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

	if ($prop['PROPERTY_TYPE'] === 'L' && 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;
			$values[(0 < intval($key) ? $key : 'n'.$newKey)] = array(
				'ID' => (0 < intval($key) ? $key : 'n'.$newKey),
				'VALUE' => strval($value['VALUE']),
				'XML_ID' => (isset($value['XML_ID']) ? strval($value['XML_ID']) : ''),
				'SORT' => (isset($value['SORT']) ? intval($value['SORT']) : 500),
				'DEF' => (isset($value['DEF']) ? ($value['DEF'] === 'Y' ? 'Y' : 'N') : 'N')
			);
			$newKey++;
		}
		$fields['VALUES'] = $values;
		unset($values);
	}

	if ($fields['PROPERTY_TYPE'].':'.$fields['USER_TYPE'] === 'S:map_yandex'
		&& isset($fields['MULTIPLE']) && $fields['MULTIPLE'] !== 'N')
	{
		$fields['MULTIPLE'] = 'N';
	}

	$property = new CIBlockProperty;
	$result = $property->Update($id, $fields);

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

	return $result;
}