• Модуль: landing
  • Путь к файлу: ~/bitrix/modules/landing/lib/hook.php
  • Класс: BitrixLandingHook
  • Вызов: Hook::copy
static function copy($from, $to, $type, $publication = false)
{
	$from = (int)$from;
	$to = (int)$to;
	$data = self::getData($from, $type);
	$existData = [];

	$classDir = self::HOOKS_PAGE_DIR;
	$classNamespace = self::HOOKS_NAMESPACE;
	$excludedHooks = BitrixLandingSiteType::getExcludedHooks();

	// first read all hooks in base dir
	foreach (self::getClassesFromDir($classDir) as $class)
	{
		if (in_array($class, $excludedHooks, true))
		{
			continue;
		}
		$classFull = __NAMESPACE__  . $classNamespace . $class;
		if (
			isset($data[$class])
			&& method_exists($classFull, self::HOOKS_ON_COPY_HANDLER)
		)
		{
			$handler = self::HOOKS_ON_COPY_HANDLER;
			if ($preparedData = $classFull::$handler($data[$class], $from, $type, $publication))
			{
				$data[$class] = $preparedData;
			}
		}
	}

	// collect exist data
	if ($data)
	{
		$res = HookData::getList([
			'select' => [
				'ID', 'HOOK', 'CODE'
			],
			'filter' => [
				'ENTITY_ID' => $to,
				'=ENTITY_TYPE' => $type,
				'=PUBLIC' => $publication ? 'Y' : 'N'
			]
		]);
		while ($row = $res->fetch())
		{
			$existData[$row['HOOK'] . '_' . $row['CODE']] = $row['ID'];
		}
	}

	// update existing keys or add new
	foreach ($data as $hookCode => $items)
	{
		foreach ($items as $code => $value)
		{
			$existKey = $hookCode . '_' . $code;
			if (is_array($value))
			{
				$value = 'serialized#' . serialize($value);
			}
			if (array_key_exists($existKey, $existData))
			{
				HookData::update($existData[$existKey], [
					'VALUE' => $value
				]);
				unset($existData[$existKey]);
			}
			else
			{
				HookData::add([
					'ENTITY_ID' => $to,
					'ENTITY_TYPE' => $type,
					'HOOK' => $hookCode,
					'CODE' => $code,
					'VALUE' => $value,
					'PUBLIC' => $publication ? 'Y' : 'N'
				]);
			}
		}
	}

	// delete unused data
	if ($existData)
	{
		foreach ($existData as $delId)
		{
			HookData::delete($delId);
		}
	}
}