• Модуль: im
  • Путь к файлу: ~/bitrix/modules/im/classes/general/im_event.php
  • Класс: CIMEvent
  • Вызов: CIMEvent::OnAddRatingVote
static function OnAddRatingVote($id, $arParams)
{
	if (!Loader::includeModule("socialnetwork"))
	{
		return true;
	}

	$ratingNotifyTag = "RATING|".($arParams['VALUE'] >= 0 ? "" : "DL|").$arParams['ENTITY_TYPE_ID']."|".$arParams['ENTITY_ID'];
	if (!empty($arParams['REACTION']))
	{
		$ratingNotifyTag .= "|".$arParams['REACTION'];
	}
	$ratingMentionNotifyTag = 'RATING_MENTION|' . ($arParams['VALUE'] >= 0 ? '' : 'DL|') . $arParams['ENTITY_TYPE_ID'] . '|' . $arParams['ENTITY_ID'];

	$contentId = LivefeedProvider::getContentId([
		"RATING_TYPE_ID" => $arParams['ENTITY_TYPE_ID'],
		"RATING_ENTITY_ID" => $arParams['ENTITY_ID']
	]);

	if (!empty($contentId['ENTITY_TYPE']))
	{
		$liveFeedEntity = LivefeedProvider::init([
			'ENTITY_TYPE' => $contentId['ENTITY_TYPE'],
			'ENTITY_ID' => $contentId['ENTITY_ID']
		]);

		if (!$liveFeedEntity)
		{
			return false;
		}

		if ($arParams['OWNER_ID'] != $arParams['USER_ID']) // AUX
		{
			$originalText = $liveFeedEntity->getSourceOriginalText();
			$auxData = $liveFeedEntity->getSourceAuxData();

			if (
				$originalText <> ''
				&& !empty($auxData)
			)
			{
				$handlerManager = new BitrixSocialnetworkCommentAuxHandlerManager();
				/** @var bool|object $handler */
				if($handler = $handlerManager->getHandlerByPostText($originalText))
				{
					$suffix = '';
					if ($provider = LivefeedProvider::getProvider($contentId['ENTITY_TYPE']))
					{
						$suffix = $provider->getSuffix();
					}

					$handler->setOptions([
						'im' => true,
						'suffix' => $suffix
					]);
					$handler->sendRatingNotification($auxData, $arParams);
					return true;
				}
			}
		}

		$title = $liveFeedEntity->getSourceTitle();
		$description = $liveFeedEntity->getSourceDescription();
		$url = $liveFeedEntity->getLiveFeedUrl();

		$arMentionedUserID = [];
		if (!empty($description))
		{
			preg_match_all("/[users*=s*([^]]*)](.+?)[/user]/is".BX_UTF_PCRE_MODIFIER, $description, $mention);
			if (!empty($mention))
			{
				$arMentionedUserID = array_merge($arMentionedUserID, $mention[1]);
			}
		}
		$arMentionedUserID = array_unique($arMentionedUserID);

		$title = CTextParser::clearAllTags(BitrixImText::removeBbCodes($title));
		$description = CTextParser::clearAllTags(BitrixImText::removeBbCodes($description));

		if (
			$arParams['OWNER_ID'] != $arParams['USER_ID']
			|| !empty($arMentionedUserID)
		)
		{
			$arParams["ENTITY_LINK"] = $url;
			$arParams["ENTITY_PARAM"] = '';

			if (
				in_array('photo_photo', $liveFeedEntity->getEventId())
				|| in_array('photo', $liveFeedEntity->getEventId())
			)
			{
				$arParams["ENTITY_PARAM"] = 'photos'; // it was also 'library' value
			}
			elseif (in_array('wiki', $liveFeedEntity->getEventId()))
			{
				$arParams["ENTITY_PARAM"] = 'wiki';
			}

			$arParams["ENTITY_TITLE"] = trim(strip_tags(str_replace(["rn","n","r"], ' ', $title)));
			$arParams["ENTITY_MESSAGE"] = trim(strip_tags(str_replace(["rn","n","r"], ' ', $description)));
			$arParams["ENTITY_BODY"] = preg_replace('/(.+?)(r|n)+.*/is'.BX_UTF_PCRE_MODIFIER,'\1', $description);

			if (
				(
					$arParams["ENTITY_TITLE"] <> ''
					|| $arParams["ENTITY_MESSAGE"] <> ''
				)
				&& $arParams["ENTITY_LINK"] <> ''
			)
			{
				$originalLink = $arParams["ENTITY_LINK"];

				$bExtranetInstalled = CModule::IncludeModule("extranet");
				if ($bExtranetInstalled)
				{
					$arSites = [];
					$extranet_site_id = CExtranet::GetExtranetSiteID();
					$intranet_site_id = CSite::GetDefSite();
					$dbSite = CSite::GetList("sort", "desc", ["ACTIVE" => "Y"]);
					while($arSite = $dbSite->Fetch())
					{
						$arSites[$arSite["ID"]] = [
							"DIR" => (trim($arSite["DIR"]) <> '' ? $arSite["DIR"] : "/"),
							"SERVER_NAME" => (trim($arSite["SERVER_NAME"]) <> '' ? $arSite["SERVER_NAME"] : COption::GetOptionString("main", "server_name", $_SERVER["HTTP_HOST"]))
						];
					}
				}
				$bSentToOwner = false;
				if ($arParams['OWNER_ID'] != $arParams['USER_ID'])
				{
					$followValue = CSocNetLogFollow::GetExactValueByRating(
						intval($arParams['OWNER_ID']),
						trim($arParams["ENTITY_TYPE_ID"]),
						intval($arParams["ENTITY_ID"])
					);

					if ($followValue != "N")
					{
						$arParams['ENTITY_LINK'] = self::GetMessageRatingEntityURL(
							$originalLink,
							intval($arParams['OWNER_ID']),
							$arSites,
							$intranet_site_id,
							$extranet_site_id
						);

						$arMessageFields = [
							"MESSAGE_TYPE" => IM_MESSAGE_SYSTEM,
							"TO_USER_ID" => intval($arParams['OWNER_ID']),
							"FROM_USER_ID" => intval($arParams['USER_ID']),
							"NOTIFY_TYPE" => IM_NOTIFY_FROM,
							"NOTIFY_MODULE" => "main",
							"NOTIFY_EVENT" => "rating_vote",
							"NOTIFY_TAG" => $ratingNotifyTag,
							"NOTIFY_SUB_TAG" => $ratingNotifyTag.'|'.intval($arParams['OWNER_ID']),
							"NOTIFY_MESSAGE" => self::GetMessageRatingVote($arParams),
							"NOTIFY_MESSAGE_OUT" => self::GetMessageRatingVote($arParams, true)
						];
						$bSentToOwner = CIMNotify::Add($arMessageFields);
					}
				}

				if (is_array($arMentionedUserID))
				{
					if (in_array($arParams['ENTITY_TYPE_ID'], ["BLOG_COMMENT", "FORUM_POST"]))
					{
						$rsLogComment = CSocNetLogComments::GetList(
							[],
							[
								"RATING_TYPE_ID" => $arParams['ENTITY_TYPE_ID'],
								"RATING_ENTITY_ID" =>  $arParams['ENTITY_ID']
							],
							false,
							false,
							["LOG_ID"]
						);
						if ($arLogComment = $rsLogComment->Fetch())
						{
							$logId = $arLogComment["LOG_ID"];
						}
					}
					elseif (in_array($arParams['ENTITY_TYPE_ID'], ["BLOG_POST"]))
					{
						$rsLog = CSocNetLog::GetList(
							[],
							[
								"RATING_TYPE_ID" => $arParams['ENTITY_TYPE_ID'],
								"RATING_ENTITY_ID" =>  $arParams['ENTITY_ID']
							],
							false,
							false,
							["ID"]
						);
						if ($arLog = $rsLog->Fetch())
						{
							$logId = $arLog["ID"];
						}
					}

					if (intval($logId) > 0)
					{
						$arParams["MENTION"] = true; // for self::GetMessageRatingVote()

						foreach ($arMentionedUserID as $mentioned_user_id)
						{
							if (
								$mentioned_user_id != $arParams['USER_ID']
								&& ($mentioned_user_id != $arParams['OWNER_ID'] || !$bSentToOwner)
								&& CSocNetLogRights::CheckForUserOnly($logId, $mentioned_user_id)
							)
							{
								$followValue = CSocNetLogFollow::GetExactValueByRating(
									intval($mentioned_user_id),
									trim($arParams["ENTITY_TYPE_ID"]),
									intval($arParams["ENTITY_ID"])
								);

								if ($followValue != "N")
								{
									$arParams['ENTITY_LINK'] = self::GetMessageRatingEntityURL(
										$originalLink,
										intval($mentioned_user_id),
										$arSites,
										$intranet_site_id,
										$extranet_site_id
									);

									$arMessageFields = [
										"MESSAGE_TYPE" => IM_MESSAGE_SYSTEM,
										"TO_USER_ID" => intval($mentioned_user_id),
										"FROM_USER_ID" => intval($arParams['USER_ID']),
										"NOTIFY_TYPE" => IM_NOTIFY_FROM,
										"NOTIFY_MODULE" => "main",
										"NOTIFY_EVENT" => "rating_vote_mentioned",
										"NOTIFY_TAG" => $ratingMentionNotifyTag,
										"NOTIFY_SUB_TAG" => $ratingMentionNotifyTag.'|'.intval($mentioned_user_id),
										"NOTIFY_MESSAGE" => self::GetMessageRatingVote($arParams),
										"NOTIFY_MESSAGE_OUT" => self::GetMessageRatingVote($arParams, true)
									];

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

	return true;
}