PageDataHandlerHook.php 5.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
<?php

namespace SGalinski\SgRoutes\Hook;

use SGalinski\SgRoutes\Domain\Model\Category;
use SGalinski\SgRoutes\Domain\Model\Route;
use SGalinski\SgRoutes\Domain\Repository\CategoryRepository;
use SGalinski\SgRoutes\Domain\Repository\RouteRepository;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Routing\SiteMatcher;
use TYPO3\CMS\Core\TypoScript\TypoScriptService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\BackendConfigurationManager;
use TYPO3\CMS\Extbase\Object\ObjectManager;

/***************************************************************
 *  Copyright notice
 *
 *  (c) sgalinski Internet Services (https://www.sgalinski.de)
 *
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
 *  free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  The GNU General Public License can be found at
 *  http://www.gnu.org/copyleft/gpl.html.
 *
 *  This script is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  This copyright notice MUST APPEAR in all copies of the script!
 ***************************************************************/
class PageDataHandlerHook {

	/**
	 * Called after a record was edited or added.
	 *
	 * @param string $status DataHandler operation status, either 'new' or 'update'
	 * @param string $table The DB table the operation was carried out on
	 * @param mixed $id The record's uid for update records, a string to look the record's uid up after it has been created
	 * @param array $fieldArray Array of changed fields and their new values
	 * @throws \TYPO3\CMS\Core\Exception\SiteNotFoundException
	 * @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
	 * @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
	 * @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException
	 */
	public function processDatamap_postProcessFieldArray(string $status, string $table, $id, array $fieldArray) {
		if ($table !== 'pages' || $status === 'new' || !isset($fieldArray['slug'])) {
			return;
		}

58 59 60 61 62 63
		$oldValues = BackendUtility::getRecord($table, $id, 'slug, sys_language_uid, l10n_parent');
		$pageLanguageUid = (int) $oldValues['sys_language_uid'];
		$parentPageId = (int) $id;

		if ($pageLanguageUid > 0) {
			$parentPageId = (int) $oldValues['l10n_parent'];
64 65
		}

66 67
		$siteMatcher = GeneralUtility::makeInstance(SiteMatcher::class);
		$site = $siteMatcher->matchByPageId($parentPageId);
68

69
		if (!$site) {
70 71 72
			return;
		}

73
		$automatedRoutingCategory = $this->getAutomatedRoutesCategory($site->getRootPageId());
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

		if (!$automatedRoutingCategory) {
			return;
		}

		$site = $siteMatcher->matchByPageId($parentPageId);
		$language = ($pageLanguageUid < 0) ? $site->getDefaultLanguage() : $site->getLanguageById($pageLanguageUid);
		$base = $language->getBase();
		$oldDestinationUrl = rtrim($base->getPath(), '/') . '/' . ltrim($oldValues['slug'], '/');
		$newDestinationUrl = rtrim($base->getPath(), '/') . '/' . ltrim($fieldArray['slug'], '/');

		if ($oldDestinationUrl === $newDestinationUrl) {
			return;
		}

89 90

		$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
91 92 93
		$routeRepository = $objectManager->get(RouteRepository::class);
		$routeExists = $routeRepository->sourceUrlRouteExists($site->getRootPageId(), $oldDestinationUrl);
		$existingSlugRoutes = $routeRepository->findByCategoryAndDestination(
94
			$site->getRootPageId(), $automatedRoutingCategory->getUid(), $oldDestinationUrl
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
		);

		if (\count($existingSlugRoutes)) {
			/** @var Route $route */
			foreach ($existingSlugRoutes as $route) {
				if ($route->getSourceUrl() === $newDestinationUrl) {
					$routeRepository->remove($route);
				} else {
					$route->setDestinationUrl($newDestinationUrl);
					$route->setTstamp($GLOBALS['EXEC_TIME']);
					$routeRepository->update($route);
				}
			}

			$routeRepository->persistAll();
		}

		if ($routeExists) {
			return;
		}

		$newRoute = new Route();
		$newRoute->setPid($site->getRootPageId());
		$newRoute->addCategory($automatedRoutingCategory);
		$newRoute->setSourceUrl($oldDestinationUrl);
		$newRoute->setDestinationUrl($newDestinationUrl);
		$newRoute->setDescription(
			'Automated slug update routing for page uid ' . $parentPageId . '(' . $language->getTwoLetterIsoCode() . ')'
		);
		$newRoute->setCrdate($GLOBALS['EXEC_TIME']);
		$newRoute->setTstamp($GLOBALS['EXEC_TIME']);
		$routeRepository->add($newRoute);
		$routeRepository->persistAll();
	}
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162

	/**
	 * Returns the automated routes category
	 *
	 * @param $rootPageUid
	 * @return Category|null
	 * @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
	 */
	protected function getAutomatedRoutesCategory($rootPageUid) {
		$rootPageUid = (int) $rootPageUid;

		if ($rootPageUid <= 0) {
			return null;
		}

		$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
		$categoryRepository = $objectManager->get(CategoryRepository::class);
		/** @var Category $category */
		$category = $categoryRepository->findOneByUsedForAutomatedRouting(TRUE);

		if ($category) {
			return $category;
		}

		$category = new Category();
		$category->setTitle('Automated Routing');
		$category->setDescription('Routing records automatically generated, based on page slug updates.');
		$category->setUsedForAutomatedRouting(TRUE);
		$category->setPid($rootPageUid);
		$categoryRepository->add($category);
		$categoryRepository->persistAll();

		return $category;
	}
163
}