Form::save

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. Form
  4. save
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/webform/form.php
  • Класс: Bitrix\Crm\WebForm\Form
  • Вызов: Form::save
public function save($onlyCheck = false)
{
	$this->errors = array();
	$result = $this->params;
	unset($result['SCRIPTS']);

	$agreements = [];
	foreach ($result['AGREEMENTS'] ?? [] as $agreement)
	{
		$agreementId = $agreement['ID'] ?? $agreement['AGREEMENT_ID'] ?? null;
		if (!$agreementId || in_array($agreementId, array_column($agreements, 'AGREEMENT_ID')))
		{
			continue;
		}

		$agreements[] = [
			'AGREEMENT_ID' => $agreementId,
			'CHECKED' => $agreement['CHECKED'] === 'Y' ? 'Y' : 'N',
			'REQUIRED' => $agreement['REQUIRED'] === 'Y' ? 'Y' : 'N',
		];
	}
	unset($result['AGREEMENTS']);
	if ($result['AGREEMENT_ID'])
	{
		if (!in_array($result['AGREEMENT_ID'], array_column($agreements, 'AGREEMENT_ID')))
		{
			$agreements[] = [
				'AGREEMENT_ID' => $result['AGREEMENT_ID'],
				'CHECKED' => $result['LICENCE_BUTTON_IS_CHECKED'] === 'Y' ? 'Y' : 'N',
				'REQUIRED' => 'Y',
			];
		}
		elseif (isset($result['LICENCE_BUTTON_IS_CHECKED']))
		{
			foreach ($agreements as $index => $agreement)
			{
				if ($agreement['AGREEMENT_ID'] != $result['AGREEMENT_ID'])
				{
					continue;
				}

				$agreements[$index]['CHECKED'] = $result['LICENCE_BUTTON_IS_CHECKED'] === 'Y' ? 'Y' : 'N';
			}
		}
	}
	$result['AGREEMENT_ID'] = null;

	$fields = $result['FIELDS'];
	unset($result['FIELDS']);


	$depGroups = $result['DEP_GROUPS'];
	unset($result['DEP_GROUPS']);
	$dependencies = $result['DEPENDENCIES'];
	unset($result['DEPENDENCIES']);

	$presetFields = $result['PRESET_FIELDS'];
	unset($result['PRESET_FIELDS']);


	unset($result['INTEGRATION']);

	$assignedById = $result['ASSIGNED_BY_ID'];
	$assignedWorkTime = $result['ASSIGNED_WORK_TIME'];
	$result['ASSIGNED_BY_ID'] = null;
	unset($result['ASSIGNED_WORK_TIME']);

	// captcha
	$captchaKey = $result['CAPTCHA_KEY'] ?? '';
	$captchaSecret = $result['CAPTCHA_SECRET'] ?? '';
	$captchaVersion = $result['CAPTCHA_VERSION'] ?? '';
	if ($captchaKey <> '' && $captchaSecret <> '')
	{
		if (
			$captchaKey !== ReCaptcha::getKey($captchaVersion)
			&& $captchaSecret !== ReCaptcha::getSecret($captchaVersion)
		)
		{
			$this->forceBuild = true;
			ReCaptcha::setKey($captchaKey, $captchaSecret, $captchaVersion);
		}
	}
	unset($result['CAPTCHA_KEY']);
	unset($result['CAPTCHA_SECRET']);
	unset($result['CAPTCHA_VERSION']);

	$result['ENTITY_SCHEME'] = (string) $result['ENTITY_SCHEME'];

	if($onlyCheck)
	{
		/*INTEGRATION*/
		$this->prepareResult('INTEGRATION',$this->getIntegration()->checkData());

		if(!in_array($result['ENTITY_SCHEME'], $this->getAllowedEntitySchemes()))
		{
			$this->errors[] = Loc::getMessage('CRM_WEBFORM_FORM_ERROR_SCHEME');
		}

		// captcha
		if($result['USE_CAPTCHA'] == 'Y')
		{
			$hasCaptchaKey = ReCaptcha::getKey(2) <> '' && ReCaptcha::getSecret(2) <> '';
			$hasCaptchaDefaultKey = ReCaptcha::getDefaultKey(2) <> '' && ReCaptcha::getDefaultSecret(2) <> '';
			if (!$hasCaptchaKey && !$hasCaptchaDefaultKey)
			{
				$this->errors[] = Loc::getMessage('CRM_WEBFORM_FORM_ERROR_CAPTCHA_KEY');
			}
		}

		$formResult = new Main\Entity\Result;
		$result['DATE_CREATE'] = new Main\Type\DateTime();
		Internals\FormTable::checkFields($formResult, $this->id, $result);
		$this->prepareResult('FIELDS', $formResult);

		foreach($presetFields as $presetField)
		{
			$presetField['FORM_ID'] = (int) $this->id;
			$presetFieldResult = new Main\Entity\Result;
			Internals\PresetFieldTable::checkFields($presetFieldResult, null, $presetField);
			$replaceList = null;
			if(!$presetFieldResult->isSuccess())
			{
				$field = EntityFieldProvider::getField($presetField['ENTITY_NAME'] . '_' . $presetField['FIELD_NAME']);
				if($field)
				{
					$replaceList = array('VALUE' => $field['caption']);
				}
			}
			$this->prepareResult('PRESET_FIELDS', $presetFieldResult, $replaceList);
		}

		$fieldCodeList = array();
		foreach($fields as $field)
		{
			$field['FORM_ID'] = (int) $this->id;

			$fieldResult = new Main\Entity\Result;
			Internals\FieldTable::checkFields($fieldResult, null, $field);
			$this->prepareResult('FIELDS', $fieldResult);

			$fieldCodeList[] = $field['CODE'];
		}

		foreach($depGroups as $depGroup)
		{
			$depGroup['FORM_ID'] = (int) $this->id;

			if(!in_array($depGroup['IF_FIELD_CODE'], array_keys(Internals\FieldDepGroupTable::getDepGroupTypes())))
			{
				continue;
			}

			$depGroupResult = new Main\Entity\Result;
			Internals\FieldDepGroupTable::checkFields($depGroupResult, null, $depGroup);
			$this->prepareResult('DEP_GROUPS', $depGroupResult);
		}

		foreach($dependencies as $dependency)
		{
			$dependency['FORM_ID'] = (int) $this->id;
			$dependency['GROUP_ID'] = (int) ($dependency['GROUP_ID'] ?? 0);

			if(!in_array($dependency['IF_FIELD_CODE'], $fieldCodeList))
			{
				continue;
			}
			if(!in_array($dependency['DO_FIELD_CODE'], $fieldCodeList))
			{
				continue;
			}

			$dependencyResult = new Main\Entity\Result;
			Internals\FieldDependenceTable::checkFields($dependencyResult, null, $dependency);
			$this->prepareResult('DEPENDENCIES', $dependencyResult);
		}

		foreach($agreements as $agreement)
		{
			$agreement['FORM_ID'] = (int) $this->id;
			$agreementResult = new Main\Entity\Result;
			Internals\AgreementTable::checkFields($agreementResult, null, $agreement);
			$this->prepareResult('AGREEMENTS', $agreementResult);
		}

		return;
	}

	if(!$this->check())
	{
		return;
	}

	if($this->id)
	{
		unset($result['ID']);
		$formResult = Internals\FormTable::update($this->id, $result);
		$isAdded = false;
	}
	else
	{
		$result['DATE_CREATE'] = new Main\Type\DateTime();
		$formResult = Internals\FormTable::add($result);
		$this->id = $formResult->getId();
		$isAdded = true;
	}

	if(!$formResult->isSuccess())
	{
		return;
	}

	/* RESPONSIBLE QUEUE */
	$assignedById = is_array($assignedById) ? $assignedById : array($assignedById);
	$responsibleQueue = new ResponsibleQueue($this->id);
	$responsibleQueue->setList($assignedById, $assignedWorkTime == 'Y');

	/* PRESET FIELDS */
	Internals\PresetFieldTable::delete(array('FORM_ID' => $this->id));
	foreach($presetFields as $presetField)
	{
		$presetFieldResult = Internals\PresetFieldTable::add(array(
			'ENTITY_NAME' => $presetField['ENTITY_NAME'],
			'FIELD_NAME' => $presetField['FIELD_NAME'],
			'VALUE' => $presetField['VALUE'],
			'FORM_ID' => $this->id
		));
		$this->prepareResult('PRESET_FIELDS', $presetFieldResult);
	}

	/*INTEGRATION*/
	$this->prepareResult('INTEGRATION',$this->getIntegration()->save());

	/* FIELDS */
	$existedFieldList = array();
	$existedFieldDb = Internals\FieldTable::getList(array(
		'select' => array('ID'),
		'filter' => array('=FORM_ID' => $this->id)
	));
	while($existedField = $existedFieldDb->fetch())
	{
		$existedFieldList[] = $existedField['ID'];
	}

	$newFieldList = array();
	foreach($fields as $field)
	{
		$field['FORM_ID'] = $this->id;

		if($field['ID'] > 0)
		{
			$fieldId = $field['ID'];
			unset($field['ID']);
			$fieldResult = Internals\FieldTable::update($fieldId, $field);
			$newFieldList[] = $fieldId;
		}
		else
		{
			$fieldResult = Internals\FieldTable::add($field);
		}

		$this->prepareResult('FIELDS', $fieldResult);
	}

	$deleteFieldList = array_diff($existedFieldList, $newFieldList);
	foreach($deleteFieldList as $deleteFieldId)
	{
		Internals\FieldTable::delete($deleteFieldId);
	}

	/* DEPENDENCIES */
	$fieldCodeList = array();
	$fieldCodeDb = Internals\FieldTable::getList(array(
		'select' => array('CODE'),
		'filter' => array('=FORM_ID' => $this->id)
	));
	while($fieldCode = $fieldCodeDb->fetch())
	{
		$fieldCodeList[] = $fieldCode['CODE'];
	}

	$fieldDepGroupDb = Internals\FieldDepGroupTable::getList(['select' => ['ID'], 'filter' => ['=FORM_ID' => $this->id]]);
	while($fieldDepGroup = $fieldDepGroupDb->fetch())
	{
		Internals\FieldDepGroupTable::delete($fieldDepGroup['ID']);
	}

	$depGroupMap = [];
	foreach($depGroups as $depGroup)
	{
		$depGroupId = $depGroup['ID'];
		$depGroup['FORM_ID'] = $this->id;
		unset($depGroup['ID']);
		$depGroupResult = Internals\FieldDepGroupTable::add($depGroup);
		$this->prepareResult('DEP_GROUPS', $depGroupResult);
		$depGroupMap[$depGroupId] = $depGroupResult->getId();
	}
	foreach($dependencies as $depIndex => $dependency)
	{
		$dependency['GROUP_ID'] = !empty($dependency['GROUP_ID']) ? $dependency['GROUP_ID'] : 0;
		$dependency['FORM_ID'] = $this->id;
		if (isset($depGroupMap[$dependency['GROUP_ID']]))
		{
			$dependency['GROUP_ID'] = $depGroupMap[$dependency['GROUP_ID']];
		}
		elseif (count($depGroupMap) === 1)
		{
			$dependency['GROUP_ID'] = current($depGroupMap);
		}
		else
		{
			$dependency['GROUP_ID'] = 0;
		}
		$dependencies[$depIndex] = $dependency;
	}

	$fieldDepDb = Internals\FieldDependenceTable::getList(['select' => ['ID'], 'filter' => ['=FORM_ID' => $this->id]]);
	while($fieldDep = $fieldDepDb->fetch())
	{
		Internals\FieldDependenceTable::delete($fieldDep['ID']);
	}
	foreach($dependencies as $dependency)
	{
		if(!in_array($dependency['IF_FIELD_CODE'], $fieldCodeList))
		{
			continue;
		}
		if(!in_array($dependency['DO_FIELD_CODE'], $fieldCodeList))
		{
			continue;
		}

		$dependency['FORM_ID'] = $this->id;

		$dependencyResult = Internals\FieldDependenceTable::add($dependency);
		$this->prepareResult('DEPENDENCIES', $dependencyResult);
	}

	$agreementDb = Internals\AgreementTable::getList(['select' => ['ID'], 'filter' => ['=FORM_ID' => $this->id]]);
	while($agreement = $agreementDb->fetch())
	{
		Internals\AgreementTable::delete($agreement['ID']);
	}
	foreach($agreements as $agreement)
	{
		$agreement['FORM_ID'] = $this->id;
		$agreementResult = Internals\AgreementTable::add($agreement);
		$this->prepareResult('AGREEMENTS', $agreementResult);
	}

	$this->buildScript();
	if ($isAdded && (int)$this->params['TYPE_ID'] === Internals\FormTable::TYPE_DEFAULT)
	{
		LandingTable::createLanding($this->id, $result['NAME']);
	}
}

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