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 SGalinski\SgMail\Session\PhpSession;

Torsten Oppermann
committed
use TYPO3\CMS\Backend\Template\Components\ButtonBar;
use TYPO3\CMS\Backend\Template\Components\DocHeaderComponent;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Imaging\Icon;
use TYPO3\CMS\Core\Imaging\IconFactory;
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
/**
* DocHeaderComponent
*
* @var DocHeaderComponent
*/
protected $docHeaderComponent;

Torsten Oppermann
committed
/**
* @var \TYPO3\CMS\Lang\Domain\Repository\LanguageRepository
* @inject
*/
protected $languageRepository;
/**
* @var \SGalinski\SgMail\Domain\Repository\TemplateRepository
* @inject
*/
protected $templateRepository;
* @var \SGalinski\SgMail\Session\PhpSession
* 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
) {
if (!($this->session instanceof PhpSession)) {
$this->session = $this->objectManager->get('SGalinski\SgMail\Session\PhpSession');
$this->session->setSessionKey('sg_mail_controller_session');
}
$registerArray = MailTemplateService::getRegisterArray();
// if no extensions are registered, redirect to empty action
if (empty($registerArray)) {
$this->redirect('empty');
}

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());
// get last selected languages
if ($selectedLanguageLeft === NULL) {
$selectedLanguageLeft = $this->session->getDataByKey('selectedLanguageLeft');
if ($selectedLanguageLeft === NULL) {
$selectedLanguageLeft = $languages[0];

Torsten Oppermann
committed
}
if ($selectedLanguageRight === NULL) {
$selectedLanguageRight = $this->session->getDataByKey('selectedLanguageRight');
if ($selectedLanguageRight === NULL) {
if (isset($languages[1])) {
$selectedLanguageRight = $languages[1];
} else {
$selectedLanguageRight = $languages[0];
if ($selectedTemplate === NULL || $selectedTemplate === '') {
$selectedExtension = key($registerArray);
$selectedTemplate = key($registerArray[$selectedExtension]);
$selectedTemplateArray = $registerArray[$selectedExtension][$selectedTemplate];
$this->session->setDataByKey('selectedLanguageLeft', $selectedLanguageLeft);
$this->session->setDataByKey('selectedLanguageRight', $selectedLanguageRight);
$templateLeft = $this->templateRepository->findOneByTemplate(
$selectedExtension, $selectedTemplate, $selectedLanguageLeft
);
$templateRight = $this->templateRepository->findOneByTemplate(

Torsten Oppermann
committed
$selectedExtension, $selectedTemplate, $selectedLanguageRight
if ($templateLeft !== NULL) {
$this->view->assign('contentLeft', $templateLeft->getContent());

Torsten Oppermann
committed
$this->view->assign('fromNameLeft', $templateLeft->getFromName());
$this->view->assign('fromMailLeft', $templateLeft->getFromMail());
$this->view->assign('ccLeft', $templateLeft->getCc());
$this->view->assign('bccLeft', $templateLeft->getBcc());
$this->view->assign('replyToLeft', $templateLeft->getReplyTo());

Torsten Oppermann
committed
} else {
$defaultTemplatePath = $registerArray[$selectedExtension][$selectedTemplate]['templatePath'];
$defaultTemplateFile = $defaultTemplatePath . $selectedLanguageLeft . '.' . 'template.html';
if (file_exists($defaultTemplateFile)) {
$this->view->assign('contentLeft', file_get_contents($defaultTemplateFile));
if ($templateRight !== NULL) {
$this->view->assign('contentRight', $templateRight->getContent());

Torsten Oppermann
committed
$this->view->assign('fromNameRight', $templateRight->getFromName());
$this->view->assign('fromMailRight', $templateRight->getFromMail());
$this->view->assign('ccRight', $templateRight->getCc());
$this->view->assign('bccRight', $templateRight->getBcc());
$this->view->assign('replyToRight', $templateRight->getReplyTo());
$defaultTemplatePath = $registerArray[$selectedExtension][$selectedTemplate]['templatePath'];
$defaultTemplateFile = $defaultTemplatePath . $selectedLanguageRight . '.' . 'template.html';
if (file_exists($defaultTemplateFile)) {
$this->view->assign('contentRight', file_get_contents($defaultTemplateFile));
}
}

Torsten Oppermann
committed
$subject = $registerArray[$selectedExtension][$selectedTemplate]['subject'];

Torsten Oppermann
committed
if (is_array($subject)) {
$this->view->assign(
'subjectLeft', $templateLeft !== NULL ? $templateLeft->getSubject() :
$registerArray[$selectedExtension][$selectedTemplate]['subject'][$selectedLanguageLeft]
);
$this->view->assign(
'subjectRight', $templateRight !== NULL ? $templateRight->getSubject() :
$registerArray[$selectedExtension][$selectedTemplate]['subject'][$selectedLanguageRight]
);
} else {
$langFile = GeneralUtility::readLLfile(
'EXT:' . $selectedExtension . '/Resources/Private/Language/locallang.xlf', $selectedLanguageLeft
);
$translatedSubject = '';
if ($langFile[$selectedLanguageLeft][$subject][0]) {
$translatedSubject = $langFile[$selectedLanguageLeft][$subject][0]['target'];
} else {
$translatedSubject = $langFile['default'][$subject][0]['target'];
}

Torsten Oppermann
committed
$this->view->assign(
'subjectLeft', $templateLeft !== NULL ? $templateLeft->getSubject() :

Torsten Oppermann
committed
);
$langFile = GeneralUtility::readLLfile(
'EXT:' . $selectedExtension . '/Resources/Private/Language/locallang.xlf', $selectedLanguageRight
);
$translatedSubject = '';
if ($langFile[$selectedLanguageLeft][$subject][0][$subject]) {
$translatedSubject = $langFile[$selectedLanguageRight][$subject][0]['target'];
} else {
$translatedSubject = $langFile['default'][$subject][0]['target'];
}

Torsten Oppermann
committed
$this->view->assign(
'subjectRight', $templateRight !== NULL ? $templateRight->getSubject() :

Torsten Oppermann
committed
);
}

Torsten Oppermann
committed
$templateDescription = $registerArray[$selectedExtension][$selectedTemplate]['description'];
if (is_array($templateDescription)) {
$templateDescription = $templateDescription[$selectedLanguageLeft];
} else {
$templateDescription = LocalizationUtility::translate(
$templateDescription, $selectedExtension
);
}

Torsten Oppermann
committed
// create doc header component
$pageUid = (int) GeneralUtility::_GP('id');
$pageInfo = BackendUtility::readPageAccess($pageUid, $GLOBALS['BE_USER']->getPagePermsClause(1));
$this->docHeaderComponent = GeneralUtility::makeInstance(DocHeaderComponent::class);
$this->docHeaderComponent->setMetaInformation($pageInfo);
$this->makeButtons();
$this->view->assign('docHeader', $this->docHeaderComponent->docHeaderContent());

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

Torsten Oppermann
committed
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/**
* create buttons for the backend module header
*/
private function makeButtons() {
/** @var ButtonBar $buttonBar */
$buttonBar = $this->docHeaderComponent->getButtonBar();
/** @var IconFactory $iconFactory */
$iconFactory = GeneralUtility::makeInstance(IconFactory::class);
// Refresh
$refreshButton = $buttonBar->makeLinkButton()
->setHref(GeneralUtility::getIndpEnv('REQUEST_URI'))
->setTitle(LocalizationUtility::translate('LLL:EXT:lang/locallang_core.xlf:labels.reload', ''))
->setIcon($iconFactory->getIcon('actions-refresh', Icon::SIZE_SMALL));
$buttonBar->addButton($refreshButton, ButtonBar::BUTTON_POSITION_RIGHT);
// shortcut button
$shortcutButton = $buttonBar->makeShortcutButton()
->setModuleName($this->request->getPluginName())
->setGetVariables(
[
'id',
'M'
]
)
->setSetVariables([]);
$buttonBar->addButton($shortcutButton, ButtonBar::BUTTON_POSITION_RIGHT);
$this->docHeaderComponent->getButtonBar();
}

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
* @param string $fromNameLeft
* @param string $fromMailLeft
* @param string $ccLeft
* @param string $bccLeft
* @param string $replyToLeft
* @param string $fromNameRight
* @param string $fromMailRight
* @param string $ccRight
* @param string $bccRight
* @param string $replyToRight

Torsten Oppermann
committed
*/
public function saveAction(
$contentLeft = NULL, $contentRight = NULL, $selectedExtension = NULL, $selectedTemplate = NULL,
$selectedLanguageLeft = NULL, $selectedLanguageRight = NULL, $subjectLeft = NULL, $subjectRight = NULL,
$fromNameLeft = NULL, $fromMailLeft = NULL, $ccLeft = NULL, $bccLeft = NULL, $replyToLeft = NULL,
$fromNameRight = NULL, $fromMailRight = NULL, $ccRight = NULL, $bccRight = NULL, $replyToRight = NULL

Torsten Oppermann
committed
) {

Torsten Oppermann
committed
$this->saveTemplate(
$selectedExtension, $selectedTemplate, $selectedLanguageLeft, $contentLeft, $subjectLeft,
$fromNameLeft, $fromMailLeft, $ccLeft, $bccLeft, $replyToLeft
);
if ($selectedLanguageLeft !== $selectedLanguageRight) {
$this->saveTemplate(
$selectedExtension, $selectedTemplate, $selectedLanguageRight, $contentRight, $subjectRight,
$fromNameRight, $fromMailRight, $ccRight, $bccRight, $replyToRight
$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);
/**
* 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
* @param string $selectedFromName
* @param string $selectedFromMail
* @param string $selectedCc
* @param string $selectedBcc
* @param string $selectedReplyTo
* @return Template $template
*/
private function saveTemplate(
$selectedExtension, $selectedTemplate, $selectedLanguage, $selectedContent, $selectedSubject,
$selectedFromName, $selectedFromMail, $selectedCc, $selectedBcc, $selectedReplyTo
) {
/** @var Template $template */
$template = $this->templateRepository->findOneByTemplate(
$selectedExtension, $selectedTemplate, $selectedLanguage
$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);
$template->setFromName($selectedFromName);
$template->setFromMail($selectedFromMail);
$template->setCc($selectedCc);
$template->setBcc($selectedBcc);
$template->setReplyTo($selectedReplyTo);
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

Torsten Oppermann
committed
* @param string $selectedLanguageRight
*/
public function sendTestMailAction(
$emailAddress, $selectedExtensionKey, $selectedTemplateKey, $selectedLanguageLeft, $selectedLanguageRight
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
/** @var \SGalinski\SgMail\Service\MailTemplateService $mailTemplateService */
$mailTemplateService = $objectManager->get(MailTemplateService::class);
$mailTemplateService->setLanguage($selectedLanguageLeft);
$mailTemplateService->setToAddresses($emailAddress);
$mailTemplateService->setFromAddress('noreply@example.org');
$mailTemplateService->setTemplateName($selectedTemplateKey);
$mailTemplateService->setExtensionKey($selectedExtensionKey);
$mailIsSend = $mailTemplateService->sendEmail(TRUE);

Torsten Oppermann
committed
if ($selectedLanguageRight !== $selectedLanguageLeft) {
/** @var \SGalinski\SgMail\Service\MailTemplateService $mailTemplateService */
$mailTemplateService = $objectManager->get(MailTemplateService::class);
$mailTemplateService->setLanguage($selectedLanguageRight);
$mailTemplateService->setToAddresses($emailAddress);
$mailTemplateService->setFromAddress('noreply@example.org');

Torsten Oppermann
committed
$mailTemplateService->setTemplateName($selectedTemplateKey);
$mailTemplateService->setExtensionKey($selectedExtensionKey);
$mailIsSend = $mailTemplateService->sendEmail(TRUE);

Torsten Oppermann
committed
}

Torsten Oppermann
committed
if ($mailIsSend) {

Torsten Oppermann
committed
$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);
}
* show a notice when no extension is registered
*/
public function emptyAction() {
}
* reset template values to default state for a specific language (delete from db)
public function resetAction($template, $extensionKey, $language = '') {
$this->templateRepository->deleteTemplate($extensionKey, $template, $language);
$message = LocalizationUtility::translate('backend.template_reset', 'sg_mail');
$this->addFlashMessage($message, '', FlashMessage::OK);
$arguments = $this->request->getArguments();
$this->redirect('index', NULL, NULL, $arguments);
}