Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
datainstaller.php
1<?php
2
4
14
16{
17 private const DEFAULT_DATA_PATH = '/bitrix/modules/location/data';
18
22 public static function installAreasAgent(): string
23 {
24 $agent = '\\Bitrix\\Location\\Infrastructure\\DataInstaller::installAreasAgent();';
25
26 $connection = Application::getInstance()->getConnection();
27
28 if (!$connection->isTableExists(AreaTable::getTableName()))
29 {
30 return $agent;
31 }
32
33 $result = self::installAreas();
34 if (!$result->isSuccess())
35 {
36 return $agent;
37 }
38
39 return '';
40 }
41
45 private static function installAreas(): Result
46 {
47 $result = new Result();
48
49 $dataPath = Application::getDocumentRoot() . self::DEFAULT_DATA_PATH;
50 if (!IO\Directory::isDirectoryExists($dataPath))
51 {
52 return $result->addError(new Error('Default data directory not found'));
53 }
54
55 $areasFile = new IO\File(Path::combine($dataPath, 'areas.php'));
56 if (!$areasFile->isExists())
57 {
58 return $result->addError(new Error('File with areas not found'));
59 }
60
61 $areas = include $areasFile->getPath();
62 if (!is_array($areas))
63 {
64 return $result->addError(new Error('No data in areas file'));
65 }
66
67 $areaRepository = new AreaRepository();
68 foreach ($areas as $area)
69 {
70 $existingArea = $areaRepository->findByTypeAndCode($area['TYPE'], $area['CODE']);
71 if ($existingArea)
72 {
73 continue;
74 }
75
76 $areaFile = new IO\File(Path::combine($dataPath, 'areas', $area['FILE']));
77 if (!$areaFile->isExists())
78 {
79 $result->addError(new Error('Area file not found'));
80 continue;
81 }
82
83 $addResult = $areaRepository->store(
84 (new Area())
85 ->setType($area['TYPE'])
86 ->setCode($area['CODE'])
87 ->setSort($area['SORT'])
88 ->setGeometry(
89 Manager::makeConverter(Manager::FORMAT_GEOJSON)
90 ->read($areaFile->getContents())
91 )
92 );
93
94 if (!$addResult->isSuccess())
95 {
96 $result->addErrors($addResult->getErrors());
97 }
98 }
99
100 return $result;
101 }
102}
static isDirectoryExists($path)