Skip to content
Snippets Groups Projects
MailController.php 7.78 KiB
Newer Older
<?php

namespace SGalinski\SgMail\Controller;

/***************************************************************
 *  Copyright notice
 *
 *  (c) sgalinski Internet Services (https://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\SgMail\Service\MailTemplateService;
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;

/**
 * Controller for the mail templating service module
 */
class MailController extends ActionController {

	/**
	 * @var \TYPO3\CMS\Lang\Domain\Repository\LanguageRepository
	 * @inject
	 */
	protected $languageRepository = NULL;

	 * @param string $selectedTemplate
	 * @param string $selectedExtension
	 * @param string $selectedLanguageLeft
	 * @param string $selectedLanguageRight
	 * @param string $selectedExtensionKey
	 * @param string $selectedTemplateKey
	public function indexAction(
		$selectedTemplate = NULL, $selectedExtension = NULL, $selectedLanguageLeft = NULL,
		$selectedLanguageRight = NULL, $selectedExtensionKey = NULL, $selectedTemplateKey = NULL
	) {
		$registerArray = MailTemplateService::getRegisterArray();
		reset(MailTemplateService::getRegisterArray());
		$firstEntry = key(MailTemplateService::getRegisterArray());
		$secondLevelFirstEntry = key(MailTemplateService::getRegisterArray()[$firstEntry]);

		$templatePath = $this->getTemplatePath($selectedExtension, $selectedTemplate);
		$activatedLanguages = $this->languageRepository->findSelected();
		$languages = ['en'];
		foreach ($activatedLanguages as $language) {
			$languages[] = $language->getLocale();
			$this->writeLanguageFile($language, $templatePath);
		$this->view->assign('templates', MailTemplateService::getRegisterArray());
		if (isset($selectedTemplate)
			&& !isset($selectedLanguageLeft)
			&& !isset($selectedLanguageRight)
		) {
			$selectedTemplateArray = $registerArray[$selectedExtension][$selectedTemplate];
			$this->view->assign('selectedTemplate', $selectedTemplateArray);
			$this->view->assign('selectedTemplateKey', $selectedTemplate);
			$this->view->assign('selectedExtensionKey', $selectedExtension);
		} elseif (!isset($selectedLanguageLeft) && !isset($selectedLanguageRight)) {
			$this->view->assign(
				'selectedTemplate', MailTemplateService::getRegisterArray()[$firstEntry][$secondLevelFirstEntry]
			);
			$this->view->assign('selectedTemplateKey', $secondLevelFirstEntry);
			$this->view->assign('selectedExtensionKey', $firstEntry);
		if (isset($selectedLanguageLeft)
			&& isset($selectedLanguageRight)
			&& isset($selectedTemplate)
			&& isset($selectedExtensionKey)
		) {
			$this->view->assign('selectedLanguageLeft', $selectedLanguageLeft);
			$this->view->assign('selectedLanguageRight', $selectedLanguageRight);
			$this->view->assign(
				'selectedTemplate', MailTemplateService::getRegisterArray()[$selectedExtensionKey][$selectedTemplateKey]
			);
			$this->view->assign('selectedTemplateKey', $selectedTemplateKey);
			$this->view->assign('selectedExtensionKey', $selectedExtensionKey);
				'contentLeft', file_get_contents($templatePath . $selectedLanguageLeft . '.sg_mail.locallang.html')
				'contentRight', file_get_contents($templatePath . $selectedLanguageRight . '.sg_mail.locallang.html')
			$this->view->assign('selectedLanguageLeft', 'en');
			$this->view->assign('contentLeft', file_get_contents($templatePath . 'en.sg_mail.locallang.html'));
			$this->view->assign('selectedLanguageRight', 'en');
			$this->view->assign('contentRight', file_get_contents($templatePath . 'en.sg_mail.locallang.html'));
	 * @param \TYPO3\CMS\Lang\Domain\Model\Language $language
	 * @param string $path
	 */
	private function writeLanguageFile(\TYPO3\CMS\Lang\Domain\Model\Language $language, $path) {
		$currentLangFile = $path . $language->getLocale() . '.sg_mail.locallang.html';
		if (!file_exists($currentLangFile)) {
			$langFileContent = 'TEST: ' . $language->getLocale();
			file_put_contents($currentLangFile, $langFileContent);
	/**
	 * Save content (left & right)
	 * if left & right is the same language, the right content will apply
	 *
	 * @param string $contentLeft
	 * @param string $contentRight
	 * @param string $selectedExtension
	 * @param string $selectedTemplate
	 * @param string $extensionKey
	 * @param string $selectedLanguageLeft
	 * @param string $selectedLanguageRight
	 */
	public function saveAction(
		$contentLeft = NULL, $contentRight = NULL, $selectedExtension = NULL, $selectedTemplate = NULL,
		$extensionKey = NULL, $selectedLanguageLeft = NULL, $selectedLanguageRight = NULL
	) {

		$templatePath = $this->getTemplatePath($selectedExtension, $selectedTemplate);
		$filename = $templatePath . $selectedLanguageLeft . '.sg_mail.locallang.html';
		file_put_contents($filename, $contentLeft);

		$filename = $templatePath . $selectedLanguageRight . '.sg_mail.locallang.html';
		file_put_contents($filename, $contentRight);

		$message = LocalizationUtility::translate('backend.success', 'SgMail');
		$this->addFlashMessage($message, '', FlashMessage::OK);

		$arguments = $this->request->getArguments();
		$this->redirect('index', NULL, NULL, $arguments);
	/**
	 * Returns the Path for this Template
	 *
	 * @param string $selectedExtension
	 * @param string $selectedTemplate
	 * @return string $templatePath
	 */
	private function getTemplatePath($selectedExtension, $selectedTemplate) {

		$registerArray = MailTemplateService::getRegisterArray();
		reset(MailTemplateService::getRegisterArray());
		$firstEntry = key(MailTemplateService::getRegisterArray());
		$secondLevelFirstEntry = key(MailTemplateService::getRegisterArray()[$firstEntry]);

		$templatePath = MailTemplateService::getRegisterArray()
		[$firstEntry][$secondLevelFirstEntry]['templatePath'];
		if (isset($selectedTemplate)) {
			$templatePath = $registerArray[$selectedExtension][$selectedTemplate]['templatePath'];
		}

		return $templatePath;
	}

	/**
	 * send a test email to a given address
	 * redirect to index action
	 *
	 * @param string $emailAddress
	 * @param string $templateKey
	 * @param string $extensionKey
	 * @param string $languageLeft
	 * @param string $languageRight
	 */
	public function sendTestMailAction(
		$emailAddress = NULL, $selectedExtensionKey = NULL, $selectedTemplateKey = NULL, $selectedLanguageLeft = NULL,
		$selectedLanguageRight = NULL
	) {

		MailTemplateService::sendEmail(
			$selectedLanguageLeft, $selectedTemplateKey, $selectedExtensionKey, $emailAddress, 'test@sgmail.de',
			'This is just a Test E-Mail'
		);

		MailTemplateService::sendEmail(
			$selectedLanguageRight, $selectedTemplateKey, $selectedExtensionKey, $emailAddress, 'test@sgmail.de',
			'This is just a Test E-Mail'
		);

		$message = LocalizationUtility::translate('backend.success_mail', 'SgMail');
		$this->addFlashMessage($message, '', FlashMessage::OK);

		$arguments = $this->request->getArguments();
		$this->redirect('index', NULL, NULL, $arguments);

	}