Files
oai-symfony/typo3conf/ext/lfeditor/Classes/Controller/AbstractBackendController.php
2026-07-03 17:53:31 +02:00

237 lines
8.2 KiB
PHP

<?php
namespace SGalinski\Lfeditor\Controller;
/***************************************************************
* 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 TYPO3\CMS\Backend\Template\ModuleTemplate;
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
/**
* Abstract Backend Controller
*/
abstract class AbstractBackendController extends AbstractController {
/**
* @var BackendUserAuthentication
*/
protected $backendUser;
/**
* @var ?ModuleTemplate
*/
protected $moduleTemplate = NULL;
/**
* Initializes any action
*
* @return void
* @throws \SGalinski\Lfeditor\Exceptions\DirectoryAccessRightsException
*/
public function initializeAction() {
parent::initializeAction();
if (\TYPO3\CMS\Core\Http\ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST'])->isBackend()) {
$this->backendUser = $GLOBALS['BE_USER'];
}
$editingMode = $this->session->getDataByKey('editingMode');
$availableEditingModes = $this->configurationService->getAvailableEditingModes();
if ($this->backendUser->isAdmin()) {
if (!\array_key_exists($editingMode, $availableEditingModes)) {
$firstAvailableEditMode = \key($availableEditingModes);
$this->session->setDataByKey('editingMode', $firstAvailableEditMode);
}
$canChangeEditingModes = \count($availableEditingModes) > 0;
} else {
$canChangeEditingModes = \count($availableEditingModes) > 0
&& $this->backendUser->user['lfeditor_change_editing_modes'] !== 0;
if (!$canChangeEditingModes || !\array_key_exists($editingMode, $availableEditingModes)) {
$lastAvailableEditMode = array_key_last($availableEditingModes);
$this->session->setDataByKey('editingMode', $lastAvailableEditMode);
}
}
$this->session->setDataByKey('defaultLanguagePermission', $this->backendUser->checkLanguageAccess(0));
$this->session->setDataByKey('canChangeEditingModes', $canChangeEditingModes);
}
/**
* Saves the the last called controller/action pair into the backend user
* configuration if available
*
* @param bool $saveWithRedirectPair
* @return void
*/
protected function setLastCalledControllerActionPair($saveWithRedirectPair = TRUE) {
if (!$this->backendUser) {
return;
}
$extensionKey = $this->request->getControllerExtensionKey();
$pair = [
'action' => $this->request->getControllerActionName(),
'controller' => $this->request->getControllerName(),
];
$this->backendUser->uc[$extensionKey . 'State']['LastActionControllerPair'] = $pair;
if ($saveWithRedirectPair) {
$this->backendUser->uc[$extensionKey . 'State']['LastActionControllerPairForRedirect'] = $pair;
}
$this->backendUser->writeUC($this->backendUser->uc);
}
/**
* Resets the last called controller/action pair combination from the
* backend user session
*
* @return void
*/
protected function resetLastCalledControllerActionPair() {
if (!$this->backendUser) {
return;
}
$extensionKey = $this->request->getControllerExtensionKey();
$this->backendUser->uc[$extensionKey . 'State']['LastActionControllerPair'] = [];
$this->backendUser->uc[$extensionKey . 'State']['LastActionControllerPairForRedirect'] = [];
$this->backendUser->writeUC($this->backendUser->uc);
if ($this->view instanceof ViewInterface) {
$this->view->assign('lastCalledControllerActionPair', NULL);
}
}
/**
* Returns the last called controller/action pair from the backend user session
*
* @return array
*/
protected function getLastCalledControllerActionPair() {
if (!$this->backendUser) {
return [];
}
$extensionKey = $this->request->getControllerExtensionKey();
$state = $this->backendUser->uc[$extensionKey . 'State']['LastActionControllerPair'];
return (!\is_array($state) ? [] : $state);
}
/**
* Returns the last called controller/action pair from the backend user session
*
* @return array
*/
protected function getLastCalledControllerActionPairForRedirect() {
if (!$this->backendUser) {
return [];
}
$extensionKey = $this->request->getControllerExtensionKey();
return $this->backendUser->uc[$extensionKey . 'State']['LastActionControllerPairForRedirect'] ?? [];
}
/**
* Redirects to the last called controller/action pair saved inside the
* backend user session
*
* @return \TYPO3\CMS\Extbase\Http\ForwardResponse|null
* @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
*/
protected function redirectToLastCalledControllerActionPair(): ?\TYPO3\CMS\Extbase\Http\ForwardResponse {
$state = $this->getLastCalledControllerActionPairForRedirect();
if (\count($state) && \trim($state['action']) !== '' && \trim($state['controller']) !== '') {
$currentAction = $this->request->getControllerActionName();
$currentController = $this->request->getControllerName();
if (!($currentController === $state['controller'] && $currentAction === $state['action'])) {
$extensionName = $this->request->getControllerExtensionName();
$moduleSignature = $this->request->getPluginName();
$extensionConfig = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName];
$availableControllers = $extensionConfig['modules'][$moduleSignature]['controllers'];
$controllerExists = isset($availableControllers[$state['controller']]);
if ($controllerExists) {
$actionExists = \in_array(
$state['action'],
$availableControllers[$state['controller']]['actions'],
TRUE
);
if ($actionExists) {
if (version_compare(
\TYPO3\CMS\Core\Utility\VersionNumberUtility::getCurrentTypo3Version(),
'11.0.0',
'<'
)) {
$this->forward($state['action'], $state['controller']);
}
return (new \TYPO3\CMS\Extbase\Http\ForwardResponse($state['action']))->withControllerName(
$state['controller']
);
}
}
}
}
return NULL;
}
/**
* Sets last called controller-action pair and assigns common view variables.
* This function should be called at the end of actions which render view
* (and does not do redirection or forwarding at the end)
*/
protected function commonViewRenderingActionSettings() {
$this->setLastCalledControllerActionPair();
$this->view->assign('editingMode', $this->session->getDataByKey('editingMode'));
$this->view->assign('editingModeOptions', $this->configurationService->getAvailableEditingModes());
$this->view->assign('adminUser', $this->backendUser->isAdmin());
$this->view->assign('defaultLanguagePermission', $this->session->getDataByKey('defaultLanguagePermission'));
$this->view->assign('canChangeEditingModes', $this->session->getDataByKey('canChangeEditingModes'));
}
/**
* Since we cannot use constructor Injection, we do have to get one dynamically. If we want to use
* our ModuleTemplate, call GetModuleTemplate before, which will init one if there is none for some reason
*/
protected function getModuleTemplate() {
if ($this->moduleTemplate === NULL) {
$moduleTemplateFactory = GeneralUtility::makeInstance(
\TYPO3\CMS\Backend\Template\ModuleTemplateFactory::class
);
$this->moduleTemplate = $moduleTemplateFactory->create($this->request);
}
}
/**
* Use the ModuleTemplateResponse to create a response object for the backend
*
* @return \Psr\Http\Message\ResponseInterface
*/
protected function createBackendResponse(): \Psr\Http\Message\ResponseInterface {
return $this->htmlResponse($this->view->render());
}
}