• Модуль: iblock
  • Путь к файлу: ~/bitrix/modules/iblock/lib/model/propertyfeature.php
  • Класс: BitrixIblockModelPropertyFeature
  • Вызов: PropertyFeature::setFeatures
static function setFeatures($propertyId, array $features): MainEntityResult
{
	$result = new MainEntityResult();

	$propertyId = (int)$propertyId;
	if ($propertyId <= 0)
	{
		$result->addError(new MainError(
			Loc::getMessage('PROPERTY_FEATURE_ERR_BAD_PROPERTY_ID')
		));
		return $result;
	}

	if (empty($features))
	{
		$result->addError(new MainError(
			Loc::getMessage('PROPERTY_FEATURE_ERR_EMPTY_FEATURE_LIST')
		));
		return $result;
	}
	$features = self::checkFeatureList($features);
	if ($features === null)
	{
		$result->addError(new MainError(
			Loc::getMessage('PROPERTY_FEATURE_ERR_BAD_FEATURE_LIST')
		));
		return $result;
	}

	$currentList = [];
	$idList = [];
	$iterator = IblockPropertyFeatureTable::getList([
		'select' => ['*'],
		'filter' => ['=PROPERTY_ID' => $propertyId]
	]);
	while ($row = $iterator->fetch())
	{
		$row['ID'] = (int)$row['ID'];
		$currentList[self::getIndex($row)] = $row;
		$idList[$row['ID']] = $row['ID'];
	}
	unset($row, $iterator);

	foreach ($features as $index => $row)
	{
		if (isset($currentList[$index]))
		{
			$internalResult = IblockPropertyFeatureTable::update(
				$currentList[$index]['ID'],
				['IS_ENABLED' => $row['IS_ENABLED']]
			);
			if ($internalResult->isSuccess())
				unset($idList[$currentList[$index]['ID']]);
		}
		else
		{
			$row['PROPERTY_ID'] = $propertyId;
			$internalResult = IblockPropertyFeatureTable::add($row);
		}
		if (!$internalResult->isSuccess())
		{
			$result->addErrors($internalResult->getErrors());
			return $result;
		}
	}
	unset($internalResult, $index, $row);
	unset($currentList);

	if (!empty($idList))
	{
		$conn = MainApplication::getConnection();
		$helper = $conn->getSqlHelper();
		$query = 'delete from '.$helper->quote(IblockPropertyFeatureTable::getTableName()).
			' where '.$helper->quote('PROPERTY_ID').' = '.$propertyId.
			' and '.$helper->quote('ID').' in ('.implode(',', $idList).')';
		$conn->queryExecute($query);
		unset($query, $helper, $conn);
	}
	unset($idList);

	return $result;
}