• Модуль: socialnetwork
  • Путь к файлу: ~/bitrix/modules/socialnetwork/classes/general/event_user_view.php
  • Класс: CAllSocNetEventUserView
  • Вызов: CAllSocNetEventUserView::SetUser
static function SetUser($entityID, $feature = false, $permX = false, $bSetFeatures = false)
{
	global $APPLICATION, $DB;

	$CacheRelatedUsers = array();

	$entityID = intval($entityID);
	if ($entityID <= 0)
	{
		$GLOBALS["APPLICATION"]->ThrowException(GetMessage("SONET_EUV_EMPTY_ENTITY_ID"), "ERROR_EMPTY_ENTITY_ID");
		return false;
	}

	$event_id = array();
	$arSocNetLogEvents = CSocNetAllowed::GetAllowedLogEvents();

	foreach ($arSocNetLogEvents as $event_tmp_id => $arLogEventTmp)
	{
		if (
			!array_key_exists("ENTITIES", $arLogEventTmp)
			|| !array_key_exists(SONET_ENTITY_USER, $arLogEventTmp["ENTITIES"])
		)
			continue;

		if (
			array_key_exists("NO_SET", $arLogEventTmp)
			&& $arLogEventTmp["NO_SET"]
		)
			continue;

		if (
			array_key_exists("OPERATION", $arLogEventTmp["ENTITIES"][SONET_ENTITY_USER])
			&& $arLogEventTmp["ENTITIES"][SONET_ENTITY_USER]["OPERATION"] == ''
		)
			continue;

		$event_id[$arLogEventTmp["ENTITIES"][SONET_ENTITY_USER]["OPERATION"]] = $event_tmp_id;
		
		if (
			array_key_exists("COMMENT_EVENT", $arLogEventTmp)
			&& is_array($arLogEventTmp["COMMENT_EVENT"])
			&& array_key_exists("OPERATION", $arLogEventTmp["COMMENT_EVENT"])				
			&& array_key_exists("EVENT_ID", $arLogEventTmp["COMMENT_EVENT"])
			&& $arLogEventTmp["COMMENT_EVENT"]["OPERATION"] <> ''
			&& $arLogEventTmp["COMMENT_EVENT"]["EVENT_ID"] <> ''
			&& $arLogEventTmp["ENTITIES"][SONET_ENTITY_USER]["OPERATION"] != $arLogEventTmp["COMMENT_EVENT"]["OPERATION"]
		)
			$event_id[$arLogEventTmp["COMMENT_EVENT"]["OPERATION"]] = $arLogEventTmp["COMMENT_EVENT"]["EVENT_ID"];
	}

	if ($feature && !array_key_exists($feature, $event_id))
		return true;

	if ($feature && !is_array($feature))
		$event_id = array($feature => $event_id[$feature]);

	foreach ($event_id as $op => $event)
	{
		if (!CSocNetEventUserView::Delete(SONET_ENTITY_USER, $entityID, $event))
		{
			$GLOBALS["APPLICATION"]->ThrowException(GetMessage("SONET_EUV_ERROR_DELETE"), "ERROR_DELETE");
			return false;
		}

		$ar_event_tmp = array($event);
		
		$arCommentEvent = CSocNetLogTools::FindLogCommentEventByLogEventID($event);
		if ($arCommentEvent)
		{
			$event_comment = $arCommentEvent["EVENT_ID"];
			if (!in_array($event_comment, $event_id))
				$ar_event_tmp[] = $event_comment;
		}

		foreach($ar_event_tmp as $event_tmp)
		{
			if ($feature && $permX)
				$perm = $permX;
			else
				$perm = CSocNetUserPerms::GetOperationPerms($entityID, $op);

			if (
				array_key_exists(SONET_ENTITY_USER, $CacheRelatedUsers)
				&& array_key_exists($entityID, $CacheRelatedUsers[SONET_ENTITY_USER])
				&& array_key_exists($perm, $CacheRelatedUsers[SONET_ENTITY_USER][$entityID])
			)
				$arRelatedUsers = $CacheRelatedUsers[SONET_ENTITY_USER][$entityID][$perm];
			else
			{
				$arRelatedUsers = array();

				switch($perm)
				{
					case SONET_RELATIONS_TYPE_FRIENDS:
						$arRelatedUsers[] = array("entity_id" => $entityID, "user_id" => $entityID);
						$dbFriends = CSocNetUserRelations::GetRelatedUsers($entityID, SONET_RELATIONS_FRIEND);
						while ($arFriends = $dbFriends->Fetch())
						{
							$friendID = (($entityID == $arFriends["FIRST_USER_ID"]) ? $arFriends["SECOND_USER_ID"] : $arFriends["FIRST_USER_ID"]);
							$arRelatedUsers[] = array("entity_id" => $entityID, "user_id" => $friendID);
						}
						break;			
					case SONET_RELATIONS_TYPE_FRIENDS2:
						$arRelatedUsers[] = array("entity_id" => $entityID, "user_id" => $entityID);
						$dbFriends = CSocNetUserRelations::GetRelatedUsers($entityID, SONET_RELATIONS_FRIEND);
						while ($arFriends = $dbFriends->Fetch())
						{
							$friendID = (($entityID == $arFriends["FIRST_USER_ID"]) ? $arFriends["SECOND_USER_ID"] : $arFriends["FIRST_USER_ID"]);
							$arRelatedUsers[] = array("entity_id" => $entityID, "user_id" => $friendID);

							$dbFriends2 = CSocNetUserRelations::GetRelatedUsers($friendID, SONET_RELATIONS_FRIEND);
							while ($arFriends2 = $dbFriends2->Fetch())
							{
								$friendID2 = (($friendID == $arFriends2["FIRST_USER_ID"]) ? $arFriends2["SECOND_USER_ID"] : $arFriends2["FIRST_USER_ID"]);
								if ($friendID2 != $entityID)
									$arRelatedUsers[] = array("entity_id" => $entityID, "user_id" => $friendID2, "user_im_id" => $friendID);
							}
						}
						break;
					case SONET_RELATIONS_TYPE_NONE:
						$arRelatedUsers[] = array("entity_id" => $entityID, "user_id" => $entityID);
						break;
					case SONET_RELATIONS_TYPE_AUTHORIZED:
						$arRelatedUsers[] = array("entity_id" => $entityID, "user_id" => 0);
						break;
					case SONET_RELATIONS_TYPE_ALL:
						$arRelatedUsers = false;
						break;
				}
			}
			if (!empty($arRelatedUsers))
				$arRelatedUsers = array_unique($arRelatedUsers);

			$CacheRelatedUsers[SONET_ENTITY_USER][$entityID][$perm] = $arRelatedUsers;
				
			if($arRelatedUsers && is_array($arRelatedUsers))
			{
				foreach($arRelatedUsers as $arRelatedUserID)
				{
					$arFields = array(
						"ENTITY_TYPE" => SONET_ENTITY_USER,
						"ENTITY_ID" => $arRelatedUserID["entity_id"],
						"EVENT_ID" => $event_tmp,
						"USER_ID" => $arRelatedUserID["user_id"],
						"USER_ANONYMOUS" => "N"
					);
						
					if (array_key_exists("user_im_id", $arRelatedUserID))
						$arFields["USER_IM_ID"] = $arRelatedUserID["user_im_id"];
					
					if (!CSocNetEventUserView::Add($arFields))
					{
						$errorMessage = "";
						if ($e = $APPLICATION->GetException())
							$errorMessage = $e->GetString();
						if ($errorMessage == '')
							$errorMessage = GetMessage("SONET_EUV_ERROR_SET");

						$APPLICATION->ThrowException($errorMessage, "ERROR_SET");
						return false;
					}
				}
			}
			elseif($arRelatedUsers === false)
			{
				$arFields = array(
					"ENTITY_TYPE" => SONET_ENTITY_USER,
					"ENTITY_ID" => $entityID,
					"EVENT_ID" => $event_tmp,
					"USER_ID" => 0,
					"USER_ANONYMOUS" => "Y"
				);
				if (!CSocNetEventUserView::Add($arFields))
				{
					$errorMessage = "";
					if ($e = $APPLICATION->GetException())
						$errorMessage = $e->GetString();
					if ($errorMessage == '')
						$errorMessage = GetMessage("SONET_EUV_ERROR_SET");

					$APPLICATION->ThrowException($errorMessage, "ERROR_SET");
					return false;
				}
			}
		}
	}

	if ($bSetFeatures)
	{
		$arActiveFeatures = array_keys(CSocNetFeatures::GetActiveFeaturesNames(SONET_ENTITY_USER, $entityID));
		foreach ($arActiveFeatures as $feature)
		{
			CSocNetEventUserView::SetFeature(SONET_ENTITY_USER, $entityID, $feature);
		}
	}

	return true;
}