Rest::fillForm

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. Rest
  4. fillForm
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/webform/embed/rest.php
  • Класс: Bitrix\Crm\WebForm\Embed\Rest
  • Вызов: Rest::fillForm
static function fillForm($query)
{
	$formId = empty($query['id']) ? null : (int) $query['id'];
	$securityCode = empty($query['sec']) ? null : $query['sec'];
	$values = (isset($query['values']) && $query['values'])
		? $query['values']
		: null;
	$properties = (isset($query['properties']) && $query['properties'])
		? $query['properties']
		: null;
	$trace = empty($query['trace']) ? null : $query['trace'];
	$consents = $query['consents'] ?? '';
	$consents = $consents ? Main\Web\Json::decode(
		Main\Text\Encoding::convertEncoding(
			$consents,
			SITE_CHARSET,
			'UTF-8'
		)
	) : [];
	$consents = is_array($consents) ? $consents : [];
	$recaptchaResponse = empty($query['recaptcha']) ? null : $query['recaptcha'];
	$signString = empty($query['security_sign']) ? null : $query['security_sign'];
	//$entities = empty($query['entities']) ? null : $query['entities'];

	$timeZoneOffset = isset($query['timeZoneOffset']) ? (int)$query['timeZoneOffset'] : null;
	if (!\CTimeZone::GetCookieValue() && $timeZoneOffset)
	{
		\CTimeZone::SetCookieValue($timeZoneOffset);
	}

	$form = null;

	try
	{
		if (!WebForm\Manager::isEmbeddingAvailable())
		{
			self::printErrors(["Form embedding feature disabled."]);
		}

		if (!$formId)
		{
			self::printErrors(["Parameter `id` required."]);
		}
		if (!$securityCode)
		{
			self::printErrors(["Parameter `sec` required."]);
		}

		$form = new WebForm\Form($formId);
		if (!$form->checkSecurityCode($securityCode))
		{
			self::printErrors(["Parameter `security_sign` is invalid."]);
		}

		if (!$form->isActive())
		{
			self::printErrors(["Form with id=`$formId` is disabled."]);
		}

		if ($form->isUsedCaptcha())
		{
			$recaptchaSecret = WebForm\ReCaptcha::getSecret(2) ?: WebForm\ReCaptcha::getDefaultSecret(2);
			$recaptchaKey = WebForm\ReCaptcha::getKey(2) ?: WebForm\ReCaptcha::getDefaultKey(2);
			if ($recaptchaSecret && $recaptchaKey)
			{
				if (!$recaptchaResponse)
				{
					self::printErrors(["Parameter `recaptcha` is invalid."]);
				}

				$recaptcha = new WebForm\ReCaptcha($recaptchaSecret);
				if (!$recaptcha->verify($recaptchaResponse))
				{
					self::printErrors([$recaptcha->getError()]);
				}
			}
		}

		$fill = $form->fill();

		///////////////////
		$values = $values ? Main\Web\Json::decode(
			Main\Text\Encoding::convertEncoding(
				$values,
				SITE_CHARSET,
				'UTF-8'
			)
		) : [];

		$properties = $properties ? Main\Web\Json::decode(
			Main\Text\Encoding::convertEncoding(
				$properties,
				SITE_CHARSET,
				'UTF-8'
			)
		) : [];

		$fill
			->setTrace($trace)
			->setValues($values)
			->setConsents($consents)
			->setProperties($properties);

		$signString = $signString ? Main\Text\Encoding::convertEncoding(
			$signString,
			SITE_CHARSET,
			'UTF-8'
		): null;

		if ($signString)
		{
			$sign = new Sign();
			if ($sign->unpack($signString))
			{
				$fill->setEntities($sign->getEntities());
			}
		}

		$result = $fill->save();
		if (!$result->getId())
		{
			self::printErrors($result->getErrors());
		}


		$gid = $result->getResultEntity()->getTrace()->getGid();
		if (!$gid)
		{
			$gid = Guest::register([
				'ENTITIES' => array_map(
					function (array $item)
					{
						return [
							'ENTITY_TYPE_ID' => \CCrmOwnerType::resolveID($item['ENTITY_TYPE']),
							'ENTITY_ID' => $item['ENTITY_ID'],
						];
					},
					$result->getResultEntity()->getResultEntities()
				)
			]);
		}

		return [
			'resultId' => $result->getId(),
			'pay' => $form->isPayable(),
			'message' => $form->getSuccessText(),
			'gid' => $gid,
			'redirect' => [
				'url' => $result->getUrl(),
				'delay' => $form->getRedirectDelay(),
			],
			'refill' => [
				'active' => $form->getRefill()['ACTIVE'] === 'Y',
				'caption' => $form->getRefill()['CAPTION'],
			]
		];
	}
	catch (RestException $restException)
	{
		return [
			'resultId' => null,
			'pay' => false,
			'message' => $form ? $form->getFailureText() : $restException->getMessage(),
			'gid' => null,
			'redirect' => [],
			'refill' => []
		];
	}
}

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