• Модуль: sale
  • Путь к файлу: ~/bitrix/modules/sale/lib/location/geoip.php
  • Класс: BitrixSaleLocationGeoIp
  • Вызов: GeoIp::specifyLocationByParents
static function specifyLocationByParents(Data $geoData, array $locations, $lang)
{
	if(empty($locations))
	{
		return [];
	}

	if(self::isParentsEmpty($geoData))
	{
		reset($locations);
		return current($locations);
	}

	$marginConditions = [
		'LOGIC' => 'OR'
	];

	foreach($locations as $location)
	{
		$marginConditions[] = [
			'LOGIC' => 'AND',
			' $location['LEFT_MARGIN'],
			'>RIGHT_MARGIN' => $location['RIGHT_MARGIN']
		];
	}

	$params = [
		'filter' => [
			$marginConditions,
			'NAME.LANGUAGE_ID' => $lang,
		],
		'select' => [
			'ID', 'LEFT_MARGIN', 'RIGHT_MARGIN',
			'LOCATION_NAME_UPPER' => 'NAME.NAME_UPPER'
		]
	];

	$res = BitrixSaleLocationLocationTable::getList($params);
	$weight = [];
	$result = [];
	$normalizer = self::getNameNormalizer($lang);
	$country = $normalizer->normalize($geoData->countryName);
	$region = $normalizer->normalize($geoData->regionName);
	$subRegion = $normalizer->normalize($geoData->subRegionName);

	while($loc = $res->fetch())
	{
		$isNameMatched = self::isNormalizedNamesMatched(
			$normalizer->normalize($loc['LOCATION_NAME_UPPER']),
			$country,
			$region,
			$subRegion
		);

		if($isNameMatched)
		{
			$locationIds = self::getLocationIdsByMargins($locations, $loc['LEFT_MARGIN'], $loc['RIGHT_MARGIN']);

			foreach($locationIds as $locationId)
			{
				if(!isset($locationId))
				{
					$weight[$locationId] = 0;
				}

				$weight[$locationId]++;
			}
		}
	}

	if(!empty($weight))
	{
		arsort($weight);
		reset($weight);
		$id = key($weight);

		if(isset($locations[$id]))
		{
			$result = $locations[$id];
		}
	}

	return $result;
}