From 5989461c489af11d82f82ba662f921ab31ffe583 Mon Sep 17 00:00:00 2001 From: Torsten Oppermann <torsten@sgalinski.de> Date: Tue, 9 Oct 2018 16:16:46 +0200 Subject: [PATCH] [TASK] Implement registration, started work on exception handling --- .../Controller/ConfigurationController.php | 145 +++++++++++++++--- Classes/Controller/MailController.php | 4 + 2 files changed, 131 insertions(+), 18 deletions(-) diff --git a/Classes/Controller/ConfigurationController.php b/Classes/Controller/ConfigurationController.php index 6c965db2..1403759d 100644 --- a/Classes/Controller/ConfigurationController.php +++ b/Classes/Controller/ConfigurationController.php @@ -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(); } } diff --git a/Classes/Controller/MailController.php b/Classes/Controller/MailController.php index 909578ce..da86f5a7 100644 --- a/Classes/Controller/MailController.php +++ b/Classes/Controller/MailController.php @@ -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)) { -- GitLab