• Модуль: landing
  • Путь к файлу: ~/bitrix/modules/landing/lib/publicaction/repo.php
  • Класс: BitrixLandingPublicActionRepo
  • Вызов: Repo::register
static function register(string $code, array $fields, array $manifest = []): PublicActionResult
{
	$result = new PublicActionResult();
	$error = new BitrixLandingError;

	// unset not allowed keys
	$notAllowed = array('callbacks');
	foreach ($notAllowed as $key)
	{
		if (isset($manifest[$key]))
		{
			unset($manifest[$key]);
		}
	}

	if (!is_array($fields))
	{
		$fields = array();
	}

	$check = false;
	$fields['XML_ID'] = trim($code);

	// check intersect item of nodes and styles for background type
	if (is_array($manifest['nodes'] ?? null))
	{
		foreach ($manifest['nodes'] as $selector => $manifestItem)
		{
			$styleItem = null;

			if (isset($manifest['style'][$selector]))
			{
				$styleItem = $manifest['style'][$selector];
			}
			if (isset($manifest['style']['nodes'][$selector]))
			{
				$styleItem = $manifest['style']['nodes'][$selector];
			}

			if ($styleItem['type'] ?? null)
			{
				if (!empty(array_intersect((array)$styleItem['type'], StyleImg::STYLES_WITH_IMAGE)))
				{
					$error->addError(
						'MANIFEST_INTERSECT_IMG',
						Loc::getMessage('LANDING_APP_MANIFEST_INTERSECT_IMG', ['#selector#' => $selector])
					);
					$result->setError($error);
					return $result;
				}
			}
		}
	}

	if (isset($fields['CONTENT']))
	{
		// sanitize content
		$fields['CONTENT'] = Manager::sanitize(
			$fields['CONTENT'],
			$bad
		);
		if ($bad)
		{
			$error->addError(
				'CONTENT_IS_BAD',
				Loc::getMessage('LANDING_APP_CONTENT_IS_BAD')
			);
			$result->setError($error);
			return $result;
		}
		// sanitize card's content
		if (
			isset($manifest['cards']) &&
			is_array($manifest['cards'])
		)
		{
			foreach ($manifest['cards'] as $cardCode => &$card)
			{
				if (
					isset($card['presets']) &&
					is_array($card['presets'])
				)
				{
					foreach ($card['presets'] as $presetCode => &$preset)
					{
						foreach (['html', 'name', 'values'] as $code)
						{
							if (isset($preset[$code]))
							{
								$preset[$code] = Manager::sanitize(
									$preset[$code],
									$bad
								);
								if ($bad)
								{
									$error->addError(
										'PRESET_CONTENT_IS_BAD',
										Loc::getMessage(
											'LANDING_APP_PRESET_CONTENT_IS_BAD',
											array(
												'#preset#' => $presetCode,
												'#card#' => $cardCode
											))
									);
									$result->setError($error);
									return $result;
								}
							}
						}
					}
					unset($preset);
				}
			}
			unset($card);
		}
	}

	$fields['MANIFEST'] = serialize($manifest);

	// set app code
	if (($app = BitrixLandingPublicAction::restApplication()))
	{
		$fields['APP_CODE'] = $app['CODE'];
	}

	// check unique
	if ($fields['XML_ID'])
	{
		$check = RepoCore::getList(array(
			'select' => array(
				'ID'
			),
			'filter' =>
				isset($fields['APP_CODE'])
				? array(
					'=XML_ID' => $fields['XML_ID'],
					'=APP_CODE' => $fields['APP_CODE']
				)
				: array(
					'=XML_ID' => $fields['XML_ID']
				)
		))->fetch();
	}

	// register (add / update)
	if ($check)
	{
		$res = RepoCore::update($check['ID'], $fields);
	}
	else
	{
		$res = RepoCore::add($fields);
	}
	if ($res->isSuccess())
	{
		if (
			isset($fields['RESET']) &&
			$fields['RESET'] == 'Y'
		)
		{
			BitrixLandingUpdateBlock::register(
				'repo_' . $res->getId()
			);
		}
		$result->setResult($res->getId());
	}
	else
	{
		$error->addFromResult($res);
		$result->setError($error);
	}

	return $result;
}