Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
saleszone.php
1<?php
2namespace Bitrix\Sale;
3
9{
10 const CONN_ENTITY_NAME = 'Bitrix\Sale\Location\SiteLocation';
11 const LOCATION_ENTITY_NAME = 'Bitrix\Sale\Location\Location';
12
13 static $zoneCache = array(); // functionality is called hell number of times outside, so a little cache is provided
14
19 public static function getAllRegions($lang)
20 {
21 static $result = null;
22
23 if($result === null)
24 {
25 $result = array();
26 $dbRegionList = \CSaleLocation::GetRegionList(array(), array(), $lang);
27
28 while ($arRegion = $dbRegionList->fetch())
29 $result[$arRegion["ID"]] = $arRegion["NAME_LANG"];
30 }
31
32 return $result;
33 }
34
39 public static function getAllCities($lang)
40 {
41 static $result = null;
42
43 if($result === null)
44 {
45 $result = array();
46 $dbCityList = \CSaleLocation::GetCityList(array(), array(), $lang);
47 while($arCity = $dbCityList->fetch())
48 $result[$arCity["ID"]] = $arCity["NAME_LANG"];
49 }
50
51 return $result;
52 }
53
60 public static function checkCountryId($countryId, $siteId)
61 {
62 if($siteId == '')
63 return false;
64
65 $cIds = static::getCountriesIds($siteId);
66 return !$cIds || in_array($countryId, $cIds) || in_array("", $cIds);
67 }
68
75 public static function checkRegionId($regionId, $siteId)
76 {
77 if($siteId == '')
78 return false;
79
80 $rIds = static::getRegionsIds($siteId);
81 return !$rIds || in_array($regionId, $rIds) || in_array("", $rIds);
82 }
83
90 public static function checkCityId($cityId, $siteId)
91 {
92 if($siteId == '')
93 return false;
94
95 $cIds = static::getCitiesIds($siteId);
96 return !$cIds || in_array($cityId, $cIds) || in_array("", $cIds);
97 }
98
105 public static function checkLocationId($locationId, $siteId)
106 {
107 if(\CSaleLocation::isLocationProMigrated())
108 {
109 if(!intval($locationId) || !mb_strlen($siteId))
110 return false;
111
112 return Location\SiteLocationTable::checkConnectionExists($siteId, $locationId);
113 }
114 else
115 {
116
117 $result = false;
118
119 $arLocation = \CSaleLocation::GetByID($locationId);
120
121 if(static::checkCountryId($arLocation["COUNTRY_ID"], $siteId)
122 && static::checkRegionId($arLocation["REGION_ID"], $siteId)
123 && static::checkCityId($arLocation["CITY_ID"], $siteId)
124 )
125 {
126 $result = true;
127 }
128
129 return $result;
130
131 }
132 }
133
134 private static function checkLocationIsInLinkedPart($locationId, $siteId)
135 {
136 $types = \CSaleLocation::getTypes();
137 $class = self::CONN_ENTITY_NAME.'Table';
138
139 if(!$class::checkLinkUsageAny($siteId))
140 return true;
141
142 if((string) $locationId == '')
143 return false;
144
145 $node = \Bitrix\Sale\Location\LocationTable::getList(array(
146 'filter' => array('=ID' => $locationId),
147 'select' => array('ID', 'LEFT_MARGIN', 'RIGHT_MARGIN')
148 ))->fetch();
149 if(!is_array($node))
150 return false;
151
152 $stat = $class::getLinkStatusForMultipleNodes(array($node), $siteId);
153
154 return $stat[$node['ID']] !== $class::LSTAT_IN_NOT_CONNECTED_BRANCH;
155 }
156
157 public static function setSelectedIds($siteId, $ids)
158 {
159 static::$zoneCache[$siteId] = $ids;
160 }
161
162 public static function getSelectedIds($siteId)
163 {
164 $typesAll = \CSaleLocation::getTypes();
165
166 $types = array();
167 if(isset($typesAll['COUNTRY']))
168 $types[] = "'".intval($typesAll['COUNTRY'])."'";
169 if(isset($typesAll['REGION']))
170 $types[] = "'".intval($typesAll['REGION'])."'";
171 if(isset($typesAll['CITY']))
172 $types[] = "'".intval($typesAll['CITY'])."'";
173
174 $typesAll = array_flip($typesAll);
175
176 if((string) $siteId != '' && \Bitrix\Sale\Location\SiteLocationTable::checkLinkUsageAny($siteId) && !empty($types))
177 {
178 $result = array();
179
180 $sql = \Bitrix\Sale\Location\SiteLocationTable::getConnectedLocationsSql(
181 $siteId,
182 array('select' => array('ID', 'LEFT_MARGIN', 'RIGHT_MARGIN')),
183 array('GET_LINKED_THROUGH_GROUPS' => true)
184 );
185
186 if((string) $sql != '')
187 {
188 $res = $GLOBALS['DB']->query("
189
190 select SL.ID, SL.TYPE_ID from b_sale_location SL
191 inner join (
192 ".$sql."
193 ) as TT on
194 (
195 (
196 (
197 SL.LEFT_MARGIN >= TT.LEFT_MARGIN
198 and
199 SL.RIGHT_MARGIN <= TT.RIGHT_MARGIN
200 )
201 or
202 (
203 SL.LEFT_MARGIN <= TT.LEFT_MARGIN
204 and
205 SL.RIGHT_MARGIN >= TT.RIGHT_MARGIN
206 )
207 )
208 and
209 SL.TYPE_ID in (".implode(', ', $types).")
210 )
211
212 group by SL.ID
213 ");
214
215 while($item = $res->fetch())
216 {
217 $typeId = $item['TYPE_ID'];
218 unset($item['TYPE_ID']);
219
220 $result[$typesAll[$typeId]][$item['ID']] = $item['ID'];
221 }
222
223 // special case: when all types are actually selected, an empty string ('') SHOULD be present among $index[$siteId][$type]
224
225 $res = Location\LocationTable::getList(array(
226 'filter' => array(
227 'TYPE_ID' => $types
228 ),
229 'runtime' => array(
230 'CNT' => array(
231 'data_type' => 'integer',
232 'expression' => array('COUNT(*)')
233 )
234 ),
235 'select' => array(
236 'CNT',
237 'TYPE_ID'
238 )
239 ));
240 while($item = $res->fetch())
241 {
242 if(intval($item['TYPE_ID']))
243 {
244 $typeCode = $typesAll[$item['TYPE_ID']];
245 if(isset($result[$typeCode]) && $item['CNT'] == count($result[$typeCode]))
246 {
247 $result[$typeCode][] = '';
248 }
249 }
250 }
251 }
252
253 return $result;
254 }
255 else
256 {
257 return array(
258 'COUNTRY' => array(''),
259 'REGION' => array(''),
260 'CITY' => array(''),
261 ); // means "all"
262 }
263 }
264
265 // returns a list of IDs of locations that are linked with $siteId and have type of $type
266 private static function getSelectedTypeIds($type, $siteId)
267 {
268 $index =& static::$zoneCache;
269
270 if(!isset($index[$siteId]))
271 {
272 $result = array();
273
274 $index[$siteId] = static::getSelectedIds($siteId);
275 }
276
277 return $index[$siteId][$type];
278 }
279
284 public static function getCitiesIds($siteId)
285 {
286 if(\CSaleLocation::isLocationProMigrated())
287 return self::getSelectedTypeIds('CITY', $siteId);
288
289 return explode(":" , \COption::GetOptionString('sale', 'sales_zone_cities', '', $siteId));
290 }
291
296 public static function getRegionsIds($siteId)
297 {
298 if(\CSaleLocation::isLocationProMigrated())
299 return self::getSelectedTypeIds('REGION', $siteId);
300
301 return explode(":" , \COption::GetOptionString('sale', 'sales_zone_regions', '', $siteId));
302 }
303
308 public static function getCountriesIds($siteId)
309 {
310 if(\CSaleLocation::isLocationProMigrated())
311 return self::getSelectedTypeIds('COUNTRY', $siteId);
312
313 return explode(":" , \COption::GetOptionString('sale', 'sales_zone_countries', '', $siteId));
314 }
315
324 public static function saveSelectedTypes($typeList, $siteId)
325 {
326 $types = \CSaleLocation::getTypes();
327
328 $locations = array(
329 Location\Connector::DB_LOCATION_FLAG => array(),
330 Location\Connector::DB_GROUP_FLAG => array()
331 );
332 if(is_array($typeList['COUNTRY']) && !empty($typeList['COUNTRY']))
333 $typeList['COUNTRY'] = array_flip($typeList['COUNTRY']);
334
335 if(is_array($typeList['REGION']) && !empty($typeList['REGION']))
336 $typeList['REGION'] = array_flip($typeList['REGION']);
337
338 if(is_array($typeList['CITY']) && !empty($typeList['CITY']))
339 $typeList['CITY'] = array_flip($typeList['CITY']);
340
341 $allCountries = isset($typeList['COUNTRY']['']);
342 $allRegions = isset($typeList['REGION']['']);
343 $allCities = isset($typeList['CITY']['']);
344
345 // no countries
346 $noCountry = isset($typeList['COUNTRY']['NULL']);
347 $noRegion = isset($typeList['REGION']['NULL']);
348
349 // make up list of ids
350 $res = Location\LocationTable::getList(array('select' => array(
351 'ID',
352 'COUNTRY_ID',
353 'REGION_ID',
354 'CITY_ID',
355 'TYPE_ID',
356 //'LNAME' => 'NAME.NAME'
357 ), 'filter' => array(
358 //'=NAME.LANGUAGE_ID' => LANGUAGE_ID
359 )));
360 while($item = $res->fetch())
361 {
362 $id = $item['ID'];
363 $countryId = intval($item['COUNTRY_ID']);
364 $regionId = intval($item['REGION_ID']);
365 $cityId = intval($item['CITY_ID']);
366 $typeId = intval($item['TYPE_ID']);
367
368 $take = false;
369 $countryTaken = false;
370 $regionTaken = false;
371
372 if($typeId == $types['COUNTRY']) // it is a country
373 {
374 if($allCountries // we take all countries
375 || // or..
376 isset($typeList['COUNTRY'][$countryId]) // we manually selected this country
377 )
378 {
379 $take = true;
380 $countryTaken = true;
381 }
382 }
383
384 if($typeId == $types['REGION']) // it is a region
385 {
386 if((
387 $allRegions // we take all regions (of selected countries)
388 && // and
389 $countryTaken // country is selected already
390 )
391 || // or ..
392 isset($typeList['REGION'][$regionId]) // we manually selected this region
393 || // or ..
394 ($noCountry && !$countryId) // we also accept regions without countries, and this region actually dont have a country
395 )
396 {
397 $take = true;
398 $regionTaken = true;
399 }
400 }
401
402 if($typeId == $types['CITY']) // it is a city
403 {
404 if((
405 $allCities // we take all cities (of selected regions of selected countries)
406 && // and
407 $regionTaken // region is selected already
408 )
409 || // or..
410 isset($typeList['REGION'][$regionId]) // we manually selected this city
411 || // or ..
412 ($noRegion && !$regionId) // we also accept cities without regions, and this city actually dont have a region
413 // it seems that cities without a country is not supported with the current logic
414 )
415 {
416 $take = true;
417 }
418 }
419
420 if(isset($typeList['CITY'][$cityId]) && $typeId == $types['CITY']) // this is a city and it is in list - take it
421 $take = true;
422
423 if($take)
424 $locations[Location\Connector::DB_LOCATION_FLAG][$id] = true;
425 }
426
427 // normalize
428 $class = self::CONN_ENTITY_NAME.'Table';
429 $locations[Location\Connector::DB_LOCATION_FLAG] = array_keys($locations[Location\Connector::DB_LOCATION_FLAG]);
430
431 $locations[Location\Connector::DB_LOCATION_FLAG] = $class::normalizeLocationList($locations[Location\Connector::DB_LOCATION_FLAG]);
432
433 // store to database
434 $class::resetMultipleForOwner($siteId <> ''? $siteId : $class::ALL_SITES, $locations);
435 }
436
443 public static function makeSearchFilter($object, $siteId)
444 {
445 $result = array();
446
447 $countries = static::getCountriesIds($siteId);
448 $regions = static::getRegionsIds($siteId);
449 $cities = static::getCitiesIds($siteId);
450
451 if(!in_array("", $cities) && $object == "city")
452 $result = array("CITY_ID" => $cities);
453 elseif(!in_array("", $regions) && ($object == "city" || $object == "region"))
454 $result = array("REGION_ID" => $regions);
455 elseif(!in_array("", $countries))
456 $result = array("COUNTRY_ID" => $countries);
457
458 return $result;
459 }
460
466 public static function getRegions($countriesIds = array(), $lang = LANGUAGE_ID)
467 {
468
469 $regions = array();
470 $regionsList = static::getAllRegions($lang);
471 $getCountryNull = in_array("NULL", $countriesIds);
472 $filter = in_array("", $countriesIds) ? array() : array(($getCountryNull ? "+" : "")."COUNTRY_ID" => $countriesIds);
473
474 $dbLocationsList = \CSaleLocation::GetList(
475 array(),
476 $filter,
477 array("REGION_ID", "COUNTRY_ID")
478 );
479
480 while($arRegion = $dbLocationsList->GetNext())
481 {
482 if($arRegion["REGION_ID"] <> '' && $arRegion["REGION_ID"] != "0")
483 $regions[$arRegion["REGION_ID"]] = $regionsList[$arRegion["REGION_ID"]];
484 }
485
486 return $regions;
487 }
488
495 public static function getCities($countriesIds = array(), $regionsIds = array(), $lang )
496 {
497 $cities = array();
498 $citiesList = static::getAllCities($lang);
499 $getRegionNull = in_array("NULL", $regionsIds);
500 $getRegionAll = in_array("", $regionsIds);
501 $getCountryNull = in_array("NULL", $countriesIds);
502 $getCountryAll = in_array("", $countriesIds);
503
504 $filter = in_array("", $regionsIds) ? array() : array(($getRegionNull ? "+" : "")."REGION_ID" => $regionsIds);
505
506 foreach($countriesIds as $countryId)
507 {
508 if(($getRegionNull || $getRegionAll) && !$getCountryAll)
509 $filter[($getCountryNull ? "+" : "")."COUNTRY_ID"] = $countryId;
510
511 $dbLocationsList = \CSaleLocation::GetList(
512 array(),
513 $filter,
514 array("CITY_ID")
515 );
516
517 while($arCity = $dbLocationsList->GetNext())
518 if($arCity["CITY_ID"] <> '')
519 $cities[$arCity["CITY_ID"]] = $citiesList[$arCity["CITY_ID"]];
520 }
521
522 return $cities;
523 }
524}
static getRegions($countriesIds=array(), $lang=LANGUAGE_ID)
static saveSelectedTypes($typeList, $siteId)
static setSelectedIds($siteId, $ids)
static getSelectedIds($siteId)
static getCities($countriesIds=array(), $regionsIds=array(), $lang)
static getAllCities($lang)
Definition saleszone.php:39
static getCitiesIds($siteId)
static checkCityId($cityId, $siteId)
Definition saleszone.php:90
static checkRegionId($regionId, $siteId)
Definition saleszone.php:75
static checkLocationId($locationId, $siteId)
static makeSearchFilter($object, $siteId)
static getCountriesIds($siteId)
static checkCountryId($countryId, $siteId)
Definition saleszone.php:60
static getRegionsIds($siteId)
static getAllRegions($lang)
Definition saleszone.php:19
$GLOBALS['____1444769544']
Definition license.php:1