1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
addresstype.php
См. документацию.
1<?php
2
4
18use CUserTypeManager;
19
20Loc::loadMessages(__FILE__);
21
26class AddressType extends BaseType
27{
28 public const
29 USER_TYPE_ID = 'address',
30 RENDER_COMPONENT = 'bitrix:fileman.field.address',
33
34 protected static $restrictionCount = null;
35
36 public static function getDescription(): array
37 {
38 return [
39 'DESCRIPTION' => Loc::getMessage('USER_TYPE_ADDRESS_DESCRIPTION'),
40 'BASE_TYPE' => CUserTypeManager::BASE_TYPE_STRING,
41 ];
42 }
43
50 public static function getApiKey(): ?string
51 {
52 $apiKey = Option::get('fileman', 'google_map_api_key', '');
53 if(Loader::includeModule('bitrix24') && \CBitrix24::isCustomDomain())
54 {
55 $apiKey = null;
56 $key = Option::get('bitrix24', 'google_map_api_key', '');
57 $keyHost = Option::get('bitrix24', 'google_map_api_key_host', '');
58 if(defined('BX24_HOST_NAME') && $keyHost === BX24_HOST_NAME)
59 {
60 $apiKey = $key;
61 }
62 }
63
64 return $apiKey;
65 }
66
73 public static function getApiKeyHint(): string
74 {
75 $hint = '';
76 if(static::getApiKey() === null)
77 {
78 if(Loader::includeModule('bitrix24'))
79 {
80 if(\CBitrix24::isCustomDomain())
81 {
82 $hint = Loc::getMessage(
83 'USER_TYPE_ADDRESS_NO_KEY_HINT_B24',
84 ['#settings_path#' => \CBitrix24::PATH_CONFIGS]
85 );
86 }
87 }
88 else
89 {
90 if(defined('ADMIN_SECTION') && ADMIN_SECTION === true)
91 {
92 $settingsPath = '/bitrix/admin/settings.php?lang=' . LANGUAGE_ID . '&mid=fileman';
93 }
94 else
95 {
96 $settingsPath = SITE_DIR . 'configs/';
97 }
98
99 if(
100 !File::isFileExists($_SERVER['DOCUMENT_ROOT'] . $settingsPath)
101 ||
102 !Directory::isDirectoryExists($_SERVER['DOCUMENT_ROOT'] . $settingsPath)
103 )
104 {
105 $settingsPath = SITE_DIR . 'settings/configs/';
106 }
107
108 $hint = Loc::getMessage(
109 'USER_TYPE_ADDRESS_NO_KEY_HINT',
110 ['#settings_path#' => $settingsPath]
111 );
112 }
113 }
114
115 return $hint;
116 }
117
122 public static function getTrialHint(): ?array
123 {
124 return null;
125 }
126
130 public static function canUseMap(): bool
131 {
132 return true;
133 }
134
138 public static function checkRestriction(): bool
139 {
140 return true;
141 }
142
146 public static function useRestriction(): bool
147 {
148 return false;
149 }
150
151 public static function getDbColumnType(): string
152 {
154 $helper = $connection->getSqlHelper();
155 return $helper->getColumnTypeByField(new \Bitrix\Main\ORM\Fields\TextField('x'));
156 }
157
158 public static function prepareSettings(array $userField): array
159 {
160 $settings = ($userField['SETTINGS'] ?? []);
161 $showMap = ($settings['SHOW_MAP'] ?? null);
162
163 return [
164 'SHOW_MAP' => ($showMap === 'N' ? 'N' : 'Y'),
165 ];
166 }
167
168 public static function checkFields(array $userField, $value): array
169 {
170 return [];
171 }
172
173 public static function onBeforeSaveAll(array $userField, $value)
174 {
175 $result = [];
176 foreach ($value as $item)
177 {
178 $processedValue = self::onBeforeSave($userField, $item);
179 if ($processedValue)
180 {
181 $result[] = $processedValue;
182 }
183 }
184
185 $fieldName = ($userField['FIELD_NAME'] ?? null);
186 unset($_POST[$fieldName . '_manual_edit']);
187
188 return $result;
189 }
190
197 public static function onBeforeSave(array $userField, $value)
198 {
199 if (!$value)
200 {
201 self::clearManualEditFlag($userField);
202
203 return null;
204 }
205
206 if (!Loader::includeModule('location') || self::isRawValue($value))
207 {
208 // if the value hasn't been set manually (e.g. from bizproc), then we have to remove the
209 // address' id because otherwise we'll end up with multiple UF values pointing to a single address
210 $fieldName = ($userField['FIELD_NAME'] ?? null);
211 $isManualAddressEdit = $_POST[$fieldName . '_manual_edit'] ?? null;
212 if (!$isManualAddressEdit)
213 {
214 $parsedValue = self::parseValue($value);
215 $value = $parsedValue[0] . '|' . $parsedValue[1][0] . ';' . $parsedValue[1][1];
216 }
217
218 self::clearManualEditFlag($userField);
219
220 return $value;
221 }
222
223 if (mb_strlen($value) > 4 && mb_substr($value, -4) === '_del')
224 {
225 $oldAddressId = (int)substr($value, 0, -4);
226 $oldAddress = Address::load($oldAddressId);
227 if ($oldAddress)
228 {
229 $oldAddress->delete();
230 }
231
232 self::clearManualEditFlag($userField);
233
234 return '';
235 }
236
237 $address = null;
238 try
239 {
240 $address = Address::fromJson($value);
241 }
242 catch (ArgumentException | \TypeError $exception)
243 {
244 if (is_string($value))
245 {
246 $addressFields = self::getAddressFieldsFromString($value);
247 $address = Address::fromArray($addressFields);
248 }
249 }
250
251 if (!$address)
252 {
253 self::clearManualEditFlag($userField);
254
255 return $value;
256 }
257
258 $saveResult = $address->save();
259 if ($saveResult->isSuccess())
260 {
261 $value = self::formatAddressToString($address);
262 }
263 else
264 {
265 $value = self::getTextAddress($address);
266 }
267
268 self::clearManualEditFlag($userField);
269
270 return $value;
271 }
272
273 private static function clearManualEditFlag(array $userField): void
274 {
275 $fieldName = ($userField['FIELD_NAME'] ?? null);
276 if (($userField['MULTIPLE'] ?? null) !== 'Y')
277 {
278 unset($_POST[$fieldName . '_manual_edit']);
279 }
280 }
281
286 public static function parseValue(?string $value): array
287 {
288 $coords = '';
289 $addressId = null;
290 if(mb_strpos($value, '|') !== false)
291 {
292 [$value, $coords, $addressId] = explode('|', $value);
293 if ($addressId)
294 {
295 $addressId = (int)$addressId;
296 }
297 if($coords !== '' && mb_strpos($coords, ';') !== false)
298 {
299 $coords = explode(';', $coords);
300 }
301 else
302 {
303 $coords = '';
304 }
305 }
306
307 $json = null;
308 if ($addressId)
309 {
310 $address = Address::load($addressId);
311 if ($address)
312 {
313 $json = $address->toJson();
314 }
315 }
316 else
317 {
318 $address = self::tryConvertFromJsonToAddress($value);
319 if ($address)
320 {
321 $json = $value;
322 $value = self::getTextAddress($address);
323 }
324 }
325
326 return [
327 $value,
328 $coords,
329 $addressId,
330 $json,
331 ];
332 }
333
334 private static function tryConvertFromJsonToAddress($value): ?Address
335 {
336 $result = null;
337 try
338 {
339 $result = Address::fromJson($value);
340 }
341 catch (\Exception | \TypeError $exception) {}
342
343 return $result;
344 }
345
346 private static function formatAddressToString(Address $address): string
347 {
348 return (
349 self::getTextAddress($address)
350 . '|'
351 . $address->getLatitude()
352 . ';'
353 . $address->getLongitude()
354 . '|'
355 . $address->getId()
356 );
357 }
358
359 private static function getTextAddress(Address $address): string
360 {
361 return $address->toString(
362 FormatService::getInstance()->findDefault(LANGUAGE_ID),
364 );
365 }
366
367 public static function isRawValue($value): bool
368 {
369 $valueParts = explode('|', $value);
370 $valuePartsCount = count($valueParts);
371 if ($valuePartsCount < 2)
372 {
373 return false;
374 }
375
376 if (mb_strpos($valueParts[1], ';') === false)
377 {
378 return false;
379 }
380
381 $possibleCoords = explode(';', $valueParts[1]);
382 if (
383 count($possibleCoords) !== 2
384 || (
385 (!is_numeric($possibleCoords[0]) || !is_numeric($possibleCoords[1]))
386 && !($possibleCoords[0] === '' && $possibleCoords[1] === '')
387 )
388 )
389 {
390 return false;
391 }
392
393 return true;
394 }
395
400 public static function getAddressFieldsByValue($value): ?array
401 {
402 if (!Loader::includeModule('location'))
403 {
404 return null;
405 }
406
407 $address = self::tryConvertFromJsonToAddress($value);
408 if (!$address)
409 {
410 $addressId = self::parseValue($value)[2];
411 if (is_numeric($addressId))
412 {
413 $address = Address::load((int)$addressId);
414 }
415 }
416
417 if ($address)
418 {
419 return $address->toArray();
420 }
421
422 return self::getAddressFieldsFromString($value);
423 }
424
430 private static function getAddressFieldsFromString(?string $addressString): ?array
431 {
432 if (!$addressString)
433 {
434 return null;
435 }
436
437 [$address, $coords] = self::parseValue($addressString);
438
439 return [
440 'latitude' => $coords[0] ?? null,
441 'longitude' => $coords[1] ?? null,
442 'fieldCollection' => [
443 Address\FieldType::ADDRESS_LINE_2 => $address,
444 ],
445 'languageId' => LANGUAGE_ID,
446 ];
447 }
448
454 public static function getFilterData(?array $userField, array $additionalSettings): array
455 {
456 return [
457 'id' => $additionalSettings['ID'],
458 'name' => $additionalSettings['NAME'],
459 'filterable' => ''
460 ];
461 }
462
467 public static function onSearchIndex(array $userField): ?string
468 {
469 if(is_array($userField['VALUE']))
470 {
471 $result = implode('\r\n', $userField['VALUE']);
472 }
473 else
474 {
475 $result = $userField['VALUE'];
476 }
477
478 return $result;
479 }
480}
$connection
Определения actionsdefinitions.php:38
static isRawValue($value)
Определения addresstype.php:367
static onBeforeSaveAll(array $userField, $value)
Определения addresstype.php:173
static getAddressFieldsByValue($value)
Определения addresstype.php:400
static getFilterData(?array $userField, array $additionalSettings)
Определения addresstype.php:454
static onBeforeSave(array $userField, $value)
Определения addresstype.php:197
static parseValue(?string $value)
Определения addresstype.php:286
static onSearchIndex(array $userField)
Определения addresstype.php:467
static checkFields(array $userField, $value)
Определения addresstype.php:168
static prepareSettings(array $userField)
Определения addresstype.php:158
static getConnection($name="")
Определения application.php:638
Определения loader.php:13
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$_SERVER["DOCUMENT_ROOT"]
Определения cron_frame.php:9
const SITE_DIR(!defined('LANG'))
Определения include.php:72
Определения Image.php:9
$settings
Определения product_settings.php:43
if(empty($signedUserToken)) $key
Определения quickway.php:257
const ADMIN_SECTION
Определения rss.php:2
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936