Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
exporttreeukrain.php
1<?php
10
11use Bitrix\Main;
14
16{
17 public static function getFilePath()
18 {
19 return __FILE__;
20 }
21
22 public static function getTableName()
23 {
24 return 'b_tmp_export_tree_ukrain';
25 }
26
27 protected $settlementParent = array();
28 protected $types = false;
29
30 protected $typeMap = array(
31 1 => 'CITY', //| місто | город |
32 2 => 'VILLAGE', //| смт | пгт |
33 3 => 'VILLAGE', //| селище | поселок |
34 4 => 'VILLAGE', //| село | село |
35 5 => 'VILLAGE', //| хутір | хутор |
36 6 => 'VILLAGE', //| ст. | ст. |
37 7 => 'VILLAGE', //| санат. | санат. |
38 8 => 'VILLAGE', //| радгосп | совхоз |
39 9 => 'VILLAGE', //| вокзал | вокзал |
40 10 => 'VILLAGE', //| лісництво | лесничество |
41 11 => 'VILLAGE', //| док | док |
42 12 => 'VILLAGE', //| поселення | поселение |
43 );
44
45 public function getMappedType($typeId)
46 {
47 $dbConnection = Main\HttpApplication::getConnection();
48
49 if($this->types == false)
50 {
51 $res = $dbConnection->query('select ID, NAME, NAME_RU from b_tmp_ukrain_settlement_type');
52 while($item = $res->fetch())
53 {
54 $this->types[$item['ID']] = array('NAME' => array(
55 'ua' => array('NAME' => $item['NAME']),
56 'ru' => array('NAME' => $item['NAME_RU'])
57 ));
58 }
59 }
60
61 return $this->typeMap[$typeId];
62 }
63
64 public function addNode($data)
65 {
66 $data['LANGNAMES'] = serialize($data['NAME']);
67 $data['NAME'] = $data['NAME']['ru']['NAME'];
68 $data['CODE'] = $this->formatCode($this->exportOffset);
69
70 $data['SYS_CODE'] = 'U_'.intval($data['ID']);
71 unset($data['ID']);
72
73 if(isset($data['ZIP']))
74 {
75 $data['EXTERNALS'] = serialize(array(
76 'ZIP' => $data['ZIP']
77 ));
78 }
79
80 $res = self::add($data);
81 if($res->isSuccess())
82 {
83 $this->exportOffset++;
84 return $data['CODE'];
85 }
86
87 return false;
88 }
89
90 public function addRegion($params)
91 {
92 $dbConnection = Main\HttpApplication::getConnection();
93 $item = $dbConnection->query("select NAME, NAME_RU from b_tmp_ukrain_region where ID = '".intval($params['ID'])."'")->fetch();
94
95 return $this->addNode(array(
96 'ID' => $params['ID'],
97 'TYPE_CODE' => 'REGION',
98 'PARENT_CODE' => $params['PARENT_CODE'],
99 'NAME' => $this->getNames($params['ID'], 'REGION')
100 ));
101 }
102
103 public function addArea($params)
104 {
105 $dbConnection = Main\HttpApplication::getConnection();
106 $item = $dbConnection->query("select NAME, NAME_RU from b_tmp_ukrain_area where ID = '".intval($params['ID'])."'")->fetch();
107
108 return $this->addNode(array(
109 'ID' => $params['ID'],
110 'TYPE_CODE' => 'AREA',
111 'PARENT_CODE' => $params['PARENT_CODE'],
112 'NAME' => $this->getNames($params['ID'], 'AREA')
113 ));
114 }
115
116 public function getNames($id, $type)
117 {
118 $dbConnection = Main\HttpApplication::getConnection();
119
120 switch($type)
121 {
122 case 'REGION':
123 $table = 'b_tmp_ukrain_region';
124 break;
125 case 'AREA':
126 $table = 'b_tmp_ukrain_area';
127 break;
128 case 'CITY':
129 $table = 'b_tmp_ukrain_city';
130 break;
131 case 'VILLAGE':
132 $table = 'b_tmp_ukrain_village';
133 break;
134 }
135
136 $item = $dbConnection->query("select NAME, NAME_RU from ".$table." where ID = '".intval($id)."'")->fetch();
137
138 $replaceFrom = array('обл.', 'р-н');
139 $replaceTo = array('область', 'район');
140
141 return array(
142 'ua' => array('NAME' => str_replace($replaceFrom, $replaceTo, $item['NAME'])),
143 'ru' => array('NAME' => str_replace($replaceFrom, $replaceTo, $item['NAME_RU']))
144 );
145 }
146
147 public function getSettlementParentCode($params)
148 {
149 $key = intval($params['AREA_ID']) ? $params['AREA_ID'] : $params['REGION_ID'];
150
151 if(!isset($this->settlementParent[$key]))
152 {
153 if(!isset($this->settlementParent[$params['REGION_ID']]))
154 {
155 // new region!
156 $code = $this->addRegion(array(
157 'ID' => $params['REGION_ID'],
158 'PARENT_CODE' => '',
159 ));
160
161 $this->settlementParent[$params['REGION_ID']] = $code;
162 }
163
164 if(intval($params['AREA_ID']))
165 {
166 if(!isset($this->settlementParent[$params['AREA_ID']]))
167 {
168 // new area!
169 $code = $this->addArea(array(
170 'ID' => $params['AREA_ID'],
171 'PARENT_CODE' => $this->settlementParent[$params['REGION_ID']],
172 ));
173
174 $this->settlementParent[$key] = $code;
175 }
176 }
177 }
178
179 return $this->settlementParent[$key];
180 }
181
182 public function buildFromUADB($options)
183 {
184 if(isset($options['NEXT_FREE_CODE']))
185 $this->exportOffset = intval($options['NEXT_FREE_CODE']);
186
187 $dbConnection = Main\HttpApplication::getConnection();
188
189 // settlements
190 $res = $dbConnection->query('select ID, ZIP, ZIP_TO, TYPE_ID, CITY_ID, REGION_ID, AREA_ID, VILLAGE_ID from b_tmp_ukrain_settlement');
191 while($item = $res->fetch())
192 {
193 $code = $this->getSettlementParentCode(array(
194 'REGION_ID' => $item['REGION_ID'],
195 'AREA_ID' => $item['AREA_ID']
196 ));
197
198 // now there can be several situations
199 $type = $this->getMappedType($item['TYPE_ID']);
200
201 // records where CITY_ID and VILLAGE_ID filled both
202 if(intval($item['CITY_ID']) && intval($item['VILLAGE_ID']))
203 {
204 $type = 'VILLAGE';
205
206 // must be attached to CITY
207 $code = $this->settlementParent[$item['CITY_ID']];
208 $id = $item['VILLAGE_ID'];
209
210 //$item['VILLAGE_ID']
211 }
212 elseif(intval($item['CITY_ID']))
213 {
214 $type = 'CITY';
215 $id = $item['CITY_ID'];
216 }
217
218 $this->settlementParent[$key] = $this->addNode(array(
219 'ID' => $item['ID'],
220 'TYPE_CODE' => $type,
221 'PARENT_CODE' => $code,
222 'NAME' => $this->getNames($id, $type),
223 'ZIP' => $item['ZIP'],
224 'ZIP_TO' => $item['ZIP_TO'],
225 ));
226 }
227
228 }
229
230 public function create()
231 {
232 $dbConnection = Main\HttpApplication::getConnection();
233
234 $table = static::getTableName();
235
236 global $DB;
237
238 if(!$DB->query('select * from '.$table.' where 1=0', true))
239 {
240 $dbConnection->query("create table ".$table." (
241
242 ID int not null auto_increment primary key,
243
244 CODE varchar(100),
245 PARENT_CODE varchar(100),
246
247 SYS_CODE varchar(100),
248
249 TYPE_CODE varchar(20),
250 FIAS_TYPE varchar(10),
251
252 NAME varchar(100) not null,
253 NAME_UA varchar(100) not null,
254 ZIP varchar(10),
255 ZIP_TO varchar(10),
256
257 LANGNAMES varchar(300),
258 EXTERNALS varchar(200),
259
260 LATITUDE varchar(30),
261 LONGITUDE varchar(30),
262
263 ALTERNATE_COORDS varchar(100),
264 BOUNDED_WITH varchar(100),
265
266 SOURCE varchar(2) default 'U'
267 )");
268
269 // SYS_CODE will be U_ + settlement id
270
271 $this->restoreIndexes();
272 }
273 }
274
275 /*
276 public function dropCodeIndex()
277 {
278 unset($this->codeIndex);
279
280 if(!empty($this->regionCodeIndex))
281 $this->codeIndex = $this->regionCodeIndex;
282 }
283
284 public function insert($data)
285 {
286 if(isset($this->codeIndex[$data['SYS_CODE']])) // already in there
287 return;
288
289 if($data['TYPE_CODE'] == 'REGION')
290 $this->regionCodeIndex[$data['SYS_CODE']] = $this->formatCode($this->exportOffset);
291
292 $this->codeIndex[$data['SYS_CODE']] = $this->formatCode($this->exportOffset);
293
294 $data['CODE'] = $this->codeIndex[$data['SYS_CODE']];
295 $data['PARENT_CODE'] = strlen($data['PARENT_SYS_CODE']) ? $this->codeIndex[$data['PARENT_SYS_CODE']] : '';
296
297 unset($data['PARENT_SYS_CODE']);
298
299 if(is_array($data['LANGNAMES']))
300 $data['LANGNAMES'] = serialize($data['LANGNAMES']);
301
302 if(is_array($data['EXTERNALS']))
303 $data['EXTERNALS'] = serialize($data['EXTERNALS']);
304
305 $this->exportOffset++;
306
307 $this->inserter->insert($data);
308 }
309
310 public static function getMap()
311 {
312 $map = parent::getMap();
313 $map['ZIP'] = array(
314 'data_type' => 'string',
315 );
316
317 return $map;
318 }
319 */
320
321 public static function getMap()
322 {
323 $map = parent::getMap();
324
325 $map['ZIP'] = array(
326 'data_type' => 'string',
327 );
328 $map['ZIP_TO'] = array(
329 'data_type' => 'string',
330 );
331
332 return $map;
333 }
334}
static formatCode($value, $length=self::CODE_LENGTH)