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

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

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

	$res = CSocNetUserToGroup::GetList(
		array(),
		array(
			"GROUP_ID" => $groupId,
			"USER_ID" => $userId,
		),
		false,
		false,
		[ 'ID', 'USER_ID', 'ROLE', 'GROUP_VISIBLE', 'GROUP_NAME', 'GROUP_SCRUM_MASTER_ID' ]
	);

	if ($relationFields = $res->Fetch())
	{
		if (!in_array($relationFields["ROLE"], [
			UserToGroupTable::ROLE_USER,
			UserToGroupTable::ROLE_MODERATOR,
		], true))
		{
			return false;
		}

		if ((int)$relationFields['USER_ID'] === (int)$relationFields['GROUP_SCRUM_MASTER_ID'])
		{
			return false;
		}

		if (!empty($relationFields['GROUP_NAME']))
		{
			$relationFields['GROUP_NAME'] = Emoji::decode($relationFields['GROUP_NAME']);
		}

		if (CSocNetUserToGroup::Delete($relationFields["ID"]))
		{
			CSocNetSubscription::DeleteEx($userId, "SG".$groupId);

			if (ModuleManager::isModuleInstalled('im'))
			{
				$chatNotificationResult = UserToGroup::addInfoToChat(array(
					'group_id' => $groupId,
					'user_id' => $userId,
					'action' => UserToGroup::CHAT_ACTION_OUT
				));

				if (!$chatNotificationResult)
				{
					CSocNetUserToGroup::notifyImToModerators(array(
						"TYPE" => "unjoin",
						"RELATION_ID" => $relationFields["ID"],
						"USER_ID" => $userId,
						"GROUP_ID" => $groupId,
						"GROUP_NAME" => $relationFields["GROUP_NAME"]
					));
				}
			}
		}
		else
		{
			$errorMessage = "";
			if ($e = $APPLICATION->GetException())
			{
				$errorMessage = $e->GetString();
			}
			if ($errorMessage === '')
			{
				$errorMessage = Loc::getMessage('SONET_UR_ERROR_CREATE_USER2GROUP');
			}

			$APPLICATION->ThrowException($errorMessage, "ERROR_DELETE_RELATION");
			return false;
		}
	}
	else
	{
		$APPLICATION->ThrowException(GetMessage("SONET_NO_USER2GROUP"), "ERROR_NO_MEMBER_REQUEST");
		return false;
	}

	CSocNetUserToGroup::__SpeedFileCheckMessages($userId);

	return true;
}