• Модуль: calendar
  • Путь к файлу: ~/bitrix/modules/calendar/classes/general/calendar_notify.php
  • Класс: CCalendarNotify
  • Вызов: CCalendarNotify::NotifyComment
static function NotifyComment($eventId, $params)
{
	if (!Loader::includeModule("im") || (int)$eventId <= 0)
	{
		return;
	}

	$userId = (int)$params["USER_ID"];
	if ($event = CCalendarEvent::GetById($eventId))
	{
		$instanceDate = false;

		if (
			!isset($params['LOG'])
			&& Loader::includeModule('socialnetwork')
		)
		{
			$dbResult = CSocNetLog::GetList(
				[],
				["ID" => $params["LOG_ID"]],
				false,
				false,
				["ID", "SOURCE_ID", "PARAMS"]
			);
			$arLog = $dbResult->Fetch();
		}
		else
		{
			$arLog = $params['LOG'];
		}

		if ($arLog)
		{
			if ($arLog['PARAMS'])
			{
				$arLog['PARAMS'] = unserialize($arLog['PARAMS'], ['allowed_classes' => false]);
				if (!is_array($arLog['PARAMS']))
				{
					$arLog['PARAMS'] = [];
				}
			}

			if (isset($arLog['PARAMS']['COMMENT_XML_ID']) && $arLog['PARAMS']['COMMENT_XML_ID'])
			{
				$instanceDate = CCalendarEvent::ExtractDateFromCommentXmlId($arLog['PARAMS']['COMMENT_XML_ID']);
			}
		}

		$strMsgAddComment = Loc::getMessage('EC_COMMENT_MESSAGE_ADD');

		$res = BitrixMainUserTable::getList([
			'filter' => ['=ID' => $userId],
			'select' => ['ID', 'PERSONAL_GENDER']
		]);

		if (($user = $res->fetch()) && in_array($user['PERSONAL_GENDER'], ['F', 'M']))
		{
			$strMsgAddComment = Loc::getMessage('EC_COMMENT_MESSAGE_ADD_'.$user['PERSONAL_GENDER']);
		}

		$imMessageFields = [
			"FROM_USER_ID" => $userId,
			"NOTIFY_TYPE" => IM_NOTIFY_FROM,
			"NOTIFY_MODULE" => "calendar",
			"NOTIFY_EVENT" => "event_comment"
		];

		$aId = $event['PARENT_ID'] ?? $event['ID'];

		// Here we don't need info about users
		$attendees = CCalendarEvent::GetAttendees($aId);
		if (is_array($attendees) && is_array($attendees[$aId]))
		{
			if (!$instanceDate)
			{
				$instanceDate = CCalendar::Date(CCalendar::Timestamp($event['DATE_FROM']), false);
			}

			$attendees = $attendees[$aId];

			$excludeUserIdList = [];

			if (
				$arLog
				&& Loader::includeModule('socialnetwork')
			)
			{
				$res = BitrixSocialnetworkLogFollowTable::getList([
					'filter' => [
						"=CODE" => "L".$arLog['ID'],
						"=TYPE" => "N"
					],
					'select' => ['USER_ID']
				]);

				while ($unFollower = $res->fetch())
				{
					$excludeUserIdList[] = (int)$unFollower["USER_ID"];
				}
			}

			$commentCropped = truncateText(CTextParser::clearAllTags($params['MESSAGE']), 120);
			foreach($attendees as $attendee)
			{
				$attendeeId = (int)$attendee['USER_ID'];
				if (in_array($attendeeId, $excludeUserIdList, true))
				{
					continue;
				}

				$url = CCalendar::GetPathForCalendarEx($attendeeId);
				$url = CHTTP::urlAddParams($url, ['EVENT_ID' => $eventId, 'EVENT_DATE' => $instanceDate]);

				if ($attendeeId !== $userId && $attendee["STATUS"] !== 'N')
				{
					$imMessageFields['TO_USER_ID'] = $attendeeId;

					$imMessageFields["NOTIFY_MESSAGE"] = str_replace(
						[
							"#EVENT_TITLE#",
							"#COMMENT#"
						],
						[
							$url ? "".$event["NAME"]."" : $event["NAME"],
							$commentCropped
						],
						$strMsgAddComment
					);
					$imMessageFields["NOTIFY_MESSAGE_OUT"] = str_replace(
							[
								"#EVENT_TITLE#",
								"#COMMENT#"
							],
							[
								$event["NAME"],
								$commentCropped
							],
							$strMsgAddComment
						).($url ? " (".$url.")" : "");

					$imMessageFields["NOTIFY_TAG"] = "CALENDAR|COMMENT|".$aId."|".$instanceDate;

					CIMNotify::Add($imMessageFields);
				}
			}
		}
	}
}