• Модуль: sale
  • Путь к файлу: ~/bitrix/modules/sale/lib/delivery/services/manager.php
  • Класс: BitrixSaleDeliveryServicesManager
  • Вызов: Manager::getActiveList
static function getActiveList($calculatingOnly = false, $restrictedIds = null)
{
	//If we alredy got all active services
	static $isDataPrepared = false;
	static $activeIds = array();
	static $canHasProfiles = array();
	static $canHasChildren = array();
	static $DontHaveRestrictionsIds = array();

	if(is_array($restrictedIds))
	{
		$unPreparedRestrictedIds = array_diff_key(
			array_flip(
				$restrictedIds
			),
			self::$cachedFields
		);

		$unPreparedRestrictedIds = array_keys($unPreparedRestrictedIds);
	}
	else
	{
		$unPreparedRestrictedIds = array();
	}

	if(!$isDataPrepared || !empty($unPreparedRestrictedIds))
	{
		$result = array();
		self::initHandlers();

		$filter = array(
			'=ACTIVE' => 'Y'
		);

		$params =  array(
			'order' => array('SORT' =>'ASC', 'NAME' => 'ASC'),
			'filter' => $filter
		);

		$params['runtime'] = array(
			new BitrixMainEntityExpressionField(
				'RESTRICTIONS_EXIST',
				'CASE WHEN EXISTS('.
				'SELECT 1 FROM b_sale_service_rstr SSR WHERE SSR.SERVICE_ID = %s AND SSR.SERVICE_TYPE = '.RestrictionsManager::SERVICE_TYPE_SHIPMENT.') THEN 1 ELSE 0 END',
				'ID'
			)
		);

		$params['select'] = array('*', 'RESTRICTIONS_EXIST');

		if(!empty($unPreparedRestrictedIds))
		{
			if($isDataPrepared)
			{
				$params['filter']['ID'] = $unPreparedRestrictedIds;
			}
			else
			{
				$params['filter'][] = array(
					"LOGIC" => "OR",
					"ID" => $unPreparedRestrictedIds,
					'RESTRICTIONS_EXIST' => 0
				);
			}
		}

		$dbRes = Table::getList($params);
		$profilesParentsIds = array();

		while($service = $dbRes->fetch())
		{
			if(!is_subclass_of($service["CLASS_NAME"], 'BitrixSaleDeliveryServicesBase'))
				continue;

			if(!$service['RESTRICTIONS_EXIST'])
				$DontHaveRestrictionsIds[] = $service['ID'];

			if($service["CLASS_NAME"]::canHasChildren())
				$canHasChildren[$service["ID"]] = true;

			if ($service['CLASS_NAME']::canHasProfiles())
				$canHasProfiles[$service["ID"]] = true;

			if($service["PARENT_ID"] != 0)
				$profilesParentsIds[$service["PARENT_ID"]] = $service['ID'];

			$result[$service["ID"]] = $service;

			if($service['ACTIVE'] == 'Y')
				$activeIds[$service["ID"]] = $service["ID"];
		}

		foreach(array_diff_key($canHasProfiles, $profilesParentsIds) as $id => $tmp)
			unset($result[$id]);

		self::$cachedFields = self::$cachedFields + $result;
		RestrictionsManager::prepareData(array_keys($result));
		ExtraServicesManager::prepareData(array_keys($result));

		//selected all active
		if(!$isDataPrepared && empty($unPreparedRestrictedIds))
			$isDataPrepared = true;
	}

	if(is_array($restrictedIds) && !empty($restrictedIds))
		$storedIds = array_diff($restrictedIds, $unPreparedRestrictedIds);
	else
		$storedIds = array();

	if(!empty($storedIds) && is_array($storedIds))
	{
		foreach($storedIds as $storedId)
		{
			if(empty(self::$cachedFields[$storedId]))
				continue;

			$service = self::$cachedFields[$storedId];

			if(!class_exists($service["CLASS_NAME"]))
				continue;

			if($service["CLASS_NAME"]::canHasChildren())
				$canHasChildren[$storedId] = true;

			if ($service['CLASS_NAME']::canHasProfiles())
				$canHasProfiles[$storedId] = true;

			if($service['ACTIVE'] == 'Y')
				$activeIds[$storedId] = $storedId;
		}
	}

	$active = array_intersect_key(self::$cachedFields, array_flip($activeIds));

	if(is_array($restrictedIds))
	{
		$result = array_intersect_key($active, array_flip($DontHaveRestrictionsIds));

		if(!empty($restrictedIds))
			$result = $result + array_intersect_key($active, array_flip($restrictedIds));
	}
	else
	{
		$result = $active;
	}

	/*
	 * Clean children if parent is not present in result.
	 * We mean that it doesn't pass restrictions checks.
	 * Or it is not active.
	 */

	foreach($result as $id => $fields)
	{
		if(intval($fields['PARENT_ID']) <= 0)
			continue;

		if(empty($result[$fields['PARENT_ID']]))
			unset($result[$id]);
	}

	if($calculatingOnly)
		$result = array_diff_key($result, $canHasChildren, $canHasProfiles);

	if(!empty($result))
		sortByColumn($result, array("SORT" => SORT_ASC, "NAME" => SORT_ASC), '', null, true);

	return $result;
}