CCrmUserType::Internalize

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. CCrmUserType
  4. Internalize
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/classes/general/crm_usertype.php
  • Класс: \CCrmUserType
  • Вызов: CCrmUserType::Internalize
public function Internalize($name, $data, $delimiter = ',', $arUserField = null)
{
	$delimiter = strval($delimiter);
	if($delimiter === '')
	{
		$delimiter = ',';
	}

	if(!$arUserField)
	{
		$arUserFields = $this->GetAbstractFields();
		$arUserField = isset($arUserFields[$name]) ? $arUserFields[$name] : null;
	}

	if(!$arUserField)
	{
		return $data; // return original data
	}

	$isMultiple = $arUserField['MULTIPLE'] === 'Y';
	if($isMultiple && !is_array($data))
	{
		$data = array_filter(array_map('trim', explode($delimiter, $data)));
	}

	$typeID = $arUserField['USER_TYPE']['USER_TYPE_ID'];
	if($typeID === 'file')
	{
		$result = null;
		if(!$isMultiple)
		{
			if(CCrmFileProxy::TryResolveFile($data, $file, array('ENABLE_UPLOAD' => true)))
			{
				$result = $file;
			}
		}
		elseif(is_array($data))
		{
			$files = array();
			foreach($data as $datum)
			{
				if(CCrmFileProxy::TryResolveFile($datum, $file, array('ENABLE_UPLOAD' => true)))
				{
					$files[] = $file;
				}
			}
			$result = $files;
		}
		$data = $result;
	}
	elseif($typeID === 'enumeration')
	{
		// Processing for type 'enumeration'

		$enums = array();
		$rsEnum = CUserFieldEnum::GetList(array(), array('USER_FIELD_ID' => $arUserField['ID']));
		while ($arEnum = $rsEnum->Fetch())
		{
			$enums[$arEnum['ID']] = $arEnum;
		}

		self::InternalizeEnumValue($data, $enums);

	}
	elseif($typeID === 'employee')
	{
		// Processing for type 'employee' (we have to implement custom processing since CUserTypeEmployee::GetList doesn't provide VALUE property)
		$enums = array();

		$rsEnum = CUser::GetList(
			'last_name',
			'asc',
			array(),
			array('FIELDS' => array('ID', 'NAME', 'SECOND_NAME', 'LAST_NAME', 'LOGIN', 'TITLE', 'EMAIL'))
		);
		while ($arEnum = $rsEnum->Fetch())
		{
			$arEnum['VALUE'] = CUser::FormatName(CSite::GetNameFormat(false), $arEnum, false, true);
			$enums[$arEnum['ID']] = $arEnum;
		}

		self::InternalizeEnumValue($data, $enums);
	}
	elseif($typeID === 'crm')
	{
		// Processing for type 'crm' (is link to LEAD, CONTACT, COMPANY or DEAL)
		self::InternalizeCrmEntityValue($data, $arUserField);
	}
	elseif($typeID === 'boolean')
	{
		$yes = mb_strtoupper(GetMessage('MAIN_YES'));
		//$no = strtoupper(GetMessage('MAIN_NO'));

		if(is_array($data))
		{
			foreach($data as &$v)
			{
				$s = mb_strtoupper($v);
				$v = ($s === $yes || $s === 'Y' || $s === 'YES' || (is_numeric($s) && intval($s) > 0)) ? 1 : 0;
			}
			unset($v);
		}
		elseif(is_string($data) && $data !== '')
		{
			$s = mb_strtoupper($data);
			$data = ($s === $yes || $s === 'Y' || $s === 'YES' || (is_numeric($s) && intval($s) > 0)) ? 1 : 0;
		}
		elseif(isset($arUserField['SETTINGS']['DEFAULT_VALUE']))
		{
			$data = $arUserField['SETTINGS']['DEFAULT_VALUE'];
		}
		else
		{
			$data = 0;
		}
	}
	elseif($typeID === 'datetime')
	{
		if(is_array($data))
		{
			foreach($data as &$v)
			{
				if(!CheckDateTime($v))
				{
					$timestamp = strtotime($v);
					$v = is_int($timestamp) && $timestamp > 0 ? ConvertTimeStamp($timestamp, 'FULL') : '';
				}
			}
			unset($v);
		}
		elseif(is_string($data) && $data !== '')
		{
			if(!CheckDateTime($data))
			{
				$timestamp = strtotime($data);
				$data = is_int($timestamp) && $timestamp > 0 ? ConvertTimeStamp($timestamp, 'FULL') : '';
			}
		}
	}
	elseif(is_callable(array($arUserField['USER_TYPE']['CLASS_NAME'], 'getlist')))
	{
		// Processing for type user defined class

		$rsEnum = call_user_func_array(
			array($arUserField['USER_TYPE']['CLASS_NAME'], 'getlist'),
			array($arUserField)
		);

		$enums = array();
		if (is_object($rsEnum))
		{
			while ($arEnum = $rsEnum->GetNext())
			{
				$enums[strval($arEnum['ID'])] = $arEnum;
			}
		}

		$fieldName = 'VALUE';
		if ($arUserField['USER_TYPE']['USER_TYPE_ID'] === 'iblock_section'
			|| $arUserField['USER_TYPE']['USER_TYPE_ID'] === 'iblock_element')
		{
			$fieldName = '~NAME';
		}

		self::InternalizeEnumValue($data, $enums, $fieldName);
	}
	return $data;
}

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