From ee140599b63246274edd49e5aef4a0ddcb14bed1 Mon Sep 17 00:00:00 2001
From: Stefan Galinski <stefan@sgalinski.de>
Date: Wed, 28 Nov 2018 19:33:11 +0100
Subject: [PATCH] [BUGFIX] Fix the broken functionality for registering own
 templates, Simplified the content a lot

---
 Classes/Finisher/Forms/FormsFinisher.php     |  2 +-
 Classes/Service/MailTemplateService.php      |  8 ++--
 Classes/Service/RegisterService.php          | 15 ++++---
 Classes/XClass/Form/FormEditorController.php | 47 +++-----------------
 4 files changed, 20 insertions(+), 52 deletions(-)

diff --git a/Classes/Finisher/Forms/FormsFinisher.php b/Classes/Finisher/Forms/FormsFinisher.php
index 965d5a55..1d2765ae 100644
--- a/Classes/Finisher/Forms/FormsFinisher.php
+++ b/Classes/Finisher/Forms/FormsFinisher.php
@@ -96,7 +96,7 @@ class FormsFinisher extends AbstractFinisher {
 
 		$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
 		$mailTemplateService = $objectManager->get(
-			MailTemplateService::class, $templateName, 'project_theme', $markers
+			MailTemplateService::class, $templateName, 'sg_mail', $markers
 		);
 
 		$ignoreMailQueue = (boolean) $this->parseOption('ignoreMailQueue');
diff --git a/Classes/Service/MailTemplateService.php b/Classes/Service/MailTemplateService.php
index 61bd20bf..9abfb2a7 100644
--- a/Classes/Service/MailTemplateService.php
+++ b/Classes/Service/MailTemplateService.php
@@ -543,14 +543,14 @@ class MailTemplateService {
 		}
 
 		// get default template content from register array
-		$defaultTemplateContent = $registerService->getRegisterArray()
-		[$this->extensionKey][$this->templateName]['templateContent'];
+		$defaultTemplateContent =
+			$registerService->getRegisterArray()[$this->extensionKey][$this->templateName]['templateContent'];
 
 		// If there is no template for this language, use the default template
 		if ($template === NULL) {
 			if ($defaultTemplateContent === NULL) {
-				$templatePath = $registerService->getRegisterArray()
-				[$this->extensionKey][$this->templateName]['templatePath'];
+				$templatePath =
+					$registerService->getRegisterArray()[$this->extensionKey][$this->templateName]['templatePath'];
 
 				// only standard template file is considered since version 4.1
 				$defaultTemplateFile = $templatePath . 'template.html';
diff --git a/Classes/Service/RegisterService.php b/Classes/Service/RegisterService.php
index 6533ad9d..98820969 100644
--- a/Classes/Service/RegisterService.php
+++ b/Classes/Service/RegisterService.php
@@ -244,16 +244,18 @@ class RegisterService implements \TYPO3\CMS\Core\SingletonInterface {
 	 * @param array $markers
 	 * @param string $subject
 	 * @param string $description
+	 * @return string
 	 */
-	public function writeRegisterFile($templateKey, $extensionKey, $markers, $subject, $description) {
-		// get the location where registrations should be stored
+	public function writeRegisterFile($templateKey, $extensionKey, $markers, $subject = '', $description = ''): string {
 		$configurationLocation = $this->getRegistrationPath();
-
 		$registerFolder = GeneralUtility::getFileAbsFileName($configurationLocation);
 		GeneralUtility::mkdir_deep($registerFolder);
-		$registerFile = GeneralUtility::getFileAbsFileName($registerFolder . '/' . $templateKey . '.php');
 
-		// build the register array
+		$hashPrefix = md5($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] . '|' . $templateKey . '.php');
+		$registerFile = GeneralUtility::getFileAbsFileName(
+			$registerFolder . '/' . $hashPrefix . '_' . $templateKey . '.php'
+		);
+
 		$newRegisterArray = [
 			'extension_key' => $extensionKey,
 			'template_key' => $templateKey,
@@ -262,7 +264,6 @@ class RegisterService implements \TYPO3\CMS\Core\SingletonInterface {
 			'markers' => []
 		];
 
-		// add the markers for this template
 		foreach ($markers as $marker) {
 			$markerName = $marker['identifier'];
 			$newRegisterArray['markers'][] = [
@@ -274,5 +275,7 @@ class RegisterService implements \TYPO3\CMS\Core\SingletonInterface {
 		}
 
 		file_put_contents($registerFile, '<?php return ' . var_export($newRegisterArray, TRUE) . ';');
+
+		return $registerFile;
 	}
 }
diff --git a/Classes/XClass/Form/FormEditorController.php b/Classes/XClass/Form/FormEditorController.php
index 0e5d631d..d3273849 100644
--- a/Classes/XClass/Form/FormEditorController.php
+++ b/Classes/XClass/Form/FormEditorController.php
@@ -27,7 +27,6 @@ namespace SGalinski\SgMail\XClass\Form;
 
 use SGalinski\SgMail\Service\MailTemplateService;
 use SGalinski\SgMail\Service\RegisterService;
-use SGalinski\SgMail\Service\TypoScriptSettingsService;
 use Symfony\Component\Yaml\Yaml;
 use TYPO3\CMS\Core\Resource\ResourceStorage;
 use TYPO3\CMS\Core\Resource\StorageRepository;
@@ -90,8 +89,9 @@ class FormEditorController extends \TYPO3\CMS\Form\Controller\FormEditorControll
 			}
 
 			// retrieve the extension and template key and jump out of loop
+			// the extension key doesn't needs to the necessarily the location of the configuration entry!
 			$extensionKey = (string) $finisher['options']['extension'];
-			$extensionKey = $extensionKey ?: 'project_theme';
+			$extensionKey = $extensionKey ?: 'sg_mail';
 			$templateKey = str_replace('_', '-', $finisher['options']['template']);
 
 			// if no template key was explicitly set, use the form identifier as template key
@@ -161,26 +161,7 @@ class FormEditorController extends \TYPO3\CMS\Form\Controller\FormEditorControll
 	 * @return string
 	 */
 	private function writeRegisterFile(array $renderables, $extensionKey, $templateKey): string {
-		// get the location where automatic registrations should be stored
-		$configurationLocation = $this->getRegistrationPath();
-		$registerFolder = GeneralUtility::getFileAbsFileName($configurationLocation);
-		GeneralUtility::mkdir_deep($registerFolder);
-
-		$hashPrefix = md5($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] . '|' . $templateKey . '.php');
-		$registerFile = GeneralUtility::getFileAbsFileName(
-			$registerFolder . '/' . $hashPrefix . '_' . $templateKey . '.php'
-		);
-
-		// build the register array
-		$newRegisterArray = [
-			'extension_key' => $extensionKey,
-			'template_key' => $templateKey,
-			'description' => $templateKey,
-			'subject' => $templateKey,
-			'markers' => []
-		];
-
-		// add the markers for this template
+		$markers = [];
 		foreach ($renderables as $element) {
 			// if markerName is explicitly set, override the registered identifier
 			$markerName = $element['identifier'];
@@ -188,7 +169,7 @@ class FormEditorController extends \TYPO3\CMS\Form\Controller\FormEditorControll
 				$markerName = $element['properties']['markerName'];
 			}
 
-			$newRegisterArray['markers'][] = [
+			$markers[] = [
 				'marker' => $markerName,
 				'type' => MailTemplateService::MARKER_TYPE_STRING,
 				'value' => $element['label'],
@@ -196,23 +177,7 @@ class FormEditorController extends \TYPO3\CMS\Form\Controller\FormEditorControll
 			];
 		}
 
-		file_put_contents($registerFile, '<?php return ' . var_export($newRegisterArray, TRUE) . ';');
-
-		return $registerFile;
-	}
-
-	/**
-	 * Returns the path to the configured location where automatic mail template registrations should be
-	 *
-	 * @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'] . '/' . RegisterService::CONFIG_PATH;
+		$registrationService = $this->objectManager->get(RegisterService::class);
+		return $registrationService->writeRegisterFile($templateKey, $extensionKey, $markers, $templateKey);
 	}
 }
-- 
GitLab