Commit cacc2226 authored by Damjan's avatar Damjan

[TASK] Refactoring of controller - separating it on smaller controllers.

Related: forge.typo3.org/issues/31889
parent af29fcb6
......@@ -26,8 +26,11 @@ namespace SGalinski\Lfeditor\Controller;
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use SGalinski\Lfeditor\Exceptions\LFException;
use SGalinski\Lfeditor\Session\PhpSession;
use TYPO3\CMS\Core\Messaging\AbstractMessage;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
/**
* Abstract Controller
......@@ -38,9 +41,22 @@ abstract class AbstractController extends ActionController {
*/
protected $session;
/**
* @inject
* @var \SGalinski\Lfeditor\Service\ConfigurationService
*/
protected $configurationService;
/**
* @inject
* @var \SGalinski\Lfeditor\Service\BackupService
*/
protected $backupService;
/**
* Initializes the actions.
* - Initializes the session object.
* - Fetches configuration.
*
* @return void
*/
......@@ -50,6 +66,158 @@ abstract class AbstractController extends ActionController {
$this->session = $this->objectManager->get('SGalinski\Lfeditor\Session\PhpSession');
$this->session->setSessionKey('tx_lfeditor_sessionVariables');
}
$this->configurationService->prepareConfig();
}
/**
* Saves in session currently selected values of select tags.
*
* @param string $extensionSelection
* @param string $languageFileSelection
* @param string $referenceLanguageSelection
* @param string $constantSelection
* @param string $languageSelection
* @param string $constantTypeSelection
* @return void
*/
protected function saveSelectionsInSession(
$extensionSelection = NULL, $languageFileSelection = NULL, $referenceLanguageSelection = NULL,
$constantSelection = NULL, $languageSelection = NULL, $constantTypeSelection = NULL
) {
/* Extension/language file select box can't be unselected.
Only situation when $extensionSelection === NULL is when the form is submitted by
selection change of some other box. That is because <f:be.menus.actionMenu> with 'optgroup' tags is used */
if ($extensionSelection) {
$this->session->setDataByKey('extensionSelection', $extensionSelection);
}
if ($languageFileSelection) {
$this->session->setDataByKey('languageFileSelection', $languageFileSelection);
}
if ($referenceLanguageSelection) {
$this->session->setDataByKey('referenceLanguageSelection', $referenceLanguageSelection);
}
if ($constantSelection) {
$this->session->setDataByKey('constantSelection', $constantSelection);
}
if ($languageSelection) {
$this->session->setDataByKey('languageSelection', $languageSelection);
}
if ($constantTypeSelection) {
$this->session->setDataByKey('constantTypeSelection', $constantTypeSelection);
}
}
/**
* Assigns view width menu options and default menu selection which is fetched from session.
*
* @param string $menuName Name of the menu, which will be used as prefix of view keys for menu options and menu selection.
* Example: menuName 'extension' will produce view keys 'extensionOptions' and 'extensionSelection'
* @param array $options menu options to be assigned to view
* @return void
*/
protected function assignViewWidthMenuVariables($menuName, $options) {
$this->view->assign($menuName . 'Options', $options);
$selection = $this->checkMenuSelection($menuName, $options);
$this->view->assign($menuName . 'Selection', $selection);
}
/**
* Checks does selection exists in session or among menu options and if it does not,
* first option becomes selected.
*
* @param string $menuName Name of the menu, which will be used as prefix of view keys for menu options and menu selection.
* Example: menuName 'extension' will produce view keys 'extensionOptions' and 'extensionSelection'
* @param array $options menu options to be checked upon.
* @return string
*/
protected function checkMenuSelection($menuName, array $options) {
$selection = $this->session->getDataByKey($menuName . 'Selection');
if (!array_key_exists($selection, $options)) {
$selection = NULL;
}
if ($selection === NULL && !empty($options)) {
reset($options);
$selection = key($options);
$this->session->setDataByKey($menuName . 'Selection', $selection);
return $selection;
}
return $selection;
}
/**
* Sets FlashMessage from LFException.
*
* @param LFException $lFException
* @return void
*/
public function addLFEFlashMessage(LFException $lFException) {
if ($lFException->getCode() == 0) {
$this->addFlashMessage(
$lFException->getMessage(),
$messageTitle = LocalizationUtility::translate('failure.failure', 'lfeditor'),
$severity = AbstractMessage::ERROR,
$storeInSession = TRUE
);
} elseif ($lFException->getCode() == 1) {
$this->addFlashMessage(
$lFException->getMessage(),
$messageTitle = '',
$severity = AbstractMessage::NOTICE,
$storeInSession = TRUE
);
}
}
/**
* Prepares language file select options for each extension and sets combined data in view.
*
* @param array $extensionOptions
* @return void
*/
protected function prepareExtensionAndLangFileOptions(array $extensionOptions) {
$extensions = [];
foreach ($extensionOptions as $extAddress => $extLabel) {
$extension['extLabel'] = $extLabel;
try {
$extension['languageFileOptions'] = $this->configurationService->menuLangFileList($extAddress);
} catch (LFException $e) {
$extension['languageFileOptions'] = [];
}
$extensions[$extAddress] = $extension;
}
$this->checkExtensionAndLangFileSelection($extensions);
$this->view->assign('extensions', $extensions);
$this->view->assign('extensionSelection', $this->session->getDataByKey('extensionSelection'));
$this->view->assign('languageFileSelection', $this->session->getDataByKey('languageFileSelection'));
}
/**
* Checks do extensionSelection and languageFileSelection exist in session or among menu options and if it does not,
* first language file and belonging extension become selected and saved to session.
*
* @param array $extensions
* @return void
*/
protected function checkExtensionAndLangFileSelection(array $extensions) {
$extensionSelection = $this->session->getDataByKey('extensionSelection');
$languageFileSelection = $this->session->getDataByKey('languageFileSelection');
$selectFirstLanguageFileAndBelongingExtension = !$extensionSelection || !$languageFileSelection ||
!$extensions[$extensionSelection]['languageFileOptions'][$languageFileSelection];
if ($selectFirstLanguageFileAndBelongingExtension) {
foreach ($extensions as $extAddress => $extension) {
if (empty($extension['languageFileOptions'])) {
continue;
}
reset($extension['languageFileOptions']);
$languageFileSelection = key($extension['languageFileOptions']);
$this->session->setDataByKey('languageFileSelection', $languageFileSelection);
$this->session->setDataByKey('extensionSelection', $extAddress);
break;
}
}
}
}
......
<?php
namespace SGalinski\Lfeditor\Controller;
/***************************************************************
* Copyright notice
*
* (c) sgalinski Internet Services (http://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 SGalinski\Lfeditor\Exceptions\LFException;
use TYPO3\CMS\Core\Messaging\AbstractMessage;
use TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
/**
* AddConstant controller. It contains extbase actions for AddConstant page.
*/
class AddConstantController extends AbstractController {
/**
* Opens addConstant view.
* It is called in 2 cases:
* - on selection of addConstant option in main menu,
* - after redirection from action which must not change the view.
*
* @return void
*/
public function addConstantAction() {
try {
$this->view->assign('controllerName', 'AddConstant');
$extensionOptions = $this->configurationService->menuExtList();
$this->prepareExtensionAndLangFileOptions($extensionOptions);
$this->prepareAddConstantViewMainSectionContent();
} catch (LFException $e) {
$this->addLFEFlashMessage($e);
}
}
/**
* This action saves in session currently selected options from selection menus in addConstant view.
* It is called on change of selection of any select menu in addConstant view.
*
* @param string $extensionSelection
* @param string $languageFileSelection
* @throws UnsupportedRequestTypeException
* @return void
*/
public function changeSelectionAction($extensionSelection = NULL, $languageFileSelection = NULL) {
$this->saveSelectionsInSession($extensionSelection, $languageFileSelection, NULL, NULL);
$this->redirect('addConstant');
}
/**
* Saves the changes made in main section of addConstant view.
*
* @param string $nameOfConstant
* @param array $addConstTextArea
* @throws UnsupportedRequestTypeException
* @return void
*/
public function addConstantSaveAction($nameOfConstant, array $addConstTextArea) {
try {
if (empty($nameOfConstant)) {
throw new LFException('failure.select.noConstDefined');
}
$this->configurationService->initFileObject(
$this->session->getDataByKey('languageFileSelection'),
$this->session->getDataByKey('extensionSelection')
);
$langData = $this->configurationService->getFileObj()->getLocalLangData();
if (!empty($langData['default'][$nameOfConstant])) {
throw new LFException('failure.langfile.constExists');
}
$newConstLanguages = [];
foreach ($addConstTextArea as $lang => $value) {
$newConstLanguages[$lang][$nameOfConstant] = $value;
}
$this->configurationService->execWrite($newConstLanguages);
$this->addFlashMessage(
LocalizationUtility::translate('lang.file.write.success', 'lfeditor'),
'',
$severity = AbstractMessage::OK,
$storeInSession = TRUE
);
} catch (LFException $e) {
$this->addLFEFlashMessage($e);
}
$this->redirect('addConstant');
}
/**
* Prepares main section content of addConstant view.
*
* @throws LFException
* @return void
*/
protected function prepareAddConstantViewMainSectionContent() {
$numTextAreaRows = $this->configurationService->getExtConfig()['numTextAreaRows'];
$langArray = $this->configurationService->getLangArray();
$this->view->assign('languages', $langArray);
$this->view->assign('numTextAreaRows', $numTextAreaRows);
}
}
?>
\ No newline at end of file
<?php
namespace SGalinski\Lfeditor\Controller;
/***************************************************************
* Copyright notice
*
* (c) sgalinski Internet Services (http://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 SGalinski\Lfeditor\Exceptions\LFException;
use SGalinski\Lfeditor\Utility\Functions;
use TYPO3\CMS\Core\Messaging\AbstractMessage;
use TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
/**
* DeleteConstant controller. It contains extbase actions for DeleteConstant page.
*/
class DeleteConstantController extends AbstractController {
/**
* Opens deleteConstant view.
* It is called in 2 cases:
* - on selection of deleteConstant option in main menu,
* - after redirection from action which must not change the view.
*
* @return void
*/
public function deleteConstantAction() {
try {
$this->view->assign('controllerName', 'DeleteConstant');
$extensionOptions = $this->configurationService->menuExtList();
$this->prepareExtensionAndLangFileOptions($extensionOptions);
$this->configurationService->initFileObject(
$this->session->getDataByKey('languageFileSelection'),
$this->session->getDataByKey('extensionSelection')
);
$langData = $this->configurationService->getFileObj()->getLocalLangData();
$constantOptions = $this->configurationService->menuConstList(
$langData, LocalizationUtility::translate('select.nothing', 'lfeditor')
);
$this->assignViewWidthMenuVariables('constant', $constantOptions);
$this->prepareDeleteConstantViewMainSectionContent();
} catch (LFException $e) {
$this->addLFEFlashMessage($e);
}
}
/**
* This action saves in session currently selected options from selection menus in deleteConstant view.
* It is called on change of selection of any select menu in deleteConstant view.
*
* @param string $extensionSelection
* @param string $languageFileSelection
* @param string $constantSelection
* @throws UnsupportedRequestTypeException
* @return void
*/
public function changeSelectionAction(
$extensionSelection = NULL, $languageFileSelection = NULL, $constantSelection = NULL
) {
$this->saveSelectionsInSession($extensionSelection, $languageFileSelection, NULL, $constantSelection);
$this->redirect('deleteConstant');
}
/**
* Saves the changes made in main section of deleteConstant view.
*
* @param boolean $delAllLang
* @throws UnsupportedRequestTypeException
* @return void
*/
public function deleteConstantSaveAction($delAllLang) {
try {
$constantSelection = $this->session->getDataByKey('constantSelection');
// get languages
if ($delAllLang) {
$languages = Functions::buildLangArray();
$langArray = array_merge(['default'], $languages);
} else {
$langArray =
Functions::buildLangArray($this->configurationService->getExtConfig()['viewLanguages']);
}
// build modArray
$newLang = [];
foreach ($langArray as $lang) {
$newLang[$lang][$constantSelection] = '';
}
$this->configurationService->execWrite($newLang, [], TRUE);
$this->addFlashMessage(
LocalizationUtility::translate('lang.file.write.success', 'lfeditor'),
'',
$severity = AbstractMessage::OK,
$storeInSession = TRUE
);
} catch (LFException $e) {
$this->addLFEFlashMessage($e);
}
$this->session->setDataByKey('deleteAllLangChecked', $delAllLang);
$this->redirect('deleteConstant');
}
/**
* Prepares main section content of deleteConstant view.
*
* @throws LFException
* @return void
*/
protected function prepareDeleteConstantViewMainSectionContent() {
$constantSelection = $this->session->getDataByKey('constantSelection');
if (empty($constantSelection) || $constantSelection == '###default###') {
throw new LFException('failure.select.noConst', 1);
}
$deleteAllLangChecked = $this->session->getDataByKey('deleteAllLangChecked');
$this->view->assign('constantSelection', $constantSelection);
$this->view->assign('deleteAllLangChecked', $deleteAllLangChecked);
}
}
?>
\ No newline at end of file
<?php
namespace SGalinski\Lfeditor\Controller;
/***************************************************************
* Copyright notice
*
* (c) sgalinski Internet Services (http://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 SGalinski\Lfeditor\Exceptions\LFException;
use SGalinski\Lfeditor\Utility\Functions;
use SGalinski\Lfeditor\Utility\SgLib;
use SGalinski\Lfeditor\Utility\Typo3Lib;
use TYPO3\CMS\Core\Messaging\AbstractMessage;
use TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
/**
* EditConstant controller. It contains extbase actions for EditConstant page.
*/
class EditConstantController extends AbstractController {
/*******************************************
* Actions called from editConstant view *
*******************************************/
/**
* Opens editConstant view.
* It is called in 2 cases:
* - on selection of editConstant option in main menu,
* - after redirection from action which must not change the view.
*
* @return void
*/
public function editConstantAction() {
try {
$this->view->assign('controllerName', 'EditConstant');
$extensionOptions = $this->configurationService->menuExtList();
$this->prepareExtensionAndLangFileOptions($extensionOptions);
$this->configurationService->initFileObject(
$this->session->getDataByKey('languageFileSelection'),
$this->session->getDataByKey('extensionSelection')
);
$langData = $this->configurationService->getFileObj()->getLocalLangData();
$constantOptions = $this->configurationService->menuConstList(
$langData, LocalizationUtility::translate('select.nothing', 'lfeditor')
);
$this->assignViewWidthMenuVariables('constant', $constantOptions);
$this->prepareEditConstantViewMainSectionContent($langData);
} catch (LFException $e) {
$this->addLFEFlashMessage($e);
}
}
/**
* This action saves in session currently selected options from selection menus in editConstant view.
* It is called on change of selection of any select menu in editConstant view.
*
* @param string $extensionSelection
* @param string $languageFileSelection
* @param string $constantSelection
* @throws UnsupportedRequestTypeException
* @return void
*/
public function changeSelectionAction(
$extensionSelection = NULL, $languageFileSelection = NULL, $constantSelection = NULL
) {
$this->saveSelectionsInSession($extensionSelection, $languageFileSelection, NULL, $constantSelection);
$this->redirect('editConstant');
}
/**
* Saves the changes made in main section of editConstant view.
*
* @param array $editConstTextArea
* @throws UnsupportedRequestTypeException
* @return void
*/
public function editConstantSaveAction(array $editConstTextArea) {
try {
$this->configurationService->execWrite($editConstTextArea);
$this->addFlashMessage(
LocalizationUtility::translate('lang.file.write.success', 'lfeditor'),
'',
$severity = AbstractMessage::OK,
$storeInSession = TRUE
);
} catch (LFException $e) {
$this->addLFEFlashMessage($e);
}
$this->redirect('editConstant');
}
/**
* Prepares main section content of editConstant view.
*
* @param array $langData
* @throws LFException
* @return void
*/
protected function prepareEditConstantViewMainSectionContent(array $langData = NULL) {
$numTextAreaRows = $this->configurationService->getExtConfig()['numTextAreaRows'];
$langArray = $this->configurationService->getLangArray();
$constantSelection = $this->session->getDataByKey('constantSelection');
if (empty($constantSelection) || $constantSelection == '###default###') {
throw new LFException('failure.select.noConst', 1);
}
$languages = [];
foreach ($langArray as $lang) {
if ($langData[$lang]) {
$languages[$lang] = $langData[$lang][$constantSelection];
} else {
$languages[$lang] = '';
}
}
$languages = preg_replace('/<br.*>/U', "\n", $languages);
$this->view->assign('constantSelection', $constantSelection);
$this->view->assign('languages', $languages);
$this->view->assign('numTextAreaRows', $numTextAreaRows);
}
/**
* Sets chosen constant and redirects to editConstant view.
*
* @param string $constantKey
* @return void
*/
protected function prepareEditConstantAction($constantKey) {
$this->session->setDataByKey('constantSelection', $constantKey);
$this->redirect('editConstant', 'EditConstant');
}
}
?>
\ No newline at end of file
<?php
namespace SGalinski\Lfeditor\Controller;
/***************************************************************
* Copyright notice
*
* (c) sgalinski Internet Services (http://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 SGalinski\Lfeditor\Exceptions\LFException;
use TYPO3\CMS\Core\Messaging\AbstractMessage;
use TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
/**
* EditFile controller. It contains extbase actions of EditFile page.
*/
class EditFileController extends AbstractController {
/**
* Opens editFile view.