51 return $scope === LOCATION_SEARCH_SCOPE_ALL || $scope === LOCATION_SEARCH_SCOPE_INTERNAL;
55 public function findByExternalId(
string $externalId,
string $sourceCode,
string $languageId)
62 $result = $this->createQuery($languageId)
63 ->addFilter(
'=EXTERNAL_ID', $externalId)
64 ->addFilter(
'=SOURCE_CODE', $sourceCode)
69 $result = \Bitrix\Location\Entity\Location\Converter\OrmConverter::createLocation($result, $languageId);
77 public function findById(
int $id,
string $languageId)
85 $res = $this->createQuery($languageId)
86 ->addFilter(
'=ID', $id)
91 $result = \Bitrix\Location\Entity\Location\Converter\OrmConverter::createLocation($res, $languageId);
107 $text = Builder::build($languageId)->normalize($text);
109 $result = $this->createQuery($languageId)
110 ->addFilter(
'%NAME.NAME_NORMALIZE', $text)
113 return \Bitrix\Location\Entity\Location\Converter\OrmConverter::createCollection($result, $languageId);
119 if($location->
getId() <= 0)
124 $ormCollection = $this->hierarchyTable::getList([
126 '=DESCENDANT_ID' => $location->
getId(),
127 '=ANCESTOR.NAME.LANGUAGE_ID' => $languageId,
129 'order' => [
'LEVEL' =>
'ASC'],
135 ])->fetchCollection();
137 $result = \Bitrix\Location\Entity\Location\Converter\OrmConverter::createParentCollection($ormCollection, $languageId);
138 $result->setDescendant($location);
146 $query = $this->locationTable::query()
152 if($res = $query->fetch())
154 $result = [(int)$res[
'ID'], (
string)$res[
'CODE']];
162 return sprintf(
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
163 mt_rand(0, 0xffff), mt_rand(0, 0xffff),
165 mt_rand(0, 0x0fff) | 0x4000,
166 mt_rand(0, 0x3fff) | 0x8000,
167 mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
176 $isNewLocation =
false;
179 if($location->
getId() <= 0)
185 $location->
setId($locationId);
186 $location->
setCode($locationCode);
194 $fields = Location\Converter\DbFieldConverter::convertToDbFields($location);
196 if($location->
getId() > 0)
198 $result = $this->locationTable::update($location->
getId(), $fields);
202 $result = $this->locationTable::add($fields);
203 $isNewLocation =
true;
205 if($result->isSuccess())
207 $location->
setId($result->getId());
211 if($result->isSuccess())
213 $res = $this->saveName($location, $isNewLocation);
215 if(!$res->isSuccess())
217 $result->addErrors($res->getErrors());
220 $res = $this->saveFields($location);
222 if(!$res->isSuccess())
224 $result->addErrors($res->getErrors());
231 private function saveFields(
Location $location)
233 if($location->
getId() <= 0)
238 $fieldsCollection = Location\Converter\OrmConverter::convertFieldsToOrm($location);
240 $this->fieldTable::deleteByLocationId($location->
getId());
241 return $fieldsCollection->save();
244 private function saveName(Location $location,
bool $isLocationNew)
246 $fields = Location\Converter\DbFieldConverter::convertNameToDbFields($location);
251 $itemExist = $this->locationNameTable::getById([
252 'LOCATION_ID' => $fields[
'LOCATION_ID'],
253 'LANGUAGE_ID' => $fields[
'LANGUAGE_ID']]
259 $result = $this->locationNameTable::update([
260 'LOCATION_ID' => $fields[
'LOCATION_ID'],
261 'LANGUAGE_ID' => $fields[
'LANGUAGE_ID']
266 $result = $this->locationNameTable::add($fields);
275 $id = $location->getId();
282 $res = $this->addressTable::getList([
283 'filter' => [
'LOCATION_ID' => (
int)$id]
286 if($row = $res->fetch())
296 $result = $this->locationTable::delete($id);
298 if($result->isSuccess())
300 $this->locationNameTable::deleteByLocationId($id);
301 $this->hierarchyTable::deleteByLocationId($id);
302 $this->fieldTable::deleteByLocationId($id);
318 if($parents->
count() <= 0)
336 foreach($items as $level => $parentLocation)
338 if($parentLocation->getId() <= 0)
340 $res = $parentLocation->save();
342 if(!$res->isSuccess())
344 $result->addErrors($res->getErrors());
351 'ANCESTOR_ID' => (int)$parentLocation->getId(),
352 'LEVEL' => (int)$level,
358 $this->hierarchyTable::insertBatch($data);
364 private function createQuery(
string $languageId)
366 return $this->locationTable::query()
367 ->addFilter(
'=NAME.LANGUAGE_ID', $languageId)
370 ->addSelect(
'FIELDS');
static getMessage($code, $replace=null, $language=null)