• Модуль: sale
  • Путь к файлу: ~/bitrix/modules/sale/lib/saleszone.php
  • Класс: BitrixSaleSalesZone
  • Вызов: SalesZone::saveSelectedTypes
static function saveSelectedTypes($typeList, $siteId)
{
	$types = CSaleLocation::getTypes();

	$locations = array(
		LocationConnector::DB_LOCATION_FLAG => array(),
		LocationConnector::DB_GROUP_FLAG => array()
	);
	if(is_array($typeList['COUNTRY']) && !empty($typeList['COUNTRY']))
		$typeList['COUNTRY'] = array_flip($typeList['COUNTRY']);

	if(is_array($typeList['REGION']) && !empty($typeList['REGION']))
		$typeList['REGION'] = array_flip($typeList['REGION']);

	if(is_array($typeList['CITY']) && !empty($typeList['CITY']))
		$typeList['CITY'] = array_flip($typeList['CITY']);

	$allCountries = isset($typeList['COUNTRY']['']);
	$allRegions = isset($typeList['REGION']['']);
	$allCities = isset($typeList['CITY']['']);

	// no countries
	$noCountry = isset($typeList['COUNTRY']['NULL']);
	$noRegion = isset($typeList['REGION']['NULL']);

	// make up list of ids
	$res = LocationLocationTable::getList(array('select' => array(
		'ID',
		'COUNTRY_ID',
		'REGION_ID',
		'CITY_ID',
		'TYPE_ID',
		//'LNAME' => 'NAME.NAME'
	), 'filter' => array(
		//'=NAME.LANGUAGE_ID' => LANGUAGE_ID
	)));
	while($item = $res->fetch())
	{
		$id = $item['ID'];
		$countryId = intval($item['COUNTRY_ID']);
		$regionId = intval($item['REGION_ID']);
		$cityId = intval($item['CITY_ID']);
		$typeId = intval($item['TYPE_ID']);

		$take = false;
		$countryTaken = false;
		$regionTaken = false;

		if($typeId == $types['COUNTRY']) // it is a country
		{
			if($allCountries // we take all countries
				|| // or..
				isset($typeList['COUNTRY'][$countryId]) // we manually selected this country
			)
			{
				$take = true;
				$countryTaken = true;
			}
		}

		if($typeId == $types['REGION']) // it is a region
		{
			if((
					$allRegions // we take all regions (of selected countries)
					&& // and
					$countryTaken // country is selected already
				)
				|| // or ..
				isset($typeList['REGION'][$regionId]) // we manually selected this region
				|| // or ..
				($noCountry && !$countryId) // we also accept regions without countries, and this region actually dont have a country
			)
			{
				$take = true;
				$regionTaken = true;
			}
		}

		if($typeId == $types['CITY']) // it is a city
		{
			if((
					$allCities // we take all cities (of selected regions of selected countries)
					&& // and
					$regionTaken // region is selected already
				)
				|| // or..
				isset($typeList['REGION'][$regionId]) // we manually selected this city
				|| // or ..
				($noRegion && !$regionId) // we also accept cities without regions, and this city actually dont have a region
				// it seems that cities without a country is not supported with the current logic
			)
			{
				$take = true;
			}
		}

		if(isset($typeList['CITY'][$cityId]) && $typeId == $types['CITY']) // this is a city and it is in list - take it
			$take = true;

		if($take)
			$locations[LocationConnector::DB_LOCATION_FLAG][$id] = true;
	}

	// normalize
	$class = self::CONN_ENTITY_NAME.'Table';
	$locations[LocationConnector::DB_LOCATION_FLAG] = array_keys($locations[LocationConnector::DB_LOCATION_FLAG]);

	$locations[LocationConnector::DB_LOCATION_FLAG] = $class::normalizeLocationList($locations[LocationConnector::DB_LOCATION_FLAG]);

	// store to database
	$class::resetMultipleForOwner($siteId <> ''? $siteId : $class::ALL_SITES, $locations);
}