Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
geo.php
1<?php
2
4
9
10Loc::loadMessages(__FILE__);
11
17{
18 const USER_TYPE_ID = 'geo';
19
21 {
22 return array(
23 "USER_TYPE_ID" => static::USER_TYPE_ID,
24 "CLASS_NAME" => __CLASS__,
25 "DESCRIPTION" => GetMessage("USER_TYPE_GEO_DESCRIPTION"),
26 "BASE_TYPE" => \CUserTypeManager::BASE_TYPE_STRING,
27 "EDIT_CALLBACK" => array(__CLASS__, 'GetPublicEdit'),
28 "VIEW_CALLBACK" => array(__CLASS__, 'GetPublicView'),
29 );
30 }
31
32 public static function getDBColumnType($arUserField)
33 {
34 $connection = \Bitrix\Main\Application::getConnection();
35 $helper = $connection->getSqlHelper();
36 return $helper->getColumnTypeByField(
37 new \Bitrix\Main\ORM\Fields\StringField('x', [
38 'size' => 100,
39 'validation' => function()
40 {
41 return [
42 new \Bitrix\Main\Entity\Validator\Length(null, 100),
43 ];
44 },
45 ])
46 );
47 }
48
49 function prepareSettings($arUserField)
50 {
51 $scale = intval($arUserField["SETTINGS"]["INIT_MAP_SCALE"]);
52 $lat = doubleval($arUserField["SETTINGS"]["INIT_MAP_LAT"]);
53 $lon = doubleval($arUserField["SETTINGS"]["INIT_MAP_LON"]);
54
55 return array(
56 'INIT_MAP_SCALE' => $scale,
57 'INIT_MAP_LAT' => $lat,
58 'INIT_MAP_LON' => $lon,
59 );
60 }
61
62 function onBeforeSave($userField, $value)
63 {
64 if($value <> '')
65 {
66 $encodedValue = GeoHash::encode(explode(';', $value));
67 $value = $encodedValue;
68 }
69
70 return $value;
71 }
72
73 public function onAfterFetch($userfield, $fetched)
74 {
75 if($fetched['VALUE'] <> '')
76 {
77 $decodedValue = implode(';', GeoHash::decode($fetched['VALUE']));
78 $fetched['VALUE'] = $decodedValue;
79 }
80
81 return $fetched['VALUE'];
82 }
83
84 function getSettingsHtml($arUserField, $arHtmlControl, $bVarsFromForm)
85 {
86 global $APPLICATION;
87
88 $mapId = (is_array($arUserField) ? $arUserField['FIELD_NAME'] : '_new') .'_settings';
89
90 $result = '';
91 if($bVarsFromForm)
92 {
93 $scale = intval($GLOBALS[$arHtmlControl["NAME"]]["INIT_MAP_SCALE"]);
94 $lat = doubleval($GLOBALS[$arHtmlControl["NAME"]]["INIT_MAP_LAT"]);
95 $lon = doubleval($GLOBALS[$arHtmlControl["NAME"]]["INIT_MAP_LON"]);
96 }
97 elseif(is_array($arUserField))
98 {
99 $scale = intval($arUserField["SETTINGS"]["INIT_MAP_SCALE"]);
100 $lat = doubleval($arUserField["SETTINGS"]["INIT_MAP_LAT"]);
101 $lon = doubleval($arUserField["SETTINGS"]["INIT_MAP_LON"]);
102 }
103 else
104 {
105 $scale = 0;
106 $lat = 0;
107 $lon = 0;
108 }
109
110 $result .= '<tr>
111 <td valign="top">'.GetMessage("USER_TYPE_GEO_INIT_MAP").':</td>
112 <td>';
113
114 ob_start();
115
116 $APPLICATION->IncludeComponent('bitrix:map.google.system', '', array(
117 'MAP_ID' => $mapId,
118 'INIT_MAP_SCALE' => $scale,
119 'INIT_MAP_LAT' => $lat,
120 'INIT_MAP_LON' => $lon,
121 ));
122?>
123 <script>
124 var mapId = '<?=$mapId?>';
125
126 function waitForMap()
127 {
128 if(window.GLOBAL_arMapObjects === null)
129 {
130 return;
131 }
132
133 if(window.GLOBAL_arMapObjects[mapId] && window.google && window.google.maps && window.google.maps.event)
134 {
135 var map = window.GLOBAL_arMapObjects[mapId];
136
137 var getDataFromMap = function()
138 {
139 BX('init_scale').value = map.getZoom();
140 BX('init_lat').value = map.getCenter().lat();
141 BX('init_lng').value = map.getCenter().lng();
142 };
143
144 map.addListener('bounds_changed', getDataFromMap);
145
146 getDataFromMap();
147 }
148 else
149 {
150 setTimeout(waitForMap, 300);
151 }
152 }
153
154 waitForMap();
155 </script>
156<?
157 $result .= ob_get_clean();
158
159 $result .= '
160 </td>
161 </tr>';
162
163
164 $result .= '<tr>
165 <td>'.GetMessage("USER_TYPE_GEO_INIT_MAP_SCALE").':</td>
166 <td>
167 <input type="text" name="'.$arHtmlControl["NAME"].'[INIT_MAP_SCALE]" size="5" maxlength="5" readonly="readonly" value="'.$scale.'" id="init_scale" />
168 </td>
169 </tr>';
170 $result .= '<tr>
171 <td>'.GetMessage("USER_TYPE_GEO_INIT_MAP_LAT").':</td>
172 <td>
173 <input type="text" name="'.$arHtmlControl["NAME"].'[INIT_MAP_LAT]" size="20" maxlength="50" readonly="readonly" value="'.$lat.'" id="init_lat" />
174 </td>
175 </tr>';
176 $result .= '<tr>
177 <td>'.GetMessage("USER_TYPE_GEO_INIT_MAP_LON").':</td>
178 <td>
179 <input type="text" name="'.$arHtmlControl["NAME"].'[INIT_MAP_LON]" size="20" maxlength="50" readonly="readonly" value="'.$lon.'" id="init_lng" />
180 </td>
181 </tr>';
182
183
184
185 return $result;
186 }
187
188 function getEditFormHTML($arUserField, $arHtmlControl)
189 {
190 return static::showAdminEdit($arUserField, $arHtmlControl, false);
191 }
192
193 function getEditFormHTMLMulty($arUserField, $arHtmlControl)
194 {
195 return static::showAdminEdit($arUserField, $arHtmlControl, true);
196 }
197
198 protected function showAdminEdit($arUserField, $arHtmlControl, $multiple = false)
199 {
200 global $APPLICATION;
201
202 ob_start();
203
204 $value = $arHtmlControl['VALUE'];
205 if(!is_array($value))
206 {
207 $value = array($value);
208 }
209
210 $pointList = array();
211 foreach($value as $point)
212 {
213 if($point <> '')
214 {
215 $pointList[] = explode(';', $point);
216 }
217 }
218
219 $center = static::getCenter($arUserField, $pointList);
220
221 $APPLICATION->IncludeComponent('bitrix:map.google.edit', '', array(
222 'MAP_ID' => $arUserField['FIELD_NAME'],
223 'MULTIPLE' => $multiple ? 'Y' : 'N',
224 'POINTS' => $pointList,
225 'INIT_MAP_SCALE' => $arUserField['SETTINGS']['INIT_MAP_SCALE'],
226 'INIT_MAP_LAT' => $center[0],
227 'INIT_MAP_LON' => $center[1],
228 ), null, array('HIDE_ICONS' => 'Y'));
229
230 if(!is_array($arHtmlControl['VALUE']))
231 {
232 $arHtmlControl['VALUE'] = array($arHtmlControl['VALUE']);
233 }
234?>
235 <span style="display: none;" id="<?=$arUserField['FIELD_NAME']?>_valuewrap">
236<?
237 foreach($arHtmlControl['VALUE'] as $value)
238 {
239?>
240 <input type="hidden" name="<?=$arHtmlControl['NAME']?>" value="<?=$value?>" />
241<?
242 }
243?>
244 </span>
245 <script>
246 BX.Fileman.Map.instance('<?=\CUtil::JSEscape($arUserField['FIELD_NAME'])?>').addListener(function(map, points)
247 {
248 var str = '';
249
250 for(var i = 0; i < points.length; i++)
251 {
252 str += '<input type="hidden" name="<?=$arHtmlControl['NAME']?>" value="' + points[i].getPosition().join(';') + '" />';
253 }
254
255 BX('<?=$arUserField['FIELD_NAME']?>_valuewrap').innerHTML = str;
256
257 });
258 </script>
259 <?
260
261 return ob_get_clean();
262 }
263
264 public static function getPublicEdit($arUserField, $arAdditionalParameters = array())
265 {
266 global $APPLICATION;
267
268 $mapId = $arUserField['FIELD_NAME'].'_edit_'.Random::getString(5);
269
270 $fieldName = static::getFieldName($arUserField, $arAdditionalParameters);
271 $value = static::getFieldValue($arUserField, $arAdditionalParameters);
272
273 $pointList = array();
274
275 foreach($value as $point)
276 {
277 if($point <> '')
278 {
279 $pointList[] = explode(';', $point);
280 }
281 }
282
283 $html = '';
284
285 $center = static::getCenter($arUserField, $pointList);
286
287 ob_start();
288 $APPLICATION->IncludeComponent('bitrix:map.google.edit', '', array(
289 'MAP_ID' => $mapId,
290 'MAP_WIDTH' => '100%',
291 'MULTIPLE' => $arUserField['MULTIPLE'] == 'Y' ? 'Y' : 'N',
292 'POINTS' => $pointList,
293 'INIT_MAP_SCALE' => $arUserField['SETTINGS']['INIT_MAP_SCALE'],
294 'INIT_MAP_LAT' => $center[0],
295 'INIT_MAP_LON' => $center[1],
296 ), null, array('HIDE_ICONS' => 'Y'));
297
298?>
299<span style="display: none;" id="<?=$arUserField['FIELD_NAME']?>_valuewrap">
300 <input type="hidden" name="<?=HtmlFilter::encode($fieldName)?>" value=""/>
301<?
302foreach($value as $point)
303{
304 if($point <> '')
305 {
306?>
307 <input type="hidden" name="<?=HtmlFilter::encode($fieldName)?>" value="<?=$point?>"/>
308<?
309 }
310}
311?>
312</span>
313<script>
314 BX.Fileman.Map.instance('<?=\CUtil::JSEscape($mapId)?>').addListener(function(map, points)
315 {
316 var str = '<input type="hidden" name="<?=\CUtil::JSEscape(HtmlFilter::encode($fieldName))?>" value=""/>';
317
318 for(var i = 0; i < points.length; i++)
319 {
320 str += '<input type="hidden" name="<?=\CUtil::JSEscape(HtmlFilter::encode($fieldName))?>" value="' + points[i].getPosition().join(';') + '" />';
321 }
322
323 BX('<?=HtmlFilter::encode($arUserField['FIELD_NAME'])?>_valuewrap').innerHTML = str;
324 var inputList = BX.Main.UF.Factory.get(BX.Main.UF.TypeGeo.USER_TYPE_ID).findInput(BX('<?=\CUtil::JSEscape($arUserField['FIELD_NAME'])?>_valuewrap'), '<?=\CUtil::JSEscape($fieldName)?>');
325
326 if(inputList.length > 0)
327 {
328 BX.fireEvent(inputList[0], 'change');
329 }
330 });
331</script>
332<?
333 $html .= ob_get_clean();
334
335 return $html;
336 }
337
338 public static function getPublicView($arUserField, $arAdditionalParameters = array())
339 {
340 global $APPLICATION;
341
342 $mapId = $arUserField['FIELD_NAME'].'_view_'.Random::getString(5);
343
344 $value = static::normalizeFieldValue($arUserField["VALUE"]);
345
346 $placemarkList = array();
347 $pointList = array();
348
349 if(count($value) > 0)
350 {
351 foreach($value as $point)
352 {
353 if($point <> '')
354 {
355 $c = explode(';', $point);
356 $placemarkList[] = array(
357 'TEXT' => '',
358 'LAT' => $c[0],
359 'LON' => $c[1]
360 );
361
362 $pointList[] = $c;
363 }
364 }
365 }
366
367 $center = static::getCenter($arUserField, $pointList);
368 ob_start();
369
370 $APPLICATION->IncludeComponent('bitrix:map.google.view', '', array(
371 'MAP_ID' => $mapId,
372 'MAP_WIDTH' => '100%',
373 'MAP_DATA' => serialize(array(
374 'google_scale' => $arUserField['SETTINGS']['INIT_MAP_SCALE'],
375 'google_lat' => $center[0],
376 'google_lon' => $center[1],
377 'PLACEMARKS' => $placemarkList
378 )),
379 ), null, array('HIDE_ICONS' => 'Y'));
380
381 return ob_get_clean();
382 }
383
384 protected static function getCenter($arUserField, $pointList)
385 {
386 $center = array(0, 0);
387 $pointCount = 0;
388
389 if(count($pointList) > 0)
390 {
391 foreach($pointList as $point)
392 {
393 if(is_array($point))
394 {
395 $center[0] += $point[0];
396 $center[1] += $point[1];
397
398 $pointCount++;
399 }
400 }
401 }
402
403 if($pointCount <= 0)
404 {
405 $center = array($arUserField['SETTINGS']['INIT_MAP_LAT'], $arUserField['SETTINGS']['INIT_MAP_LON']);
406 }
407 else
408 {
409 $center[0] = $center[0] / $pointCount;
410 $center[1] = $center[1] / $pointCount;
411 }
412
413 return $center;
414 }
415}
onAfterFetch($userfield, $fetched)
Definition geo.php:73
prepareSettings($arUserField)
Definition geo.php:49
static getPublicView($arUserField, $arAdditionalParameters=array())
Definition geo.php:338
showAdminEdit($arUserField, $arHtmlControl, $multiple=false)
Definition geo.php:198
getEditFormHTMLMulty($arUserField, $arHtmlControl)
Definition geo.php:193
getEditFormHTML($arUserField, $arHtmlControl)
Definition geo.php:188
static getDBColumnType($arUserField)
Definition geo.php:32
onBeforeSave($userField, $value)
Definition geo.php:62
static getPublicEdit($arUserField, $arAdditionalParameters=array())
Definition geo.php:264
getSettingsHtml($arUserField, $arHtmlControl, $bVarsFromForm)
Definition geo.php:84
static getCenter($arUserField, $pointList)
Definition geo.php:384
static loadMessages($file)
Definition loc.php:64
static decode($geohash)
Definition geohash.php:74
static encode(array $coordinate, $length=self::MAX_LENGTH)
Definition geohash.php:19