- Модуль: rest
- Путь к файлу: ~/bitrix/modules/rest/lib/userfieldproxy.php
- Класс: BitrixRestUserFieldProxy
- Вызов: UserFieldProxy::add
public function add(array $fields)
{
global $APPLICATION;
if(!$this->checkCreatePermission())
{
throw new RestException('Access denied.');
}
if($this->entityID === '')
{
throw new RestException('Operation is not allowed. Entity ID is not defined.');
}
//Try get default field label
$defaultLabel = isset($fields['LABEL']) ? trim($fields['LABEL']) : '';
self::sanitizeFields($fields);
$fields['ENTITY_ID'] = $this->entityID;
$errors = array();
$userTypeID = isset($fields['USER_TYPE_ID']) ? trim($fields['USER_TYPE_ID']) : '';
if($userTypeID === '')
{
$errors[] = "The 'USER_TYPE_ID' field is not found.";
}
$fields['USER_TYPE_ID'] = $userTypeID;
$fieldName = isset($fields['FIELD_NAME']) ? trim($fields['FIELD_NAME']) : '';
if($fieldName === '')
{
$errors[] = "The 'FIELD_NAME' field is not found.";
}
$fieldName = mb_strtoupper($fieldName);
$prefix = $this->namePrefix;
if($prefix !== '')
{
$fullPrefix = 'UF_'.$prefix.'_';
$fullPrefixLen = mb_strlen($fullPrefix);
if(strncmp($fieldName, $fullPrefix, $fullPrefixLen) !== 0)
{
$fieldName = strncmp($fieldName, 'UF_', 3) === 0
? $fullPrefix.mb_substr($fieldName, 3)
: $fullPrefix.$fieldName;
}
}
else
{
$fullPrefix = 'UF_';
$fullPrefixLen = 3;
if(strncmp($fieldName, $fullPrefix, $fullPrefixLen) !== 0)
{
$fieldName = 'UF_'. $fieldName;
}
}
$fields['FIELD_NAME'] = $fieldName;
if(!empty($errors))
{
throw new RestException(implode("n", $errors));
}
if($defaultLabel === '')
{
$defaultLabel = $fieldName;
}
self::prepareLabels($fields, 'LIST_FILTER_LABEL', $defaultLabel);
self::prepareLabels($fields, 'LIST_COLUMN_LABEL', $defaultLabel);
self::prepareLabels($fields, 'EDIT_FORM_LABEL', $defaultLabel);
self::prepareLabels($fields, 'ERROR_MESSAGE', $defaultLabel);
self::prepareLabels($fields, 'HELP_MESSAGE', $defaultLabel);
$fields['MULTIPLE'] = isset($fields['MULTIPLE']) && mb_strtoupper($fields['MULTIPLE']) === 'Y' ? 'Y' : 'N';
$fields['MANDATORY'] = isset($fields['MANDATORY']) && mb_strtoupper($fields['MANDATORY']) === 'Y' ? 'Y' : 'N';
$fields['SHOW_FILTER'] = isset($fields['SHOW_FILTER']) && mb_strtoupper($fields['SHOW_FILTER']) === 'Y' ? 'E' : 'N'; // E - 'By mask' is default
$isMultiple = isset($fields['MULTIPLE']) && $fields['MULTIPLE'] === 'Y';
$settings = isset($fields['SETTINGS']) && is_array($fields['SETTINGS']) ? $fields['SETTINGS'] : array();
$effectiveSettings = array();
switch ($userTypeID)
{
case 'string':
{
$effectiveSettings['DEFAULT_VALUE'] = isset($settings['DEFAULT_VALUE'])
? $settings['DEFAULT_VALUE'] : '';
$effectiveSettings['ROWS'] = $settings['ROWS'] > 0
? $settings['ROWS'] : 1;
break;
}
case 'integer':
{
$effectiveSettings['DEFAULT_VALUE'] = isset($settings['DEFAULT_VALUE'])
? $settings['DEFAULT_VALUE'] : '';
break;
}
case 'double':
{
$effectiveSettings['DEFAULT_VALUE'] = isset($settings['DEFAULT_VALUE'])
? $settings['DEFAULT_VALUE'] : '';
$effectiveSettings['PRECISION'] = $settings['PRECISION'] >= 0
? $settings['PRECISION'] : 2;
break;
}
case 'boolean':
{
$effectiveSettings['DEFAULT_VALUE'] = isset($settings['DEFAULT_VALUE'])
&& $settings['DEFAULT_VALUE'] > 0 ? 1 : 0;
$display = isset($settings['DISPLAY']) ? $settings['DISPLAY'] : '';
$effectiveSettings['DISPLAY'] = $display !== ''? mb_strtoupper($display) : 'CHECKBOX';
$fields['MULTIPLE'] = 'N';
break;
}
case 'date':
case 'datetime':
{
$defaultValue = isset($settings['DEFAULT_VALUE']) ? $settings['DEFAULT_VALUE'] : array();
if(!is_array($defaultValue))
{
$defaultValue = array('VALUE' => $defaultValue, 'TYPE' => 'NONE');
}
$effectiveSettings['DEFAULT_VALUE'] = array(
'VALUE' => isset($defaultValue['VALUE'])
? CRestUtil::unConvertDateTime($defaultValue['VALUE']) : '',
'TYPE' => isset($defaultValue['TYPE']) && $defaultValue['TYPE'] !== ''
? mb_strtoupper($defaultValue['TYPE']) : 'NONE'
);
break;
}
case 'enumeration':
{
$display = isset($settings['DISPLAY']) ? $settings['DISPLAY'] : '';
$effectiveSettings['DISPLAY'] = $display !== ''? mb_strtoupper($display) : 'LIST';
$height = isset($settings['LIST_HEIGHT']) ? (int)$settings['LIST_HEIGHT'] : 0;
$effectiveSettings['LIST_HEIGHT'] = $height > 0 ? $height : 1;
$listItems = isset($fields['LIST']) && is_array($fields['LIST']) ? $fields['LIST'] : array();
$effectiveListItems = array();
$counter = 0;
$defaultItemKey = '';
foreach($listItems as $item)
{
$itemValue = isset($item['VALUE']) ? trim($item['VALUE'], " tnr") : '';
if($itemValue === '')
{
continue;
}
$effectiveItem = array('VALUE' => $itemValue);
$itemSort = isset($item['SORT']) && is_numeric($item['SORT']) ? (int)$item['SORT'] : 0;
if($itemSort > 0)
{
$effectiveItem['SORT'] = $itemSort;
}
if($itemSort > 0)
{
$effectiveItem['SORT'] = $itemSort;
}
$itemKey = "n{$counter}";
$counter++;
if(isset($item['DEF']))
{
$isDefault = mb_strtoupper($item['DEF']) === 'Y';
if($isMultiple)
{
$effectiveItem['DEF'] = $isDefault ? 'Y' : 'N';
}
elseif($isDefault && $defaultItemKey === '')
{
$defaultItemKey = $itemKey;
}
}
$effectiveListItems[$itemKey] = &$effectiveItem;
unset($effectiveItem);
}
if(!$isMultiple && $defaultItemKey !== '')
{
foreach($effectiveListItems as $key => &$item)
{
$item['DEF'] = $key === $defaultItemKey ? 'Y' : 'N';
}
unset($item);
}
$fields['LIST'] = $effectiveListItems;
break;
}
case 'iblock_section':
case 'iblock_element':
{
$effectiveSettings['IBLOCK_TYPE_ID'] = isset($settings['IBLOCK_TYPE_ID']) ? $settings['IBLOCK_TYPE_ID'] : '';
$effectiveSettings['IBLOCK_ID'] = isset($settings['IBLOCK_ID']) ? (int)$settings['IBLOCK_ID'] : 0;
$effectiveSettings['DEFAULT_VALUE'] = isset($settings['DEFAULT_VALUE']) ? $settings['DEFAULT_VALUE'] : '';
$display = isset($settings['DISPLAY']) ? $settings['DISPLAY'] : '';
$effectiveSettings['DISPLAY'] = $display !== ''? mb_strtoupper($display) : 'LIST';
$height = isset($settings['LIST_HEIGHT']) ? (int)$settings['LIST_HEIGHT'] : 0;
$effectiveSettings['LIST_HEIGHT'] = $height > 0 ? $height : 1;
$effectiveSettings['ACTIVE_FILTER'] = isset($settings['ACTIVE_FILTER'])
&& mb_strtoupper($settings['ACTIVE_FILTER']) === 'Y' ? 'Y' : 'N';
break;
}
case 'crm_status':
{
$effectiveSettings['ENTITY_TYPE'] = isset($settings['ENTITY_TYPE']) ? $settings['ENTITY_TYPE'] : '';
break;
}
case 'crm':
{
$effectiveSettings['LEAD'] = isset($settings['LEAD']) && mb_strtoupper($settings['LEAD']) === 'Y' ? 'Y' : 'N';
$effectiveSettings['CONTACT'] = isset($settings['CONTACT']) && mb_strtoupper($settings['CONTACT']) === 'Y' ? 'Y' : 'N';
$effectiveSettings['COMPANY'] = isset($settings['COMPANY']) && mb_strtoupper($settings['COMPANY']) === 'Y' ? 'Y' : 'N';
$effectiveSettings['DEAL'] = isset($settings['DEAL']) && mb_strtoupper($settings['DEAL']) === 'Y' ? 'Y' : 'N';
break;
}
case 'employee':
{
if($fields['SHOW_FILTER'] !== 'N')
{
$fields['SHOW_FILTER'] = 'I'; // Force exact match for 'USER' field type
}
break;
}
default:
{
$userTypeList = PlacementTable::getHandlersList(UserFieldType::PLACEMENT_UF_TYPE);
foreach($userTypeList as $userType)
{
if($userType['ADDITIONAL'] === $userTypeID)
{
$fields['USER_TYPE_ID'] = UserFieldCallback::getUserTypeId($userType);
}
}
$fields['SHOW_FILTER'] = 'N';
}
}
$fields['SETTINGS'] = $effectiveSettings;
$entity = new CUserTypeEntity();
$ID = $entity->Add($fields);
if($ID <= 0)
{
$exc = $APPLICATION->GetException();
$errors[] = $exc !== false ? $exc->GetString() : 'Fail to create new user field.';
}
elseif ($userTypeID === 'enumeration' && isset($fields['LIST']) && is_array($fields['LIST']))
{
$enum = new CUserFieldEnum();
if(!$enum->SetEnumValues($ID, $fields['LIST']))
{
$exc = $APPLICATION->GetException();
$errors[] = $exc !== false ? $exc->GetString() : 'Fail to save enumumeration field values.';
}
}
if(!empty($errors))
{
throw new RestException(implode("n", $errors), RestException::ERROR_CORE);
}
return $ID;
}