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 {
*/
protected $description = '';
/**
* @var bool
*/
protected $usedForAutomatedRouting = '';
/**
* @return string
*/
public function getTitle() {
public function getTitle(): string {
return $this->title;
}
......@@ -60,7 +65,7 @@ class Category extends AbstractEntity {
/**
* @return string
*/
public function getDescription() {
public function getDescription(): string {
return $this->description;
}
......@@ -71,4 +76,17 @@ class Category extends AbstractEntity {
$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 {
$querySettings->setEnableFieldsToBeIgnored(['disabled']);
$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 {
return;
}
/** @var ObjectManager $objectManager */
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
/** @var TypoScriptService $typoScriptService */
$typoScriptService = $objectManager->get(TypoScriptService::class);
/** @var BackendConfigurationManager $configurationManager */
$configurationManager = $objectManager->get(BackendConfigurationManager::class);
$pageSetup = $configurationManager->getTypoScriptSetup();
$moduleSettings = [];
if (\is_array($pageSetup['module.']['tx_sgroutes.']['settings.'] ?? FALSE)) {
$moduleSettings = $typoScriptService->convertTypoScriptArrayToPlainArray(
$pageSetup['module.']['tx_sgroutes.']['settings.']
);
$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'];
}
$automatedRoutingCategoryUid = (int) ($moduleSettings['automatedRoutingCategory'] ?? 0);
$siteMatcher = GeneralUtility::makeInstance(SiteMatcher::class);
$site = $siteMatcher->matchByPageId($parentPageId);
if ($automatedRoutingCategoryUid <= 0) {
if (!$site) {
return;
}
$categoryRepository = $objectManager->get(CategoryRepository::class);
/** @var Category $automatedRoutingCategory */
$automatedRoutingCategory = $categoryRepository->findByUid($automatedRoutingCategoryUid);
$automatedRoutingCategory = $this->getAutomatedRoutesCategory($site->getRootPageId());
if (!$automatedRoutingCategory) {
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);
$language = ($pageLanguageUid < 0) ? $site->getDefaultLanguage() : $site->getLanguageById($pageLanguageUid);
$base = $language->getBase();
......@@ -103,10 +86,12 @@ class PageDataHandlerHook {
return;
}
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
$routeRepository = $objectManager->get(RouteRepository::class);
$routeExists = $routeRepository->sourceUrlRouteExists($site->getRootPageId(), $oldDestinationUrl);
$existingSlugRoutes = $routeRepository->findByCategoryAndDestination(
$site->getRootPageId(), $automatedRoutingCategoryUid, $oldDestinationUrl
$site->getRootPageId(), $automatedRoutingCategory->getUid(), $oldDestinationUrl
);
if (\count($existingSlugRoutes)) {
......@@ -141,4 +126,38 @@ class PageDataHandlerHook {
$routeRepository->add($newRoute);
$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 [
'origUid' => 't3_origuid',
'delete' => 'deleted',
'hideTable' => TRUE,
'enablecolumns' => [
'disabled' => 'hidden',
'starttime' => 'starttime',
'endtime' => 'endtime',
],
'iconfile' => 'EXT:sg_routes/Resources/Public/Icons/tx_sgroutes_domain_model_route.svg'
],
'interface' => [
'showRecordFieldList' => 'hidden, title, description',
'showRecordFieldList' => 'title, description, used_for_automated_routing',
],
'types' => [
'1' => [
'showitem' => '--palette--;;hidden;;1, title, description',
'showitem' => 'title, description, used_for_automated_routing',
],
],
'columns' => [
......@@ -55,5 +50,12 @@ return [
'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 {
10 = EXT:sg_routes/Resources/Private/Backend/Layouts/
}
}
settings {
automatedRoutingCategory =
}
}
......@@ -18,8 +18,4 @@ module.tx_sgroutes {
10 = {$module.tx_sgroutes.view.layoutRootPaths.10}
}
}
settings {
automatedRoutingCategory = {$module.tx_sgroutes.settings.automatedRoutingCategory}
}
}
......@@ -29,6 +29,10 @@
<source>Title</source>
<target>Titel</target>
</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">
<source>Redirect Log</source>
<target>Redirect Log</target>
......
......@@ -24,6 +24,9 @@
<trans-unit id="tx_sgroutes_domain_model_category.title">
<source>Title</source>
</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">
<source>Redirect Log</source>
</trans-unit>
......
......@@ -48,6 +48,7 @@ CREATE TABLE tx_sgroutes_domain_model_category (
title varchar(255) DEFAULT '' 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,
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