FieldSynchronizer::createUserFieldBySystemField

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. FieldSynchronizer
  4. createUserFieldBySystemField
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/webform/fieldsynchronizer.php
  • Класс: Bitrix\Crm\WebForm\FieldSynchronizer
  • Вызов: FieldSynchronizer::createUserFieldBySystemField
protected function createUserFieldBySystemField($dstEntityTypeName, $srcEntityTypeName, $srcFieldName)
{
	$userFieldId = null;

	$srcEntity = Entity::getMap($srcEntityTypeName);
	$srcEntityFields = EntityFieldProvider::getFieldsInternal($srcEntityTypeName, $srcEntity);
	$srcField = $this->findField($srcFieldName, $srcEntityFields);

	if(!$srcField)
	{
		return null;
	}

	$dstFieldName = $this->getUserFieldBySystemField($dstEntityTypeName, $srcEntityTypeName, $srcFieldName);
	if($dstFieldName || !$this->isCreateMode)
	{
		return $dstFieldName;
	}


	$typeId = null;
	$userFieldSettings = null;
	switch($srcField['type'])
	{
		case 'checkbox':
		case 'radio':
		case 'list':
			$typeId = 'enumeration';
			if($srcField['type'] == 'checkbox' || $srcField['type'] == 'radio')
			{
				$userFieldSettings['DISPLAY'] = 'CHECKBOX';
			}
			else
			{
				$userFieldSettings['DISPLAY'] = 'LIST';
			}
			break;

		case 'file':
		case 'string':
		case 'date':
			$typeId = $srcField['type'];
			break;

		default:
			$typeId = 'string';
	}

	$xmlId = $this->getXmlIdUserFieldBySystemField($srcEntityTypeName, $srcFieldName);

	$dstEntityTypeId = \CCrmOwnerType::ResolveID($dstEntityTypeName);
	$entityId = \CCrmOwnerType::ResolveUserFieldEntityID($dstEntityTypeId);
	$userTypeEntity = new \CUserTypeEntity();

	$resultDb = $userTypeEntity->GetList(
		array(),
		array('ENTITY_ID' => $entityId, 'XML_ID' => $xmlId)
	);
	if($dstField = $resultDb->Fetch())
	{
		return $dstField['FIELD_NAME'];
	}
	if(!$this->isCreateMode)
	{
		return null;
	}


	do
	{
		$dstFieldName = 'UF_CRM_'.mb_strtoupper(uniqid());
		$resultDb = $userTypeEntity->GetList(
			array(),
			array('ENTITY_ID' => $entityId, 'FIELD_NAME' => $dstFieldName)
		);
	}
	while(is_array($resultDb->Fetch()));

	$dstField = array(
		'XML_ID' => $xmlId,
		'FIELD_NAME' => $dstFieldName,
		'ENTITY_ID' => $entityId,
		'USER_TYPE_ID' => $typeId,
		'SORT' =>100,
		'MULTIPLE' => $srcField['multiple'] ? 'Y' : 'N',
		'MANDATORY' => $srcField['required'] ? 'Y' : 'N',
		'SHOW_FILTER' => 'N',
		'SHOW_IN_LIST' => 'N',
		'EDIT_FORM_LABEL' => array(LANGUAGE_ID => $srcField['caption']),
		'LIST_COLUMN_LABEL' => array(LANGUAGE_ID => $srcField['caption']),
		'LIST_FILTER_LABEL' => array(LANGUAGE_ID => $srcField['caption']),
	);

	if($userFieldSettings)
	{
		$dstField['SETTINGS'] = $userFieldSettings;
	}

	$userFieldId = $userTypeEntity->Add($dstField);
	if($userFieldId && $typeId === 'enumeration' && $srcField['items'])
	{
		$enumList = array();
		$enumQty = 0;
		foreach($srcField['items'] as $item){
			$enum = array(
				'XML_ID' => $item['ID'],
				'VALUE' => $item['VALUE'],
				'SORT' => ($enumQty + 1) * 10,
			);
			$enumList["n{$enumQty}"] = $enum;
			$enumQty++;
		}

		$enumEntity = new \CUserFieldEnum();
		$enumEntity->SetEnumValues($userFieldId, $enumList);

		$GLOBALS['CACHE_MANAGER']->ClearByTag('crm_fields_list_' . $entityId);
	}

	return $dstFieldName;
}

Добавить комментарий