• Модуль: tasks
  • Путь к файлу: ~/bitrix/modules/tasks/classes/general/task.php
  • Класс: CTasks
  • Вызов: CTasks::Add
public function Add($arFields, $arParams = [])
{
	$userId = null;
	if (
		isset($arParams['USER_ID'])
		&& (int)$arParams['USER_ID'] > 0
	)
	{
		$userId = (int)$arParams['USER_ID'];
	}

	if ($userId === null)
	{
		$userId = User::getId();
		if (!$userId)
		{
			$userId = 1; // nasty, but for compatibility :(
		}
	}

	$handler = new Task($userId);

	$correctDatePlan = ($arParams['CORRECT_DATE_PLAN'] ?? true);
	if ($correctDatePlan !== 'N' && $correctDatePlan !== false)
	{
		$handler->withCorrectDatePlan();
	}

	$spawnedByAgent = ($arParams['SPAWNED_BY_AGENT'] ?? false);
	if ($spawnedByAgent === 'Y' || $spawnedByAgent === true)
	{
		$handler->fromAgent();
	}

	$checkRightsOnFiles = ($arParams['CHECK_RIGHTS_ON_FILES'] ?? false);
	if ($checkRightsOnFiles === 'Y' || $checkRightsOnFiles === true)
	{
		$handler->withFilesRights();
	}

	$cloneDiskFileAttachment = ($arParams['CLONE_DISK_FILE_ATTACHMENT'] ?? false);
	if ($cloneDiskFileAttachment === 'Y' || $cloneDiskFileAttachment === true)
	{
		$handler->withCloneAttachments();
	}

	if (isset($arFields['META::EVENT_GUID']))
	{
		$handler->setEventGuid($arFields['META::EVENT_GUID']);
		unset($arFields['META::EVENT_GUID']);
	}

	try
	{
		$task = $handler->add($arFields);
	}
	catch (BitrixTasksControlExceptionTaskAddException $e)
	{
		$msg = $e->getMessage();
		if (!$msg)
		{
			$msg = GetMessage("TASKS_UNKNOWN_ADD_ERROR");
		}
		$this->_errors[] = [
			"text" => $msg,
			"id" => "ERROR_UNKNOWN_ADD_TASK_ERROR",
		];
		return false;
	}
	catch (Exception $e)
	{
		$this->_errors[] = [
			"text" => $e->getMessage(),
			"id" => "ERROR_UNKNOWN_ADD_TASK_ERROR",
		];
		return false;
	}

	return $task->getId();
}