• Модуль: landing
  • Путь к файлу: ~/bitrix/modules/landing/lib/transfer/import/landing.php
  • Класс: BitrixLandingTransferImportLanding
  • Вызов: Landing::replaceLanding
static function replaceLanding(Event $event): ?array
{
	$code = $event->getParameter('CODE');
	$content = $event->getParameter('CONTENT');
	$ratio = $event->getParameter('RATIO');
	$contextUser = $event->getParameter('CONTEXT_USER');
	$additional = $event->getParameter('ADDITIONAL_OPTION');
	$structure = new ConfigurationStructure($contextUser);

	if (!isset($content['~DATA']))
	{
		return null;
	}

	$return = [
		'RATIO' => $ratio[$code] ?? [],
		'ERROR_MESSAGES' => []
	];

	$replaceLid = (int)$additional['replaceLid'];
	if ($replaceLid <= 0)
	{
		$return['ERROR_MESSAGES'] = 'Not set landing ID for replace';

		return $return;
	}

	if (isset($return['RATIO']['TYPE']))
	{
		SiteCoreType::setScope($return['RATIO']['TYPE']);
	}
	LandingCore::setEditMode();
	$landing = LandingCore::createInstance($replaceLid);
	if (!$landing->exist())
	{
		$return['ERROR_MESSAGES'] = 'Raplaced landing is not exists';

		return $return;
	}

	// no landing imported
	$return['RATIO']['LANDINGS'][$replaceLid] = $replaceLid;

	if (!self::isNeedImport($event))
	{
		return $return;
	}

	$data = $content['~DATA'];
	$data = self::prepareData($data);

	$additionalFieldsBefore = self::getAdditionalFieldsForReplaceByLanding($replaceLid);
	if (is_array($ratio[$code]['ADDITIONAL_FIELDS_SITE']) && !empty($ratio[$code]['ADDITIONAL_FIELDS_SITE']))
	{
		$data = self::mergeAdditionalFieldsForReplace($data, $ratio[$code]['ADDITIONAL_FIELDS_SITE']);
		$data = self::prepareAdditionalFiles($data, $structure);
		self::saveAdditionalFieldsToLanding($data, $replaceLid);
		self::saveAdditionalFilesToLanding($data, $replaceLid);
	}

	if (isset($data['BLOCKS']) && is_array($data['BLOCKS']))
	{
		$data = self::prepareBlocksData($data, $event);
		$blocksBefore = [];
		$blocksAfter = [];

		History::deactivate();
		foreach ($landing->getBlocks() as $block)
		{
			$blockId = $block->getId();
			$block->setAccess(Block::ACCESS_X);
			if ($landing->markDeletedBlock($block->getId(), true))
			{
				$blocksBefore[] = $blockId;
			}
		}

		foreach ($data['BLOCKS'] as $oldBlockId => $block)
		{
			if (is_array($block) && !empty($block))
			{
				$pending = false;
				$newBlockId = self::importBlock(
					$landing,
					$block,
					$structure,
					$pending
				);
				$blocksAfter[] = $newBlockId;
				$return['RATIO']['BLOCKS'][$oldBlockId] = $newBlockId;
				if ($pending)
				{
					$return['RATIO']['BLOCKS_PENDING'][] = $newBlockId;
				}
			}
		}

		// find form block and replace form ID if need
		$meta = $landing->getMeta();
		if ($meta['SITE_SPECIAL'] === 'Y')
		{
			$specialType = SiteCoreType::getSiteTypeForms($meta['SITE_CODE']);
		}
		if (
			isset($specialType)
			&& $specialType === 'crm_forms'
			&& Loader::includeModule('crm')
		)
		{
			// find form
			$res = CrmWebFormInternalsLandingTable::getList([
				'select' => [
					'FORM_ID'
				],
				'filter' => [
					'=LANDING_ID' => $replaceLid
				]
			]);
			$row = $res->fetch();
			$formId = $row ? $row['FORM_ID'] : null;
			if ($formId)
			{
				foreach ($landing->getBlocks() as $block)
				{
					$manifest = $block->getManifest();
					if (($manifest['block']['subtype'] ?? null) === 'form')
					{
						Form::setFormIdToBlock($block->getId(), $formId);
						if ($block->getAccess() > Block::ACCESS_W)
						{
							BlockTable::update($block->getId(), [
								'ACCESS' => Block::ACCESS_W
							]);
						}
					}
				}
			}
		}

		if (Manager::isAutoPublicationEnabled())
		{
			$landing->publication();
		}

		History::activate();
		$history = new History($replaceLid, History::ENTITY_TYPE_LANDING);
		$history->push('REPLACE_LANDING', [
			'lid' => $replaceLid,
			'template' => $code,
			'blocksBefore' => $blocksBefore,
			'blocksAfter' => $blocksAfter,
			'additionalFieldsBefore' => $additionalFieldsBefore,
			'additionalFieldsAfter' => $data['ADDITIONAL_FIELDS'],
		]);
	}

	return $return;
}