• Модуль: socialnetwork
  • Путь к файлу: ~/bitrix/modules/socialnetwork/classes/general/user_group.php
  • Класс: CAllSocNetUserToGroup
  • Вызов: CAllSocNetUserToGroup::TransferModerator2Member
static function TransferModerator2Member($userID, $groupId, $relationIdList): bool
{
	global $APPLICATION, $USER;

	$userID = (int)$userID;
	if ($userID <= 0)
	{
		$APPLICATION->ThrowException(GetMessage("SONET_UR_EMPTY_USERID"), "ERROR_USERID");
		return false;
	}

	$groupId = (int)$groupId;
	if ($groupId <= 0)
	{
		$APPLICATION->ThrowException(GetMessage("SONET_UR_EMPTY_GROUPID"), "ERROR_GROUPID");
		return false;
	}

	if (!is_array($relationIdList))
	{
		return true;
	}

	$arGroup = CSocNetGroup::GetByID($groupId);
	if (!$arGroup || !is_array($arGroup))
	{
		$APPLICATION->ThrowException(GetMessage("SONET_UG_ERROR_NO_GROUP_ID"), "ERROR_NO_GROUP");
		return false;
	}

	$bSuccess = true;
	$arSuccessRelations = array();
	$bIMIncluded = false;
	$groupSiteId = SITE_ID;

	if (Loader::includeModule('im'))
	{
		$bIMIncluded = true;
		$groupSiteId = CSocNetGroup::GetDefaultSiteId($groupId, $arGroup["SITE_ID"]);
	}

	$workgroupsPage = COption::GetOptionString("socialnetwork", "workgroups_page", "/workgroups/", $groupSiteId);
	$groupUrlTemplate = Path::get('group_path_template', $groupSiteId);
	$groupUrlTemplate = "#GROUPS_PATH#".mb_substr($groupUrlTemplate, mb_strlen($workgroupsPage));
	$groupUrl = str_replace(array("#group_id#", "#GROUP_ID#"), $groupId, $groupUrlTemplate);
	$relationsToUpdateCount = 0;

	foreach ($relationIdList as $relationId)
	{
		$relationId = (int)$relationId;
		if ($relationId <= 0)
		{
			continue;
		}

		$arRelation = CSocNetUserToGroup::GetByID($relationId);
		if (
			!$arRelation
			|| (int)$arRelation["GROUP_ID"] !== $groupId
			|| $arRelation["ROLE"] !== UserToGroupTable::ROLE_MODERATOR
		)
		{
			continue;
		}

		$relationsToUpdateCount++;

		$arFields = array(
			"ROLE" => UserToGroupTable::ROLE_USER,
			"=DATE_UPDATE" => CDatabase::CurrentTimeFunction(),
		);
		if (CSocNetUserToGroup::Update($arRelation["ID"], $arFields))
		{
			$arSuccessRelations[] = $arRelation;

			if ($bIMIncluded)
			{
				$arTmp = CSocNetLogTools::ProcessPath(
					array(
						"GROUP_URL" => $groupUrl
					),
					$arRelation["USER_ID"],
					$groupSiteId
				);
				$groupUrl = $arTmp["URLS"]["GROUP_URL"];
				$serverName = (
				mb_strpos($groupUrl, "http://") === 0
					|| mb_strpos($groupUrl, "https://") === 0
						? ""
						: $arTmp["SERVER_NAME"]
				);
				$domainName = (
				mb_strpos($groupUrl, "http://") === 0
					|| mb_strpos($groupUrl, "https://") === 0
						? ""
						: (
							isset($arTmp["DOMAIN"])
							&& !empty($arTmp["DOMAIN"])
								? "//".$arTmp["DOMAIN"]
								: ""
						)
				);

				$arMessageFields = array(
					"TO_USER_ID" => $arRelation["USER_ID"],
					"FROM_USER_ID" => $userID,
					"NOTIFY_TYPE" => IM_NOTIFY_FROM,
					"NOTIFY_MODULE" => "socialnetwork",
					"NOTIFY_EVENT" => "moderators_group",
					"NOTIFY_TAG" => "SOCNET|MOD_GROUP|" . (int)$userID . "|" . $groupId . "|" . $arRelation["ID"] . "|" . $arRelation["USER_ID"],
					"NOTIFY_MESSAGE" => str_replace(
						array("#NAME#"),
						array("".$arGroup["NAME"].""),
						GetMessage("SONET_UG_MOD2MEMBER_MESSAGE")
					),
					"NOTIFY_MESSAGE_OUT" => str_replace(
						array("#NAME#"),
						array($arGroup["NAME"]),
						GetMessage("SONET_UG_MOD2MEMBER_MESSAGE")
					)." (".$serverName.$groupUrl.")"
				);

				CIMNotify::Add($arMessageFields);
			}
		}
		else
		{
			$errorMessage = "";
			if ($e = $APPLICATION->GetException())
			{
				$errorMessage = $e->GetString();
			}
			if ($errorMessage === '')
			{
				$errorMessage = Loc::getMessage("SONET_UR_ERROR_CREATE_USER2GROUP");
			}

			$APPLICATION->ThrowException($errorMessage, "ERROR_MOD2MEMBER");
			$bSuccess = false;
		}
	}

	if ($relationsToUpdateCount <= 0)
	{
		$APPLICATION->ThrowException(GetMessage("SONET_UR_ERROR_MEM2MOD_EMPTY_CORRECT_LIST"), "MOD2MEM_EMPTY_CORRECT_LIST");
		return false;
	}

	$successfulUserIdList = array();
	foreach ($arSuccessRelations as $arRel)
	{
		$arNotifyParams = array(
			"TYPE" => "unmoderate",
			"RELATION_ID" => $arRel["ID"],
			"USER_ID" => $arRel["USER_ID"],
			"GROUP_ID" => $arRel["GROUP_ID"],
			"GROUP_NAME" => $arRel["GROUP_NAME"],
			"EXCLUDE_USERS" => array($USER->GetID())
		);
		CSocNetUserToGroup::NotifyImToModerators($arNotifyParams);

		$successfulUserIdList[] = $arRel["USER_ID"];
	}

	$successfulUserIdList = array_unique($successfulUserIdList);

	if (!empty($successfulUserIdList))
	{
		IntegrationImChatWorkgroup::setChatManagers(array(
			'group_id' => $groupId,
			'user_id' => $successfulUserIdList,
			'set' => false
		));
	}

	if (
		$bSuccess
		&& count($arSuccessRelations) <= 0
	)
	{
		$APPLICATION->ThrowException(GetMessage("SONET_UR_ERROR_MOD2MEM_INCORRECT_PARAMS"), "MOD2MEM_INCORRECT_PARAMS");
		$bSuccess = false;
	}

	return $bSuccess;
}