Commit d34f7e85 authored by Paul Ilea's avatar Paul Ilea

[TASK] Auto-create automatic-routing category if none is found

parent dcabe5cf
...@@ -42,11 +42,16 @@ class Category extends AbstractEntity { ...@@ -42,11 +42,16 @@ class Category extends AbstractEntity {
*/ */
protected $description = ''; protected $description = '';
/**
* @var bool
*/
protected $usedForAutomatedRouting = '';
/** /**
* @return string * @return string
*/ */
public function getTitle() { public function getTitle(): string {
return $this->title; return $this->title;
} }
...@@ -60,7 +65,7 @@ class Category extends AbstractEntity { ...@@ -60,7 +65,7 @@ class Category extends AbstractEntity {
/** /**
* @return string * @return string
*/ */
public function getDescription() { public function getDescription(): string {
return $this->description; return $this->description;
} }
...@@ -71,4 +76,17 @@ class Category extends AbstractEntity { ...@@ -71,4 +76,17 @@ class Category extends AbstractEntity {
$this->description = $description; $this->description = $description;
} }
/**
* @return bool
*/
public function getUsedForAutomatedRouting(): bool {
return $this->usedForAutomatedRouting;
}
/**
* @param bool $usedForAutomatedRouting
*/
public function setUsedForAutomatedRouting($usedForAutomatedRouting) {
$this->usedForAutomatedRouting = (bool) $usedForAutomatedRouting;
}
} }
...@@ -43,4 +43,29 @@ class CategoryRepository extends Repository { ...@@ -43,4 +43,29 @@ class CategoryRepository extends Repository {
$querySettings->setEnableFieldsToBeIgnored(['disabled']); $querySettings->setEnableFieldsToBeIgnored(['disabled']);
$this->setDefaultQuerySettings($querySettings); $this->setDefaultQuerySettings($querySettings);
} }
/**
* Queries for automated routing categories and returns the first result
*
* @param $rootPageUid
* @return object
*/
public function findAutomatedRoutingByRootPage($rootPageUid) {
$rootPageUid = (int) $rootPageUid;
$query = $this->createQuery();
$querySettings = $query->getQuerySettings();
$querySettings->setRespectStoragePage(TRUE);
$querySettings->setStoragePageIds([$rootPageUid]);
$query->setQuerySettings($querySettings);
$query->setLimit(1);
return $query->matching($query->equals('usedForAutomatedRouting', 1))->execute()->getFirst();
}
/**
* Commits the object updates to the database
*/
public function persistAll() {
$this->persistenceManager->persistAll();
}
} }
...@@ -55,44 +55,27 @@ class PageDataHandlerHook { ...@@ -55,44 +55,27 @@ class PageDataHandlerHook {
return; return;
} }
/** @var ObjectManager $objectManager */ $oldValues = BackendUtility::getRecord($table, $id, 'slug, sys_language_uid, l10n_parent');
$objectManager = GeneralUtility::makeInstance(ObjectManager::class); $pageLanguageUid = (int) $oldValues['sys_language_uid'];
/** @var TypoScriptService $typoScriptService */ $parentPageId = (int) $id;
$typoScriptService = $objectManager->get(TypoScriptService::class);
/** @var BackendConfigurationManager $configurationManager */ if ($pageLanguageUid > 0) {
$configurationManager = $objectManager->get(BackendConfigurationManager::class); $parentPageId = (int) $oldValues['l10n_parent'];
$pageSetup = $configurationManager->getTypoScriptSetup();
$moduleSettings = [];
if (\is_array($pageSetup['module.']['tx_sgroutes.']['settings.'] ?? FALSE)) {
$moduleSettings = $typoScriptService->convertTypoScriptArrayToPlainArray(
$pageSetup['module.']['tx_sgroutes.']['settings.']
);
} }
$automatedRoutingCategoryUid = (int) ($moduleSettings['automatedRoutingCategory'] ?? 0); $siteMatcher = GeneralUtility::makeInstance(SiteMatcher::class);
$site = $siteMatcher->matchByPageId($parentPageId);
if ($automatedRoutingCategoryUid <= 0) { if (!$site) {
return; return;
} }
$categoryRepository = $objectManager->get(CategoryRepository::class); $automatedRoutingCategory = $this->getAutomatedRoutesCategory($site->getRootPageId());
/** @var Category $automatedRoutingCategory */
$automatedRoutingCategory = $categoryRepository->findByUid($automatedRoutingCategoryUid);
if (!$automatedRoutingCategory) { if (!$automatedRoutingCategory) {
return; return;
} }
$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'];
}
$siteMatcher = GeneralUtility::makeInstance(SiteMatcher::class);
$site = $siteMatcher->matchByPageId($parentPageId); $site = $siteMatcher->matchByPageId($parentPageId);
$language = ($pageLanguageUid < 0) ? $site->getDefaultLanguage() : $site->getLanguageById($pageLanguageUid); $language = ($pageLanguageUid < 0) ? $site->getDefaultLanguage() : $site->getLanguageById($pageLanguageUid);
$base = $language->getBase(); $base = $language->getBase();
...@@ -103,10 +86,12 @@ class PageDataHandlerHook { ...@@ -103,10 +86,12 @@ class PageDataHandlerHook {
return; return;
} }
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
$routeRepository = $objectManager->get(RouteRepository::class); $routeRepository = $objectManager->get(RouteRepository::class);
$routeExists = $routeRepository->sourceUrlRouteExists($site->getRootPageId(), $oldDestinationUrl); $routeExists = $routeRepository->sourceUrlRouteExists($site->getRootPageId(), $oldDestinationUrl);
$existingSlugRoutes = $routeRepository->findByCategoryAndDestination( $existingSlugRoutes = $routeRepository->findByCategoryAndDestination(
$site->getRootPageId(), $automatedRoutingCategoryUid, $oldDestinationUrl $site->getRootPageId(), $automatedRoutingCategory->getUid(), $oldDestinationUrl
); );
if (\count($existingSlugRoutes)) { if (\count($existingSlugRoutes)) {
...@@ -141,4 +126,38 @@ class PageDataHandlerHook { ...@@ -141,4 +126,38 @@ class PageDataHandlerHook {
$routeRepository->add($newRoute); $routeRepository->add($newRoute);
$routeRepository->persistAll(); $routeRepository->persistAll();
} }
/**
* 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;
}
} }
...@@ -14,19 +14,14 @@ return [ ...@@ -14,19 +14,14 @@ return [
'origUid' => 't3_origuid', 'origUid' => 't3_origuid',
'delete' => 'deleted', 'delete' => 'deleted',
'hideTable' => TRUE, 'hideTable' => TRUE,
'enablecolumns' => [
'disabled' => 'hidden',
'starttime' => 'starttime',
'endtime' => 'endtime',
],
'iconfile' => 'EXT:sg_routes/Resources/Public/Icons/tx_sgroutes_domain_model_route.svg' 'iconfile' => 'EXT:sg_routes/Resources/Public/Icons/tx_sgroutes_domain_model_route.svg'
], ],
'interface' => [ 'interface' => [
'showRecordFieldList' => 'hidden, title, description', 'showRecordFieldList' => 'title, description, used_for_automated_routing',
], ],
'types' => [ 'types' => [
'1' => [ '1' => [
'showitem' => '--palette--;;hidden;;1, title, description', 'showitem' => 'title, description, used_for_automated_routing',
], ],
], ],
'columns' => [ 'columns' => [
...@@ -55,5 +50,12 @@ return [ ...@@ -55,5 +50,12 @@ return [
'eval' => 'trim' 'eval' => 'trim'
], ],
], ],
'used_for_automated_routing' => [
'exclude' => 0,
'label' => 'LLL:EXT:sg_routes/Resources/Private/Language/locallang_db.xlf:tx_sgroutes_domain_model_category.used_for_automated_routing',
'config' => [
'type' => 'check'
],
],
], ],
]; ];
...@@ -18,9 +18,5 @@ module.tx_sgroutes { ...@@ -18,9 +18,5 @@ module.tx_sgroutes {
10 = EXT:sg_routes/Resources/Private/Backend/Layouts/ 10 = EXT:sg_routes/Resources/Private/Backend/Layouts/
} }
} }
settings {
automatedRoutingCategory =
}
} }
...@@ -18,8 +18,4 @@ module.tx_sgroutes { ...@@ -18,8 +18,4 @@ module.tx_sgroutes {
10 = {$module.tx_sgroutes.view.layoutRootPaths.10} 10 = {$module.tx_sgroutes.view.layoutRootPaths.10}
} }
} }
settings {
automatedRoutingCategory = {$module.tx_sgroutes.settings.automatedRoutingCategory}
}
} }
...@@ -29,6 +29,10 @@ ...@@ -29,6 +29,10 @@
<source>Title</source> <source>Title</source>
<target>Titel</target> <target>Titel</target>
</trans-unit> </trans-unit>
<trans-unit id="tx_sgroutes_domain_model_category.used_for_automated_routing">
<source>Used for automatically created redirects</source>
<target>Wird für automatisch erstellte Weiterleitungen verwendet</target>
</trans-unit>
<trans-unit id="tx_sgroutes_domain_model_log"> <trans-unit id="tx_sgroutes_domain_model_log">
<source>Redirect Log</source> <source>Redirect Log</source>
<target>Redirect Log</target> <target>Redirect Log</target>
......
...@@ -24,6 +24,9 @@ ...@@ -24,6 +24,9 @@
<trans-unit id="tx_sgroutes_domain_model_category.title"> <trans-unit id="tx_sgroutes_domain_model_category.title">
<source>Title</source> <source>Title</source>
</trans-unit> </trans-unit>
<trans-unit id="tx_sgroutes_domain_model_category.used_for_automated_routing">
<source>Used for automatically created redirects</source>
</trans-unit>
<trans-unit id="tx_sgroutes_domain_model_log"> <trans-unit id="tx_sgroutes_domain_model_log">
<source>Redirect Log</source> <source>Redirect Log</source>
</trans-unit> </trans-unit>
......
...@@ -48,6 +48,7 @@ CREATE TABLE tx_sgroutes_domain_model_category ( ...@@ -48,6 +48,7 @@ CREATE TABLE tx_sgroutes_domain_model_category (
title varchar(255) DEFAULT '' NOT NULL, title varchar(255) DEFAULT '' NOT NULL,
description text NOT NULL, description text NOT NULL,
used_for_automated_routing tinyint(4) unsigned DEFAULT '0' NOT NULL,
tstamp int(11) unsigned DEFAULT '0' NOT NULL, tstamp int(11) unsigned DEFAULT '0' NOT NULL,
crdate int(11) unsigned DEFAULT '0' NOT NULL, crdate int(11) unsigned DEFAULT '0' NOT NULL,
......
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