- Модуль: rest
- Путь к файлу: ~/bitrix/modules/rest/lib/userfieldproxy.php
- Класс: BitrixRestUserFieldProxy
- Вызов: UserFieldProxy::update
public function update($ID, array $fields)
{
global $APPLICATION;
if(!is_int($ID))
{
$ID = (int)$ID;
}
if($ID <= 0)
{
throw new RestException('ID is not defined or invalid.');
}
if(!$this->checkUpdatePermission())
{
throw new RestException('Access denied.');
}
if($this->entityID === '')
{
throw new RestException('Operation is not allowed. Entity ID is not defined.');
}
$entity = new CUserTypeEntity();
$persistedFields = $entity->GetByID($ID);
if(!is_array($persistedFields))
{
throw new RestException("The entity with ID '{$ID}' is not found.", RestException::ERROR_NOT_FOUND);
}
$entityID = isset($persistedFields['ENTITY_ID']) ? $persistedFields['ENTITY_ID'] : '';
if($entityID !== $this->entityID)
{
throw new RestException('Access denied.');
}
//User type ID can't be changed.
$userTypeID = isset($persistedFields['USER_TYPE_ID']) ? $persistedFields['USER_TYPE_ID'] : '';
if($userTypeID === '')
{
throw new RestException("Could not find 'USER_TYPE_ID' in persisted entity with ID '{$ID}'.");
}
$isMultiple = isset($persistedFields['MULTIPLE']) && $persistedFields['MULTIPLE'] === 'Y';
self::sanitizeFields($fields);
if(isset($fields['LIST_FILTER_LABEL']))
{
self::prepareLabels($fields, 'LIST_FILTER_LABEL', '');
}
elseif(isset($persistedFields['LIST_FILTER_LABEL']))
{
$fields['LIST_FILTER_LABEL'] = $persistedFields['LIST_FILTER_LABEL'];
}
if(isset($fields['LIST_COLUMN_LABEL']))
{
self::prepareLabels($fields, 'LIST_COLUMN_LABEL', '');
}
elseif(isset($persistedFields['LIST_COLUMN_LABEL']))
{
$fields['LIST_COLUMN_LABEL'] = $persistedFields['LIST_COLUMN_LABEL'];
}
if(isset($fields['EDIT_FORM_LABEL']))
{
self::prepareLabels($fields, 'EDIT_FORM_LABEL', '');
}
elseif(isset($persistedFields['EDIT_FORM_LABEL']))
{
$fields['EDIT_FORM_LABEL'] = $persistedFields['EDIT_FORM_LABEL'];
}
if(isset($fields['ERROR_MESSAGE']))
{
self::prepareLabels($fields, 'ERROR_MESSAGE', '');
}
elseif(isset($persistedFields['ERROR_MESSAGE']))
{
$fields['ERROR_MESSAGE'] = $persistedFields['ERROR_MESSAGE'];
}
if(isset($fields['HELP_MESSAGE']))
{
self::prepareLabels($fields, 'HELP_MESSAGE', '');
}
elseif(isset($persistedFields['HELP_MESSAGE']))
{
$fields['HELP_MESSAGE'] = $persistedFields['HELP_MESSAGE'];
}
$settings = isset($fields['SETTINGS']) && is_array($fields['SETTINGS']) ? $fields['SETTINGS'] : array();
$effectiveSettings = isset($persistedFields['SETTINGS']) && is_array($persistedFields['SETTINGS'])
? $persistedFields['SETTINGS'] : array();
if(isset($fields['SHOW_FILTER']))
{
$fields['SHOW_FILTER'] = mb_strtoupper($fields['SHOW_FILTER']) === 'Y' ? 'E' : 'N'; // E - 'By mask' is default
}
switch ($userTypeID)
{
case 'string':
{
if(isset($settings['DEFAULT_VALUE']))
{
$effectiveSettings['DEFAULT_VALUE'] = $settings['DEFAULT_VALUE'];
}
if(isset($settings['ROWS']))
{
$effectiveSettings['ROWS'] = min(max($settings['ROWS'], 1), 50);
}
break;
}
case 'integer':
{
if(isset($settings['DEFAULT_VALUE']))
{
$effectiveSettings['DEFAULT_VALUE'] = $settings['DEFAULT_VALUE'];
}
break;
}
case 'double':
{
if(isset($settings['DEFAULT_VALUE']))
{
$effectiveSettings['DEFAULT_VALUE'] = $settings['DEFAULT_VALUE'];
}
if(isset($settings['PRECISION']))
{
$effectiveSettings['PRECISION'] = $settings['PRECISION'] >= 0
? $settings['PRECISION'] : 2;
}
break;
}
case 'boolean':
{
if(isset($settings['DEFAULT_VALUE']))
{
$effectiveSettings['DEFAULT_VALUE'] = $settings['DEFAULT_VALUE'] > 0 ? 1 : 0;
}
if(isset($settings['DISPLAY']))
{
$effectiveSettings['DISPLAY'] = $settings['DISPLAY'] !== ''
? mb_strtoupper($settings['DISPLAY']) : 'CHECKBOX';
}
unset($fields['MULTIPLE']);
break;
}
case 'datetime':
{
if(isset($settings['DEFAULT_VALUE']))
{
$defaultValue = $settings['DEFAULT_VALUE'];
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':
{
if(isset($settings['DISPLAY']))
{
$effectiveSettings['DISPLAY'] = $settings['DISPLAY'] !== ''
? mb_strtoupper($settings['DISPLAY']) : 'LIST';
}
if(isset($settings['LIST_HEIGHT']))
{
$effectiveSettings['LIST_HEIGHT'] = $settings['LIST_HEIGHT'] > 0 ? $settings['LIST_HEIGHT'] : 1;
}
if(isset($fields['LIST']))
{
$listItems = is_array($fields['LIST']) ? $fields['LIST'] : array();
$effectiveListItems = array();
$counter = 0;
$defaultItemKey = '';
foreach($listItems as $item)
{
$itemValue = isset($item['VALUE']) ? trim($item['VALUE'], " tnr") : '';
$effectiveItem = array('VALUE' => $itemValue);
$itemXmlID = isset($item['XML_ID']) ? $item['XML_ID'] : '';
if($itemXmlID !== '')
{
$effectiveItem['XML_ID'] = $itemXmlID;
}
$itemSort = isset($item['SORT']) && is_numeric($item['SORT']) ? (int)$item['SORT'] : 0;
if($itemSort > 0)
{
$effectiveItem['SORT'] = $itemSort;
}
$itemID = isset($item['ID']) && is_numeric($item['ID']) ? (int)$item['ID'] : 0;
if($itemID > 0)
{
$itemKey = strval($itemID);
if(isset($item['DEL']) && mb_strtoupper($item['DEL']) === 'Y')
{
$effectiveItem['DEL'] = 'Y';
}
}
else
{
$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;
}
}
if(!empty($item))
{
$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':
{
if(isset($settings['IBLOCK_TYPE_ID']))
{
$effectiveSettings['IBLOCK_TYPE_ID'] = $settings['IBLOCK_TYPE_ID'];
}
if(isset($settings['IBLOCK_ID']))
{
$effectiveSettings['IBLOCK_ID'] = $settings['IBLOCK_ID'] > 0 ? $settings['IBLOCK_ID'] : 0;
}
if(isset($settings['DEFAULT_VALUE']))
{
$effectiveSettings['DEFAULT_VALUE'] = $settings['DEFAULT_VALUE'];
}
if(isset($settings['DISPLAY']))
{
$effectiveSettings['DISPLAY'] = $settings['DISPLAY'] !== ''
? mb_strtoupper($settings['DISPLAY']) : 'LIST';
}
if(isset($settings['LIST_HEIGHT']))
{
$effectiveSettings['LIST_HEIGHT'] = $settings['LIST_HEIGHT'] > 0 ? $settings['LIST_HEIGHT'] : 1;
}
if(isset($settings['ACTIVE_FILTER']))
{
$effectiveSettings['ACTIVE_FILTER'] = mb_strtoupper($settings['ACTIVE_FILTER']) === 'Y' ? 'Y' : 'N';
}
break;
}
case 'crm_status':
{
if(isset($settings['ENTITY_TYPE']))
{
$effectiveSettings['ENTITY_TYPE'] = $settings['ENTITY_TYPE'];
}
break;
}
case 'crm':
{
if(isset($settings['LEAD']))
{
$effectiveSettings['LEAD'] = mb_strtoupper($settings['LEAD']) === 'Y' ? 'Y' : 'N';
}
if(isset($settings['CONTACT']))
{
$effectiveSettings['CONTACT'] = mb_strtoupper($settings['CONTACT']) === 'Y' ? 'Y' : 'N';
}
if(isset($settings['COMPANY']))
{
$effectiveSettings['COMPANY'] = mb_strtoupper($settings['COMPANY']) === 'Y' ? 'Y' : 'N';
}
if(isset($settings['DEAL']))
{
$effectiveSettings['DEAL'] = mb_strtoupper($settings['DEAL']) === 'Y' ? 'Y' : 'N';
}
break;
}
case 'employee':
{
if(isset($fields['SHOW_FILTER']) && $fields['SHOW_FILTER'] !== 'N')
{
$fields['SHOW_FILTER'] = 'I'; // Force exact match for 'USER' field type
}
break;
}
}
$fields['SETTINGS'] = $effectiveSettings;
if($entity->Update($ID, $fields) === false)
{
$exc = $APPLICATION->GetException();
throw new RestException(
$exc !== false ? $exc->GetString() : 'Fail to update user field.',
RestException::ERROR_CORE
);
}
elseif($userTypeID === 'enumeration' && isset($fields['LIST']) && is_array($fields['LIST']))
{
$enum = new CUserFieldEnum();
if(!$enum->SetEnumValues($ID, $fields['LIST']))
{
$exc = $APPLICATION->GetException();
throw new RestException(
$exc !== false ? $exc->GetString() : 'Fail to save enumumeration field values.',
RestException::ERROR_CORE
);
}
}
return true;
}