Skip to content
Snippets Groups Projects
Commit 5989461c authored by Torsten Oppermann's avatar Torsten Oppermann
Browse files

[TASK] Implement registration, started work on exception handling

parent 9da3895b
No related branches found
No related tags found
1 merge request!12Feature backend config
......@@ -27,21 +27,26 @@ namespace SGalinski\SgMail\Controller;
***************************************************************/
use SGalinski\SgMail\Service\BackendService;
use SGalinski\SgMail\Service\MailTemplateService;
use SGalinski\SgMail\Service\TypoScriptSettingsService;
use SGalinski\SgMail\Session\PhpSession;
use TYPO3\CMS\Backend\Template\Components\DocHeaderComponent;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
use TYPO3\CMS\Core\Exception;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException;
use TYPO3\CMS\Extbase\Mvc\Exception\StopActionException;
use TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
/**
* Controller for the configuration mode of the backend module
*/
class ConfigurationController extends ActionController {
const DEFAULT_EXTENSION_KEY = 'sg_mail';
/**
* DocHeaderComponent
*
......@@ -64,19 +69,6 @@ class ConfigurationController extends ActionController {
public function indexAction($selectedTemplate = NULL, $selectedExtension = NULL, array $filters = []) {
$pageUid = (int) GeneralUtility::_GP('id');
// set session key
if (!($this->session instanceof PhpSession)) {
$this->session = $this->objectManager->get(PhpSession::class);
$this->session->setSessionKey('sg_mail_controller_session');
} else {
$this->session->setSessionKey('sg_mail_controller_session');
}
// store the user selection in the session
if ($this->request->hasArgument('controller')) {
$this->session->setDataByKey('mode', BackendService::BACKEND_MODE_EDITOR_CONTROLLER);
}
$registerArray = BackendService::getNonBlacklistedTemplates($pageUid);
if ($selectedTemplate === NULL || $selectedTemplate === '') {
if (!empty($registerArray)) {
......@@ -107,6 +99,8 @@ class ConfigurationController extends ActionController {
/**
* Create the template or display errors that occured
*
* @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
*/
public function createAction() {
try {
......@@ -118,9 +112,124 @@ class ConfigurationController extends ActionController {
$subject = $configuration['subject'];
$description = $configuration['description'];
$this->redirect('index');
} catch (NoSuchArgumentException $e) {
// parse csv (,,,;,,,;)
$markersCsv = str_getcsv($csv, ';');
$markers = [];
foreach ($markersCsv as $markerCsv) {
$rowArray = GeneralUtility::trimExplode(',', $markerCsv);
$markers[] = [
'identifier' => $rowArray[0],
'value' => $rowArray[1],
'description' => $rowArray[2]
];
}
// write the new Register.php file
$this->writeRegisterFile($templateName, self::DEFAULT_EXTENSION_KEY, $markers, $subject, $description);
// call register function in mail template service class
MailTemplateService::registerExtensions();
// clear caches
$this->clearCaches();
// store selected template & extension key in the session
if (!($this->session instanceof PhpSession)) {
$this->session = $this->objectManager->get(PhpSession::class);
$this->session->setSessionKey('sg_mail_controller_session');
} else {
$this->session->setSessionKey('sg_mail_controller_session');
}
$this->session->setDataByKey('selectedTemplate', $templateName);
$this->session->setDataByKey('selectedExtension', self::DEFAULT_EXTENSION_KEY);
$this->redirect('index', 'Mail', NULL, ['message' => LocalizationUtility::translate('backend.create.message.success', 'sg_mail')]);
} catch (Exception $e) {
}
}
/**
* Write the mail registration file
*
* @TODO move this to the register service class when security update is merged. refactor in FormEditorController xclass as well
*
* @param string $templateKey
* @param string $extensionKey
* @param array $markers
* @param string $subject
* @param string $description
*/
private function writeRegisterFile($templateKey, $extensionKey, $markers, $subject, $description) {
// get the location where registrations should be stored
$configurationLocation = $this->getRegistrationPath();
$registerFolder = GeneralUtility::getFileAbsFileName(
$configurationLocation
);
// create folder
GeneralUtility::mkdir($registerFolder);
$registerFile = GeneralUtility::getFileAbsFileName(
$registerFolder . '/' . $templateKey . '.php'
);
// build the register array
$newRegisterArray = [
'extension_key' => $extensionKey,
'template_key' => $templateKey,
'description' => $description,
'subject' => $subject,
'markers' => []
];
// add the markers for this template
foreach ($markers as $marker) {
$markerName = $marker['identifier'];
$newRegisterArray['markers'][] = [
'marker' => $markerName,
'type' => MailTemplateService::MARKER_TYPE_STRING,
'value' => $marker['value'],
'description' => $marker['description']
];
}
file_put_contents($registerFile, '<?php return ' . var_export($newRegisterArray, TRUE) . ';');
}
/**
* Returns the path to the configured location where automatic mail template registrations should be
*
* @TODO move this to the register service class when security update is merged. refactor in FormEditorController xclass as well
*
* @return string
*/
private function getRegistrationPath(): string {
// get typoscript settings from sg mail
/** @var TypoScriptSettingsService $typoScriptSettingsService */
$typoScriptSettingsService = $this->objectManager->get(TypoScriptSettingsService::class);
$tsSettings = $typoScriptSettingsService->getSettings(0, 'tx_sgmail');
// get the location where automatic registrations should be stored
return 'EXT:' . $tsSettings['mail']['configurationLocation'] . '/' . MailTemplateService::CONFIG_PATH;
}
/**
* Clear the sgmail register cache
*
* @TODO move this to the register service class when security update is merged. refactor in FormEditorController xclass as well
*
* @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
*/
private function clearCaches() {
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
$cacheManager = $objectManager->get(CacheManager::class);
/** @var FrontendInterface $cache */
$cache = $cacheManager->getCache(MailTemplateService::CACHE_NAME);
/** @var FrontendInterface $cache */
$cache->flush();
}
}
......@@ -81,6 +81,10 @@ class MailController extends ActionController {
* @throws \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException
*/
public function indexAction(array $parameters = []) {
if ($this->request->hasArgument('message')) {
}
$pid = (int) GeneralUtility::_GP('id');
if (!($this->session instanceof PhpSession)) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment