Form::prepareAction

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. Form
  4. prepareAction
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/controller/form.php
  • Класс: Bitrix\Crm\Controller\Form
  • Вызов: Form::prepareAction
public function prepareAction(array $options, array $preparing)
{
	if (!$this->checkFormAccess())
	{
		return [];
	}

	if (!empty($preparing['templateId']))
	{
		$preparing = Main\DI\ServiceLocator::getInstance()
			->get('crm.service.webform.scenario')
			->prepareForm($preparing['templateId'], $options)
		;
	}

	if (!empty($preparing['integration']))
	{
		$preparing['fields'] = [];
		$fieldList = [];
		$cases = $preparing['integration']['cases'] ?? [];
		foreach ($cases as $case)
		{
			foreach (($case['fieldsMapping'] ?? []) as $field)
			{
				if (!$field['crmFieldKey'])
				{
					continue;
				}

				$fieldList[] = $field['crmFieldKey'];
			}
		}

		$preparing['fields'] = array_map(
			function ($name)
			{
				return ['name' => $name];
			},
			array_unique($fieldList)
		);
		$options['data']['fields'] = [];
	}

	if (!empty($preparing['fields']) && is_array($preparing['fields']))
	{
		$fieldNames = [];
		foreach ($preparing['fields'] as $field)
		{
			if (!is_array($field) || empty($field['name']))
			{
				continue;
			}

			$fieldNames[$field['name']] = $field;
		}

		if (!empty($options['data']['fields']))
		{
			$fields = array_map(
				static function ($field) use ($fieldNames) : ?array
				{
					if (!$fieldNew = $fieldNames[$field['name']])
					{
						return null;
					}

					return array_replace_recursive($field, $fieldNew);
				},
				$options['data']['fields'],
			);
			$options['data']['fields'] = array_filter($fields);
		}
	}

	if (!empty($preparing['agreements']) && is_array($preparing['agreements']))
	{
		$existed = [];
		foreach ($preparing['agreements'] as $item)
		{
			if ($item && is_numeric($item))
			{
				$existed[] = $item;
				continue;
			}

			if (!is_array($item) || empty($item['id']))
			{
				continue;
			}

			$existed[] = $item['id'];
		}

		if (!empty($options['data']['agreements']))
		{
			$options['data']['agreements'] = array_filter(
				$options['data']['agreements'],
				function ($item) use ($existed)
				{
					return in_array($item['id'], $existed);
				}
			);
		}
	}

	$formOptions = WebForm\Options::createFromArray($options);

	if (!empty($preparing['agreements']) && is_array($preparing['agreements']))
	{
		$existed = array_column($options['data']['agreements'] ?? [], 'id');
		foreach ($preparing['agreements'] as $agreement)
		{
			$id = is_numeric($agreement) ? $agreement : null;
			$id = !empty($agreement['id']) ? $agreement['id'] : $id;
			if (!in_array($id, $existed))
			{
				$existed[] = $id;
				$formOptions->getConfig()->appendAgreement($id);
			}
		}
	}

	if (!empty($preparing['fields']) && is_array($preparing['fields']))
	{
		$fieldNames = array_column($options['data']['fields'] ?? [], 'name');
		foreach ($preparing['fields'] as $field)
		{
			if (!is_array($field))
			{
				continue;
			}

			if (!empty($field['name']) && in_array($field['name'], $fieldNames) && !$field['inPreparing'])
			{
				continue;
			}

			if (!empty($field['name']) || !empty($field['type']))
			{
				//if (in_array($field['type'], ['br', 'hr', '']))
				$field['inPreparing'] = false;
				$formOptions->getConfig()->appendField($field);
			}
		}
	}

	//$result = $formOptions->save();
	//$this->addErrors($result->getErrors());

	return $formOptions->getArray();
}

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