Commit 7a93e603 authored by Damjan's avatar Damjan
Browse files

Using real code for fetching extension menu items

-Use of namespaces

Injection of mainService as singleton

new method for settiong the view variables: assignViewWidthMenuVariables

Related: forge.typo3.org/issues/31889
parent 1a19d5f1
......@@ -39,7 +39,8 @@ abstract class AbstractController extends ActionController {
protected $session;
/**
* Initializes the actions
* Initializes the actions.
* - Initializes the session object.
*
* @return void
*/
......
......@@ -25,11 +25,63 @@ namespace SGalinski\Lfeditor\Controller;
*
* 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\Lang\LanguageService;
/**
* Main controller. It contains all extbase actions of Lfeditor extension.
*/
class MainController extends AbstractController {
/**
* @inject
* @var \SGalinski\Lfeditor\Service\MainService
*/
protected $mainService;
// /**
// * @var array
// */
// protected $extConfig;
// /**
// * Constructor
// */
// public function __construct() {
// // prepare configuration
// $this->prepareConfig();
//
// // set error wrap
// $errorWrap = '<p class="tx-lfeditor-error">|</p>';
// $noticeWrap = '<p class="tx-lfeditor-notice">|</p>';
// LFException::setWrap($errorWrap, $noticeWrap);
//
// parent::init();
// }
/**
* Initializes the actions.
* - Initializes session object.
* - Fetches configuration.
*
* @return void
*/
public function initializeAction() {
parent::initializeAction();
$this->mainService->prepareConfig();
}
/**
* Initializes the system.
*
* @return void
* @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException //TODO: Handle every exception in controllers and display flash message.
*/
public function initializeLfeditorAction() {
// //TODO: I might not need this. I am preparing config in every request before every action...
// $this->mainService->prepareConfig();
$this->redirect('general');
}
/**************************************
* Actions called from general view *
......@@ -37,17 +89,44 @@ class MainController extends AbstractController {
/**
* Opens general view.
* It is called in 3 cases:
* - on extension startup,
* It is called in 2 cases:
* - on selection of general option in main menu,
* - after redirection from action which must not change the view.
*
* @return void
*/
public function generalAction() {
//TODO: Initialise options of all select menus in general view.
$this->view->assign('mainMenuSelection', 'general');
$this->loadSelectionsFromSession();
try {
$extensionOptions = $this->mainService->menuExtList();
$this->assignViewWidthMenuVariables('extension', $extensionOptions);
// //TODO: In same way (like extension menu) handle all other select menus that exist in the view
// $languageFileOptions = $this->mainService->menuLangFileList();
// $this->assignViewWidthMenuVariables('languageFile', $languageFileOptions);
} catch (LFException $e) {
/** @var LanguageService $lang */
$lang = $GLOBALS['LANG'];
if ($e->getCode() == 0) {
$this->addFlashMessage(
$e->getMessage(),
$messageTitle = $lang->getLL('failure.failure'),
$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
);
}
}
}
}
/**
......@@ -75,6 +154,7 @@ class MainController extends AbstractController {
//TODO: Delete. test $this->session->destroy();
//TODO: Save the changes.
$this->session->destroy();
debug(phpinfo(), 'phpInfo');
$this->redirect('general');
}
......@@ -132,7 +212,7 @@ class MainController extends AbstractController {
* @param string $languageFileSelection
* @return void
*/
private function saveSelectionsInSession($extensionSelection = NULL, $languageFileSelection = NULL) {
protected function saveSelectionsInSession($extensionSelection = NULL, $languageFileSelection = NULL) {
if ($extensionSelection === 'NULL') {
$extensionSelection = NULL;
}
......@@ -145,13 +225,22 @@ class MainController extends AbstractController {
}
/**
* Fetches from session currently selected values of select tags and puts them in view variables.
* 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
*/
private function loadSelectionsFromSession() {
$this->view->assign('extensionSelection', $this->session->getDataByKey('extensionSelection'));
$this->view->assign('languageFileSelection', $this->session->getDataByKey('languageFileSelection'));
protected function assignViewWidthMenuVariables($menuName, $options) {
$this->view->assign($menuName . 'Options', $options);
$selection = $this->session->getDataByKey($menuName . 'Selection');
if ($selection === NULL && !empty($options)) {
reset($options);
$selection = current($options);
}
$this->view->assign($menuName . 'Selection', $selection);
debug($this->session->getData(), 'ses');//TODO: Delete debug.
}
......
<?php
namespace SGalinski\Lfeditor\Exceptions;
//TODO: Change all old Copyright notices with new ones, which refer to sgalinski.
/***************************************************************
* Copyright notice
......@@ -22,51 +25,23 @@
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use Exception;
use TYPO3\CMS\Lang\LanguageService;
/**
* adds a new error exception
*/
class LFException extends Exception {
/**
* @var string
*/
private static $errorWrap = '|';
/**
* @var string
*/
private static $noticeWrap = '|';
/**
* @var string
*/
protected $generatedContent = '';
/**
* @param string $content
* @return void
*/
public function setGeneratedContent($content) {
$this->generatedContent = $content;
}
/**
* @return string
*/
public function getGeneratedContent() {
return $this->generatedContent;
}
/**
* Constructor
*
* @param string $msg error message (this message would be translated by TYPO3)
* @param integer $wrapType type of wrap (0 = error (default), 1 = notice)
* @param integer $severityCode (0 = error (default), 1 = notice)
* @param string $staticMsg static message (appended at the localized string)
*/
public function __construct($msg, $wrapType = 0, $staticMsg = '') {
public function __construct($msg, $severityCode = 0, $staticMsg = '') {
if (!empty($msg)) {
/** @var \TYPO3\CMS\Lang\LanguageService $lang */
/** @var LanguageService $lang */
$lang = $GLOBALS['LANG'];
$msg = $lang->getLL($msg);
}
......@@ -75,35 +50,9 @@ class LFException extends Exception {
$msg = 'LFExeption: no error message given !!!';
}
parent::__construct($this->prepare($msg . ' ' . $staticMsg, $wrapType));
}
/**
* set the error wrap
*
* @param string $errorWrap error wrap
* @param string $noticeWrap notice wrap
* @return void
*/
public static function setWrap($errorWrap, $noticeWrap) {
self::$errorWrap = $errorWrap;
self::$noticeWrap = $noticeWrap;
parent::__construct($msg . ' ' . $staticMsg, $severityCode);
}
/**
* preparation of the error message
*
* @param string $msg error message (this message would be translated)
* @param integer $wrapType type of wrap (0 = error (default), 1 = notice)
* @return string prepared message
*/
private function prepare($msg, $wrapType = 0) {
if (!$wrapType) {
return str_replace('|', $msg, self::$errorWrap);
} else {
return str_replace('|', $msg, self::$noticeWrap);
}
}
}
?>
......@@ -22,6 +22,7 @@
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use SGalinski\Lfeditor\Exceptions\LFException;
/** general file functions */
require_once(t3lib_extMgm::extPath('lfeditor') . 'Classes/Service/FileService.php');
......@@ -38,7 +39,7 @@ class FileBackupService extends FileService {
/**
* @var array
*/
private $metaArray = array();
private $metaArray = [];
/**
* @var string
......@@ -66,7 +67,7 @@ class FileBackupService extends FileService {
*/
public function init($file, $path, $metaFile) {
// init
$this->setVar(array('metaFile' => $metaFile));
$this->setVar(['metaFile' => $metaFile]);
parent::init($file, $path);
// read meta file
......@@ -161,7 +162,7 @@ class FileBackupService extends FileService {
} elseif ($mode == 3) {
return $this->metaArray[$extName][$workspace][$langFile];
} else {
return array();
return [];
}
}
......@@ -237,12 +238,12 @@ class FileBackupService extends FileService {
*/
private function genMetaXML() {
// define assocTagNames
$options['parentTagMap'] = array(
$options['parentTagMap'] = [
'' => 'extKey',
'extKey' => 'workspace',
'workspace' => 'langFile',
'langFile' => 'file',
);
];
return t3lib_div::array2xml($this->getMetaInfos(), '', 0, 'LFBackupMeta', 0, $options);
}
......@@ -323,7 +324,7 @@ class FileBackupService extends FileService {
// delete backup file
try {
SgLib::deleteFiles(array($file));
SgLib::deleteFiles([$file]);
} catch (Exception $e) {
throw new LFException('failure.backup.notDeleted', 0,
'(' . $e->getMessage(), ')');
......@@ -362,7 +363,7 @@ class FileBackupService extends FileService {
}
// read array
$localLang = $originLang = array();
$localLang = $originLang = [];
foreach ($phpArray['data'] as $langKey => $informations) {
// read origin
try {
......@@ -386,7 +387,7 @@ class FileBackupService extends FileService {
// convert all values back to their original charsets
if (!Typo3Lib::isTypo3BackendInUtf8Mode()) {
$this->localLang = Typo3Lib::utf8($localLang, FALSE, array('default'));
$this->localLang = Typo3Lib::utf8($localLang, FALSE, ['default']);
} else {
$this->localLang = $localLang;
}
......@@ -403,7 +404,7 @@ class FileBackupService extends FileService {
private function prepareBackupContent() {
// convert all values to utf-8
if (!Typo3Lib::isTypo3BackendInUtf8Mode()) {
$localLang = Typo3Lib::utf8($this->localLang, TRUE, array('default'));
$localLang = Typo3Lib::utf8($this->localLang, TRUE, ['default']);
} else {
$localLang = $this->localLang;
}
......@@ -429,10 +430,10 @@ class FileBackupService extends FileService {
}
// define assocTagNames
$options['parentTagMap'] = array(
$options['parentTagMap'] = [
'data' => 'languageKey',
'langData' => 'label'
);
];
// get xml
return t3lib_div::array2xml($phpArray, '', 0, 'LFBackup', 0, $options);
......@@ -450,7 +451,7 @@ class FileBackupService extends FileService {
// get and set name of backup
$backupName = t3lib_div::shortMD5(md5($xml)) . '.bak';
$this->setVar(array('relFile' => $backupName));
$this->setVar(['relFile' => $backupName]);
// get new meta information
$metaArray = $this->getMetaInfos(3);
......
<?php
namespace SGalinski\Lfeditor\Service;
/***************************************************************
* 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\Typo3Lib;
use TYPO3\CMS\Core\Localization\Locales;
use TYPO3\CMS\Core\SingletonInterface;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Lang\LanguageService;
class MainService implements SingletonInterface {
//TODO: rename to ConfigurationService.
/**
* @var array extension configuration
* @see prepareConfig()
*/
protected $extConfig = [];
/**
* @var string
*/
protected $invalidLanguages = '';
/**
* preparation and check of the configuration
*
* Note that the default value will be set, if a option check fails.
*
* @return array
*/
public function prepareConfig() {
// unserialize the configuration
$this->extConfig = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['lfeditor']);
// regular expressions
$this->extConfig['searchRegex'] = '/^[a-z0-9_]*locallang[a-z0-9_-]*\.(php|xml|xlf)$/i';
if (!preg_match('/^\/.*\/.*$/', $this->extConfig['extIgnore'])) {
$this->extConfig['extIgnore'] = '/^csh_.*$/';
}
// some integer values
$this->extConfig['viewStateExt'] = (int) $this->extConfig['viewStateExt'];
$this->extConfig['numTextAreaRows'] = intval($this->extConfig['numTextAreaRows']);
$this->extConfig['numSiteConsts'] = intval($this->extConfig['numSiteConsts']);
$this->extConfig['anzBackup'] = intval($this->extConfig['anzBackup']);
$this->extConfig['viewStateExt'] = intval($this->extConfig['viewStateExt']);
// paths and files (dont need to exist)
$this->extConfig['pathBackup'] = Typo3Lib::fixFilePath(
PATH_site . '/' .
$this->extConfig['pathBackup']
) . '/';
$this->extConfig['metaFile'] = Typo3Lib::fixFilePath(
PATH_site . '/' .
$this->extConfig['metaFile']
);
$this->extConfig['pathXLLFiles'] = Typo3Lib::fixFilePath(
PATH_site . '/' .
$this->extConfig['pathXLLFiles']
) . '/';
// files
$this->extConfig['pathCSS'] = 'tx_lfeditor_mod1.css';
$this->extConfig['pathTinyMCEConfig'] = PATH_site .
ExtensionManagementUtility::siteRelPath('lfeditor') . 'Resources/Public/Scripts/TinyMCEConfig.js';
// languages (default is forbidden)
if (!empty($this->extConfig['viewLanguages'])) {
$langs = explode(',', $this->extConfig['viewLanguages']);
unset($this->extConfig['viewLanguages']);
$availableLanguages = [];
if (GeneralUtility::compat_version('6.0')) {
//TODO: Make object manager in constructor and save it in a class field.
/** @var ObjectManager $objectManager */
$objectManager = GeneralUtility::makeInstance('\TYPO3\CMS\Extbase\Object\ObjectManager');
/** @var Locales $locales */
$locales = $objectManager->get('TYPO3\CMS\Core\Localization\Locales');
$availableLanguages = $locales->getLanguages();
} else {
$languages = preg_split(
'|', TYPO3_languages
); //TODO: I assume that this constant will be available if user has version older then 6.0.
//(2012-06-17 c4742c3 #38128 [TASK] Remove t3lib/config_default.php (Christian Kuhn))
foreach ($languages as $language) {
$availableLanguages[$language] = TRUE;
}
}
foreach ($langs as $lang) {
if (!isset($availableLanguages[$lang])) {
if ($this->invalidLanguages === '') {
$this->invalidLanguages = $lang;
} else {
$this->invalidLanguages .= ', ' . $lang;
}
}
if ($lang != 'default') {
$this->extConfig['viewLanguages'][] = $lang;
}
}
}
return $this->extConfig;
}
/**
* Finds extensions for the extension menu selector.
*
* Note: $this->extConfig must be initialized before call of this method (method prepareConfig() must be executed before this method).
*
* @throws LFException raised if no extensions are found
* @return array
*/
public function menuExtList() {
//TODO: Replace double slash '//' with single slash in file path. (e.g. /var/www/kickstarter.dev/typo3conf/ext//dez_coupons)
/** @var LanguageService $lang */
$lang = $GLOBALS['LANG'];
// search extensions
$tmpExtList = [];
try {
// local extensions
if ($this->extConfig['viewLocalExt']) {
if (count(
$content = Functions::searchExtensions(
PATH_site . Typo3Lib::pathLocalExt, $this->extConfig['viewStateExt'],
$this->extConfig['extIgnore']
)
)
) {
$tmpExtList[$lang->getLL('ext.local')] = $content;
}
}
// global extensions
if ($this->extConfig['viewGlobalExt']) {
if (count(
$content = Functions::searchExtensions(
PATH_site . Typo3Lib::pathGlobalExt, $this->extConfig['viewStateExt'],
$this->extConfig['extIgnore']
)
)
) {
$tmpExtList[$lang->getLL('ext.global')] = $content;
}
}
// system extensions
if ($this->extConfig['viewSysExt']) {
if (count(
$content = Functions::searchExtensions(
PATH_site . Typo3Lib::pathSysExt, $this->extConfig['viewStateExt'],
$this->extConfig['extIgnore']
)
)
) {
$tmpExtList[$lang->getLL('ext.system')] = $content;
}
}
} catch (Exception $e) {
throw new LFException('failure.failure', 0, '(' . $e->getMessage() . ')');
}
// check extension array
if (!count($tmpExtList)) {
throw new LFException('failure.search.noExtension');
}
// create list
/** @var array $extList */
$extList = Functions::prepareExtList($tmpExtList);
$extList = array_merge([PATH_site . 'fileadmin' => 'fileadmin/', ''], $extList);
return $extList;
}
/*************************
* Getters and setters *
*************************/
/**
* @return array
*/
public function getExtConfig() {
return $this->extConfig;
}
/**
* @param array $extConfig
*/
public function setExtConfig($extConfig) {
$this->extConfig = $extConfig;
}
/**
* @return string
*/
public function getInvalidLanguages() {
return $this->invalidLanguages;
}
/**
* @param string $invalidLanguages
*/
public function setInvalidLanguages($invalidLanguages) {
$this->invalidLanguages = $invalidLanguages;
}
}
\ No newline at end of file
<?php
namespace SGalinski\Lfeditor\Utility;
/***************************************************************
* Copyright notice
*
......@@ -22,13 +24,15 @@
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Lang\LanguageService;
/**
* contains functions for the 'lfeditor' extension