From bb34b57178f034eaf9da0344aade813b9cab4805 Mon Sep 17 00:00:00 2001
From: Torsten Oppermann <torsten@sgalinski.de>
Date: Tue, 27 Sep 2016 22:49:19 +0200
Subject: [PATCH] [TASK] Adding Service class to read ts Settings in other
 classes

---
 Classes/Service/MailTemplateService.php       | 16 +++-
 Classes/Service/TypoScriptSettingsService.php | 78 +++++++++++++++++++
 Configuration/TypoScript/setup.ts             |  1 +
 3 files changed, 92 insertions(+), 3 deletions(-)
 create mode 100644 Classes/Service/TypoScriptSettingsService.php

diff --git a/Classes/Service/MailTemplateService.php b/Classes/Service/MailTemplateService.php
index 2345f09f..be75c87c 100644
--- a/Classes/Service/MailTemplateService.php
+++ b/Classes/Service/MailTemplateService.php
@@ -66,7 +66,7 @@ class MailTemplateService {
 	/**
 	 * @var string language
 	 */
-	private $language = 'de';
+	private $language;
 
 	/**
 	 * @var boolean ignoreMailQueue
@@ -98,12 +98,23 @@ class MailTemplateService {
 	 */
 	private $subject;
 
+	/**
+	 * holds the TypoScript configuration for sg_mail
+	 *
+	 * @var array tsSettings
+	 */
+	private $tsSettings = [];
+
 	/**
 	 * MailTemplateService constructor.
 	 */
 	public function __construct() {
 		$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
 		$this->mailMessage = $objectManager->get(MailMessage::class);
+
+		$typoScriptSettingsService = $objectManager->get(TypoScriptSettingsService::class);
+		$this->tsSettings = $typoScriptSettingsService->getSettings(0, 'tx_sgmail');
+		$this->language = $this->tsSettings['templateDefaultLanguage'];
 	}
 
 	/**
@@ -140,8 +151,7 @@ class MailTemplateService {
 	 * Send the Email
 	 */
 	public function sendEmail() {
-		$registerArray = MailTemplateService::getRegisterArray();
-		$templateEntry = $registerArray[$this->extensionKey][$this->templateName];
+		$templateEntry = self::$registerArray[$this->extensionKey][$this->templateName];
 
 		$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
 		$emailView = $objectManager->get(StandaloneView::class);
diff --git a/Classes/Service/TypoScriptSettingsService.php b/Classes/Service/TypoScriptSettingsService.php
new file mode 100644
index 00000000..e10ac5fb
--- /dev/null
+++ b/Classes/Service/TypoScriptSettingsService.php
@@ -0,0 +1,78 @@
+<?php
+
+namespace SGalinski\SgMail\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 TYPO3\CMS\Core\SingletonInterface;
+use TYPO3\CMS\Extbase\Configuration\BackendConfigurationManager;
+use TYPO3\CMS\Extbase\Service\TypoScriptService;
+
+/**
+ * Returns the typoscript setup based on the page id
+ *
+ */
+class TypoScriptSettingsService implements SingletonInterface {
+	/**
+	 * @inject
+	 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
+	 */
+	protected $objectManager;
+
+	/**
+	 * @var array
+	 */
+	protected static $typoScriptCache = [];
+
+	/**
+	 * Returns the typoscript settings of a given page and extension
+	 *
+	 * @param int $pageId
+	 * @param string $extensionKey
+	 * @return array
+	 */
+	public function getSettings($pageId, $extensionKey) {
+		$pageId = (int) $pageId;
+		if (!isset(self::$typoScriptCache[$extensionKey . $pageId])) {
+			$tmpId = $_POST['id'];
+			$_POST['id'] = $pageId;
+
+			/** @var TypoScriptService $typoScriptService */
+			$class = 'TYPO3\CMS\Extbase\Service\TypoScriptService';
+			$typoScriptService = $this->objectManager->get($class);
+
+			/** @var BackendConfigurationManager $configurationManager */
+			$class = 'TYPO3\CMS\Extbase\Configuration\BackendConfigurationManager';
+			$configurationManager = $this->objectManager->get($class);
+
+			self::$typoScriptCache[$extensionKey . $pageId] = $typoScriptService->convertTypoScriptArrayToPlainArray(
+				(array) $configurationManager->getTypoScriptSetup()['module.'][$extensionKey . '.']['settings.']
+			);
+			$_POST['id'] = $tmpId;
+		}
+
+		return self::$typoScriptCache[$extensionKey . $pageId];
+	}
+}
diff --git a/Configuration/TypoScript/setup.ts b/Configuration/TypoScript/setup.ts
index 786cd790..aa2d2713 100644
--- a/Configuration/TypoScript/setup.ts
+++ b/Configuration/TypoScript/setup.ts
@@ -10,6 +10,7 @@ module.tx_sgmail {
 			from.test = info@test.de
 		}
 
+		templateDefaultLanguage = de
 		template.filename = mail_template.html
 	}
 }
-- 
GitLab