• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/requisite/importhelper.php
  • Класс: Bitrix\Crm\Requisite\ImportHelper
  • Вызов: ImportHelper::associatePreset
protected function associatePreset($row)
{
	$result = new Main\Result();

	$countryId = 0;
	$presetId = 0;

	// check country by id
	$allowedCountries = EntityRequisite::getAllowedRqFieldCountries();
	if (isset($this->headerGroupCountryIdMap['requisite'][0][$this->rqFieldPrefix.'PRESET_COUNTRY_ID'])
		&& isset($row[$this->headerIndex[$this->rqFieldPrefix.'PRESET_COUNTRY_ID']]))
	{
		$value = $row[$this->headerIndex[$this->rqFieldPrefix.'PRESET_COUNTRY_ID']];
		if (is_string($value) && $value <> '')
		{
			$value = (int)$value;
			if ($value > 0 && in_array($value, $allowedCountries, true))
				$countryId = $value;
		}
	}

	// check country by name
	if ($countryId <= 0)
	{
		$countryList = EntityPreset::getCountryList();
		if (isset($this->headerGroupCountryIdMap['requisite'][0][$this->rqFieldPrefix.'PRESET_COUNTRY_NAME'])
			&& isset($row[$this->headerIndex[$this->rqFieldPrefix.'PRESET_COUNTRY_NAME']]))
		{
			$value = $row[$this->headerIndex[$this->rqFieldPrefix.'PRESET_COUNTRY_NAME']];
			if (is_string($value))
			{
				$value = trim($value);
				if ($value <> '')
				{
					$value = array_search($value, $countryList, true);
					if ($value !== false && in_array($value, $allowedCountries, true))
						$countryId = $value;
				}
			}
		}
	}

	// associate preset
	if ($this->assocPreset)
	{
		if ($this->assocPresetById)
		{
			if (isset($this->headerGroupCountryIdMap['requisite'][0][$this->rqFieldPrefix.'PRESET_ID'])
				&& isset($row[$this->headerIndex[$this->rqFieldPrefix.'PRESET_ID']]))
			{
				$value = $row[$this->headerIndex[$this->rqFieldPrefix.'PRESET_ID']];
				if (is_string($value) && $value <> '')
				{
					$value = (int)trim($value);
					if ($value < 0)
						$value = 0;
					$this->updatePresetCacheById($value);
					$presetId = $value;
				}
			}
		}
		else
		{
			if (isset($this->headerGroupCountryIdMap['requisite'][0][$this->rqFieldPrefix.'PRESET_NAME'])
				&& isset($row[$this->headerIndex[$this->rqFieldPrefix.'PRESET_NAME']]))
			{
				$value = $row[$this->headerIndex[$this->rqFieldPrefix.'PRESET_NAME']];
				if (is_string($value))
				{
					if ($value <> '')
					{
						if (mb_strlen($value) > 255)
							$value = mb_substr($value, 0, 255);
						$this->updatePresetCacheByName($value, $countryId);
						if (is_array(self::$presetCacheByName[$value][$countryId])
							&& count(self::$presetCacheByName[$value][$countryId]) > 0)
						{
							$presetId = self::$presetCacheByName[$value][$countryId][0]['ID'];
						}
					}
				}
			}
		}
		if (!is_array(self::$presetCacheById[$presetId]) && $this->useDefPreset)
		{
			$value = $this->defPresetId;
			if ($value < 0)
				$value = 0;
			$this->updatePresetCacheById($value);
			$presetId = $value;
		}
	}
	else
	{
		$value = $this->defPresetId;
		if ($value < 0)
			$value = 0;
		$this->updatePresetCacheById($value);
		$presetId = $value;
	}

	if (is_array(self::$presetCacheById[$presetId]) && $countryId > 0
		&& $countryId !== self::$presetCacheById[$presetId]['COUNTRY_ID'])
	{
		$presetId = 0;
	}

	if (!is_array(self::$presetCacheById[$presetId]))
	{
		$result->addError(
			new Main\Error(
				Loc::getMessage('CRM_RQ_IMP_HLPR_ERR_PRESET_ASSOC'),
				self::ERR_PRESET_ASSOC
			)
		);
	}
	else
	{
		$result->setData(self::$presetCacheById[$presetId]);
	}

	return $result;
}