222 public function add(array $fields)
230 if($this->entityID ===
'')
232 throw new RestException(
'Operation is not allowed. Entity ID is not defined.');
236 $defaultLabel = isset($fields[
'LABEL']) ? trim($fields[
'LABEL']) :
'';
242 $userTypeID = isset($fields[
'USER_TYPE_ID']) ? trim($fields[
'USER_TYPE_ID']) :
'';
243 if($userTypeID ===
'')
245 $errors[] =
"The 'USER_TYPE_ID' field is not found.";
247 $fields[
'USER_TYPE_ID'] = $userTypeID;
249 $fieldName = isset($fields[
'FIELD_NAME']) ? trim($fields[
'FIELD_NAME']) :
'';
250 if($fieldName ===
'')
252 $errors[] =
"The 'FIELD_NAME' field is not found.";
255 $fieldName = mb_strtoupper($fieldName);
259 $fullPrefix =
'UF_'.$prefix.
'_';
260 $fullPrefixLen = mb_strlen($fullPrefix);
261 if(strncmp($fieldName, $fullPrefix, $fullPrefixLen) !== 0)
263 $fieldName = strncmp($fieldName,
'UF_', 3) === 0
264 ? $fullPrefix.mb_substr($fieldName, 3)
265 : $fullPrefix.$fieldName;
272 if(strncmp($fieldName, $fullPrefix, $fullPrefixLen) !== 0)
274 $fieldName =
'UF_'. $fieldName;
278 $fields[
'FIELD_NAME'] = $fieldName;
285 if($defaultLabel ===
'')
287 $defaultLabel = $fieldName;
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';
300 $isMultiple = isset($fields[
'MULTIPLE']) && $fields[
'MULTIPLE'] ===
'Y';
302 $settings = isset($fields[
'SETTINGS']) && is_array($fields[
'SETTINGS']) ? $fields[
'SETTINGS'] : array();
303 $effectiveSettings = array();
308 $effectiveSettings[
'DEFAULT_VALUE'] = isset($settings[
'DEFAULT_VALUE'])
309 ? $settings[
'DEFAULT_VALUE'] :
'';
311 $effectiveSettings[
'ROWS'] = $settings[
'ROWS'] > 0
312 ? $settings[
'ROWS'] : 1;
317 $effectiveSettings[
'DEFAULT_VALUE'] = isset($settings[
'DEFAULT_VALUE'])
318 ? $settings[
'DEFAULT_VALUE'] :
'';
323 $effectiveSettings[
'DEFAULT_VALUE'] = isset($settings[
'DEFAULT_VALUE'])
324 ? $settings[
'DEFAULT_VALUE'] :
'';
326 $effectiveSettings[
'PRECISION'] = $settings[
'PRECISION'] >= 0
327 ? $settings[
'PRECISION'] : 2;
332 $effectiveSettings[
'DEFAULT_VALUE'] = isset($settings[
'DEFAULT_VALUE'])
333 && $settings[
'DEFAULT_VALUE'] > 0 ? 1 : 0;
335 $display = isset($settings[
'DISPLAY']) ? $settings[
'DISPLAY'] :
'';
336 $effectiveSettings[
'DISPLAY'] = $display !==
''? mb_strtoupper($display) :
'CHECKBOX';
338 $fields[
'MULTIPLE'] =
'N';
344 $defaultValue = isset($settings[
'DEFAULT_VALUE']) ? $settings[
'DEFAULT_VALUE'] : array();
345 if(!is_array($defaultValue))
347 $defaultValue = array(
'VALUE' => $defaultValue,
'TYPE' =>
'NONE');
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'
360 $display = isset($settings[
'DISPLAY']) ? $settings[
'DISPLAY'] :
'';
361 $effectiveSettings[
'DISPLAY'] = $display !==
''? mb_strtoupper($display) :
'LIST';
363 $height = isset($settings[
'LIST_HEIGHT']) ? (int)$settings[
'LIST_HEIGHT'] : 0;
364 $effectiveSettings[
'LIST_HEIGHT'] = $height > 0 ? $height : 1;
366 $listItems = isset($fields[
'LIST']) && is_array($fields[
'LIST']) ? $fields[
'LIST'] : array();
367 $effectiveListItems = array();
370 $defaultItemKey =
'';
371 foreach($listItems as $item)
373 $itemValue = isset($item[
'VALUE']) ? trim($item[
'VALUE'],
" \t\n\r") :
'';
374 if($itemValue ===
'')
379 $effectiveItem = array(
'VALUE' => $itemValue);
380 $itemSort = isset($item[
'SORT']) && is_numeric($item[
'SORT']) ? (int)$item[
'SORT'] : 0;
383 $effectiveItem[
'SORT'] = $itemSort;
388 $effectiveItem[
'SORT'] = $itemSort;
391 $itemKey =
"n{$counter}";
394 if(isset($item[
'DEF']))
396 $isDefault = mb_strtoupper($item[
'DEF']) ===
'Y';
399 $effectiveItem[
'DEF'] = $isDefault ?
'Y' :
'N';
401 elseif($isDefault && $defaultItemKey ===
'')
403 $defaultItemKey = $itemKey;
407 $effectiveListItems[$itemKey] = &$effectiveItem;
408 unset($effectiveItem);
411 if(!$isMultiple && $defaultItemKey !==
'')
413 foreach($effectiveListItems as $key => &$item)
415 $item[
'DEF'] = $key === $defaultItemKey ?
'Y' :
'N';
419 $fields[
'LIST'] = $effectiveListItems;
422 case 'iblock_section':
423 case 'iblock_element':
425 $effectiveSettings[
'IBLOCK_TYPE_ID'] = isset($settings[
'IBLOCK_TYPE_ID']) ? $settings[
'IBLOCK_TYPE_ID'] :
'';
426 $effectiveSettings[
'IBLOCK_ID'] = isset($settings[
'IBLOCK_ID']) ? (int)$settings[
'IBLOCK_ID'] : 0;
427 $effectiveSettings[
'DEFAULT_VALUE'] = isset($settings[
'DEFAULT_VALUE']) ? $settings[
'DEFAULT_VALUE'] :
'';
429 $display = isset($settings[
'DISPLAY']) ? $settings[
'DISPLAY'] :
'';
430 $effectiveSettings[
'DISPLAY'] = $display !==
''? mb_strtoupper($display) :
'LIST';
432 $height = isset($settings[
'LIST_HEIGHT']) ? (int)$settings[
'LIST_HEIGHT'] : 0;
433 $effectiveSettings[
'LIST_HEIGHT'] = $height > 0 ? $height : 1;
434 $effectiveSettings[
'ACTIVE_FILTER'] = isset($settings[
'ACTIVE_FILTER'])
435 && mb_strtoupper($settings[
'ACTIVE_FILTER']) ===
'Y' ?
'Y' :
'N';
440 $effectiveSettings[
'ENTITY_TYPE'] = isset($settings[
'ENTITY_TYPE']) ? $settings[
'ENTITY_TYPE'] :
'';
445 $effectiveSettings[
'LEAD'] = isset($settings[
'LEAD']) && mb_strtoupper($settings[
'LEAD']) ===
'Y' ?
'Y' :
'N';
446 $effectiveSettings[
'CONTACT'] = isset($settings[
'CONTACT']) && mb_strtoupper($settings[
'CONTACT']) ===
'Y' ?
'Y' :
'N';
447 $effectiveSettings[
'COMPANY'] = isset($settings[
'COMPANY']) && mb_strtoupper($settings[
'COMPANY']) ===
'Y' ?
'Y' :
'N';
448 $effectiveSettings[
'DEAL'] = isset($settings[
'DEAL']) && mb_strtoupper($settings[
'DEAL']) ===
'Y' ?
'Y' :
'N';
453 if($fields[
'SHOW_FILTER'] !==
'N')
455 $fields[
'SHOW_FILTER'] =
'I';
463 foreach($userTypeList as $userType)
465 if($userType[
'ADDITIONAL'] === $userTypeID)
467 $fields[
'USER_TYPE_ID'] = UserField\Callback::getUserTypeId($userType);
471 $fields[
'SHOW_FILTER'] =
'N';
475 $fields[
'SETTINGS'] = $effectiveSettings;
477 $entity = new \CUserTypeEntity();
478 $ID = $entity->Add($fields);
481 $exc = $APPLICATION->GetException();
482 $errors[] = $exc !==
false ? $exc->GetString() :
'Fail to create new user field.';
484 elseif ($userTypeID ===
'enumeration' && isset($fields[
'LIST']) && is_array($fields[
'LIST']))
486 $enum = new \CUserFieldEnum();
487 if(!$enum->SetEnumValues($ID, $fields[
'LIST']))
489 $exc = $APPLICATION->GetException();
490 $errors[] = $exc !==
false ? $exc->GetString() :
'Fail to save enumumeration field values.';
562 public function getList(array $order, array $filter)
569 if($this->entityID ===
'')
571 throw new RestException(
'Operation is not allowed. Entity ID is not defined.');
575 if(isset($filter[
'USER_TYPE_ID']))
578 foreach($handlerList as $handler)
580 if($handler[
'ADDITIONAL'] === $filter[
'USER_TYPE_ID'])
582 $filter[
'USER_TYPE_ID'] = Callback::getUserTypeId($handler);
587 $entity = new \CUserTypeEntity();
588 $dbResult = $entity->GetList($order, $filter);
590 while($fields = $dbResult->Fetch())
592 $userTypeID = isset($fields[
'USER_TYPE_ID']) ? $fields[
'USER_TYPE_ID'] :
'';
593 if($userTypeID ===
'datetime'
594 && isset($fields[
'SETTINGS'])
595 && isset($fields[
'SETTINGS'][
'DEFAULT_VALUE'])
596 && isset($fields[
'SETTINGS'][
'DEFAULT_VALUE'][
'VALUE'])
597 && $fields[
'SETTINGS'][
'DEFAULT_VALUE'][
'VALUE'] !==
'')
599 $fields[
'SETTINGS'][
'DEFAULT_VALUE'][
'VALUE'] = \CRestUtil::ConvertDateTime($fields[
'SETTINGS'][
'DEFAULT_VALUE'][
'VALUE']);
602 if($userTypeID ===
'enumeration')
604 $fields[
'LIST'] = array();
606 $enumEntity = new \CUserFieldEnum();
607 $dbResultEnum = $enumEntity->GetList(array(
'SORT' =>
'ASC'), array(
'USER_FIELD_ID' => $fields[
'ID']));
608 while($enum = $dbResultEnum->Fetch())
610 $fields[
'LIST'][] = array(
612 'SORT' => $enum[
'SORT'],
613 'VALUE' => $enum[
'VALUE'],
614 'DEF' => $enum[
'DEF']
618 elseif(preg_match(
"/^".UserField\Callback::USER_TYPE_ID_PREFIX.
"_([\d]+)_/", $userTypeID, $matches))
620 $fields[
'USER_TYPE_ID'] = str_replace($matches[0],
'', $fields[
'USER_TYPE_ID']);
623 $fields[
'USER_TYPE_OWNER'] = $appInfo[
'CLIENT_ID'];
629 $result[
'total'] = count($result);
632 public function update($ID, array $fields)
651 if($this->entityID ===
'')
653 throw new RestException(
'Operation is not allowed. Entity ID is not defined.');
656 $entity = new \CUserTypeEntity();
658 $persistedFields = $entity->GetByID($ID);
659 if(!is_array($persistedFields))
664 $entityID = isset($persistedFields[
'ENTITY_ID']) ? $persistedFields[
'ENTITY_ID'] :
'';
671 $userTypeID = isset($persistedFields[
'USER_TYPE_ID']) ? $persistedFields[
'USER_TYPE_ID'] :
'';
672 if($userTypeID ===
'')
674 throw new RestException(
"Could not find 'USER_TYPE_ID' in persisted entity with ID '{$ID}'.");
677 $isMultiple = isset($persistedFields[
'MULTIPLE']) && $persistedFields[
'MULTIPLE'] ===
'Y';
681 if(isset($fields[
'LIST_FILTER_LABEL']))
685 elseif(isset($persistedFields[
'LIST_FILTER_LABEL']))
687 $fields[
'LIST_FILTER_LABEL'] = $persistedFields[
'LIST_FILTER_LABEL'];
690 if(isset($fields[
'LIST_COLUMN_LABEL']))
694 elseif(isset($persistedFields[
'LIST_COLUMN_LABEL']))
696 $fields[
'LIST_COLUMN_LABEL'] = $persistedFields[
'LIST_COLUMN_LABEL'];
699 if(isset($fields[
'EDIT_FORM_LABEL']))
703 elseif(isset($persistedFields[
'EDIT_FORM_LABEL']))
705 $fields[
'EDIT_FORM_LABEL'] = $persistedFields[
'EDIT_FORM_LABEL'];
708 if(isset($fields[
'ERROR_MESSAGE']))
712 elseif(isset($persistedFields[
'ERROR_MESSAGE']))
714 $fields[
'ERROR_MESSAGE'] = $persistedFields[
'ERROR_MESSAGE'];
717 if(isset($fields[
'HELP_MESSAGE']))
721 elseif(isset($persistedFields[
'HELP_MESSAGE']))
723 $fields[
'HELP_MESSAGE'] = $persistedFields[
'HELP_MESSAGE'];
726 $settings = isset($fields[
'SETTINGS']) && is_array($fields[
'SETTINGS']) ? $fields[
'SETTINGS'] : array();
727 $effectiveSettings = isset($persistedFields[
'SETTINGS']) && is_array($persistedFields[
'SETTINGS'])
728 ? $persistedFields[
'SETTINGS'] : array();
730 if(isset($fields[
'SHOW_FILTER']))
732 $fields[
'SHOW_FILTER'] = mb_strtoupper($fields[
'SHOW_FILTER']) ===
'Y' ?
'E' :
'N';
739 if(isset($settings[
'DEFAULT_VALUE']))
741 $effectiveSettings[
'DEFAULT_VALUE'] = $settings[
'DEFAULT_VALUE'];
744 if(isset($settings[
'ROWS']))
746 $effectiveSettings[
'ROWS'] = min(max($settings[
'ROWS'], 1), 50);
752 if(isset($settings[
'DEFAULT_VALUE']))
754 $effectiveSettings[
'DEFAULT_VALUE'] = $settings[
'DEFAULT_VALUE'];
760 if(isset($settings[
'DEFAULT_VALUE']))
762 $effectiveSettings[
'DEFAULT_VALUE'] = $settings[
'DEFAULT_VALUE'];
765 if(isset($settings[
'PRECISION']))
767 $effectiveSettings[
'PRECISION'] = $settings[
'PRECISION'] >= 0
768 ? $settings[
'PRECISION'] : 2;
774 if(isset($settings[
'DEFAULT_VALUE']))
776 $effectiveSettings[
'DEFAULT_VALUE'] = $settings[
'DEFAULT_VALUE'] > 0 ? 1 : 0;
779 if(isset($settings[
'DISPLAY']))
781 $effectiveSettings[
'DISPLAY'] = $settings[
'DISPLAY'] !==
''
782 ? mb_strtoupper($settings[
'DISPLAY']) :
'CHECKBOX';
785 unset($fields[
'MULTIPLE']);
790 if(isset($settings[
'DEFAULT_VALUE']))
792 $defaultValue = $settings[
'DEFAULT_VALUE'];
793 if(!is_array($defaultValue))
795 $defaultValue = array(
'VALUE' => $defaultValue,
'TYPE' =>
'NONE');
798 $effectiveSettings[
'DEFAULT_VALUE'] = array(
799 'VALUE' => isset($defaultValue[
'VALUE'])
800 ? \CRestUtil::unConvertDateTime($defaultValue[
'VALUE']) :
'',
801 'TYPE' => isset($defaultValue[
'TYPE']) && $defaultValue[
'TYPE'] !==
''
802 ? mb_strtoupper($defaultValue[
'TYPE']) :
'NONE'
809 if(isset($settings[
'DISPLAY']))
811 $effectiveSettings[
'DISPLAY'] = $settings[
'DISPLAY'] !==
''
812 ? mb_strtoupper($settings[
'DISPLAY']) :
'LIST';
815 if(isset($settings[
'LIST_HEIGHT']))
817 $effectiveSettings[
'LIST_HEIGHT'] = $settings[
'LIST_HEIGHT'] > 0 ? $settings[
'LIST_HEIGHT'] : 1;
820 if(isset($fields[
'LIST']))
822 $listItems = is_array($fields[
'LIST']) ? $fields[
'LIST'] : array();
823 $effectiveListItems = array();
826 $defaultItemKey =
'';
827 foreach($listItems as $item)
829 $itemValue = isset($item[
'VALUE']) ? trim($item[
'VALUE'],
" \t\n\r") :
'';
831 $effectiveItem = array(
'VALUE' => $itemValue);
832 $itemXmlID = isset($item[
'XML_ID']) ? $item[
'XML_ID'] :
'';
833 if($itemXmlID !==
'')
835 $effectiveItem[
'XML_ID'] = $itemXmlID;
837 $itemSort = isset($item[
'SORT']) && is_numeric($item[
'SORT']) ? (int)$item[
'SORT'] : 0;
840 $effectiveItem[
'SORT'] = $itemSort;
843 $itemID = isset($item[
'ID']) && is_numeric($item[
'ID']) ? (int)$item[
'ID'] : 0;
846 $itemKey = strval($itemID);
847 if(isset($item[
'DEL']) && mb_strtoupper($item[
'DEL']) ===
'Y')
849 $effectiveItem[
'DEL'] =
'Y';
854 $itemKey =
"n{$counter}";
858 if(isset($item[
'DEF']))
860 $isDefault = mb_strtoupper($item[
'DEF']) ===
'Y';
863 $effectiveItem[
'DEF'] = $isDefault ?
'Y' :
'N';
865 elseif($isDefault && $defaultItemKey ===
'')
867 $defaultItemKey = $itemKey;
873 $effectiveListItems[$itemKey] = &$effectiveItem;
875 unset($effectiveItem);
878 if(!$isMultiple && $defaultItemKey !==
'')
880 foreach($effectiveListItems as $key => &$item)
882 $item[
'DEF'] = $key === $defaultItemKey ?
'Y' :
'N';
886 $fields[
'LIST'] = $effectiveListItems;
891 case 'iblock_section':
892 case 'iblock_element':
894 if(isset($settings[
'IBLOCK_TYPE_ID']))
896 $effectiveSettings[
'IBLOCK_TYPE_ID'] = $settings[
'IBLOCK_TYPE_ID'];
899 if(isset($settings[
'IBLOCK_ID']))
901 $effectiveSettings[
'IBLOCK_ID'] = $settings[
'IBLOCK_ID'] > 0 ? $settings[
'IBLOCK_ID'] : 0;
904 if(isset($settings[
'DEFAULT_VALUE']))
906 $effectiveSettings[
'DEFAULT_VALUE'] = $settings[
'DEFAULT_VALUE'];
909 if(isset($settings[
'DISPLAY']))
911 $effectiveSettings[
'DISPLAY'] = $settings[
'DISPLAY'] !==
''
912 ? mb_strtoupper($settings[
'DISPLAY']) :
'LIST';
915 if(isset($settings[
'LIST_HEIGHT']))
917 $effectiveSettings[
'LIST_HEIGHT'] = $settings[
'LIST_HEIGHT'] > 0 ? $settings[
'LIST_HEIGHT'] : 1;
920 if(isset($settings[
'ACTIVE_FILTER']))
922 $effectiveSettings[
'ACTIVE_FILTER'] = mb_strtoupper($settings[
'ACTIVE_FILTER']) ===
'Y' ?
'Y' :
'N';
929 if(isset($settings[
'ENTITY_TYPE']))
931 $effectiveSettings[
'ENTITY_TYPE'] = $settings[
'ENTITY_TYPE'];
937 if(isset($settings[
'LEAD']))
939 $effectiveSettings[
'LEAD'] = mb_strtoupper($settings[
'LEAD']) ===
'Y' ?
'Y' :
'N';
942 if(isset($settings[
'CONTACT']))
944 $effectiveSettings[
'CONTACT'] = mb_strtoupper($settings[
'CONTACT']) ===
'Y' ?
'Y' :
'N';
947 if(isset($settings[
'COMPANY']))
949 $effectiveSettings[
'COMPANY'] = mb_strtoupper($settings[
'COMPANY']) ===
'Y' ?
'Y' :
'N';
952 if(isset($settings[
'DEAL']))
954 $effectiveSettings[
'DEAL'] = mb_strtoupper($settings[
'DEAL']) ===
'Y' ?
'Y' :
'N';
960 if(isset($fields[
'SHOW_FILTER']) && $fields[
'SHOW_FILTER'] !==
'N')
962 $fields[
'SHOW_FILTER'] =
'I';
968 $fields[
'SETTINGS'] = $effectiveSettings;
970 if($entity->Update($ID, $fields) ===
false)
972 $exc = $APPLICATION->GetException();
974 $exc !==
false ? $exc->GetString() :
'Fail to update user field.',
978 elseif($userTypeID ===
'enumeration' && isset($fields[
'LIST']) && is_array($fields[
'LIST']))
980 $enum = new \CUserFieldEnum();
981 if(!$enum->SetEnumValues($ID, $fields[
'LIST']))
983 $exc = $APPLICATION->GetException();
985 $exc !==
false ? $exc->GetString() :
'Fail to save enumumeration field values.',
1102 $label = isset($fields[$name]) ? $fields[$name] :
null;
1103 if(is_string($label) && $label !==
'')
1110 $labels = is_array($label) ? $label : array();
1111 $default = $defaultLabel;
1114 $langIDs = array_keys(self::getAllLanguages());
1115 $fields[$name] = array();
1116 foreach($langIDs as $lid)
1118 $fields[$name][$lid] = isset($labels[$lid]) && is_string($labels[$lid]) && $labels[$lid] !==
''
1119 ? $labels[$lid] : $default;