Commit 5153c68f authored by Damjan's avatar Damjan

[FEATURE] File transformation

- File transformation functionality.
- Refactor: moving execWrite to ConfigurationService.
- Removal of commented code.

Related: forge.typo3.org/issues/31889
parent 8a482e75
......@@ -72,7 +72,6 @@ class MainController extends AbstractController {
*/
public function generalAction() {
$this->view->assign('mainMenuSelection', 'general');
// $this->session->destroy();//todo:delete. debug
try {
$extensionOptions = $this->configurationService->menuExtList();
$this->assignViewWidthMenuVariables('extension', $extensionOptions);
......@@ -139,10 +138,25 @@ class MainController extends AbstractController {
'authorEmail' => $authorEmail,
'description' => $metaDescription
];
$this->execWrite([], $metaArray);
//TODO: Make file transformation here, or in separate form/action if possible.
$this->configurationService->execWrite([], $metaArray);
//TODO: Make file merge/split here, or in separate form/action if possible.
if (!empty($transformFile)
&& $this->configurationService->getFileObj()->getVar('fileType') != $transformFile
) {
$newFile = SgLib::setFileExtension(
$transformFile, $this->configurationService->getFileObj()->getVar('relFile')
);
$this->configurationService->execTransform($transformFile, $newFile);
// if ($this->MOD_SETTINGS['wsList'] != 'xll') {
// header(
// 'Location: ' . t3lib_div::getIndpEnv('TYPO3_REQUEST_SCRIPT') .
// '?M=user_txlfeditorM1&SET[langFileList]=' . $newFile
// );
// }
}
parent::addFlashMessage(
LocalizationUtility::translate('lang.file.write.success', 'lfeditor'),
'',
......@@ -438,7 +452,7 @@ class MainController extends AbstractController {
// write if no session continued
if (!$langDataSessionContinued) {
$this->execWrite($_SESSION[$sessID]['langfileEditNewLangData']);
$this->configurationService->execWrite($_SESSION[$sessID]['langfileEditNewLangData']);
parent::addFlashMessage(
LocalizationUtility::translate('lang.file.write.success', 'lfeditor'),
'',
......@@ -523,7 +537,7 @@ class MainController extends AbstractController {
*/
public function editConstantSaveAction(array $editConstTextArea) {
try {
$this->execWrite($editConstTextArea);
$this->configurationService->execWrite($editConstTextArea);
parent::addFlashMessage(
LocalizationUtility::translate('lang.file.write.success', 'lfeditor'),
......@@ -643,7 +657,7 @@ class MainController extends AbstractController {
$newConstLanguages[$lang][$nameOfConstant] = $value;
}
$this->execWrite($newConstLanguages);
$this->configurationService->execWrite($newConstLanguages);
parent::addFlashMessage(
LocalizationUtility::translate('lang.file.write.success', 'lfeditor'),
......@@ -703,7 +717,6 @@ class MainController extends AbstractController {
$this->session->getDataByKey('extensionSelection')
);
// TODO: I can initFileObject within getFileObj method, if fileObj === NULL
$langData = $this->configurationService->getFileObj()->getLocalLangData();
$constantOptions = $this->configurationService->menuConstList(
$langData, LocalizationUtility::translate('select.nothing', 'lfeditor')
......@@ -760,7 +773,7 @@ class MainController extends AbstractController {
$newLang[$lang][$constantSelection] = '';
}
$this->execWrite($newLang, [], TRUE);
$this->configurationService->execWrite($newLang, [], TRUE);
parent::addFlashMessage(
LocalizationUtility::translate('lang.file.write.success', 'lfeditor'),
......@@ -882,7 +895,7 @@ class MainController extends AbstractController {
$newLang[$lang][$oldConstantName] = '';
}
$this->execWrite($newLang, [], TRUE);
$this->configurationService->execWrite($newLang, [], TRUE);
parent::addFlashMessage(
LocalizationUtility::translate('lang.file.write.success', 'lfeditor'),
......@@ -1195,7 +1208,7 @@ class MainController extends AbstractController {
if ($currentFluidTreeElem['isBottom']) {
$iconName = 'Bottom' . $iconName;
}
if ($hasChildren) { // TODO: rename key in structure to 'children'
if ($hasChildren) {
$iconName = 'tree' . ($treeHide && $level != 0 ? 'Plus' : 'Minus') . $iconName;
} else {
$iconName = 'join' . $iconName;
......@@ -1292,86 +1305,6 @@ 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 */ // TODO: Dont use type declaration if it is not needed.
$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.
*
......
......@@ -38,7 +38,6 @@ use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
class ConfigurationService extends AbstractService {
//TODO: rename to ConfigurationService.
/**
* @var array extension configuration
* @see prepareConfig()
......@@ -60,6 +59,11 @@ class ConfigurationService extends AbstractService {
*/
protected $fileObj;
/**
* @var \SGalinski\Lfeditor\Service\FileBasePHPService
*/
protected $convObj;
/**
* preparation and check of the configuration
*
......@@ -381,6 +385,141 @@ class ConfigurationService extends AbstractService {
}
}
/**
* 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
*/
public function execWrite($modArray, $modMetaArray = [], $forceDel = FALSE) {
// checks
if (!is_array($modArray)) {
throw new LFException('failure.file.notWritten');
}
// execute backup
if ($this->getExtConfig()['execBackup']) {
/** @var BackupService $backupService */ // TODO: Dont use type declaration if it is not needed.
$backupService = $this->objectManager->get('SGalinski\Lfeditor\Service\BackupService');
// $backupService->execBackup(); //TODO: make backup work.
}
$fileObject = $this->getFileObj();
if ($fileObject == NULL) {
$this->initFileObject(
$this->session->getDataByKey('languageFileSelection'),
$this->session->getDataByKey('extensionSelection')
);
$fileObject = $this->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->initFileObject(
$this->session->getDataByKey('languageFileSelection'),
$this->session->getDataByKey('extensionSelection')
);
}
/**
* converts language files between different formats
*
* @throws LFException raised if transforming or deletion of old files failed
* @throws Exception|LFException
* @param string $type new file format
* @param string $newFile new relative file
* @return void
*/
public function execTransform($type, $newFile) {
// copy current object to convObj
$this->convObj = clone $this->fileObj;
unset($this->fileObj);
// init new language file object (dont try to read file)
$this->initFileObject($newFile, $this->convObj->getVar('absPath'), 'base', FALSE);
// recreate originLang
$dirNameOfAbsFile = dirname($this->fileObj->getVar('absFile'));
$origins = $this->convObj->getOriginLangData();
foreach ($origins as $langKey => $file) {
// localized or merged language origin
$newFile = SgLib::setFileExtension($type, $file);
if ($this->convObj->getVar('workspace') == 'base') {
if ($this->convObj->checkLocalizedFile(basename($file), $langKey)) {
$newFile = $dirNameOfAbsFile . '/' . $this->fileObj->nameLocalizedFile($langKey);
}
}
$this->fileObj->setOriginLangData(Typo3Lib::fixFilePath($newFile), $langKey);
}
// recreate meta data
$meta = $this->convObj->getMetaData();
foreach ($meta as $metaIndex => $metaValue) {
$this->fileObj->setMetaData($metaIndex, $metaValue);
}
// write new language file
$this->extConfig['execBackup'] = 0;
$this->execWrite($this->convObj->getLocalLangData());
// delete all old files
try {
$delFiles = $this->convObj->getOriginLangData();
if (is_array($delFiles) && count($delFiles)) {
SgLib::deleteFiles($delFiles);
}
} catch (Exception $e) {
throw new LFException(
'failure.langfile.notDeleted', 0,
'(' . $e->getMessage() . ')'
);
}
}
/*************************
* Getters and setters *
*************************/
......
......@@ -126,7 +126,6 @@ class Typo3Lib {
$localconf = PATH_typo3conf . 'localconf.php';
/** t3lib_install */
//TODO: Apply new way of configuration handling (http://stackoverflow.com/questions/22477417/typo3-t3lib-install-is-removed-from-typo3-core)
require_once(PATH_t3lib . 'class.t3lib_install.php');
// get current content
......
{namespace lfe=SGalinski\Lfeditor\ViewHelpers}
<lfe:addJavaScriptFile javaScriptFile="{f:uri.resource(path: 'Scripts/FormUtility.js')}" />
<lfe:addJavaScriptFile javaScriptFile="{f:uri.resource(path: 'Scripts/Lfeditor.js')}" />
<lfe:addJavaScriptFile javaScriptFile="{f:uri.resource(path: 'Scripts/textareaResize.js')}" />
......@@ -6,20 +7,8 @@
<lfe:addCssFile cssFile="{f:uri.resource(path: 'StyleSheets/Lfeditor.css')}" />
<f:be.container enableClickMenu="FALSE" loadPrototype="FALSE" loadExtJs="FALSE">
<div class="typo3-fullDoc">
<div id="typo3-docheader">
<f:comment>
<!--<div id="typo3-docheader-row1">-->
<!--<div class="buttonsleft">-->
<!--<f:render section="iconButtons" />-->
<!--</div>-->
<!--<div class="buttonsright">-->
<!--<f:be.buttons.shortcut />-->
<!--</div>-->
<!--</div>-->
</f:comment>
<div id="typo3-docheader-row2">
<div class="docheader-row2-left">
<f:be.menus.actionMenu>
......@@ -29,7 +18,7 @@
<f:be.menus.actionMenuItem label="{f:translate(key:'function.langfile.edit')}"
controller="Main"
action="editFile"
arguments="{buttonType: 0}"/>
arguments="{buttonType: 0}" />
<f:be.menus.actionMenuItem label="{f:translate(key:'function.const.edit.edit')}"
controller="Main"
action="editConstant" />
......@@ -71,29 +60,4 @@
</div>
</div>
</div>
</f:be.container>
<f:comment>
<f:debug title="All available variables">{_all}</f:debug>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<div>
This is Default layout. <br>
<f:form id="mainMenuForm" action="mainMenuSelect">
<f:form.select name="mainMenuSelection"
value="{mainMenuSelection}"
options="{ general: 'General select item', editFile: 'Edit file select item'}" options="{NULL: '--Select something--', extension1: 'Test extension 1', extension2: 'Test extension 2'}"
additionalAttributes="{onchange: 'submitForm(\'mainMenuForm\');'}"
/>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<f:render section="selectItems" />
</f:form>
<br><br>
<!--<f:link.action action="editFile">Edit file link</f:link.action>-->
<!--<br>-->
<!--<f:link.action action="general">General link</f:link.action>-->
<!--<br><br>-->
<f:render section="main" />
</div>
</f:comment>
\ No newline at end of file
</f:be.container>
\ No newline at end of file
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