• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/controller/calllist.php
  • Класс: Bitrix\Crm\Controller\CallList
  • Вызов: CallList::updateCallList
static function updateCallList(array $query, int $nav, \CRestServer $server): bool
{
	$query = array_change_key_case($query, CASE_UPPER);

	self::checkRequiredParams(['LIST_ID', 'ENTITY_TYPE', 'ENTITIES'], $query, $server);

	$entityTypeId = \CCrmOwnerType::resolveId($query['ENTITY_TYPE']);
	if (!in_array($entityTypeId, [\CCrmOwnerType::Contact, \CCrmOwnerType::Company], true))
	{
		throw new RestException(
			'EntityType is incorrect',
			self::ENTITY_TYPE_ERROR,
			$server::STATUS_WRONG_REQUEST
		);
	}

	if (!is_array($query['ENTITIES']))
	{
		throw new RestException(
			'Entities is not array',
			self::ENTITIES_ERROR,
			$server::STATUS_WRONG_REQUEST
		);
	}
	if (!self::isEntitiesExist($query['ENTITY_TYPE'], $query['ENTITIES'], $server))
	{
		throw new RestException(
			'Incorrect entities id',
			self::ENTITIES_ERROR,
			$server::STATUS_WRONG_REQUEST
		);
	}

	$userPermissions = \Bitrix\Crm\Service\Container::getInstance()->getUserPermissions();

	if (!$userPermissions->isAdmin())
	{
		$canReadType = $userPermissions->canReadType($entityTypeId);
		if (!$canReadType)
		{
			throw new RestException(
				'Access Denied',
				self::ACCESS_ERROR,
				$server::STATUS_FORBIDDEN
			);
		}
	}
	try
	{
		$callList = \Bitrix\Crm\CallList\CallList::createWithId((int)$query['LIST_ID'], true);
	}
	catch (SystemException $exception)
	{
		throw new RestException(
			'Incorrect list id or access denied',
			self::LIST_ID_ERROR,
			$server::STATUS_WRONG_REQUEST
		);
	}
	if ((int)$callList->getEntityTypeId() !== (int)$entityTypeId)
	{
		throw new RestException(
			'Discrepancy between the type of call participants and incoming type',
			self::ENTITY_TYPE_ERROR,
			$server::STATUS_WRONG_REQUEST
		);
	}

	$currentEntitiesId = [];
	foreach ($callList->getItems() as $item)
	{
		$currentEntitiesId[] = (int)$item->getElementId();
	}

	$intersect = array_intersect($currentEntitiesId, $query['ENTITIES']);
	$addEntitiesId = array_diff($query['ENTITIES'], $intersect);
	$deleteEntities = array_diff($currentEntitiesId, $intersect);

	$addEntitiesId = self::filterAllowedItems($addEntitiesId, $entityTypeId);

	$callList->addEntities($addEntitiesId);

	if (isset($query['WEBFORM_ID']))
	{
		if (!self::isWebFormExist($query['WEBFORM_ID']))
		{
			throw new RestException(
				'Incorrect webform id',
				self::WEBFORM_ERROR,
				$server::STATUS_WRONG_REQUEST
			);
		}
		$callList->setWebformId($query['WEBFORM_ID']);
	}
	else
	{
		$callList->setWebformId(null);
	}

	$callList->persist();
	$callList->deleteItems($deleteEntities);

	return true;
}