Commit 938deaa7 authored by Paul Ilea's avatar Paul Ilea

[FEATURE] Add unique source url check for BE Module editing

parent cb1fe688
......@@ -131,14 +131,25 @@ class RouteRepository extends Repository {
*
* @param int $pageUid
* @param string $sourceUrl
* @param array $excludeUids
* @return bool
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
*/
public function sourceUrlRouteExists($pageUid, $sourceUrl): bool {
public function sourceUrlRouteExists($pageUid, $sourceUrl, array $excludeUids = []): bool {
$query = $this->createQuery();
$querySettings = $query->getQuerySettings();
$querySettings->setStoragePageIds([$pageUid]);
$query->setQuerySettings($querySettings);
$query->matching($query->equals('source_url', \trim($sourceUrl)));
$constraints = [
$query->equals('source_url', \trim($sourceUrl))
];
if (\count($excludeUids)) {
$constraints[] = $query->logicalNot($query->in('uid', $excludeUids));
}
$query->matching($query->logicalAnd($constraints));
$query->setLimit(1);
return \count($query->execute(TRUE)) > 0;
......
<?php
namespace SGalinski\SgRoutes\Hook;
use SGalinski\SgRoutes\Domain\Repository\RouteRepository;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Core\Messaging\FlashMessageService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
/***************************************************************
* 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 RouteDataHandlerHook {
/**
* 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
*/
public function processDatamap_postProcessFieldArray(string $status, string $table, $id, array &$fieldArray) {
if ($table !== 'tx_sgroutes_domain_model_route') {
return;
}
$id = (int) $id;
$sourceUrl = '';
if (isset($fieldArray['source_url'])) {
$sourceUrl = \trim($fieldArray['source_url']);
}
if ($status === 'new' || isset($fieldArray['pid'])) {
$pid = (int) $fieldArray['pid'];
} else {
$oldValues = BackendUtility::getRecord($table, $id, 'pid, source_url');
$pid = (int) $oldValues['pid'];
if ($sourceUrl === '') {
$sourceUrl = \trim($oldValues['source_url']);
}
}
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
$routeRepository = $objectManager->get(RouteRepository::class);
$routeExistsForSourceUrl = $routeRepository->sourceUrlRouteExists($pid, $sourceUrl, [$id]);
if ($routeExistsForSourceUrl) {
/** @var FlashMessage $message */
$message = GeneralUtility::makeInstance(
FlashMessage::class,
LocalizationUtility::translate(
'LLL:EXT:sg_routes/Resources/Private/Language/locallang_db.xlf:error.invalidSourceUrl', NULL,
[$sourceUrl]
),
'', // header is optional
FlashMessage::ERROR,
TRUE // whether message should be stored in session
);
/** @var $flashMessageService FlashMessageService */
$flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
$flashMessageService->getMessageQueueByIdentifier()->enqueue($message);
unset($fieldArray['source_url']);
}
}
}
......@@ -9,6 +9,10 @@
<authorEmail>fabian@sgalinski.de</authorEmail>
</header>
<body>
<trans-unit id="error.invalidSourceUrl">
<source>The Source URL "%s" is already redirected by another record.</source>
<target>Die Quell-URL "%s" wird bereits von einer anderen Datensatz weitergeleitet.</target>
</trans-unit>
<trans-unit id="tx_sgroutes_domain_model_category" approved="yes">
<source>Category</source>
<target>Kategorie</target>
......
......@@ -9,6 +9,9 @@
<authorEmail>fabian@sgalinski.de</authorEmail>
</header>
<body>
<trans-unit id="error.invalidSourceUrl">
<source>The Source URL "%s" is already redirected by another record.</source>
</trans-unit>
<trans-unit id="tx_sgroutes_domain_model_category">
<source>Category</source>
</trans-unit>
......
......@@ -62,6 +62,9 @@ call_user_func(
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processDatamapClass'][] =
SGalinski\SgRoutes\Hook\PageDataHandlerHook::class;
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processDatamapClass'][] =
SGalinski\SgRoutes\Hook\RouteDataHandlerHook::class;
if (!is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['cms/layout/db_layout.php']['drawHeaderHook'])) {
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['cms/layout/db_layout.php']['drawHeaderHook'] = [];
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment