• Модуль: socialnetwork
  • Путь к файлу: ~/bitrix/modules/socialnetwork/classes/general/log_tools_photo.php
  • Класс: CSocNetLogToolsPhoto
  • Вызов: CSocNetLogToolsPhoto::OnAfterPhotoUpload
static function OnAfterPhotoUpload($arFields, $arComponentParams, $arComponentResult)
{
	global $USER, $DB;

	static $arSiteWorkgroupsPage;

	if (!CModule::IncludeModule("iblock"))
	{
		return;
	}

	if (
		!array_key_exists("IS_SOCNET", $arComponentParams)
		|| $arComponentParams["IS_SOCNET"] != "Y"
	)
	{
		return;
	}

	$bPassword = false;
	$arComponentResult["SECTION"]["PATH"] = array();
	
	$rsSectionAlbum = CIBlockSection::GetList(
		array(), 
		array(
			"ID" => intval($arFields["IBLOCK_SECTION"])
		),
		false, 
		array("ID", "LEFT_MARGIN", "RIGHT_MARGIN", "DEPTH_LEVEL")
	);

	if ($arSectionAlbum = $rsSectionAlbum->Fetch())
	{
		$dbSection = CIBlockSection::GetList(
			array("LEFT_MARGIN" => "ASC"),
			array(
				"IBLOCK_ID" => intval($arComponentParams["IBLOCK_ID"]),
				"<=LEFT_BORDER" => intval($arSectionAlbum["LEFT_MARGIN"]),
				">=RIGHT_BORDER" => intval($arSectionAlbum["RIGHT_MARGIN"]),
				"<=DEPTH_LEVEL" => intval($arSectionAlbum["DEPTH_LEVEL"]),
			),
			false, 
			array("ID", "IBLOCK_ID", "NAME", "CREATED_BY", "DEPTH_LEVEL", "LEFT_MARGIN", "RIGHT_MARGIN", "UF_PASSWORD")
		);

		while ($arPath = $dbSection->Fetch())
		{
			$arComponentResult["SECTION"]["PATH"][] = $arPath;
		}
	}

	foreach($arComponentResult["SECTION"]["PATH"] as $arPathSection)
	{
		if (trim($arPathSection["UF_PASSWORD"] ?? '') <> '')
		{
			$bPassword = true;
			break;
		}
	}

	if (
		array_key_exists("USER_ALIAS", $arComponentParams)
		&& $arComponentParams["USER_ALIAS"] <> ''
	)
	{
		$arTmp = explode("_", $arComponentParams["USER_ALIAS"]);
		if (
			is_array($arTmp)
			&& count($arTmp) == 2
		)
		{
			$entity_type = $arTmp[0];
			$entity_id = $arTmp[1];

			if ($entity_type == "group")
				$entity_type = SONET_ENTITY_GROUP;
			elseif ($entity_type == "user")
				$entity_type = SONET_ENTITY_USER;
		}

		if (
			(
				!in_array($entity_type, array(SONET_ENTITY_GROUP, SONET_ENTITY_USER))
				|| intval($entity_id) <= 0
			)
			&& count($arComponentResult["SECTION"]["PATH"]) > 0
		)
		{
			$entity_type = SONET_ENTITY_USER;
			$entity_id = $arComponentResult["SECTION"]["PATH"][0]["CREATED_BY"];
		}
	}

	if (
		!in_array($entity_type, array(SONET_ENTITY_GROUP, SONET_ENTITY_USER))
		|| intval($entity_id) <= 0
	)
		return;

	if (!$arSiteWorkgroupsPage && IsModuleInstalled("extranet") && $entity_type == SONET_ENTITY_GROUP)
	{
		$rsSite = CSite::GetList("sort", "desc", Array("ACTIVE" => "Y"));
		while($arSite = $rsSite->Fetch())
			$arSiteWorkgroupsPage[$arSite["ID"]] = COption::GetOptionString("socialnetwork", "workgroups_page", $arSite["DIR"]."workgroups/", $arSite["ID"]);
	}

	if (is_set($arComponentParams["DETAIL_URL"]) && is_array($arSiteWorkgroupsPage) && $entity_type == SONET_ENTITY_GROUP)
		foreach($arSiteWorkgroupsPage as $groups_page)
			if (mb_strpos($arComponentParams["DETAIL_URL"], $groups_page) === 0)
				$arComponentParams["DETAIL_URL"] = "#GROUPS_PATH#".mb_substr($arComponentParams["DETAIL_URL"], mb_strlen($groups_page), mb_strlen($arComponentParams["DETAIL_URL"]) - mb_strlen($groups_page));

	$db_res = CSocNetLog::GetList(
		array(),
		array(
			"ENTITY_TYPE" => $entity_type,
			"ENTITY_ID" => $entity_id,
			"EVENT_ID" => "photo",
			"EXTERNAL_ID" => $arFields["IBLOCK_SECTION"]."_".$arFields["MODIFIED_BY"],
			">=LOG_UPDATE" => ConvertTimeStamp(AddToTimeStamp(array("MI" => -30))+CTimeZone::GetOffset(), "FULL")
		)
	);

	if (
		array_key_exists("SECTION", $arComponentResult)
		&& array_key_exists("NAME", $arComponentResult["SECTION"])
	)
		$strSectionName = $arComponentResult["SECTION"]["NAME"];
	else
		$strSectionName = $arComponentResult["SECTION"]["PATH"][count($arComponentResult["SECTION"]["PATH"])-1]["NAME"] ?? '';
		
	if (
		array_key_exists("URL", $arComponentResult)
		&& array_key_exists("SECTION_EMPTY", $arComponentResult["URL"])
	)
		$strSectionUrl = $arComponentResult["URL"]["SECTION_EMPTY"];
	else
		$strSectionUrl = $arComponentResult["SECTION_URL"] ?? '';

	$strSectionUrl = str_replace(
		array("#SECTION_ID#", "#section_id#"),
		$arFields["IBLOCK_SECTION"],
		$strSectionUrl
	);

	if ($db_res && $res = $db_res->Fetch())
	{
		if ($res["PARAMS"] <> '')
		{
			$arResParams = unserialize($res["PARAMS"], [ 'allowed_classes' => false ]);
			array_push($arResParams["arItems"], $arFields["ID"]);
		}
		else
			return;

		$arLogParams = array(
			"COUNT" => $arResParams["COUNT"]+1,
			"IBLOCK_TYPE" => $arComponentParams["IBLOCK_TYPE"],
			"IBLOCK_ID" => $arComponentParams["IBLOCK_ID"],
			"ALIAS" => $arComponentParams["USER_ALIAS"],
			"DETAIL_URL" => $arResParams["DETAIL_URL"],
			"arItems" => $arResParams["arItems"]
		);

		$arSonetFields = array(
			"=LOG_UPDATE" => $DB->CurrentTimeFunction(),
			"PARAMS" => serialize($arLogParams)
		);

		CSocNetLog::Update($res["ID"], $arSonetFields);
		if (!$bPassword)
		{
			CSocNetLogRights::SetForSonet($res["ID"], $entity_type, $entity_id, "photo", "view");
		}			

		$logID = $res["ID"];
	}
	else
	{
		$arLogParams = array(
			"COUNT" => 1,
			"IBLOCK_TYPE" => $arComponentParams["IBLOCK_TYPE"],
			"IBLOCK_ID" => $arComponentParams["IBLOCK_ID"],
			"DETAIL_URL" => $arComponentParams["DETAIL_URL"],
			"ALIAS" => $arComponentParams["USER_ALIAS"],
			"arItems" => array($arFields["ID"])
		);

		$sAuthorName = GetMessage("SONET_PHOTO_LOG_GUEST");
		if ($USER->IsAuthorized())
		{
			$sAuthorName = trim($USER->GetFullName());
			$sAuthorName = (empty($sAuthorName) ? $USER->GetLogin() : $sAuthorName);
		}

		$arSonetFields = array(
			"ENTITY_TYPE" => $entity_type,
			"ENTITY_ID" => $entity_id,
			"EVENT_ID" => "photo",
			"=LOG_DATE" => $DB->CurrentTimeFunction(),
			"TITLE_TEMPLATE" => str_replace("#AUTHOR_NAME#", $sAuthorName, GetMessage("SONET_PHOTO_LOG_1")),
			"TITLE" => str_replace("#COUNT#", "1", GetMessage("SONET_PHOTO_LOG_2")),
			"MESSAGE" => "",
			"URL" => $strSectionUrl,
			"MODULE_ID" => false,
			"CALLBACK_FUNC" => false,
			"EXTERNAL_ID" => $arFields["IBLOCK_SECTION"]."_".$arFields["MODIFIED_BY"],
			"PARAMS" => serialize($arLogParams),
			"SOURCE_ID" => $arFields["IBLOCK_SECTION"]
		);

		$arSonetFields["TEXT_MESSAGE"] = str_replace(array("#TITLE#"),
			array($strSectionName),
			GetMessage("SONET_PHOTO_LOG_MAIL_TEXT"));

		if ($USER->IsAuthorized())
			$arSonetFields["USER_ID"] = $USER->GetID();

		$logID = CSocNetLog::Add($arSonetFields, false);
		if (intval($logID) > 0)
		{
			$uniqueId = round((microtime(true) - mktime(0,0,0,1,1,2017))*10);

			CSocNetLog::Update($logID, array(
				"TMP_ID" => $logID,
				"RATING_TYPE_ID" => "IBLOCK_SECTION",
				"RATING_ENTITY_ID" => $uniqueId
			));

			if ($bPassword)
			{
				CSocNetLogRights::DeleteByLogID($logID);
				CSocNetLogRights::Add($logID, array("U".$USER->GetID(), "SA"));
			}
			else
			{
				CSocNetLogRights::SetForSonet($logID, $entity_type, $entity_id, "photo", "view", true);
			}			

			CSocNetLog::CounterIncrement($logID);
		}
	}
	
	if ($entity_type == SONET_ENTITY_GROUP)
	{
		$dbRight = CSocNetLogRights::GetList(array(), array("LOG_ID" => $logID));
		while ($arRight = $dbRight->Fetch())
		{
			if ($arRight["GROUP_CODE"] == "SG".$entity_id."_".SONET_ROLES_USER)
			{
				$title_tmp = str_replace(Array("rn", "n"), " ", $strSectionName);
				$title = TruncateText($title_tmp, 100);
				$title_out = TruncateText($title_tmp, 255);

				$arNotifyParams = array(
					"LOG_ID" => $logID,
					"GROUP_ID" => array($entity_id),
					"NOTIFY_MESSAGE" => "",
					"FROM_USER_ID" => $arFields["MODIFIED_BY"],
					"URL" => $strSectionUrl,
					"MESSAGE" => GetMessage("SONET_IM_NEW_PHOTO", Array(
						"#title#" => "".$title."",
					)),
					"MESSAGE_OUT" => GetMessage("SONET_IM_NEW_PHOTO", Array(
						"#title#" => $title_out
					))." (#URL#)",
					"EXCLUDE_USERS" => array($arFields["MODIFIED_BY"])
				);

				CSocNetSubscription::NotifyGroup($arNotifyParams);
				break;
			}
		}
	}
}