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

	global $DB;

	$cacheWasEnabled = CTaskNotifications::enableStaticCache();

	$status = intval($status);
	if ($status > 0 && $status < 8)
	{
		$arRecipientsIDs = CTaskNotifications::GetRecipientsIDs(array_merge($arTask, $arFields));
		if (sizeof($arRecipientsIDs) && (User::getId() || $arTask["CREATED_BY"]))
		{
			$occurAsUserId = CTasksTools::getOccurAsUserId();
			if ( ! $occurAsUserId )
				$occurAsUserId = User::getId() ? User::getId() : $arTask["CREATED_BY"];

			// If task was redone
			if (((int)$status === Status::NEW || (int)$status === Status::PENDING) &&
				((int)$arTask['REAL_STATUS'] === Status::SUPPOSEDLY_COMPLETED))
			{
				$statusMessage = CTaskNotifications::getGenderMessage($occurAsUserId, 'TASKS_TASK_STATUS_MESSAGE_REDOED');
				$messagePush = CTaskNotifications::makePushMessage('TASKS_TASK_STATUS_MESSAGE_REDOED', $occurAsUserId, $arTask);
			}
			elseif ((int)$status === Status::PENDING && (int)$arTask['REAL_STATUS'] === Status::DEFERRED)
			{
				$statusMessage = CTaskNotifications::getGenderMessage($occurAsUserId, 'TASKS_TASK_STATUS_MESSAGE_1');
				$messagePush = CTaskNotifications::makePushMessage('TASKS_TASK_STATUS_MESSAGE_1', $occurAsUserId, $arTask);
			}
			else
			{
				$statusMessage = CTaskNotifications::getGenderMessage($occurAsUserId, 'TASKS_TASK_STATUS_MESSAGE_'.$status);
				$messagePush = CTaskNotifications::makePushMessage('TASKS_TASK_STATUS_MESSAGE_'.$status, $occurAsUserId, $arTask);
			}

			$message = str_replace(
				"#TASK_TITLE#",
				self::formatTaskName($arTask['ID'], $arTask['TITLE'], $arTask['GROUP_ID'],false),
				$statusMessage
			);
			$message_email = str_replace(
				"#TASK_TITLE#",
				self::formatTaskName($arTask['ID'], $arTask['TITLE'], $arTask['GROUP_ID'],false),
				$statusMessage
			);

			if ((int)$status === Status::DECLINED)
			{
				$message = str_replace("#TASK_DECLINE_REASON#", $arTask["DECLINE_REASON"], $message);
				$message_email = str_replace("#TASK_DECLINE_REASON#", $arTask["DECLINE_REASON"], $message_email);
				$messagePush = str_replace("#TASK_DECLINE_REASON#", $arTask["DECLINE_REASON"], $messagePush);
			}

			CTaskNotifications::sendMessageEx($arTask["ID"], $occurAsUserId, $arRecipientsIDs, array(
				'INSTANT' => $message,
				'EMAIL' => $message_email,
				'PUSH' => $messagePush
			), array(
				'ENTITY_CODE' => 'TASK',
				'ENTITY_OPERATION' => 'STATUS',
				'EVENT_DATA' => array(
					'ACTION'   => 'TASK_STATUS_CHANGED_MESSAGE',
					'arTask'   => $arTask,
					'arFields' => $arFields
				),
			));

			/*
			CTaskNotifications::SendMessage($occurAsUserId, $arRecipientsIDs,
				$message, $arTask["ID"], $message_email,
				array(
					'ACTION'   => 'TASK_STATUS_CHANGED_MESSAGE',
					'arTask'   => $arTask,
					'arFields' => $arFields
				)
			);
			*/
		}
	}

	// sonet log
	if (CModule::IncludeModule("socialnetwork"))
	{
		if ((int)$status === Status::PENDING)
			$message = GetMessage("TASKS_SONET_TASK_STATUS_MESSAGE_" . Status::NEW);
		else
			$message = GetMessage("TASKS_SONET_TASK_STATUS_MESSAGE_" . $status);

		if ((int)$status === Status::DECLINED)
			$message = str_replace("#TASK_DECLINE_REASON#", $arTask["DECLINE_REASON"], $message);

		$bCrmTask = self::isCrmTask($arTask);

		$arSoFields = array(
			"TITLE" => $arTask["TITLE"],
			"=LOG_UPDATE" => (
				$arTask["CHANGED_DATE"] <> ''?
					(MakeTimeStamp($arTask["CHANGED_DATE"], CSite::GetDateFormat("FULL", SITE_ID)) > time()+CTimeZone::GetOffset()?
						BitrixTasksUtilDb::charToDateFunction($arTask["CHANGED_DATE"], "FULL", SITE_ID) :
						$DB->CurrentTimeFunction()) :
					$DB->CurrentTimeFunction()
			),
			"MESSAGE" => "",
			"TEXT_MESSAGE" => $message,
			"PARAMS" => serialize(
				array(
					"TYPE" => "status",
					'CHANGED_BY' => $arFields['CHANGED_BY'],
					'PREV_REAL_STATUS' => isset($arTask['REAL_STATUS']) ? $arTask['REAL_STATUS'] : false
				)
			)
		);

		$arSoFields['=LOG_DATE'] = $arSoFields['=LOG_UPDATE'];

		// All tasks posts in live feed should be from director
		if (isset($arFields['CREATED_BY']))
			$arSoFields["USER_ID"] = $arFields['CREATED_BY'];

		$loggedInUserId = false;
		if (User::getId())
			$loggedInUserId = (int) User::getId();

		$arLogFilter = self::getSonetLogFilter($arTask["ID"], $bCrmTask);

		if (empty($arLogFilter))
		{
			return null;
		}

		$dbRes = CSocNetLog::GetList(
			["ID" => "DESC"],
			$arLogFilter,
			false,
			false,
			["ID", "ENTITY_TYPE", "ENTITY_ID"]
		);
		while ($log = $dbRes->Fetch())
		{
			$logId = $log['ID'];
			$authorId = (int)$arTask['CREATED_BY'];

			CSocNetLog::Update($logId, $arSoFields);

			// Add author to list of users that view log about task in livefeed
			// But only when some other person change task
			if ($authorId !== $loggedInUserId)
			{
				$authorGroupCode = 'U'.$authorId;

				$rightsResult = CSocNetLogRights::GetList([], [
					'LOG_ID' => $logId,
					'GROUP_CODE' => $authorGroupCode,
				]);

				// If task's author hasn't rights yet, give them
				if (!$rightsResult->fetch())
				{
					$follow = !UserOption::isOptionSet($arTask['ID'], $authorId, UserOptionOption::MUTED);
					CSocNetLogRights::Add($logId, [$authorGroupCode], false, $follow);
				}
			}
		}
	}

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