• Модуль: main
  • Путь к файлу: ~/bitrix/modules/main/lib/ui/selector/entities.php
  • Класс: BitrixMainUISelectorEntities
  • Вызов: Entities::getLastSort
static function getLastSort($params = array())
{
	global $USER;

	$result = array(
		'DATA' => array(),
		'DATA_ADDITIONAL' => array()
	);

	$userId = (
		isset($params["USER_ID"])
		&& intval($params["USER_ID"]) > 0
			? intval($params["USER_ID"])
			: false
	);

	$contextFilter = (
		isset($params["CONTEXT_FILTER"])
		&& is_array($params["CONTEXT_FILTER"])
			? $params["CONTEXT_FILTER"]
			: false
	);

	$codeFilter = (
		$params["CODE_FILTER"] ?? false
	);

	if (
		$codeFilter
		&& !is_array($codeFilter)
	)
	{
		$codeFilter = array($codeFilter);
	}

	if (!$userId)
	{
		if ($USER->IsAuthorized())
		{
			$userId = $USER->getId();
		}
		else
		{
			return $result;
		}
	}

	$cacheTtl = defined("BX_COMP_MANAGED_CACHE") ? 3153600 : 3600*4;
	$cacheId = 'dest_sort_2'.$userId.serialize($params);
	$cacheDir = self::getCacheDir([
		'userId' => $userId,
	]);

	$cache = new CPHPCache;
	if($cache->initCache($cacheTtl, $cacheId, $cacheDir))
	{
		$cacheData = $cache->GetVars();
		$destAll = $cacheData['DEST_ALL'] ?? array();
		$dataAdditionalUsers = $cacheData['DATA_ADDITIONAL_USERS'] ?? array();
	}
	else
	{
		$dataAdditionalUsers = array();

		$cache->startDataCache();
		$filter = array(
			"USER_ID" => $userId
		);

		if (
			ModuleManager::isModuleInstalled('mail')
			&& ModuleManager::isModuleInstalled('intranet')
			&& (
				!isset($params["ALLOW_EMAIL_INVITATION"])
				|| !$params["ALLOW_EMAIL_INVITATION"]
			)
		)
		{
			$filter["!=CODE_USER.EXTERNAL_AUTH_ID"] = 'email';
		}

		if (!empty($params["CODE_TYPE"]))
		{
			$filter["=CODE_TYPE"] = mb_strtoupper($params["CODE_TYPE"]);
		}
		elseif (
			empty($params["CRM"])
			|| $params["CRM"] != 'Y'
		)
		{
			$filter["!=CODE_TYPE"] = "CRM";
		}

		if (
			is_array($contextFilter)
			&& !empty($contextFilter)
		)
		{
			$filter["CONTEXT"] = $contextFilter;
		}

		if (
			is_array($codeFilter)
			&& !empty($codeFilter)
		)
		{
			$filter["CODE"] = $codeFilter;
		}

		$runtime = array();
		$order = array();

		if (!empty($params["DEST_CONTEXT"]))
		{
			$conn = BitrixMainApplication::getConnection();
			$helper = $conn->getSqlHelper();

			$runtime = array(
				new BitrixMainEntityExpressionField('CONTEXT_SORT', "CASE WHEN CONTEXT = '".$helper->forSql(mb_strtoupper($params["DEST_CONTEXT"]))."' THEN 1 ELSE 0 END")
			);

			$order = array(
				'CONTEXT_SORT' => 'DESC'
			);
		}

		$order['LAST_USE_DATE'] = 'DESC';

		$emailUserCodeList = $emailCrmUserCodeList = array();

		if (
			ModuleManager::isModuleInstalled('mail')
			&& ModuleManager::isModuleInstalled('intranet')
			&& isset($params["ALLOW_EMAIL_INVITATION"])
			&& $params["ALLOW_EMAIL_INVITATION"]
		)
		{
			$res = FinderDestTable::getList(array(
				'order' => $order,
				'filter' => array(
					"USER_ID" => $USER->getId(),
					"=CODE_USER.EXTERNAL_AUTH_ID" => 'email',
					"=CODE_TYPE" => 'U'
				),
				'select' => array('CODE'),
				'runtime' => $runtime,
				'limit' => self::LIST_USER_LIMIT
			));
			while($dest = $res->fetch())
			{
				$emailUserCodeList[] = $dest['CODE'];
			}
			$dataAdditionalUsers['UE'] = $emailUserCodeList;
		}

		if (
			!empty($params["DEST_CONTEXT"])
			&& $params["DEST_CONTEXT"] == "CRM_POST"
		)
		{
			$res = FinderDestTable::getList(array(
				'order' => $order,
				'filter' => array(
					"USER_ID" => $USER->getId(),
					"!=CODE_USER.UF_USER_CRM_ENTITY" => false,
					"=CODE_TYPE" => 'U'
				),
				'select' => array('CODE'),
				'runtime' => $runtime,
				'limit' => self::LIST_USER_LIMIT
			));
			while($dest = $res->fetch())
			{
				$emailCrmUserCodeList[] = $dest['CODE'];
			}
			$dataAdditionalUsers['UCRM'] = $emailCrmUserCodeList;
		}

		$res = FinderDestTable::getList(array(
			'order' => $order,
			'filter' => $filter,
			'select' => array(
				'CONTEXT',
				'CODE',
				'LAST_USE_DATE'
			),
			'runtime' => $runtime
		));

		$destAll = array();

		while($dest = $res->fetch())
		{
			$dest["LAST_USE_DATE"] = MakeTimeStamp($dest["LAST_USE_DATE"]->toString());
			$destAll[] = $dest;
		}

		$cache->endDataCache(array(
			"DEST_ALL" => $destAll,
			"DATA_ADDITIONAL_USERS" => $dataAdditionalUsers
		));
	}

	$resultData = array();

	foreach ($destAll as $dest)
	{
		if(!isset($resultData[$dest["CODE"]]))
		{
			$resultData[$dest["CODE"]] = array();
		}

		$contextType = (
			isset($params["DEST_CONTEXT"])
			&& mb_strtoupper($params["DEST_CONTEXT"]) == mb_strtoupper($dest["CONTEXT"])
				? "Y"
				: "N"
		);

		if (
			$contextType == "Y"
			|| !isset($resultData[$dest["CODE"]]["N"])
			|| $dest["LAST_USE_DATE"] > $resultData[$dest["CODE"]]["N"]
		)
		{
			$resultData[$dest["CODE"]][$contextType] = $dest["LAST_USE_DATE"];
		}
	}

	$result['DATA'] = $resultData;
	$result['DATA_ADDITIONAL'] = $dataAdditionalUsers;

	return $result;
}