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