1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
userfieldproxy.php
См. документацию.
1<?
2namespace Bitrix\Rest;
3use Bitrix\Main;
4use Bitrix\Rest\Api\UserFieldType;
5use Bitrix\Rest\UserField\Callback;
6
7abstract class UserFieldProxy
8{
9 protected $entityID = '';
11 protected $user = null;
12 protected $isAdminUser = null;
13 protected $isAuthorizedUser = null;
14 protected $namePrefix = '';
15 private static $langs = null;
16 private static $fieldsInfo = null;
17 private static $langIncluded = false;
18
19 function __construct($entityID, \CUser $user = null)
20 {
21 if(!is_string($entityID))
22 {
23 throw new Main\ArgumentTypeException('entityID', 'string');
24 }
25
26 $this->entityID = $entityID;
27 $this->user = $user !== null ? $user : $this->getCurrentUser();
28 }
29 public function getEntityID()
30 {
31 return $this->entityID;
32 }
33
34 public function getNamePrefix()
35 {
36 return $this->namePrefix;
37 }
38 public function setNamePrefix($prefix)
39 {
40 if(!is_string($prefix))
41 {
42 throw new Main\ArgumentTypeException('prefix', 'string');
43 }
44
45 $this->namePrefix = $prefix !== ''? mb_strtoupper($prefix) : '';
46 }
47 public static function getFields()
48 {
49 if(self::$fieldsInfo === null)
50 {
51 //isReadOnly - Can not be defined by user. Read only access.
52 //isImmutable - Can be defined by user during creation. It can not be changed.
53 //isMultiple - It is multiple (array will be returned).
54
55 self::includeLangFile();
56 self::$fieldsInfo = array(
57 'ID' => array('type' => 'int', 'title' => GetMessage('REST_UF_ID'), 'isReadOnly'=> true),
58 'ENTITY_ID' => array('type' => 'string', 'title' => GetMessage('REST_UF_ENTITY_ID'), 'isImmutable' => true),
59 'FIELD_NAME' => array('type' => 'string', 'title' => GetMessage('REST_UF_FIELD_NAME'), 'isImmutable' => true),
60 'USER_TYPE_ID' => array('type' => 'string', 'title' => GetMessage('REST_UF_USER_TYPE_ID'), 'isImmutable' => true),
61 'XML_ID' => array('type' => 'string', 'title' => GetMessage('REST_UF_XML_ID')),
62 'SORT' => array('type' => 'int', 'title' => GetMessage('REST_UF_SORT')),
63 'MULTIPLE' => array('type' => 'char', 'title' => GetMessage('REST_UF_MULTIPLE')),
64 'MANDATORY' => array('type' => 'char', 'title' => GetMessage('REST_UF_MANDATORY')),
65 'SHOW_FILTER' => array('type' => 'char', 'title' => GetMessage('REST_UF_SHOW_FILTER')),
66 'SHOW_IN_LIST' => array('type' => 'char', 'title' => GetMessage('REST_UF_SHOW_IN_LIST')),
67 'EDIT_IN_LIST' => array('type' => 'char', 'title' => GetMessage('REST_UF_EDIT_IN_LIST')),
68 'IS_SEARCHABLE' => array('type' => 'char', 'title' => GetMessage('REST_UF_IS_SEARCHABLE')),
69 'EDIT_FORM_LABEL' => array('type' => 'string', 'title' => GetMessage('REST_UF_EDIT_FORM_LABEL')),
70 'LIST_COLUMN_LABEL' => array('type' => 'string', 'title' => GetMessage('REST_UF_LIST_COLUMN_LABEL')),
71 'LIST_FILTER_LABEL' => array('type' => 'string', 'title' => GetMessage('REST_UF_LIST_FILTER_LABEL')),
72 'ERROR_MESSAGE' => array('type' => 'string', 'title' => GetMessage('REST_UF_ERROR_MESSAGE')),
73 'HELP_MESSAGE' => array('type' => 'string', 'title' => GetMessage('REST_UF_HELP_MESSAGE')),
74 'LIST' => array('type' => 'uf_enum_element', 'title' => GetMessage('REST_UF_LIST'), 'isMultiple'=> true),
75 'SETTINGS' => array('type' => 'object', 'title' => GetMessage('REST_UF_SETTINGS'))
76 );
77 }
78
79 return self::$fieldsInfo;
80 }
81 public static function getEnumerationElementFields()
82 {
83 self::includeLangFile();
84 return array(
85 'ID' => array('type' => 'int', 'title' => GetMessage('REST_UF_ID'), 'isReadOnly'=> true),
86 'SORT' => array('type' => 'int', 'title' => GetMessage('REST_UF_SORT')),
87 'VALUE' => array('type' => 'string', 'title' => GetMessage('REST_UF_VALUE')),
88 'DEF' => array('type' => 'string', 'title' => GetMessage('REST_UF_IS_DEF')),
89 'DEL' => array('type' => 'string', 'title' => GetMessage('REST_UF_DEL')),
90 );
91 }
92 public static function getSettingsFields($typeID)
93 {
94 if(!is_string($typeID))
95 {
96 throw new Main\ArgumentTypeException('typeID', 'string');
97 }
98
99 if($typeID === '')
100 {
101 throw new Main\ArgumentException('Empty string is specified', 'typeID');
102 }
103
104
105 self::includeLangFile();
106 switch($typeID)
107 {
108 case 'string':
109 {
110 return array(
111 'DEFAULT_VALUE' => array('type' => 'string', 'title' => GetMessage('REST_UF_DEFAULT_VALUE')),
112 'ROWS' => array('type' => 'int', 'title' => GetMessage('REST_UF_ROWS'))
113 );
114 }
115 case 'integer':
116 {
117 return array(
118 'DEFAULT_VALUE' => array('type' => 'int', 'title' => GetMessage('REST_UF_DEFAULT_VALUE'))
119 );
120 }
121 case 'double':
122 {
123 return array(
124 'DEFAULT_VALUE' => array('type' => 'double', 'title' => GetMessage('REST_UF_DEFAULT_VALUE')),
125 'PRECISION' => array('type' => 'int', 'title' => GetMessage('REST_UF_PRECISION'))
126 );
127 }
128 case 'boolean':
129 {
130 return array(
131 'DEFAULT_VALUE' => array('type' => 'int', 'title' => GetMessage('REST_UF_DEFAULT_VALUE')),
132 'DISPLAY' => array('type' => 'string', 'title' => GetMessage('REST_UF_DISPLAY'))
133 );
134 }
135 case 'datetime':
136 {
137 return array(
138 'DEFAULT_VALUE' => array('type' => 'datetime', 'title' => GetMessage('REST_UF_DEFAULT_VALUE'))
139 );
140 }
141 case 'enumeration':
142 {
143 return array(
144 'DISPLAY' => array('type' => 'string', 'title' => GetMessage('REST_UF_DISPLAY')),
145 'LIST_HEIGHT' => array('type' => 'int', 'title' => GetMessage('REST_UF_LIST_HEIGHT')),
146 );
147 }
148 case 'iblock_section':
149 case 'iblock_element':
150 {
151 return array(
152 'DEFAULT_VALUE' => array('type' => 'int', 'title' => GetMessage('REST_UF_DEFAULT_VALUE')),
153 'IBLOCK_ID' => array('type' => 'int', 'title' => GetMessage('REST_UF_IBLOCK_ID')),
154 'IBLOCK_TYPE_ID' => array('type' => 'string', 'title' => GetMessage('REST_UF_IBLOCK_TYPE_ID')),
155 'DISPLAY' => array('type' => 'string', 'title' => GetMessage('REST_UF_DISPLAY')),
156 'LIST_HEIGHT' => array('type' => 'int', 'title' => GetMessage('REST_UF_LIST_HEIGHT')),
157 'ACTIVE_FILTER' => array('type' => 'char', 'title' => GetMessage('REST_UF_ACTIVE_FILTER'))
158 );
159 }
160 case 'crm_status':
161 {
162 return array(
163 'ENTITY_TYPE' => array('type' => 'string', 'title' => GetMessage('REST_UF_ENTITY_TYPE'))
164 );
165 }
166 case 'crm':
167 {
168 return array(
169 'LEAD' => array('type' => 'char', 'title' => GetMessage('REST_UF_CRM_LEAD')),
170 'CONTACT' => array('type' => 'char', 'title' => GetMessage('REST_UF_CRM_CONTACT')),
171 'COMPANY' => array('type' => 'char', 'title' => GetMessage('REST_UF_CRM_COMPANY')),
172 'DEAL' => array('type' => 'char', 'title' => GetMessage('REST_UF_CRM_DEAL'))
173 );
174 }
175 default:
176 {
177 return array();
178 }
179 }
180 }
181 public static function getTypes(\CRestServer $server = null)
182 {
183 self::includeLangFile();
184 $result = array(
185 array('ID' => 'string', 'title' => GetMessage('REST_UF_TYPE_STRING')),
186 array('ID' => 'integer', 'title' => GetMessage('REST_UF_TYPE_INTEGER')),
187 array('ID' => 'double', 'title' => GetMessage('REST_UF_TYPE_DOUBLE')),
188 array('ID' => 'boolean', 'title' => GetMessage('REST_UF_TYPE_BOOLEAN')),
189 array('ID' => 'enumeration', 'title' => GetMessage('REST_UF_TYPE_ENUMERATION')),
190 array('ID' => 'datetime', 'title' => GetMessage('REST_UF_TYPE_DATETIME')),
191 array('ID' => 'date', 'title' => GetMessage('REST_UF_TYPE_DATE')),
192 array('ID' => 'money', 'title' => GetMessage('REST_UF_TYPE_MONEY')),
193 array('ID' => 'url', 'title' => GetMessage('REST_UF_TYPE_URL')),
194 array('ID' => 'address', 'title' => GetMessage('REST_UF_TYPE_ADDRESS')),
195 array('ID' => 'file', 'title' => GetMessage('REST_UF_TYPE_FILE')),
196 array('ID' => 'employee', 'title' => GetMessage('REST_UF_TYPE_EMPLOYEE')),
197 array('ID' => 'crm_status', 'title' => GetMessage('REST_UF_TYPE_CRM_STATUS')),
198 array('ID' => 'iblock_section', 'title' => GetMessage('REST_UF_TYPE_IBLOCK_SECTION')),
199 array('ID' => 'iblock_element', 'title' => GetMessage('REST_UF_TYPE_IBLOCK_ELEMENT')),
200 array('ID' => 'crm', 'title' => GetMessage('REST_UF_TYPE_CRM'))
201 );
202
203 if($server !== null && $server->getAuthType() === OAuth\Auth::AUTH_TYPE)
204 {
205 $clientInfo = AppTable::getByClientId($server->getClientId());
206 $placementHandlerList = PlacementTable::getHandlersList(UserFieldType::PLACEMENT_UF_TYPE);
207
208 foreach($placementHandlerList as $handler)
209 {
210 if($handler['APP_ID'] === $clientInfo['ID'])
211 {
212 $result[] = array(
213 'ID' => $handler['ADDITIONAL'],
214 'title' => $handler['TITLE']
215 );
216 }
217 }
218 }
219
220 return $result;
221 }
222 public function add(array $fields)
223 {
224 global $APPLICATION;
225 if(!$this->checkCreatePermission())
226 {
227 throw new RestException('Access denied.');
228 }
229
230 if($this->entityID === '')
231 {
232 throw new RestException('Operation is not allowed. Entity ID is not defined.');
233 }
234
235 //Try get default field label
236 $defaultLabel = isset($fields['LABEL']) ? trim($fields['LABEL']) : '';
237
239 $fields['ENTITY_ID'] = $this->entityID;
240 $errors = array();
241
242 $userTypeID = isset($fields['USER_TYPE_ID']) ? trim($fields['USER_TYPE_ID']) : '';
243 if($userTypeID === '')
244 {
245 $errors[] = "The 'USER_TYPE_ID' field is not found.";
246 }
247 $fields['USER_TYPE_ID'] = $userTypeID;
248
249 $fieldName = isset($fields['FIELD_NAME']) ? trim($fields['FIELD_NAME']) : '';
250 if($fieldName === '')
251 {
252 $errors[] = "The 'FIELD_NAME' field is not found.";
253 }
254
255 $fieldName = mb_strtoupper($fieldName);
256 $prefix = $this->namePrefix;
257 if($prefix !== '')
258 {
259 $fullPrefix = 'UF_'.$prefix.'_';
260 $fullPrefixLen = mb_strlen($fullPrefix);
261 if(strncmp($fieldName, $fullPrefix, $fullPrefixLen) !== 0)
262 {
263 $fieldName = strncmp($fieldName, 'UF_', 3) === 0
264 ? $fullPrefix.mb_substr($fieldName, 3)
265 : $fullPrefix.$fieldName;
266 }
267 }
268 else
269 {
270 $fullPrefix = 'UF_';
271 $fullPrefixLen = 3;
272 if(strncmp($fieldName, $fullPrefix, $fullPrefixLen) !== 0)
273 {
274 $fieldName = 'UF_'. $fieldName;
275 }
276 }
277
278 $fields['FIELD_NAME'] = $fieldName;
279
280 if(!empty($errors))
281 {
282 throw new RestException(implode("\n", $errors));
283 }
284
285 if($defaultLabel === '')
286 {
287 $defaultLabel = $fieldName;
288 }
289
290 self::prepareLabels($fields, 'LIST_FILTER_LABEL', $defaultLabel);
291 self::prepareLabels($fields, 'LIST_COLUMN_LABEL', $defaultLabel);
292 self::prepareLabels($fields, 'EDIT_FORM_LABEL', $defaultLabel);
293 self::prepareLabels($fields, 'ERROR_MESSAGE', $defaultLabel);
294 self::prepareLabels($fields, 'HELP_MESSAGE', $defaultLabel);
295
296 $fields['MULTIPLE'] = isset($fields['MULTIPLE']) && mb_strtoupper($fields['MULTIPLE']) === 'Y' ? 'Y' : 'N';
297 $fields['MANDATORY'] = isset($fields['MANDATORY']) && mb_strtoupper($fields['MANDATORY']) === 'Y' ? 'Y' : 'N';
298 $fields['SHOW_FILTER'] = isset($fields['SHOW_FILTER']) && mb_strtoupper($fields['SHOW_FILTER']) === 'Y' ? 'E' : 'N'; // E - 'By mask' is default
299
300 $isMultiple = isset($fields['MULTIPLE']) && $fields['MULTIPLE'] === 'Y';
301
302 $settings = isset($fields['SETTINGS']) && is_array($fields['SETTINGS']) ? $fields['SETTINGS'] : array();
303 $effectiveSettings = array();
304 switch ($userTypeID)
305 {
306 case 'string':
307 {
308 $effectiveSettings['DEFAULT_VALUE'] = isset($settings['DEFAULT_VALUE'])
309 ? $settings['DEFAULT_VALUE'] : '';
310
311 $effectiveSettings['ROWS'] = $settings['ROWS'] > 0
312 ? $settings['ROWS'] : 1;
313 break;
314 }
315 case 'integer':
316 {
317 $effectiveSettings['DEFAULT_VALUE'] = isset($settings['DEFAULT_VALUE'])
318 ? $settings['DEFAULT_VALUE'] : '';
319 break;
320 }
321 case 'double':
322 {
323 $effectiveSettings['DEFAULT_VALUE'] = isset($settings['DEFAULT_VALUE'])
324 ? $settings['DEFAULT_VALUE'] : '';
325
326 $effectiveSettings['PRECISION'] = $settings['PRECISION'] >= 0
327 ? $settings['PRECISION'] : 2;
328 break;
329 }
330 case 'boolean':
331 {
332 $effectiveSettings['DEFAULT_VALUE'] = isset($settings['DEFAULT_VALUE'])
333 && $settings['DEFAULT_VALUE'] > 0 ? 1 : 0;
334
335 $display = isset($settings['DISPLAY']) ? $settings['DISPLAY'] : '';
336 $effectiveSettings['DISPLAY'] = $display !== ''? mb_strtoupper($display) : 'CHECKBOX';
337
338 $fields['MULTIPLE'] = 'N';
339 break;
340 }
341 case 'date':
342 case 'datetime':
343 {
344 $defaultValue = isset($settings['DEFAULT_VALUE']) ? $settings['DEFAULT_VALUE'] : array();
345 if(!is_array($defaultValue))
346 {
347 $defaultValue = array('VALUE' => $defaultValue, 'TYPE' => 'NONE');
348 }
349
350 $effectiveSettings['DEFAULT_VALUE'] = array(
351 'VALUE' => isset($defaultValue['VALUE'])
352 ? \CRestUtil::unConvertDateTime($defaultValue['VALUE']) : '',
353 'TYPE' => isset($defaultValue['TYPE']) && $defaultValue['TYPE'] !== ''
354 ? mb_strtoupper($defaultValue['TYPE']) : 'NONE'
355 );
356 break;
357 }
358 case 'enumeration':
359 {
360 $display = isset($settings['DISPLAY']) ? $settings['DISPLAY'] : '';
361 $effectiveSettings['DISPLAY'] = $display !== ''? mb_strtoupper($display) : 'LIST';
362
363 $height = isset($settings['LIST_HEIGHT']) ? (int)$settings['LIST_HEIGHT'] : 0;
364 $effectiveSettings['LIST_HEIGHT'] = $height > 0 ? $height : 1;
365
366 $listItems = isset($fields['LIST']) && is_array($fields['LIST']) ? $fields['LIST'] : array();
367 $effectiveListItems = array();
368
369 $counter = 0;
370 $defaultItemKey = '';
371 foreach($listItems as $item)
372 {
373 $itemValue = isset($item['VALUE']) ? trim($item['VALUE'], " \t\n\r") : '';
374 if($itemValue === '')
375 {
376 continue;
377 }
378
379 $effectiveItem = array('VALUE' => $itemValue);
380 $itemSort = isset($item['SORT']) && is_numeric($item['SORT']) ? (int)$item['SORT'] : 0;
381 if($itemSort > 0)
382 {
383 $effectiveItem['SORT'] = $itemSort;
384 }
385
386 if($itemSort > 0)
387 {
388 $effectiveItem['SORT'] = $itemSort;
389 }
390
391 $itemKey = "n{$counter}";
392 $counter++;
393
394 if(isset($item['DEF']))
395 {
396 $isDefault = mb_strtoupper($item['DEF']) === 'Y';
397 if($isMultiple)
398 {
399 $effectiveItem['DEF'] = $isDefault ? 'Y' : 'N';
400 }
401 elseif($isDefault && $defaultItemKey === '')
402 {
403 $defaultItemKey = $itemKey;
404 }
405 }
406
407 if(isset($item['XML_ID']) && is_string($item['XML_ID']) && $item['XML_ID'] != '')
408 {
409 $effectiveItem['XML_ID'] = $item['XML_ID'];
410 }
411
412 $effectiveListItems[$itemKey] = &$effectiveItem;
413 unset($effectiveItem);
414 }
415
416 if(!$isMultiple && $defaultItemKey !== '')
417 {
418 foreach($effectiveListItems as $key => &$item)
419 {
420 $item['DEF'] = $key === $defaultItemKey ? 'Y' : 'N';
421 }
422 unset($item);
423 }
424 $fields['LIST'] = $effectiveListItems;
425 break;
426 }
427 case 'iblock_section':
428 case 'iblock_element':
429 {
430 $effectiveSettings['IBLOCK_TYPE_ID'] = isset($settings['IBLOCK_TYPE_ID']) ? $settings['IBLOCK_TYPE_ID'] : '';
431 $effectiveSettings['IBLOCK_ID'] = isset($settings['IBLOCK_ID']) ? (int)$settings['IBLOCK_ID'] : 0;
432 $effectiveSettings['DEFAULT_VALUE'] = isset($settings['DEFAULT_VALUE']) ? $settings['DEFAULT_VALUE'] : '';
433
434 $display = isset($settings['DISPLAY']) ? $settings['DISPLAY'] : '';
435 $effectiveSettings['DISPLAY'] = $display !== ''? mb_strtoupper($display) : 'LIST';
436
437 $height = isset($settings['LIST_HEIGHT']) ? (int)$settings['LIST_HEIGHT'] : 0;
438 $effectiveSettings['LIST_HEIGHT'] = $height > 0 ? $height : 1;
439 $effectiveSettings['ACTIVE_FILTER'] = isset($settings['ACTIVE_FILTER'])
440 && mb_strtoupper($settings['ACTIVE_FILTER']) === 'Y' ? 'Y' : 'N';
441 break;
442 }
443 case 'crm_status':
444 {
445 $effectiveSettings['ENTITY_TYPE'] = isset($settings['ENTITY_TYPE']) ? $settings['ENTITY_TYPE'] : '';
446 break;
447 }
448 case 'crm':
449 {
450 $effectiveSettings['LEAD'] = isset($settings['LEAD']) && mb_strtoupper($settings['LEAD']) === 'Y' ? 'Y' : 'N';
451 $effectiveSettings['CONTACT'] = isset($settings['CONTACT']) && mb_strtoupper($settings['CONTACT']) === 'Y' ? 'Y' : 'N';
452 $effectiveSettings['COMPANY'] = isset($settings['COMPANY']) && mb_strtoupper($settings['COMPANY']) === 'Y' ? 'Y' : 'N';
453 $effectiveSettings['DEAL'] = isset($settings['DEAL']) && mb_strtoupper($settings['DEAL']) === 'Y' ? 'Y' : 'N';
454 break;
455 }
456 case 'employee':
457 {
458 if($fields['SHOW_FILTER'] !== 'N')
459 {
460 $fields['SHOW_FILTER'] = 'I'; // Force exact match for 'USER' field type
461 }
462 break;
463 }
464 default:
465 {
466 $userTypeList = PlacementTable::getHandlersList(UserFieldType::PLACEMENT_UF_TYPE);
467
468 foreach($userTypeList as $userType)
469 {
470 if($userType['ADDITIONAL'] === $userTypeID)
471 {
472 $fields['USER_TYPE_ID'] = UserField\Callback::getUserTypeId($userType);
473 }
474 }
475
476 $fields['SHOW_FILTER'] = 'N';
477 }
478 }
479
480 $fields['SETTINGS'] = $effectiveSettings;
481
482 $entity = new \CUserTypeEntity();
483 $ID = $entity->Add($fields);
484 if($ID <= 0)
485 {
486 $exc = $APPLICATION->GetException();
487 $errors[] = $exc !== false ? $exc->GetString() : 'Fail to create new user field.';
488 }
489 elseif ($userTypeID === 'enumeration' && isset($fields['LIST']) && is_array($fields['LIST']))
490 {
491 $enum = new \CUserFieldEnum();
492 if(!$enum->SetEnumValues($ID, $fields['LIST']))
493 {
494 $exc = $APPLICATION->GetException();
495 $errors[] = $exc !== false ? $exc->GetString() : 'Fail to save enumumeration field values.';
496 }
497 }
498
499 if(!empty($errors))
500 {
501 throw new RestException(implode("\n", $errors), RestException::ERROR_CORE);
502 }
503
504 return $ID;
505 }
506 public function get($ID)
507 {
508 if(!is_int($ID))
509 {
510 $ID = (int)$ID;
511 }
512
513 if($ID <= 0)
514 {
515 throw new RestException('ID is not defined or invalid.');
516 }
517
518 if(!$this->checkReadPermission())
519 {
520 throw new RestException('Access denied.');
521 }
522
523 if($this->entityID === '')
524 {
525 throw new RestException('Operation is not allowed. Entity ID is not defined.');
526 }
527
528 $entity = new \CUserTypeEntity();
529 $result = $entity->GetByID($ID);
530 if(!is_array($result))
531 {
532 throw new RestException("The entity with ID '{$ID}' is not found.", RestException::ERROR_NOT_FOUND);
533 }
534
535 $entityID = isset($result['ENTITY_ID']) ? $result['ENTITY_ID'] : '';
536 if($entityID !== $this->entityID)
537 {
538 throw new RestException('Access denied.');
539 }
540
541 if($result['USER_TYPE_ID'] === 'enumeration')
542 {
543 $result['LIST'] = array();
544
545 $enumEntity = new \CUserFieldEnum();
546 $dbResultEnum = $enumEntity->GetList(array('SORT' => 'ASC'), array('USER_FIELD_ID' => $ID));
547 while($enum = $dbResultEnum->Fetch())
548 {
549 $result['LIST'][] = array(
550 'ID' => $enum['ID'],
551 'SORT' => $enum['SORT'],
552 'VALUE' => $enum['VALUE'],
553 'DEF' => $enum['DEF']
554 );
555 }
556 }
557 elseif(preg_match("/^".UserField\Callback::USER_TYPE_ID_PREFIX."_([\d]+)_/", $result['USER_TYPE_ID'], $matches))
558 {
559 $result['USER_TYPE_ID'] = str_replace($matches[0], '', $result['USER_TYPE_ID']);
560
561 $appInfo = AppTable::getByClientId($matches[1]);
562 $result['USER_TYPE_OWNER'] = $appInfo['CLIENT_ID'];
563 }
564
565 return $result;
566 }
567 public function getList(array $order, array $filter)
568 {
569 if(!$this->checkReadPermission())
570 {
571 throw new RestException('Access denied.');
572 }
573
574 if($this->entityID === '')
575 {
576 throw new RestException('Operation is not allowed. Entity ID is not defined.');
577 }
578 $filter['ENTITY_ID'] = $this->entityID;
579
580 if(isset($filter['USER_TYPE_ID']))
581 {
582 $handlerList = PlacementTable::getHandlersList(UserFieldType::PLACEMENT_UF_TYPE);
583 foreach($handlerList as $handler)
584 {
585 if($handler['ADDITIONAL'] === $filter['USER_TYPE_ID'])
586 {
587 $filter['USER_TYPE_ID'] = Callback::getUserTypeId($handler);
588 }
589 }
590 }
591
592 $entity = new \CUserTypeEntity();
593 $dbResult = $entity->GetList($order, $filter);
594 $result = array();
595 while($fields = $dbResult->Fetch())
596 {
597 $userTypeID = isset($fields['USER_TYPE_ID']) ? $fields['USER_TYPE_ID'] : '';
598 if($userTypeID === 'datetime'
599 && isset($fields['SETTINGS'])
600 && isset($fields['SETTINGS']['DEFAULT_VALUE'])
601 && isset($fields['SETTINGS']['DEFAULT_VALUE']['VALUE'])
602 && $fields['SETTINGS']['DEFAULT_VALUE']['VALUE'] !== '')
603 {
604 $fields['SETTINGS']['DEFAULT_VALUE']['VALUE'] = \CRestUtil::ConvertDateTime($fields['SETTINGS']['DEFAULT_VALUE']['VALUE']);
605 }
606
607 if($userTypeID === 'enumeration')
608 {
609 $fields['LIST'] = array();
610
611 $enumEntity = new \CUserFieldEnum();
612 $dbResultEnum = $enumEntity->GetList(array('SORT' => 'ASC'), array('USER_FIELD_ID' => $fields['ID']));
613 while($enum = $dbResultEnum->Fetch())
614 {
615 $fields['LIST'][] = array(
616 'ID' => $enum['ID'],
617 'SORT' => $enum['SORT'],
618 'VALUE' => $enum['VALUE'],
619 'DEF' => $enum['DEF'],
620 'XML_ID' => $enum['XML_ID'],
621 );
622 }
623 }
624 elseif(preg_match("/^".UserField\Callback::USER_TYPE_ID_PREFIX."_([\d]+)_/", $userTypeID, $matches))
625 {
626 $fields['USER_TYPE_ID'] = str_replace($matches[0], '', $fields['USER_TYPE_ID']);
627
628 $appInfo = AppTable::getByClientId($matches[1]);
629 $fields['USER_TYPE_OWNER'] = $appInfo['CLIENT_ID'];
630 }
631
632 $result[] = $fields;
633 }
634
635 $result['total'] = count($result);
636 return $result;
637 }
638 public function update($ID, array $fields)
639 {
640 global $APPLICATION;
641
642 if(!is_int($ID))
643 {
644 $ID = (int)$ID;
645 }
646
647 if($ID <= 0)
648 {
649 throw new RestException('ID is not defined or invalid.');
650 }
651
652 if(!$this->checkUpdatePermission())
653 {
654 throw new RestException('Access denied.');
655 }
656
657 if($this->entityID === '')
658 {
659 throw new RestException('Operation is not allowed. Entity ID is not defined.');
660 }
661
662 $entity = new \CUserTypeEntity();
663
664 $persistedFields = $entity->GetByID($ID);
665 if(!is_array($persistedFields))
666 {
667 throw new RestException("The entity with ID '{$ID}' is not found.", RestException::ERROR_NOT_FOUND);
668 }
669
670 $entityID = isset($persistedFields['ENTITY_ID']) ? $persistedFields['ENTITY_ID'] : '';
671 if($entityID !== $this->entityID)
672 {
673 throw new RestException('Access denied.');
674 }
675
676 //User type ID can't be changed.
677 $userTypeID = isset($persistedFields['USER_TYPE_ID']) ? $persistedFields['USER_TYPE_ID'] : '';
678 if($userTypeID === '')
679 {
680 throw new RestException("Could not find 'USER_TYPE_ID' in persisted entity with ID '{$ID}'.");
681 }
682
683 $isMultiple = isset($persistedFields['MULTIPLE']) && $persistedFields['MULTIPLE'] === 'Y';
684
686
687 if(isset($fields['LIST_FILTER_LABEL']))
688 {
689 self::prepareLabels($fields, 'LIST_FILTER_LABEL', '');
690 }
691 elseif(isset($persistedFields['LIST_FILTER_LABEL']))
692 {
693 $fields['LIST_FILTER_LABEL'] = $persistedFields['LIST_FILTER_LABEL'];
694 }
695
696 if(isset($fields['LIST_COLUMN_LABEL']))
697 {
698 self::prepareLabels($fields, 'LIST_COLUMN_LABEL', '');
699 }
700 elseif(isset($persistedFields['LIST_COLUMN_LABEL']))
701 {
702 $fields['LIST_COLUMN_LABEL'] = $persistedFields['LIST_COLUMN_LABEL'];
703 }
704
705 if(isset($fields['EDIT_FORM_LABEL']))
706 {
707 self::prepareLabels($fields, 'EDIT_FORM_LABEL', '');
708 }
709 elseif(isset($persistedFields['EDIT_FORM_LABEL']))
710 {
711 $fields['EDIT_FORM_LABEL'] = $persistedFields['EDIT_FORM_LABEL'];
712 }
713
714 if(isset($fields['ERROR_MESSAGE']))
715 {
716 self::prepareLabels($fields, 'ERROR_MESSAGE', '');
717 }
718 elseif(isset($persistedFields['ERROR_MESSAGE']))
719 {
720 $fields['ERROR_MESSAGE'] = $persistedFields['ERROR_MESSAGE'];
721 }
722
723 if(isset($fields['HELP_MESSAGE']))
724 {
725 self::prepareLabels($fields, 'HELP_MESSAGE', '');
726 }
727 elseif(isset($persistedFields['HELP_MESSAGE']))
728 {
729 $fields['HELP_MESSAGE'] = $persistedFields['HELP_MESSAGE'];
730 }
731
732 $settings = isset($fields['SETTINGS']) && is_array($fields['SETTINGS']) ? $fields['SETTINGS'] : array();
733 $effectiveSettings = isset($persistedFields['SETTINGS']) && is_array($persistedFields['SETTINGS'])
734 ? $persistedFields['SETTINGS'] : array();
735
736 if(isset($fields['SHOW_FILTER']))
737 {
738 $fields['SHOW_FILTER'] = mb_strtoupper($fields['SHOW_FILTER']) === 'Y' ? 'E' : 'N'; // E - 'By mask' is default
739 }
740
741 switch ($userTypeID)
742 {
743 case 'string':
744 {
745 if(isset($settings['DEFAULT_VALUE']))
746 {
747 $effectiveSettings['DEFAULT_VALUE'] = $settings['DEFAULT_VALUE'];
748 }
749
750 if(isset($settings['ROWS']))
751 {
752 $effectiveSettings['ROWS'] = min(max($settings['ROWS'], 1), 50);
753 }
754 break;
755 }
756 case 'integer':
757 {
758 if(isset($settings['DEFAULT_VALUE']))
759 {
760 $effectiveSettings['DEFAULT_VALUE'] = $settings['DEFAULT_VALUE'];
761 }
762 break;
763 }
764 case 'double':
765 {
766 if(isset($settings['DEFAULT_VALUE']))
767 {
768 $effectiveSettings['DEFAULT_VALUE'] = $settings['DEFAULT_VALUE'];
769 }
770
771 if(isset($settings['PRECISION']))
772 {
773 $effectiveSettings['PRECISION'] = $settings['PRECISION'] >= 0
774 ? $settings['PRECISION'] : 2;
775 }
776 break;
777 }
778 case 'boolean':
779 {
780 if(isset($settings['DEFAULT_VALUE']))
781 {
782 $effectiveSettings['DEFAULT_VALUE'] = $settings['DEFAULT_VALUE'] > 0 ? 1 : 0;
783 }
784
785 if(isset($settings['DISPLAY']))
786 {
787 $effectiveSettings['DISPLAY'] = $settings['DISPLAY'] !== ''
788 ? mb_strtoupper($settings['DISPLAY']) : 'CHECKBOX';
789 }
790
791 unset($fields['MULTIPLE']);
792 break;
793 }
794 case 'datetime':
795 {
796 if(isset($settings['DEFAULT_VALUE']))
797 {
798 $defaultValue = $settings['DEFAULT_VALUE'];
799 if(!is_array($defaultValue))
800 {
801 $defaultValue = array('VALUE' => $defaultValue, 'TYPE' => 'NONE');
802 }
803
804 $effectiveSettings['DEFAULT_VALUE'] = array(
805 'VALUE' => isset($defaultValue['VALUE'])
806 ? \CRestUtil::unConvertDateTime($defaultValue['VALUE']) : '',
807 'TYPE' => isset($defaultValue['TYPE']) && $defaultValue['TYPE'] !== ''
808 ? mb_strtoupper($defaultValue['TYPE']) : 'NONE'
809 );
810 }
811 break;
812 }
813 case 'enumeration':
814 {
815 if(isset($settings['DISPLAY']))
816 {
817 $effectiveSettings['DISPLAY'] = $settings['DISPLAY'] !== ''
818 ? mb_strtoupper($settings['DISPLAY']) : 'LIST';
819 }
820
821 if(isset($settings['LIST_HEIGHT']))
822 {
823 $effectiveSettings['LIST_HEIGHT'] = $settings['LIST_HEIGHT'] > 0 ? $settings['LIST_HEIGHT'] : 1;
824 }
825
826 if(isset($fields['LIST']))
827 {
828 $listItems = is_array($fields['LIST']) ? $fields['LIST'] : array();
829 $effectiveListItems = array();
830
831 $counter = 0;
832 $defaultItemKey = '';
833 foreach($listItems as $item)
834 {
835 $itemValue = isset($item['VALUE']) ? trim($item['VALUE'], " \t\n\r") : '';
836
837 $effectiveItem = array('VALUE' => $itemValue);
838 $itemXmlID = isset($item['XML_ID']) ? $item['XML_ID'] : '';
839 if($itemXmlID !== '')
840 {
841 $effectiveItem['XML_ID'] = $itemXmlID;
842 }
843 $itemSort = isset($item['SORT']) && is_numeric($item['SORT']) ? (int)$item['SORT'] : 0;
844 if($itemSort > 0)
845 {
846 $effectiveItem['SORT'] = $itemSort;
847 }
848
849 $itemID = isset($item['ID']) && is_numeric($item['ID']) ? (int)$item['ID'] : 0;
850 if($itemID > 0)
851 {
852 $itemKey = strval($itemID);
853 if(isset($item['DEL']) && mb_strtoupper($item['DEL']) === 'Y')
854 {
855 $effectiveItem['DEL'] = 'Y';
856 }
857 }
858 else
859 {
860 $itemKey = "n{$counter}";
861 $counter++;
862 }
863
864 if(isset($item['DEF']))
865 {
866 $isDefault = mb_strtoupper($item['DEF']) === 'Y';
867 if($isMultiple)
868 {
869 $effectiveItem['DEF'] = $isDefault ? 'Y' : 'N';
870 }
871 elseif($isDefault && $defaultItemKey === '')
872 {
873 $defaultItemKey = $itemKey;
874 }
875 }
876
877 if(!empty($item))
878 {
879 $effectiveListItems[$itemKey] = &$effectiveItem;
880 }
881 unset($effectiveItem);
882 }
883
884 if(!$isMultiple && $defaultItemKey !== '')
885 {
886 foreach($effectiveListItems as $key => &$item)
887 {
888 $item['DEF'] = $key === $defaultItemKey ? 'Y' : 'N';
889 }
890 unset($item);
891 }
892 $fields['LIST'] = $effectiveListItems;
893 }
894
895 break;
896 }
897 case 'iblock_section':
898 case 'iblock_element':
899 {
900 if(isset($settings['IBLOCK_TYPE_ID']))
901 {
902 $effectiveSettings['IBLOCK_TYPE_ID'] = $settings['IBLOCK_TYPE_ID'];
903 }
904
905 if(isset($settings['IBLOCK_ID']))
906 {
907 $effectiveSettings['IBLOCK_ID'] = $settings['IBLOCK_ID'] > 0 ? $settings['IBLOCK_ID'] : 0;
908 }
909
910 if(isset($settings['DEFAULT_VALUE']))
911 {
912 $effectiveSettings['DEFAULT_VALUE'] = $settings['DEFAULT_VALUE'];
913 }
914
915 if(isset($settings['DISPLAY']))
916 {
917 $effectiveSettings['DISPLAY'] = $settings['DISPLAY'] !== ''
918 ? mb_strtoupper($settings['DISPLAY']) : 'LIST';
919 }
920
921 if(isset($settings['LIST_HEIGHT']))
922 {
923 $effectiveSettings['LIST_HEIGHT'] = $settings['LIST_HEIGHT'] > 0 ? $settings['LIST_HEIGHT'] : 1;
924 }
925
926 if(isset($settings['ACTIVE_FILTER']))
927 {
928 $effectiveSettings['ACTIVE_FILTER'] = mb_strtoupper($settings['ACTIVE_FILTER']) === 'Y' ? 'Y' : 'N';
929 }
930
931 break;
932 }
933 case 'crm_status':
934 {
935 if(isset($settings['ENTITY_TYPE']))
936 {
937 $effectiveSettings['ENTITY_TYPE'] = $settings['ENTITY_TYPE'];
938 }
939 break;
940 }
941 case 'crm':
942 {
943 if(isset($settings['LEAD']))
944 {
945 $effectiveSettings['LEAD'] = mb_strtoupper($settings['LEAD']) === 'Y' ? 'Y' : 'N';
946 }
947
948 if(isset($settings['CONTACT']))
949 {
950 $effectiveSettings['CONTACT'] = mb_strtoupper($settings['CONTACT']) === 'Y' ? 'Y' : 'N';
951 }
952
953 if(isset($settings['COMPANY']))
954 {
955 $effectiveSettings['COMPANY'] = mb_strtoupper($settings['COMPANY']) === 'Y' ? 'Y' : 'N';
956 }
957
958 if(isset($settings['DEAL']))
959 {
960 $effectiveSettings['DEAL'] = mb_strtoupper($settings['DEAL']) === 'Y' ? 'Y' : 'N';
961 }
962 break;
963 }
964 case 'employee':
965 {
966 if(isset($fields['SHOW_FILTER']) && $fields['SHOW_FILTER'] !== 'N')
967 {
968 $fields['SHOW_FILTER'] = 'I'; // Force exact match for 'USER' field type
969 }
970 break;
971 }
972 }
973
974 $fields['SETTINGS'] = $effectiveSettings;
975
976 if($entity->Update($ID, $fields) === false)
977 {
978 $exc = $APPLICATION->GetException();
979 throw new RestException(
980 $exc !== false ? $exc->GetString() : 'Fail to update user field.',
982 );
983 }
984 elseif($userTypeID === 'enumeration' && isset($fields['LIST']) && is_array($fields['LIST']))
985 {
986 $enum = new \CUserFieldEnum();
987 if(!$enum->SetEnumValues($ID, $fields['LIST']))
988 {
989 $exc = $APPLICATION->GetException();
990 throw new RestException(
991 $exc !== false ? $exc->GetString() : 'Fail to save enumumeration field values.',
993 );
994 }
995 }
996 return true;
997 }
998 public function delete($ID)
999 {
1000 global $APPLICATION;
1001
1002 if(!is_int($ID))
1003 {
1004 $ID = (int)$ID;
1005 }
1006
1007 if($ID <= 0)
1008 {
1009 throw new RestException('ID is not defined or invalid.');
1010 }
1011
1012 if(!$this->checkDeletePermission())
1013 {
1014 throw new RestException('Access denied.');
1015 }
1016
1017 $entity = new \CUserTypeEntity();
1018 $dbResult = $entity->GetList(array(), array('ID' => $ID));
1019 $persistedFields = is_object($dbResult) ? $dbResult->Fetch() : null;
1020 if(!is_array($persistedFields))
1021 {
1022 throw new RestException("The entity with ID '{$ID}' is not found.", RestException::ERROR_NOT_FOUND);
1023 }
1024
1025 $entityID = isset($persistedFields['ENTITY_ID']) ? $persistedFields['ENTITY_ID'] : '';
1026 if($entityID !== $this->entityID)
1027 {
1028 throw new RestException('Access denied.');
1029 }
1030
1031 if($entity->Delete($ID) === false)
1032 {
1033 $exc = $APPLICATION->GetException();
1034 throw new RestException(
1035 $exc !== false ? $exc->GetString() : 'Fail to delete user field.',
1037 );
1038 }
1039 return true;
1040 }
1041
1042 protected static function sanitizeFields(array &$fields)
1043 {
1044 $fieldsInfo = self::getFields();
1045 foreach($fields as $k => $v)
1046 {
1047 if(!isset($fieldsInfo[$k]))
1048 {
1049 unset($fields[$k]);
1050 }
1051 }
1052 }
1053 protected function isAuthorizedUser()
1054 {
1055 if($this->isAuthorizedUser === null)
1056 {
1057 $this->isAuthorizedUser = $this->user->IsAuthorized();
1058 }
1060 }
1061 protected function isAdminUser()
1062 {
1063 if($this->isAdminUser !== null)
1064 {
1065 return $this->isAdminUser;
1066 }
1067
1068 $this->isAdminUser = $this->user->IsAdmin();
1069
1070 if(!$this->isAdminUser
1072 && \Bitrix\Main\Loader::includeModule('bitrix24'))
1073 {
1074 try
1075 {
1076 $this->isAdminUser = \CBitrix24::IsPortalAdmin($this->user->GetID());
1077 }
1078 catch(\Exception $exc)
1079 {
1080 }
1081 }
1082
1083 return $this->isAdminUser;
1084 }
1085 protected function getCurrentUser()
1086 {
1087 return isset($USER) && ((get_class($USER) === 'CUser') || ($USER instanceof \CUser))
1088 ? $USER : (new \CUser());
1089 }
1090 protected static function getAllLanguages()
1091 {
1092 if(self::$langs !== null)
1093 {
1094 return self::$langs;
1095 }
1096
1097 self::$langs = array();
1098 $entity = new \CLanguage();
1099 $dbResult = $entity->GetList();
1100 while($lang = $dbResult->Fetch())
1101 {
1102 self::$langs[$lang['LID']] = array('LID' => $lang['LID'], 'NAME' => $lang['NAME']);
1103 }
1104 return self::$langs;
1105 }
1106 protected static function prepareLabels(array &$fields, $name, $defaultLabel)
1107 {
1108 $label = isset($fields[$name]) ? $fields[$name] : null;
1109 if(is_string($label) && $label !== '')
1110 {
1111 $labels = array();
1112 $default = $label;
1113 }
1114 else
1115 {
1116 $labels = is_array($label) ? $label : array();
1117 $default = $defaultLabel;
1118 }
1119
1120 $langIDs = array_keys(self::getAllLanguages());
1121 $fields[$name] = array();
1122 foreach($langIDs as $lid)
1123 {
1124 $fields[$name][$lid] = isset($labels[$lid]) && is_string($labels[$lid]) && $labels[$lid] !== ''
1125 ? $labels[$lid] : $default;
1126 }
1127 }
1128 protected function checkCreatePermission()
1129 {
1130 return $this->isAdminUser();
1131 }
1132 protected function checkReadPermission()
1133 {
1134 return $this->isAdminUser();
1135 }
1136 protected function checkUpdatePermission()
1137 {
1138 return $this->isAdminUser();
1139 }
1140 protected function checkDeletePermission()
1141 {
1142 return $this->isAdminUser();
1143 }
1144
1145 private static function includeLangFile()
1146 {
1147 if(!self::$langIncluded)
1148 {
1149 self::$langIncluded = IncludeModuleLangFile(__FILE__);
1150 }
1151 }
1152}
if($_SERVER $defaultValue['REQUEST_METHOD']==="GET" &&!empty($RestoreDefaults) && $bizprocPerms==="W" &&check_bitrix_sessid())
Определения options.php:32
global $APPLICATION
Определения include.php:80
static isModuleInstalled($moduleName)
Определения modulemanager.php:125
static getByClientId($clientId)
Определения app.php:967
static getHandlersList($placement, $skipInstallCheck=false, int $userId=null)
Определения placement.php:266
const ERROR_CORE
Определения restexception.php:15
const ERROR_NOT_FOUND
Определения restexception.php:17
static getUserTypeId($userTypeInfo)
Определения callback.php:88
__construct($entityID, \CUser $user=null)
Определения userfieldproxy.php:19
static getSettingsFields($typeID)
Определения userfieldproxy.php:92
getList(array $order, array $filter)
Определения userfieldproxy.php:567
add(array $fields)
Определения userfieldproxy.php:222
static getFields()
Определения userfieldproxy.php:47
static sanitizeFields(array &$fields)
Определения userfieldproxy.php:1042
static getTypes(\CRestServer $server=null)
Определения userfieldproxy.php:181
setNamePrefix($prefix)
Определения userfieldproxy.php:38
update($ID, array $fields)
Определения userfieldproxy.php:638
static getAllLanguages()
Определения userfieldproxy.php:1090
static getEnumerationElementFields()
Определения userfieldproxy.php:81
static prepareLabels(array &$fields, $name, $defaultLabel)
Определения userfieldproxy.php:1106
</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
if($ajaxMode) $ID
Определения get_user.php:27
$entity
$errors
Определения iblock_catalog_edit.php:74
$filter
Определения iblock_catalog_list.php:54
global $USER
Определения csv_new_run.php:40
if(!defined('SITE_ID')) $lang
Определения include.php:91
IncludeModuleLangFile($filepath, $lang=false, $bReturnArray=false)
Определения tools.php:3778
GetMessage($name, $aReplace=null)
Определения tools.php:3397
$name
Определения menu_edit.php:35
Определения auth.php:9
$order
Определения payment.php:8
$counter
Определения options.php:5
$settings
Определения product_settings.php:43
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
if(empty($signedUserToken)) $key
Определения quickway.php:257
</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
$matches
Определения index.php:22
$k
Определения template_pdf.php:567
$dbResult
Определения updtr957.php:3
$fields
Определения yandex_run.php:501