• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/format/entityaddressformatter.php
  • Класс: Bitrix\Crm\Format\EntityAddressFormatter
  • Вызов: EntityAddressFormatter::prepareLines
static function prepareLines(array $data, array $options = null)
{
	if(!is_array($options))
	{
		$options = array();
	}

	$formatID = isset($options['FORMAT']) ? (int)$options['FORMAT'] : self::Undefined;
	if($formatID !== self::Undefined && !self::isDefined($formatID))
	{
		$formatID = self::Undefined;
	}
	if($formatID === self::Undefined)
	{
		$formatID = self::getFormatID();
	}

	$lines = array();
	$address1 = isset($data['ADDRESS_1']) ? $data['ADDRESS_1'] : '';
	$address1Index = -1;
	if($address1 !== '')
	{
		$address1Index = count($lines);
		$lines[] = $address1;
	}

	$address2 = isset($data['ADDRESS_2']) ? $data['ADDRESS_2'] : '';
	if($address2 !== '')
	{
		$lines[] = $address2;
	}

	$city = isset($data['CITY']) ? $data['CITY'] : '';
	$region = isset($data['REGION']) ? $data['REGION'] : '';
	$province = isset($data['PROVINCE']) ? $data['PROVINCE'] : '';
	$postalCode = isset($data['POSTAL_CODE']) ? $data['POSTAL_CODE'] : '';
	$country = isset($data['COUNTRY']) ? $data['COUNTRY'] : '';

	if($formatID === self::RUS)
	{
		if($city !== '')
		{
			$lines[] = $city;
		}

		if($region !== '')
		{
			$lines[] = $region;
		}

		if($province !== '')
		{
			$lines[] = $province;
		}

		if($country !== '')
		{
			$lines[] = $country;
		}

		if($postalCode !== '')
		{
			$lines[] = $postalCode;
		}
	}
	elseif($formatID === self::RUS2)
	{
		$tempLines = array();
		
		if($postalCode !== '')
		{
			$tempLines[] = $postalCode;
		}

		if($country !== '')
		{
			$tempLines[] = $country;
		}

		if($province !== '')
		{
			$tempLines[] = $province;
		}

		if($region !== '')
		{
			$tempLines[] = $region;
		}

		if($city !== '')
		{
			$tempLines[] = $city;
		}

		if (!empty($tempLines))
		{
			$lines = array_merge($tempLines, $lines);
			$address1Index += count($tempLines);
		}

		unset($tempLines);
	}
	elseif($formatID === self::EU)
	{
		$localities = array();
		if($postalCode !== '')
		{
			$localities[] = $postalCode;
		}

		if($city !== '')
		{
			$localities[] = $city;
		}

		if($region !== '')
		{
			$localities[] = $region;
		}

		if($province !== '')
		{
			$localities[] = $province;
		}

		if (!empty($localities))
		{
			$lines[] = implode(' ', $localities);
		}

		if($country !== '')
		{
			$lines[] = $country;
		}
	}
	elseif($formatID === self::UK)
	{
		if($city !== '')
		{
			$lines[] = mb_strtoupper($city);
		}

		if($region !== '')
		{
			$lines[] = mb_strtoupper($region);
		}

		if($province !== '')
		{
			$lines[] = mb_strtoupper($province);
		}

		if($postalCode !== '')
		{
			$lines[] = mb_strtoupper($postalCode);
		}

		if($country !== '')
		{
			$lines[] = $country;
		}
	}
	else //if($formatID === self::USA)
	{
		$localities = array();
		if($city !== '')
		{
			$localities[] = $city;
		}

		if($region !== '')
		{
			$localities[] = $region;
		}

		if($province !== '')
		{
			$localities[] = $province;
		}

		if($postalCode !== '')
		{
			$localities[] = mb_strtoupper($postalCode);
		}

		if (!empty($localities))
		{
			$lines[] = implode(' ', $localities);
		}

		if($country !== '')
		{
			$lines[] = mb_strtoupper($country);
		}
	}

	$isNl2Br = (isset($options['NL2BR']) && $options['NL2BR']);
	$separatorType = isset($options['SEPARATOR']) ? (int)$options['SEPARATOR'] : AddressSeparator::Undefined;
	$separator = AddressSeparator::getSeparator($separatorType);

	if(isset($options['HTML_ENCODE']) && $options['HTML_ENCODE'] === true)
	{
		array_walk($lines, function (&$v, $k) use ($address1Index, $isNl2Br, $separatorType, $separator) {
			$v = htmlspecialcharsbx($v);
			if ($isNl2Br && $k === $address1Index && $separatorType !== AddressSeparator::NewLine)
			{
				$v = str_replace("\n", $separator, $v);
			}
		});
	}
	else if ($isNl2Br && $separatorType !== AddressSeparator::NewLine)
	{
		$lines[$address1Index] = str_replace("\n", $separator, $lines[$address1Index]);
	}

	return $lines;
}