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\Domain\Model\Template;

Torsten Oppermann
committed
use SGalinski\SgMail\Service\MailTemplateService;
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
/**
* Controller for the mail templating service module
*/
class MailController extends ActionController {

Torsten Oppermann
committed
/**
* @var \TYPO3\CMS\Lang\Domain\Repository\LanguageRepository
* @inject
*/
protected $languageRepository = NULL;
/**
* @var \SGalinski\SgMail\Domain\Repository\TemplateRepository
* @inject
*/
protected $templateRepository = NULL;
* Show template Selection and enable content input + mail preview
* @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
) {

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

Torsten Oppermann
committed
$activatedLanguages = $this->languageRepository->findSelected();
$languages = ['en'];
foreach ($activatedLanguages as $language) {
$languages[] = $language->getLocale();

Torsten Oppermann
committed
}

Torsten Oppermann
committed
$this->view->assign('languages', $languages);

Torsten Oppermann
committed
$this->view->assign('templates', MailTemplateService::getRegisterArray());
if (isset($selectedTemplate)
&& !isset($selectedLanguageLeft)
&& !isset($selectedLanguageRight)
) {
$selectedTemplateArray = $registerArray[$selectedExtension][$selectedTemplate];

Torsten Oppermann
committed
$this->view->assign('selectedTemplate', $selectedTemplateArray);
$this->view->assign('selectedTemplateKey', $selectedTemplate);
$this->view->assign('selectedExtensionKey', $selectedExtension);

Torsten Oppermann
committed
} elseif (!isset($selectedLanguageLeft) && !isset($selectedLanguageRight)) {
if (isset(MailTemplateService::getRegisterArray()[$firstEntry][$secondLevelFirstEntry])) {
$this->view->assign(
'selectedTemplate', MailTemplateService::getRegisterArray()[$firstEntry][$secondLevelFirstEntry]
);
}

Torsten Oppermann
committed
$this->view->assign('selectedLanguageLeft', $this->settings['templateDefaultLanguage']);
$this->view->assign('selectedLanguageRight', $this->settings['templateDefaultLanguage']);
$this->view->assign('selectedTemplateKey', $secondLevelFirstEntry);

Torsten Oppermann
committed
$this->view->assign('selectedExtensionKey', $firstEntry);

Torsten Oppermann
committed
}
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);
/** @var /SGalinski/SgMail/Domain/Model/Template $result */
$templateLeft = $this->templateRepository->findTemplate(
$selectedExtensionKey, $selectedTemplateKey, $selectedLanguageLeft
)->getFirst();
$this->view->assign(
$templateLeft !== NULL ? $templateLeft->getContent() : ''
);
$this->view->assign(
'subjectLeft',
$templateLeft !== NULL ? $templateLeft->getSubject() : ''
/** @var /SGalinski/SgMail/Domain/Model/Template $result */
$templateRight = $this->templateRepository->findTemplate(
$selectedExtensionKey, $selectedTemplateKey, $selectedLanguageRight
)->getFirst();
$this->view->assign(
$templateRight !== NULL ? $templateRight->getContent() : ''
);
$this->view->assign(
'subjectRight',
$templateRight !== NULL ? $templateRight->getSubject() : ''
);

Torsten Oppermann
committed
} else {
/** @var /SGalinski/SgMail/Domain/Model/Template $result */
$templateLeft = $this->templateRepository->findTemplate(
$firstEntry, $secondLevelFirstEntry, 'en'
)->getFirst();
'contentLeft',
$templateLeft !== NULL ? $templateLeft->getContent() : ''
'subjectLeft',
$templateLeft !== NULL ? $templateLeft->getSubject() : ''
/** @var /SGalinski/SgMail/Domain/Model/Template $result */
$templateRight = $this->templateRepository->findTemplate(
$firstEntry, $secondLevelFirstEntry, 'en'
)->getFirst();
$this->view->assign(
'contentRight',
$templateRight !== NULL ? $templateRight->getContent() : ''
);
$this->view->assign(
'subjectRight',
$templateRight !== NULL ? $templateRight->getSubject() : ''
);
}

Torsten Oppermann
committed
/**
* Save content (left & right)
* if left & right is the same language, the right content will apply only if its another language

Torsten Oppermann
committed
*
* @param string $contentLeft
* @param string $contentRight
* @param string $selectedExtension
* @param string $selectedTemplate
* @param string $selectedLanguageLeft
* @param string $selectedLanguageRight
* @param string $subjectLeft
* @param string $subjectRight

Torsten Oppermann
committed
*/
public function saveAction(
$contentLeft = NULL, $contentRight = NULL, $selectedExtension = NULL, $selectedTemplate = NULL,
$selectedLanguageLeft = NULL, $selectedLanguageRight = NULL, $subjectLeft = NULL, $subjectRight = NULL

Torsten Oppermann
committed
) {
$this->saveTemplate($selectedExtension, $selectedTemplate, $selectedLanguageLeft, $contentLeft, $subjectLeft);

Torsten Oppermann
committed
if ($selectedLanguageLeft !== $selectedLanguageRight) {
$this->saveTemplate(
$selectedExtension, $selectedTemplate, $selectedLanguageRight, $contentRight, $subjectRight
);
}

Torsten Oppermann
committed
$message = LocalizationUtility::translate('backend.success', 'sg_mail');
$this->addFlashMessage($message, '', FlashMessage::OK);
$arguments = $this->request->getArguments();

Torsten Oppermann
committed
$this->redirect('index', NULL, NULL, $arguments);
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/**
* Save or update the template in the DB, depending if it already exists or not
*
* @param string $selectedExtension
* @param string $selectedTemplate
* @param string $selectedLanguage
* @param string $selectedContent
* @param string $selectedSubject
* @return Template $template
*/
private function saveTemplate(
$selectedExtension, $selectedTemplate, $selectedLanguage, $selectedContent, $selectedSubject
) {
/** @var Template $template */
$template = $this->templateRepository->findTemplate(
$selectedExtension, $selectedTemplate, $selectedLanguage
)->getFirst();
$templateAlreadyExists = TRUE;
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
if ($template === NULL) {
$templateAlreadyExists = FALSE;
$template = $objectManager->get(Template::class);
}
$template->setExtensionKey($selectedExtension);
$template->setTemplateName($selectedTemplate);
$template->setLanguage($selectedLanguage);
$template->setContent($selectedContent);
$template->setSubject($selectedSubject);
if ($templateAlreadyExists) {
$this->templateRepository->update($template);
} else {
$this->templateRepository->add($template);
}
return $template;
}
/**
* send a test email to a given address
* redirect to index action
*
* @param string $emailAddress
* @param string $selectedTemplateKey
* @param string $selectedExtensionKey
* @param string $selectedLanguageLeft
*/
public function sendTestMailAction(
$emailAddress, $selectedExtensionKey, $selectedTemplateKey, $selectedLanguageLeft
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
/** @var \SGalinski\SgMail\Service\MailTemplateService $mailTemplateService */
$mailTemplateService = $objectManager->get(MailTemplateService::class);
$mailTemplateService->setLanguage($selectedLanguageLeft);
$mailTemplateService->setToAddresses($emailAddress);
$mailTemplateService->setFromAddress($this->settings['mail']['test']['from']);
$mailTemplateService->setSubject($this->settings['mail']['test']['subject']);
$mailTemplateService->setTemplateName($selectedTemplateKey);
$mailTemplateService->setExtensionKey($selectedExtensionKey);

Torsten Oppermann
committed
if ($mailTemplateService->sendEmail()) {
$message = LocalizationUtility::translate('backend.success_mail', 'sg_mail');
$this->addFlashMessage($message, '', FlashMessage::OK);
} else {
$message = LocalizationUtility::translate('backend.failure_mail', 'sg_mail');
$this->addFlashMessage($message, '', FlashMessage::ERROR);
}
$arguments = $this->request->getArguments();
$this->redirect('index', NULL, NULL, $arguments);
}