Commit 1a04da20 authored by Kevin Ditscheid's avatar Kevin Ditscheid

[BUGFIX] Fix sg_routes backend module

parent 25d65b67
<?php
namespace SGalinski\SgRoutes\Middleware;
/**
*
* 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!
*/
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use TYPO3\CMS\Core\Http\Response;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
/**
* Resolver for generating frontend links
*
* @package SGalinski\SgRoutes\Middleware
* @author Kevin Ditscheid <kevin.ditscheid@sgalinski.de>
*/
class FrontendLinkResolver implements MiddlewareInterface {
/**
* Process an incoming server request.
*
* Processes an incoming server request in order to produce a response.
* If unable to produce the response itself, it may delegate to the provided
* request handler to do so.
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface {
$queryParams = $request->getQueryParams();
if (!isset($queryParams['tx_sgroutes_frontendlink'])) {
return $handler->handle($request);
}
$contentObjectRenderer = GeneralUtility::makeInstance(ContentObjectRenderer::class);
$response = new Response();
$response->getBody()
->write(
$contentObjectRenderer->typoLink(
'',
[
'parameter' => $queryParams['tx_sgroutes_frontendlink']['destination_url'],
'returnLast' => 'url'
]
)
);
return $response;
}
}
......@@ -42,6 +42,7 @@ use TYPO3\CMS\Core\Package\PackageManager;
use TYPO3\CMS\Core\Service\DependencyOrderingService;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\HttpUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Frontend\Controller\ErrorController;
use TYPO3\CMS\Frontend\Http\RequestHandler;
......@@ -174,9 +175,6 @@ class RoutingService {
*
* @param array $routes
* @return string
* @throws \TYPO3\CMS\Core\Cache\Exception\InvalidDataException
* @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
* @throws \TYPO3\CMS\Core\Exception
*/
public function getHtaccessEntries(array $routes = []): string {
$htaccessEntries = '';
......@@ -198,8 +196,24 @@ class RoutingService {
\is_numeric($route['destination_url']) ||
\substr($route['destination_url'], 0, 5) === 't3://'
) {
$this->initializeFrontendResponse();
$route['destination_url'] = $GLOBALS['TSFE']->cObj->getTypoLink_URL($route['destination_url']);
$report = [
'error' => 0
];
$route['destination_url'] = GeneralUtility::getUrl(
GeneralUtility::getIndpEnv('TYPO3_REQUEST_HOST') . '/?' . HttpUtility::buildQueryString(
[
'tx_sgroutes_frontendlink' => [
'destination_url' => $route['destination_url']
]
]
),
0,
NULL,
$report
);
if ($report['error'] !== 0) {
$route['destination_url'] = '';
}
}
if ($route['destination_url'] !== '') {
// only add the htaccess rule if there is a destination, because otherwise it will be 404 anyway
......@@ -212,41 +226,6 @@ class RoutingService {
return $htaccessEntries;
}
/**
* Initialize a frontend response object and with it the TSFE object
*
* @return \Psr\Http\Message\ResponseInterface
* @throws \TYPO3\CMS\Core\Cache\Exception\InvalidDataException
* @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
* @throws \TYPO3\CMS\Core\Exception
*/
protected function initializeFrontendResponse(): ResponseInterface {
$cacheManager = GeneralUtility::makeInstance(CacheManager::class);
$middlewareStackResolver = GeneralUtility::makeInstance(
MiddlewareStackResolver::class,
GeneralUtility::makeInstance(PackageManager::class),
GeneralUtility::makeInstance(DependencyOrderingService::class),
$cacheManager->getCache('cache_core')
);
$middlewareStack = $middlewareStackResolver->resolve('frontend');
$requestHandler = GeneralUtility::makeInstance(RequestHandler::class);
$middlewareDispatcher = GeneralUtility::makeInstance(
MiddlewareDispatcher::class,
$requestHandler,
$middlewareStack
);
$request = ServerRequestFactory::fromGlobals();
if (!isset($request->getQueryParams()['id'])) {
// find the root page of the current domain
$rootUid = $this->getRootUidByDomain($request->getUri()->getHost(), $request->getUri()->getPath());
$queryParams = $request->getQueryParams();
$queryParams['id'] = $rootUid;
$request = $request->withQueryParams($queryParams);
}
return $middlewareDispatcher->handle($request);
}
/**
* Returns the root uid based on the given page uid
*
......
......@@ -31,6 +31,12 @@ return [
'before' => [
'typo3/cms-frontend/page-resolver'
]
],
'sgalinski/sg-routes/frontendlink-resolver' => [
'target' => \SGalinski\SgRoutes\Middleware\FrontendLinkResolver::class,
'after' => [
'typo3/cms-frontend/tsfe'
]
]
]
];
......@@ -4,13 +4,11 @@
<f:be.container
includeRequireJsModules="{
0: 'TYPO3/CMS/Backend/AjaxDataHandler',
1: '{f:if(condition: \'{typo3Version} < 8000000 \', then: \'TYPO3/CMS/Backend/ClickMenu\', else: \'TYPO3/CMS/Backend/ContextMenu\')}',
2: 'TYPO3/CMS/Backend/Tooltip'}">
1: 'TYPO3/CMS/Backend/ContextMenu',
2: 'TYPO3/CMS/Backend/Tooltip',
3: 'TYPO3/CMS/SgRoutes/Backend'}"
addJsInlineLabels="{0: 'backend.delete_route', 1: 'backend.delete_all', 2: 'backend.htaccess'}">
<sg:addJavaScriptFile javaScriptFile="{f:uri.resource(path: 'Scripts/Backend.js')}" />
<f:format.raw>
<sg:inlineLanguageLabels labels="backend.delete_route, backend.delete_all, backend.htaccess" />
</f:format.raw>
<div class="module" data-module-id="" data-module-name="">
<div class="module-docheader t3js-module-docheader">
<div class="module-docheader-bar module-docheader-bar-navigation t3js-module-docheader-bar t3js-module-docheader-bar-navigation">
......
......@@ -27,16 +27,11 @@
<small>
<f:format.raw><f:translate key="backend.filters.categories.description" /></f:format.raw>
</small>
<script>
var CategoryEditLinks = {};
</script>
<f:for each="{categoryOptions}" key="uid" as="category">
<script>
CategoryEditLinks["{uid}"] = "{sg:backend.editLink(table: 'tx_sgroutes_domain_model_category', uid: uid) -> f:format.raw()}";
</script>
<div id="sg-routes-categoryeditlinks-{uid}" data-url="{sg:backend.editLink(table: 'tx_sgroutes_domain_model_category', uid: uid)}"></div>
</f:for>
<br />
<a href="#" class="btn btn-default" onclick="editSelectedCategory();">
<a href="#" class="btn btn-default" onclick="TYPO3.SgRoutes.editSelectedCategory();">
<span class="t3js-icon icon icon-size-small icon-state-default icon-actions-document-new">
<span class="icon-markup">
<img src="/typo3/sysext/core/Resources/Public/Icons/T3Icons/actions/actions-document-open.svg" width="16" height="16">
......
/***************************************************************
* 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!
***************************************************************/
define([
'jquery'
], function($) {
'use strict';
var SgRoutes = {
init: function() {
$('.btn-delete-all').on('click', SgRoutes.deleteAllListener);
$.get(TYPO3.settings.ajaxUrls['sg_routes::ajaxPing']);
},
/**
* Deletes all routes
*
* @param _event
*/
deleteAllListener: function(_event) {
_event.preventDefault();
var confirm = TYPO3.lang['backend.delete_all'];
if (window.confirm(confirm)) {
window.location = $(_event.currentTarget).attr('href');
}
},
/**
* opens the selected category edit form
*
* @return {boolean}
*/
editSelectedCategory: function() {
var selected = $('#filter-categories').val();
if (selected) {
var editLink = $('#sg-routes-categoryeditlinks-' + selected[0]);
if (editLink.length > 0) {
window.location.href = editLink.data('url') + '&returnUrl=' + T3_THIS_LOCATION;
}
}
}
};
TYPO3.SgRoutes = SgRoutes;
SgRoutes.init();
return SgRoutes;
});
(function($) {
function deleteAllListener(_event) {
_event.preventDefault();
var confirm = SG.lang.SgRoutes['backend.delete_all'];
if (window.confirm(confirm)) {
window.location = $(_event.currentTarget).attr('href');
}
}
$(document).ready(function() {
$('.btn-delete-all').on('click', deleteAllListener);
$.get(TYPO3.settings.ajaxUrls['sg_routes::ajaxPing']);
});
})(TYPO3.jQuery);
// functions for backend docheader functionality
function jumpExt(URL, anchor) { //
var anc = anchor ? anchor : "";
window.location.href = URL + (T3_THIS_LOCATION ? "&returnUrl=" + T3_THIS_LOCATION : "") + anc;
return false;
}
function jumpSelf(URL) { //
window.location.href = URL + (T3_RETURN_URL ? "&returnUrl=" + T3_RETURN_URL : "");
return false;
}
function jumpToUrl(URL) {
window.location.href = URL;
return false;
}
function setHighlight(id) { //
top.fsMod.recentIds["web"] = id;
top.fsMod.navFrameHighlightedID["web"] = "pages" + id + "_" + top.fsMod.currentBank; // For highlighting
if (top.content && top.content.nav_frame && top.content.nav_frame.refresh_nav) {
top.content.nav_frame.refresh_nav();
}
}
/**
* Switches to the spefied page in the BE
*
* @param {number} uid
* @param {string} path
*/
function sgRouteGoToPage(uid, path) {
if (top.nav) {
top.nav.invokePageId(uid, gotToPageCallback);
} else {
var tree = top.Ext.getCmp('typo3-pagetree');
if (tree) {
var elements = path.split('/');
var pageNode = tree.activeTree.getNodeById(elements[elements.length - 1]);
tree.activeTree.selectPath(path);
if (pageNode) {
tree.activeTree.commandProvider.singleClick(pageNode, tree.activeTree);
}
}
var separator = '?';
if (top.currentSubScript.indexOf('?') !== -1) {
separator = '&';
}
top.TYPO3.Backend.ContentContainer.setUrl(
top.currentSubScript + separator + 'id=' + uid
);
}
}
/**
* Callback for page selection in the pagetree
*/
function gotToPageCallback(path) {
var callback = top.Ext.createDelegate(top.nav.mainTree.selectPath, top.nav.mainTree);
callback.apply(this, arguments);
var node = top.nav.getSelected();
if (node) {
top.TYPO3.Components.PageTree.Actions.singleClick(node, top.TYPO3.Components.PageTree.Tree);
}
}
/**
* opens the selected category edit form
*
* @return {boolean}
*/
function editSelectedCategory() {
var selected = TYPO3.jQuery('#filter-categories').val();
if (selected && CategoryEditLinks[selected[0]]) {
jumpToUrl(CategoryEditLinks[selected[0]] + '&returnUrl=' + T3_THIS_LOCATION);
}
return false;
}
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