• Модуль: tasks
  • Путь к файлу: ~/bitrix/modules/tasks/classes/general/tasknotifications.php
  • Класс: CTaskNotifications
  • Вызов: CTaskNotifications::sendAddMessage
static function sendAddMessage($arFields, $arParams = array())
{
	if (self::useNewNotifications())
	{
		$task = BitrixTasksInternalsRegistryTaskRegistry::getInstance()->getObject($arFields['ID'], true);
		if (!$task)
		{
			return;
		}
		$controller = new BitrixTasksInternalsNotificationController();
		$controller->onTaskCreated($task, $arParams);
		$controller->push();
		return;
	}

	$isBbCodeDescription = true;
	if (isset($arFields['DESCRIPTION_IN_BBCODE']) && ($arFields['DESCRIPTION_IN_BBCODE'] === 'N'))
		$isBbCodeDescription = false;

	if (isset($arFields['XML_ID']) && mb_strlen($arFields['XML_ID']))
	{
		// Don't send any messages when created built-in tasks
		if (in_array($arFields['XML_ID'], self::$arBuiltInTasksXmlIds, true))
			return;
	}

	$cacheWasEnabled = self::enableStaticCache();

	$spawnedByAgent = false;

	if (is_array($arParams))
	{
		if (
			isset($arParams['SPAWNED_BY_AGENT'])
			&& (
				($arParams['SPAWNED_BY_AGENT'] === 'Y')
				|| ($arParams['SPAWNED_BY_AGENT'] === true)
			)
		)
		{
			$spawnedByAgent = true;
		}
	}

	$arUsers = CTaskNotifications::__GetUsers($arFields);

	$createdBy = 0;
	if (isset($arFields['CREATED_BY']) && $arFields['CREATED_BY'] > 0)
	{
		$createdBy = (int)$arFields['CREATED_BY'];
	}

	$bExcludeLoggedUser = true;
	if (
		$spawnedByAgent ||
		(
			$createdBy &&
			$createdBy !== User::getId() &&
			array_key_exists(User::getId(), $arUsers)
		)
	)
	{
		$bExcludeLoggedUser = false;
	}

	$arRecipientsIDs = CTaskNotifications::GetRecipientsIDs($arFields, $bExcludeLoggedUser);

	$effectiveUserId = false;

	if ($spawnedByAgent)
	{
		$effectiveUserId = ($createdBy? $createdBy : 1);
	}
	elseif (User::getId())
	{
		$effectiveUserId = User::getId();
	}
	elseif ($createdBy)
	{
		$effectiveUserId = $createdBy;
	}

	if (sizeof($arRecipientsIDs) && ($effectiveUserId !== false))
	{
		$arRecipientsIDs = array_unique($arRecipientsIDs);

		$strResponsible = CTaskNotifications::__Users2String(($arFields["RESPONSIBLE_ID"] ?? null), $arUsers, ($arFields["NAME_TEMPLATE"] ?? null));
		$invariantDescription = GetMessage("TASKS_MESSAGE_RESPONSIBLE_ID").': '.$strResponsible."rn";
		if ($strAccomplices = CTaskNotifications::__Users2String(($arFields["ACCOMPLICES"] ?? null), $arUsers, ($arFields["NAME_TEMPLATE"] ?? null)))
		{
			$invariantDescription .= GetMessage("TASKS_MESSAGE_ACCOMPLICES").": ".$strAccomplices."rn";
		}
		if ($strAuditors = CTaskNotifications::__Users2String(($arFields["AUDITORS"] ?? null), $arUsers, ($arFields["NAME_TEMPLATE"] ?? null)))
		{
			$invariantDescription .= GetMessage("TASKS_MESSAGE_AUDITORS").": ".$strAuditors."rn";
		}

		// There is can be different messages for users (caused by differnent users' timezones)
		$arVolatileDescriptions = [];

		// Is there correct deadline (it cause volatile part of message for different timezones)?
		if (($arFields['DEADLINE'] ?? null) && MakeTimeStamp($arFields['DEADLINE']) > 0)
		{
			// Get unix timestamp for DEADLINE
			$utsDeadline = MakeTimeStamp($arFields['DEADLINE']) - self::getUserTimeZoneOffset();

			// Collect recipients' timezones
			foreach ($arRecipientsIDs as $userId)
			{
				$tzOffset = (int) self::getUserTimeZoneOffset($userId);

				if ( ! isset($arVolatileDescriptions[$tzOffset]) )
				{
					// Make bitrix timestamp for given user
					$bitrixTsDeadline = $utsDeadline + $tzOffset;

					$deadlineAsString = BitrixTasksUI::formatDateTime($bitrixTsDeadline, '^'.BitrixTasksUI::getDateTimeFormat());

					$arVolatileDescriptions[$tzOffset] = [
						'recipients'  => [],
						'description' => GetMessage('TASKS_MESSAGE_DEADLINE').': '.$deadlineAsString."rn",
					];
				}

				$arVolatileDescriptions[$tzOffset]['recipients'][] = $userId;
			}
		}

		// If there is no volatile part of descriptions, send to all recipients at once
		if (empty($arVolatileDescriptions))
		{
			$arVolatileDescriptions[] = array(
				'recipients'  => $arRecipientsIDs,
				'description' => ''
			);
		}

		$occurAsUserId = CTasksTools::getOccurAsUserId();
		if ( ! $occurAsUserId )
			$occurAsUserId = $effectiveUserId;

		$descs = array();
		foreach ($arVolatileDescriptions as $arData)
		{
			foreach($arData['recipients'] as $uid)
			{
				$descs[$uid] = $arData['description'];
			}
		}

		$taskName = self::formatTaskName($arFields['ID'], $arFields['TITLE'], ($arFields['GROUP_ID'] ?? 0));
		$addMessage = self::getGenderMessage($occurAsUserId, 'TASKS_NEW_TASK_MESSAGE');

		$messages = [
			'INSTANT' => str_replace('#TASK_TITLE#', $taskName, $addMessage),
			'EMAIL' => str_replace('#TASK_TITLE#', strip_tags($taskName), $addMessage),
			'PUSH' => self::makePushMessage('TASKS_NEW_TASK_MESSAGE', $occurAsUserId, $arFields),
		];
		$parameters = [
			'ENTITY_CODE' => 'TASK',
			'ENTITY_OPERATION' => 'ADD',
			'EVENT_DATA' => [
				'ACTION' => 'TASK_ADD',
				'arFields' => $arFields,
			],
			'CALLBACK' => [
				'BEFORE_SEND' => function ($message) use ($isBbCodeDescription, $invariantDescription, $descs) {
					$description = $invariantDescription.$descs[$message['TO_USER_IDS'][0]];
					$message['MESSAGE']['INSTANT'] = str_replace(
						'#TASK_EXTRA#',
						$description,
						$message['MESSAGE']['INSTANT']
					);
					$message['MESSAGE']['EMAIL'] = str_replace(
						'#TASK_EXTRA#',
						$description,
						$message['MESSAGE']['EMAIL']
					);
					return $message;
				}
			],
		];

		self::sendMessageEx($arFields['ID'], $occurAsUserId, $arRecipientsIDs, $messages, $parameters);
	}

	// sonet log, not for CRM
	if (
		!isset($arFields["UF_CRM_TASK"])
		|| (
			is_array($arFields["UF_CRM_TASK"])
			&& (
				!isset($arFields["UF_CRM_TASK"][0])
				|| $arFields["UF_CRM_TASK"][0] == ''
			)
		)
		|| (
			!is_array($arFields["UF_CRM_TASK"])
			&& $arFields["UF_CRM_TASK"] == ''
		)
	)
	{
		self::SendMessageToSocNet($arFields, $spawnedByAgent);
	}

	if($cacheWasEnabled)
	{
		self::disableStaticCache();
	}
}