• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/activity/entity/configurablerestapp.php
  • Класс: Bitrix\Crm\Activity\Entity\ConfigurableRestApp
  • Вызов: ConfigurableRestApp::save
public function save(): Result
{
	$result = new Result();

	if (!$this->getLayoutDto())
	{
		$result->addError(\Bitrix\Crm\Controller\ErrorCode::getRequiredArgumentMissingError('layout'));

		return $result;
	}

	$fields = [
		'SUBJECT' => $this->getLayoutDto()->header ? $this->getLayoutDto()->header->title : null,
	];
	if ($this->getDeadline())
	{
		$fields['END_TIME'] = $this->getDeadline()->toString();
	}
	$fields['RESPONSIBLE_ID'] = $this->getResponsibleId();
	$fields['COMPLETED'] = $this->getCompleted() ? 'Y' : 'N';
	$fields['IS_INCOMING_CHANNEL'] = $this->getIsIncomingChannel() ? 'Y' : 'N';
	$fields['PROVIDER_DATA'] = Json::encode($this->getLayoutDto());
	$fields['PROVIDER_PARAMS'] =[
		'clientId' => $this->getRestClientId(),
		'badgeCode' => $this->getBadgeCode(),
	];
	$fields['PING_OFFSETS'] = $this->getPingOffsets();
	$fields['ORIGINATOR_ID'] = $this->getOriginatorId();
	$fields['ORIGIN_ID'] = $this->getOriginId();

	if ($this->getId())
	{
		if (!$this->hasPermissionToUpdate($fields))
		{
			$result->addError(\Bitrix\Crm\Controller\ErrorCode::getAccessDeniedError());

			return $result;
		}

		$existedActivity = \CCrmActivity::GetList(
			[],
			[
				'=ID' => $this->getId(),
				'CHECK_PERMISSIONS' => 'N',
			],
			false,
			false,
			[
				'ID',
				'COMPLETED',
				'PROVIDER_ID',
			]
		)->Fetch();

		if (!$existedActivity)
		{
			$result->addError(\Bitrix\Crm\Controller\ErrorCode::getNotFoundError());

			return $result;
		}
		if ($existedActivity['PROVIDER_ID'] !== \Bitrix\Crm\Activity\Provider\ConfigurableRestApp::getId())
		{
			$result->addError(\Bitrix\Crm\Controller\ErrorCode::getNotFoundError());

			return $result;
		}
		$isSuccess = \CCrmActivity::Update($this->getId(), $fields, false);

		if (!$isSuccess)
		{
			foreach (\CCrmActivity::GetErrorMessages() as $errorMessage)
			{
				$result->addError(new Error($errorMessage));
			}
		}
	}
	else
	{
		if (!$this->hasPermissionToAdd())
		{
			$result->addError(\Bitrix\Crm\Controller\ErrorCode::getAccessDeniedError());

			return $result;
		}

		$fields['BINDINGS'] = [
			[
				'OWNER_TYPE_ID' => $this->owner->getEntityTypeId(),
				'OWNER_ID' => $this->owner->getEntityId(),
			]
		];

		$provider = new \Bitrix\Crm\Activity\Provider\ConfigurableRestApp();
		$result = $provider->createActivity($provider::PROVIDER_TYPE_ID_DEFAULT, $fields);
		if ($result->isSuccess())
		{
			$this->id = (int)$result->getData()['id'];
		}
	}

	return $result;
}