Commit 78022042 authored by Damjan's avatar Damjan
Browse files

[TASK] Addition of editConstant and deleteConstant views

Refactoring of MainController:
- making addFlashMessage function with LFException parameter
- adding function which executes writing of language files. The function is called from save actions

Related: forge.typo3.org/issues/31889
parent 305a1cdd
......@@ -25,16 +25,21 @@ namespace SGalinski\Lfeditor\Controller;
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use Exception;
use SGalinski\Lfeditor\Exceptions\LFException;
use SGalinski\Lfeditor\Service\BackupService;
use SGalinski\Lfeditor\Utility\Functions;
use SGalinski\Lfeditor\Utility\SgLib;
use TYPO3\CMS\Core\Messaging\AbstractMessage;
use TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
/**
* Main controller. It contains all extbase actions of Lfeditor extension.
*/
class MainController extends AbstractController {
// TODO: Apply coding guidelines
/**
* @inject
* @var \SGalinski\Lfeditor\Service\ConfigurationService
......@@ -57,7 +62,6 @@ class MainController extends AbstractController {
* Initializes the system.
*
* @return void
* @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException //TODO: Handle every exception in controllers and display flash message.
*/
// public function initLfeditorAction() {
//// //TODO: I might not need this. I am preparing config in every request before every action...
......@@ -84,7 +88,6 @@ class MainController extends AbstractController {
$extensionOptions = $this->configurationService->menuExtList();
$this->assignViewWidthMenuVariables('extension', $extensionOptions);
//TODO: In same way (like extension menu) handle all other select menus that exist in the view
$languageFileOptions = $this->configurationService->menuLangFileList(
$this->session->getDataByKey('extensionSelection')
);
......@@ -105,23 +108,7 @@ class MainController extends AbstractController {
);
} catch (LFException $e) {
if ($e->getCode() == 0) {
$this->addFlashMessage(
$e->getMessage(),
$messageTitle = LocalizationUtility::translate('failure.failure', 'lfeditor'),
$severity = AbstractMessage::ERROR,
$storeInSession = TRUE
);
} else {
if ($e->getCode() == 1) {
$this->addFlashMessage(
$e->getMessage(),
$messageTitle = 'Notification', //TODO: Need to be added in lang file.
$severity = AbstractMessage::NOTICE,
$storeInSession = TRUE
);
}
}
$this->addFlashMessage($e);
}
debug($this->session->getData(), 'ses');//TODO: Delete debug.
}
......@@ -134,7 +121,7 @@ class MainController extends AbstractController {
* @param string $extensionSelection
* @param string $languageFileSelection
* @param string $referenceLanguageSelection
* @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException
* @throws UnsupportedRequestTypeException
*
* @return void
*/
......@@ -154,7 +141,7 @@ class MainController extends AbstractController {
* @param string $authorEmail
* @param string $metaDescription
* @param string $transformFile
* @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException
* @throws UnsupportedRequestTypeException
* @return void
*/
public function generalSaveAction(
......@@ -162,54 +149,25 @@ class MainController extends AbstractController {
$metaDescription = NULL, $transformFile = NULL
) {
try {
$fileObject = $this->configurationService->getFileObj();
if ($fileObject == NULL) {
$this->configurationService->initFileObject(
$this->session->getDataByKey('languageFileSelection'),
$this->session->getDataByKey('extensionSelection')
);
$fileObject = $this->configurationService->getFileObj();
}
if ($this->configurationService->getExtConfig()['execBackup']) {
/** @var BackupService $backupService */
$backupService = $this->objectManager->get('SGalinski\Lfeditor\Service\BackupService');
// $backupService->execBackup(); //TODO: make backup work.
}
$fileObject->setMetaData('type', $metaTypeSelection);
$fileObject->setMetaData('csh_table', $metaCSHTable);
$fileObject->setMetaData('authorName', $authorName);
$fileObject->setMetaData('authorEmail', $authorEmail);
$fileObject->setMetaData('description', $metaDescription);
$fileObject->writeFile();
//TODO: Make file transformation here or in seperate form/action if possible.
$this->configurationService->initFileObject(
$this->session->getDataByKey('languageFileSelection'),
$this->session->getDataByKey('extensionSelection')
$metaArray = [
'type' => $metaTypeSelection,
'csh_table' => $metaCSHTable,
'authorName' => $authorName,
'authorEmail' => $authorEmail,
'description' => $metaDescription
];
$this->execWrite([], $metaArray);
//TODO: Make file transformation here, or in separate form/action if possible.
//TODO: Make file merge/split here, or in separate form/action if possible.
parent::addFlashMessage(
LocalizationUtility::translate('lang.file.write.success', 'lfeditor'),
'',
$severity = AbstractMessage::OK,
$storeInSession = TRUE
);
} catch (LFException $e) {
if ($e->getCode() == 0) {
$this->addFlashMessage(
$e->getMessage(),
$messageTitle = LocalizationUtility::translate('failure.failure', 'lfeditor'),
$severity = AbstractMessage::ERROR,
$storeInSession = TRUE
);
} else {
if ($e->getCode() == 1) {
$this->addFlashMessage(
$e->getMessage(),
$messageTitle = 'Notification', //TODO: Need to be added in lang file.
$severity = AbstractMessage::NOTICE,
$storeInSession = TRUE
);
}
}
$this->addFlashMessage($e);
}
$this->redirect('general');
}
......@@ -237,23 +195,6 @@ class MainController extends AbstractController {
$this->view->assign('authorEmail', $infoArray['default']['meta']['authorEmail']);
$this->view->assign('numTextAreaRows', $numTextAreaRows);
$this->view->assign('metaDescription', $description);
// // get output
// $email = '';
// if (is_array($mailIt) && !$sendMail) {
// // add mailIt pre selection
// foreach ($infoArray as $langKey => $info) {
// $infoArray[$langKey]['email'] = (isset($mailIt[$langKey]) ? TRUE : FALSE);
// }
//
// $email = tx_lfeditor_mod1_template::outputGeneralEmail($infoArray['default']['meta'], $numTextAreaRows);
// }
// $fileType = $this->fileObj->getVar('fileType');
// $content = $email . tx_lfeditor_mod1_template::outputGeneral(
// $infoArray, $referenceLanguageSelection,
// $numTextAreaRows, ($this->fileObj->getVar('workspace') !== 'base' || $fileType === 'xlf' ? FALSE : TRUE)
// );
}
/***************************************
......@@ -280,8 +221,8 @@ class MainController extends AbstractController {
*
* @param string $extensionSelection
* @param string $languageFileSelection
* @throws UnsupportedRequestTypeException
* @return void
* @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException
*/
public function editFileChangeSelectionAction($extensionSelection = NULL, $languageFileSelection = NULL) {
$this->saveSelectionsInSession($extensionSelection, $languageFileSelection);
......@@ -291,14 +232,250 @@ class MainController extends AbstractController {
/**
* Saves the changes made in main section of editFile view.
*
* @throws UnsupportedRequestTypeException
* @return void
* @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException
*/
public function editFileSaveAction() {
//TODO: Save the changes.
$this->redirect('editFile');
}
/*******************************************
* 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('mainMenuSelection', 'editConstant');
/** @var array $extensionOptions */
$extensionOptions = $this->configurationService->menuExtList();
$this->assignViewWidthMenuVariables('extension', $extensionOptions);
/** @var array $languageFileOptions */
$languageFileOptions = $this->configurationService->menuLangFileList(
$this->session->getDataByKey('extensionSelection')
);
$this->assignViewWidthMenuVariables('languageFile', $languageFileOptions);
$this->configurationService->initFileObject(
$this->session->getDataByKey('languageFileSelection'),
$this->session->getDataByKey('extensionSelection')
);
/** @var array $langData */
$langData = $this->configurationService->getFileObj()->getLocalLangData();
/** @var array $constantOptions */
$constantOptions = $this->configurationService->menuConstList(
$langData, LocalizationUtility::translate('select.nothing', 'lfeditor')
);
$this->assignViewWidthMenuVariables('constant', $constantOptions);
$this->prepareEditConstantViewMainSectionContent($langData);
} catch (LFException $e) {
$this->addFlashMessage($e);
}
debug($this->session->getData(), 'ses');//TODO: Delete debug.
}
/**
* 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 editConstantChangeSelectionAction(
$extensionSelection = NULL, $languageFileSelection = NULL, $constantSelection = NULL
) {
$this->saveSelectionsInSession($extensionSelection, $languageFileSelection, NULL, $constantSelection);
$this->redirect('editConstant');
}
//TODO: Add success messages after all successful operations.
//TODO: Add keyword array in front of all array parameters.
/**
* Saves the changes made in main section of editConstant view.
*
* @param array $editConstTextArea
* @throws UnsupportedRequestTypeException
* @return void
*/
public function editConstantSaveAction(array $editConstTextArea) {
try {
$this->execWrite($editConstTextArea);
parent::addFlashMessage(
LocalizationUtility::translate('lang.file.write.success', 'lfeditor'),
'',
$severity = AbstractMessage::OK,
$storeInSession = TRUE
);
} catch (LFException $e) {
$this->addFlashMessage($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];
}
}
$this->view->assign('constantSelection', $constantSelection);
$this->view->assign('languages', $languages);
$this->view->assign('numTextAreaRows', $numTextAreaRows);
}
/*********************************************
* Actions called from deleteConstant view *
*********************************************/
/**
* 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('mainMenuSelection', 'deleteConstant');
/** @var array $extensionOptions */
$extensionOptions = $this->configurationService->menuExtList();
$this->assignViewWidthMenuVariables('extension', $extensionOptions);
/** @var array $languageFileOptions */
$languageFileOptions = $this->configurationService->menuLangFileList(
$this->session->getDataByKey('extensionSelection')
);
$this->assignViewWidthMenuVariables('languageFile', $languageFileOptions);
$this->configurationService->initFileObject(
$this->session->getDataByKey('languageFileSelection'),
$this->session->getDataByKey('extensionSelection')
);
/** @var array $langData */
$langData = $this->configurationService->getFileObj()->getLocalLangData();
/** @var array $constantOptions */
$constantOptions = $this->configurationService->menuConstList(
$langData, LocalizationUtility::translate('select.nothing', 'lfeditor')
);
$this->assignViewWidthMenuVariables('constant', $constantOptions);
$this->prepareDeleteConstantViewMainSectionContent();
} catch (LFException $e) {
$this->addFlashMessage($e);
}
debug($this->session->getData(), 'ses');//TODO: Delete debug.
}
/**
* 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 deleteConstantChangeSelectionAction(
$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->execWrite($newLang, [], TRUE);
parent::addFlashMessage(
LocalizationUtility::translate('lang.file.write.success', 'lfeditor'),
'',
$severity = AbstractMessage::OK,
$storeInSession = TRUE
);
} catch (LFException $e) {
$this->addFlashMessage($e);
}
$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);
}
$this->view->assign('constantSelection', $constantSelection);
}
/**************************
* Additional functions *
**************************/
......@@ -309,10 +486,12 @@ class MainController extends AbstractController {
* @param string $extensionSelection
* @param string $languageFileSelection
* @param string $referenceLanguageSelection
* @param string $constantSelection
* @return void
*/
protected function saveSelectionsInSession(
$extensionSelection = NULL, $languageFileSelection = NULL, $referenceLanguageSelection = NULL
$extensionSelection = NULL, $languageFileSelection = NULL, $referenceLanguageSelection = NULL,
$constantSelection = NULL
) {
if ($extensionSelection === 'NULL') {
$extensionSelection = NULL;
......@@ -328,6 +507,11 @@ class MainController extends AbstractController {
$referenceLanguageSelection = NULL;
}
$this->session->setDataByKey('referenceLanguageSelection', $referenceLanguageSelection);
if ($constantSelection === 'NULL') {
$constantSelection = NULL;
}
$this->session->setDataByKey('constantSelection', $constantSelection);
}
/**
......@@ -353,6 +537,109 @@ class MainController extends AbstractController {
$this->view->assign($menuName . 'Selection', $selection);
}
/**
* Executes writing of language files
*
* @throws LFException raised if file could not be written or some param criterias are not correct
* @throws Exception|LFException
* @param array $modArray changes (constants with empty values will be deleted)
* @param array $modMetaArray meta changes (indexes with empty values will be deleted)
* @param boolean $forceDel set to true if you want delete default constants
* @return void
*/
protected function execWrite($modArray, $modMetaArray = [], $forceDel = FALSE) {
// checks
if (!is_array($modArray)) {
throw new LFException('failure.file.notWritten');
}
// execute backup
if ($this->configurationService->getExtConfig()['execBackup']) {
/** @var BackupService $backupService */
$backupService = $this->objectManager->get('SGalinski\Lfeditor\Service\BackupService');
// $backupService->execBackup(); //TODO: make backup work.
}
$fileObject = $this->configurationService->getFileObj();
if ($fileObject == NULL) {
$this->configurationService->initFileObject(
$this->session->getDataByKey('languageFileSelection'),
$this->session->getDataByKey('extensionSelection')
);
$fileObject = $this->configurationService->getFileObj();
}
// set new language data
foreach ($modArray as $langKey => $data) {
if (is_array($data)) {
foreach ($data as $const => $value) {
$fileObject->setLocalLangData($const, $value, $langKey, $forceDel);
}
}
}
// set changed meta data
foreach ($modMetaArray as $metaIndex => $metaValue) {
$fileObject->setMetaData($metaIndex, $metaValue);
}
// write new language data
$fileObject->writeFile();
// delete possible language files
$absFile = $fileObject->getVar('absFile');
$originLang = $fileObject->getOriginLangData();
$emptyFiles = [];
foreach ($originLang as $lang => $origin) {
if ($origin == $absFile || !is_file($origin)) {
continue;
}
$langData = $fileObject->getLocalLangData($lang);
if (is_array($langData) && !count($langData)) {
$emptyFiles[] = $origin;
}
}
// delete all empty language files
try {
if (count($emptyFiles)) {
SgLib::deleteFiles($emptyFiles);
}
} catch (Exception $e) {
throw new LFException('failure.langfile.notDeleted', 0, '(' . $e->getMessage() . ')');
}
// reinitialize fileobject
$this->configurationService->initFileObject(
$this->session->getDataByKey('languageFileSelection'),
$this->session->getDataByKey('extensionSelection')
);
}
/**
* Sets FlashMessage from LFException.
*
* @param LFException $lFException
* @return void
*/
public function addFlashMessage(LFException $lFException) {
if ($lFException->getCode() == 0) {
parent::addFlashMessage(
$lFException->getMessage(),
$messageTitle = LocalizationUtility::translate('failure.failure', 'lfeditor'),
$severity = AbstractMessage::ERROR,
$storeInSession = TRUE
);
} elseif ($lFException->getCode() == 1) {
parent::addFlashMessage(
$lFException->getMessage(),
$messageTitle = '',
$severity = AbstractMessage::NOTICE,
$storeInSession = TRUE
);
}
}
}
?>
\ No newline at end of file
......@@ -253,7 +253,7 @@ class ConfigurationService extends AbstractService {
}
/**
* adds items to the MOD_MENU array. Used for the language menu selector
* Used for the language menu selector
*
* @param array $langData language data
* @param string $default optional default value (if you dont want a default let it empty)
......@@ -284,6 +284,35 @@ class ConfigurationService extends AbstractService {
return $langList;
}
/**
* Used for the editConstant-List
*
* @param array $langData language data
* @param string $default name of default entry
* @return array
*/
public function menuConstList($langData, $default) {
// generate constant list
$constList = [];
$languages = Functions::buildLangArray();
foreach ($languages as $language) {
if (!is_array($langData[$language]) || !count($langData[$language])) {
continue;
}
/** @var array $constants */
$constants = array_keys($langData[$language]);
foreach ($constants as $constant) {
$constList[str_replace('#', '$*-*$', $constant)] = $constant;
}
}
// sorting and default entry
asort($constList);
$constList = array_merge(['###default###' => $default], $constList);
return $constList;
}
/**
* creates and instantiates a file object
*
......
......@@ -26,6 +26,12 @@
<f:be.menus.actionMenuItem label="{f:translate(key:'function.langfile.edit')}"
controller="Main"