Пример #1
0
 /**
  * A very important function. Here we decide what locations we need to take, 
  * making a descision based on $_REQUEST from sales zone selector.
  * 
  * Then we normalize the selection and store to database.
  * 
  * Also this function is used in data migrator.
  */
 public static function saveSelectedTypes($typeList, $siteId)
 {
     $types = \CSaleLocation::getTypes();
     $locations = array(Location\Connector::DB_LOCATION_FLAG => array(), Location\Connector::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 = Location\LocationTable::getList(array('select' => array('ID', 'COUNTRY_ID', 'REGION_ID', 'CITY_ID', 'TYPE_ID'), 'filter' => array()));
     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']) {
             if ($allCountries || isset($typeList['COUNTRY'][$countryId])) {
                 $take = true;
                 $countryTaken = true;
             }
         }
         if ($typeId == $types['REGION']) {
             if ($allRegions && $countryTaken || isset($typeList['REGION'][$regionId]) || $noCountry && !$countryId) {
                 $take = true;
                 $regionTaken = true;
             }
         }
         if ($typeId == $types['CITY']) {
             if ($allCities && $regionTaken || isset($typeList['REGION'][$regionId]) || $noRegion && !$regionId) {
                 $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[Location\Connector::DB_LOCATION_FLAG][$id] = true;
         }
     }
     // normalize
     $class = self::CONN_ENTITY_NAME . 'Table';
     $locations[Location\Connector::DB_LOCATION_FLAG] = array_keys($locations[Location\Connector::DB_LOCATION_FLAG]);
     $locations[Location\Connector::DB_LOCATION_FLAG] = $class::normalizeLocationList($locations[Location\Connector::DB_LOCATION_FLAG]);
     // store to database
     $class::resetMultipleForOwner(strlen($siteId) ? $siteId : $class::ALL_SITES, $locations);
 }