CCrmEntityHelper::PrepareMultiFieldFilter

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. CCrmEntityHelper
  4. PrepareMultiFieldFilter
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/classes/general/entity_helper.php
  • Класс: \CCrmEntityHelper
  • Вызов: CCrmEntityHelper::PrepareMultiFieldFilter
static function PrepareMultiFieldFilter(&$arFilter, $arFieldTypes = array(), $comparisonType = '%', $lockComparisonType = false)
{
	if(!is_array($arFieldTypes) && is_string($arFieldTypes))
	{
		$arFieldTypes = array($arFieldTypes);
	}

	if(!is_array($arFieldTypes) || count($arFieldTypes) === 0)
	{
		// Default field types
		$arFieldTypes = self::$DEFAULT_FIELD_TYPES;
	}

	if(!is_string($comparisonType))
	{
		$comparisonType = '%';
	}

	if($comparisonType === '')
	{
		$comparisonType = '%';
	}

	if(isset($arFilter['FM']))
	{
		unset($arFilter['FM']);
	}

	foreach($arFieldTypes as $fieldType)
	{
		if(!isset($arFilter[$fieldType]))
		{
			continue;
		}

		$fieldValue = $arFilter[$fieldType];
		if(is_array($fieldValue))
		{
			$fieldValue = count($fieldValue) ? $fieldValue[0] : '';
		}

		if(!is_string($fieldValue))
		{
			$fieldValue = strval($fieldValue);
		}

		$fieldValue = trim($fieldValue);
		if($fieldValue === '')
		{
			unset($arFilter[$fieldType]);
			continue;
		}

		if(!isset($arFilter['FM']))
		{
			$arFilter['FM'] = array();
		}

		$curentComparisonType = $comparisonType;
		if(!$lockComparisonType)
		{
			if(preg_match('/^%([^%]+)%$/', $fieldValue, $m) === 1)
			{
				$fieldValue = $m[1];
				$curentComparisonType = '%';
			}
			elseif(preg_match('/^%([^%]+)$/', $fieldValue, $m) === 1)
			{
				$fieldValue = $m[1];
				$curentComparisonType = '%=';
			}
			elseif(preg_match('/^([^%]+)%$/', $fieldValue, $m) === 1)
			{
				$fieldValue = $m[1];
				$curentComparisonType = '=%';
			}
		}

		if($curentComparisonType === '=%')
		{
			$fieldValue = preg_replace('/%/', '', $fieldValue);
			$arFilter['FM'][] = array('TYPE_ID' => $fieldType, '=%VALUE' => "{$fieldValue}%");
		}
		elseif($curentComparisonType === '%=')
		{
			$fieldValue = preg_replace('/%/', '', $fieldValue);
			$arFilter['FM'][] = array('TYPE_ID' => $fieldType, '%=VALUE' => "%{$fieldValue}");
		}
		else
		{
			$arFilter['FM'][] = array('TYPE_ID' => $fieldType, "{$curentComparisonType}VALUE" => $fieldValue);
		}

		unset($arFilter[$fieldType]);
	}
}

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