• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/classes/general/crm_pay_system.php
  • Класс: \CCrmPaySystem
  • Вызов: CCrmPaySystem::getRequisiteFieldSelectItems
static function getRequisiteFieldSelectItems($fieldsUsedInSettings)
{
	$result = array();

	$preset = new \Bitrix\Crm\EntityPreset();
	$requisite = new EntityRequisite();
	$allowedCountries = $requisite->getAllowedRqFieldCountries();

	$addressTypesByCountry = [];
	$addressTitleList = EntityAddressType::getDescriptions(EntityAddressType::getAllIDs());

	// rq fields
	$rqFields = array();
	$tmpFields = $requisite->getRqFields();
	foreach ($tmpFields as $fieldName)
	{
		if ($fieldName === EntityRequisite::ADDRESS)
		{
			foreach (EntityAddressType::getAllIDs() as $addressType)
			{
				$rqFields[$fieldName.'_'.$addressType] = true;
			}
		}
		else
		{
			$rqFields[$fieldName] = true;
		}
	}

	// rq fields by country
	$rqFieldsByCountry = array();
	foreach ($requisite->getRqFieldsCountryMap() as $fieldName => $fieldCountryIds)
	{
		if (is_array($fieldCountryIds))
		{
			foreach ($fieldCountryIds as $countryId)
			{
				if ($fieldName === EntityRequisite::ADDRESS)
				{
					if (!isset($addressTypesByCountry[$countryId]))
					{
						$addressTypesByCountry[$countryId] = EntityAddressType::getIdsByZonesOrValues(
							[
								EntityAddress::getZoneId(),
								EntityRequisite::getAddressZoneByCountry($countryId)
							]
						);
					}
					foreach ($addressTypesByCountry[$countryId] as $addressType)
					{
						$rqFieldsByCountry[$countryId][$fieldName.'_'.$addressType] = true;
					}
				}
				else
				{
					$rqFieldsByCountry[$countryId][$fieldName] = true;
				}
			}
		}
	}

	// allowed fields
	$fieldsAllowed = array_merge($rqFields, array_fill_keys($requisite->getUserFields(), true));

	// used fields
	$usedCountries = array();
	$usedFieldsByCountry = array();
	if (is_array($fieldsUsedInSettings))
	{
		foreach ($fieldsUsedInSettings as $index)
		{
			$parts = explode('|', $index, 2);
			if (is_array($parts) && count($parts) === 2)
			{
				$fieldName = $parts[0];
				$fieldCountryId = (int)$parts[1];
				if (!empty($fieldName) && in_array($fieldCountryId, $allowedCountries, true))
				{
					if (!is_array($usedFieldsByCountry[$fieldCountryId]))
						$usedFieldsByCountry[$fieldCountryId] = array();
					$usedFieldsByCountry[$fieldCountryId][$fieldName] = true;
					$usedCountries[$fieldCountryId] = true;
				}
			}
		}
	}

	$currentCountryId = \Bitrix\Crm\EntityPreset::getCurrentCountryId();

	// active fields
	$activeFieldsByCountry = array();
	$tmpFields = $preset->getSettingsFieldsOfPresets(
		\Bitrix\Crm\EntityPreset::Requisite,
		'active',
		array(
			'ARRANGE_BY_COUNTRY' => true,
			'FILTER_BY_COUNTRY_IDS' => $allowedCountries
		)
	);
	foreach ($tmpFields as $countryId => $fieldList)
	{
		foreach ($fieldList as $fieldName)
		{
			if ($fieldName === EntityRequisite::ADDRESS)
			{
				if (!isset($addressTypesByCountry[$countryId]))
				{
					$addressTypesByCountry[$countryId] = EntityAddressType::getIdsByZonesOrValues(
						[
							EntityAddress::getZoneId(),
							EntityRequisite::getAddressZoneByCountry($countryId)
						]
					);
				}
				foreach ($addressTypesByCountry[$countryId] as $addressType)
				{
					$activeFieldsByCountry[$countryId][$fieldName.'_'.$addressType] = true;
				}
			}
			else
			{
				$activeFieldsByCountry[$countryId][$fieldName] = true;
			}
			$usedCountries[$countryId] = true;
		}
	}

	// rq fields for backward compatibility
	$rqbcFields = array(
		'RQ_COMPANY_NAME' => true,
		'RQ_INN' => true,
		'RQ_KPP' => true,
		'RQ_ADDR_'.EntityAddressType::Primary => true,
		'RQ_ADDR_'.EntityAddressType::Registered => true,
		'RQ_EMAIL' => true,
		'RQ_PHONE' => true,
		'RQ_FAX' => true,
		'RQ_CONTACT' => true,
		'RQ_NAME' => true
	);

	$fieldsTitleMap = $requisite->getRqFieldTitleMap();
	$userFieldTitles = $requisite->getUserFieldsTitles();

	$countrySort = array();
	if (isset($usedCountries[$currentCountryId]))
		$countrySort[] = $currentCountryId;
	foreach ($allowedCountries as $countryId)
	{
		if ($countryId !== $currentCountryId && isset($usedCountries[$countryId]))
			$countrySort[] = $countryId;
	}

	$countryTitleList = array();
	foreach (\Bitrix\Crm\EntityPreset::getCountryList() as $k => $v)
		$countryTitleList[$k] = $v;

	$result[] = array('id' => '', 'title' => GetMessage('CRM_PS_SELECT_NONE'));
	$addressPrefix = EntityRequisite::ADDRESS;
	$isUTFMode = \Bitrix\Crm\EntityPreset::isUTFMode();
	foreach ($countrySort as $countryId)
	{
		$groupExists = false;
		$groupItem = array('type' => 'group', 'title' => $countryTitleList[$countryId]);
		$isCountryToShow = ($isUTFMode || $countryId === $currentCountryId);
		foreach (array_keys($fieldsAllowed) as $fieldName)
		{
			if ((isset($activeFieldsByCountry[$countryId][$fieldName])
					&& $isCountryToShow)
				|| isset($usedFieldsByCountry[$countryId][$fieldName])
				|| (isset($rqbcFields[$fieldName])
					&& isset($rqFieldsByCountry[$countryId][$fieldName])
					&& $isCountryToShow))
			{
				$matches = array();
				if (preg_match('/'.$addressPrefix.'_(\d+)/'.BX_UTF_PCRE_MODIFIER, $fieldName, $matches))
				{
					$addressType = (int)$matches[1];
					if (isset($addressTitleList[$addressType]))
					{
						if (!$groupExists)
						{
							$result[] = $groupItem;
							$groupExists = true;
						}
						$result[] = array(
							'id' => $fieldName.'|'.$countryId,
							'title' => $addressTitleList[$addressType]
						);
					}
				}
				else
				{
					$title = isset($fieldsTitleMap[$fieldName][$countryId])
						? $fieldsTitleMap[$fieldName][$countryId]
						: (isset($userFieldTitles[$fieldName]) ? $userFieldTitles[$fieldName] : '');
					if (empty($title))
						$title = $fieldName;
					if (!$groupExists)
					{
						$result[] = $groupItem;
						$groupExists = true;
					}
					$result[] = array('id' => $fieldName.'|'.$countryId, 'title' => $title);
				}
			}
		}
	}

	return $result;
}