<?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; use SGalinski\SgMail\Service\BackendService; use SGalinski\SgMail\Service\MailTemplateService; use SGalinski\SgMail\Session\PhpSession; use TYPO3\CMS\Backend\Template\Components\DocHeaderComponent; use TYPO3\CMS\Backend\Utility\BackendUtility; use TYPO3\CMS\Core\Messaging\FlashMessage; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Core\Utility\VersionNumberUtility; 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 { /** * DocHeaderComponent * * @var DocHeaderComponent */ protected $docHeaderComponent; /** * @var \TYPO3\CMS\Lang\Domain\Repository\LanguageRepository * @inject */ protected $languageRepository; /** * @var \SGalinski\SgMail\Domain\Repository\TemplateRepository * @inject */ protected $templateRepository; /** * @var \SGalinski\SgMail\Session\PhpSession */ protected $session; /** * Show template Selection and enable content input + mail preview * * @param array $parameters * @throws \InvalidArgumentException * @throws \UnexpectedValueException * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException * @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException */ public function indexAction(array $parameters = []) { $pid = (int) GeneralUtility::_GP('id'); if (!($this->session instanceof PhpSession)) { $this->session = $this->objectManager->get(PhpSession::class); $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'); } $languages = BackendService::getLanguages(); $this->view->assign('languages', $languages); $this->view->assign('templates', MailTemplateService::getRegisterArray()); // get language selection or read from cache or get default $selectedLanguages = BackendService::getSelectedLanguages( $parameters['selectedLanguageLeft'], $parameters['selectedLanguageRight'], $this->session, $languages ); if ($parameters['selectedTemplate'] === NULL || $parameters['selectedTemplate'] === '') { $parameters['selectedExtension'] = key($registerArray); $parameters['selectedTemplate'] = key($registerArray[$parameters['selectedExtension']]); } $selectedTemplates = BackendService::getSelectedTemplates( $parameters['selectedExtension'], $parameters['selectedTemplate'], $selectedLanguages, $this->session, $pid ); if ($selectedTemplates['left'] instanceof Template) { $this->view->assign('contentLeft', $selectedTemplates['left']->getContent()); $this->view->assign('fromNameLeft', $selectedTemplates['left']->getFromName()); $this->view->assign('fromMailLeft', $selectedTemplates['left']->getFromMail()); $this->view->assign('ccLeft', $selectedTemplates['left']->getCc()); $this->view->assign('bccLeft', $selectedTemplates['left']->getBcc()); $this->view->assign('replyToLeft', $selectedTemplates['left']->getReplyTo()); } else { $defaultTemplatePath = $registerArray[$parameters['selectedExtension']][$parameters['selectedTemplate']]['templatePath']; $defaultTemplateFile = $defaultTemplatePath . $selectedLanguages['left'] . '.' . 'template.html'; if (file_exists($defaultTemplateFile)) { $this->view->assign('contentLeft', file_get_contents($defaultTemplateFile)); } } if ($selectedTemplates['right'] instanceof Template) { $this->view->assign('contentRight', $selectedTemplates['right']->getContent()); $this->view->assign('fromNameRight', $selectedTemplates['right']->getFromName()); $this->view->assign('fromMailRight', $selectedTemplates['right']->getFromMail()); $this->view->assign('ccRight', $selectedTemplates['right']->getCc()); $this->view->assign('bccRight', $selectedTemplates['right']->getBcc()); $this->view->assign('replyToRight', $selectedTemplates['right']->getReplyTo()); } else { $defaultTemplatePath = $registerArray[$parameters['selectedExtension']][$parameters['selectedTemplate']]['templatePath']; $defaultTemplateFile = $defaultTemplatePath . $selectedLanguages['right'] . '.' . 'template.html'; if (file_exists($defaultTemplateFile)) { $this->view->assign('contentRight', file_get_contents($defaultTemplateFile)); } } $subject = $registerArray[$parameters['selectedExtension']][$parameters['selectedTemplate']]['subject']; if (is_array($subject)) { $this->view->assign( 'subjectLeft', $selectedTemplates['left'] !== NULL ? $selectedTemplates['left']->getSubject() : $registerArray[$parameters['selectedExtension']][$parameters['selectedTemplate']]['subject'][$selectedLanguages['left']] ); $this->view->assign( 'subjectRight', $selectedTemplates['right'] !== NULL ? $selectedTemplates['right']->getSubject() : $registerArray[$parameters['selectedExtension']][$parameters['selectedTemplate']]['subject'][$selectedLanguages['right']] ); } else { $translatedSubjects = BackendService::getTranslatedTemplateSubject( $subject, $parameters['selectedExtension'], $selectedLanguages ); $this->view->assign( 'subjectLeft', $selectedTemplates['left'] !== NULL ? $selectedTemplates['left']->getSubject() : $translatedSubjects['left'] ); $this->view->assign( 'subjectRight', $selectedTemplates['right'] !== NULL ? $selectedTemplates['right']->getSubject() : $translatedSubjects['right'] ); } $templateDescription = $registerArray[$parameters['selectedExtension']][$parameters['selectedTemplate']]['description']; if (is_array($templateDescription)) { $templateDescription = $templateDescription[$selectedLanguages['left']]; } else { $templateDescription = LocalizationUtility::translate( $templateDescription, $parameters['selectedExtension'] ); } // create doc header component $pageUid = (int) GeneralUtility::_GP('id'); $pageInfo = BackendUtility::readPageAccess($pageUid, $GLOBALS['BE_USER']->getPagePermsClause(1)); if ($pageInfo && $pageInfo['is_siteroot'] === '1') { $this->view->assign('templateDescription', $templateDescription); $this->view->assign( 'selectedTemplate', $registerArray[$parameters['selectedExtension']][$parameters['selectedTemplate']] ); $this->view->assign('selectedTemplateKey', $parameters['selectedTemplate']); $this->view->assign('selectedExtensionKey', $parameters['selectedExtension']); $this->view->assign('selectedLanguageLeft', $selectedLanguages['left']); $this->view->assign('selectedLanguageRight', $selectedLanguages['right']); $this->view->assign('mode', 'editor'); } else { $this->view->assign('pages', BackendService::getPages()); } $this->docHeaderComponent = GeneralUtility::makeInstance(DocHeaderComponent::class); $this->docHeaderComponent->setMetaInformation($pageInfo); BackendService::makeButtons($this->docHeaderComponent, $this->request); $this->view->assign('docHeader', $this->docHeaderComponent->docHeaderContent()); $this->view->assign('typo3Version', VersionNumberUtility::convertVersionNumberToInteger(TYPO3_version)); $this->view->assign('beUserMail', $GLOBALS['BE_USER']->user['email']); } /** * 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 * @throws \InvalidArgumentException * @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException * @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException */ private function saveTemplate( $selectedExtension, $selectedTemplate, $selectedLanguage, $selectedContent, $selectedSubject, $selectedFromName, $selectedFromMail, $selectedCc, $selectedBcc, $selectedReplyTo ) { $pid = (int) GeneralUtility::_GP('id'); /** @var Template $template */ $template = $this->templateRepository->findOneByTemplate( $selectedExtension, $selectedTemplate, $selectedLanguage, $pid ); $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 array $parameters * @throws \InvalidArgumentException * @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException * @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException * @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException */ public function sendTestMailAction(array $parameters = []) { $objectManager = GeneralUtility::makeInstance(ObjectManager::class); /** @var \SGalinski\SgMail\Service\MailTemplateService $mailTemplateService */ $mailTemplateService = $objectManager->get(MailTemplateService::class); $mailTemplateService->setLanguage($parameters['selectedLanguageLeft']); $mailTemplateService->setToAddresses($parameters['emailAddress']); $mailTemplateService->setFromAddress('noreply@example.org'); $mailTemplateService->setTemplateName($parameters['selectedTemplate']); $mailTemplateService->setExtensionKey($parameters['selectedExtension']); $mailIsSend = $mailTemplateService->sendEmail(TRUE); if ($parameters['selectedLanguageRight'] !== $parameters['selectedLanguageLeft']) { /** @var \SGalinski\SgMail\Service\MailTemplateService $mailTemplateService */ $mailTemplateService = $objectManager->get(MailTemplateService::class); $mailTemplateService->setLanguage($parameters['selectedLanguageRight']); $mailTemplateService->setToAddresses($parameters['emailAddress']); $mailTemplateService->setFromAddress('noreply@example.org'); $mailTemplateService->setTemplateName($parameters['selectedTemplate']); $mailTemplateService->setExtensionKey($parameters['selectedExtension']); $mailIsSend = $mailTemplateService->sendEmail(TRUE); } if ($mailIsSend) { $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); } $this->saveTemplate( $parameters['selectedExtension'], $parameters['selectedTemplate'], $parameters['selectedLanguageLeft'], $parameters['contentLeft'], $parameters['subjectLeft'], $parameters['fromNameLeft'], $parameters['fromMailLeft'], $parameters['ccLeft'], $parameters['bccLeft'], $parameters['replyToLeft'] ); if ($parameters['selectedLanguageleft'] !== $parameters['selectedLanguageRight']) { $this->saveTemplate( $parameters['selectedExtension'], $parameters['selectedTemplate'], $parameters['selectedLanguageRight'], $parameters['contentRight'], $parameters['subjectRight'], $parameters['fromNameRight'], $parameters['fromMailRight'], $parameters['ccRight'], $parameters['bccRight'], $parameters['replyToRight'] ); } $message = LocalizationUtility::translate('backend.success', 'sg_mail'); $this->addFlashMessage($message, '', FlashMessage::OK); $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) * * @param string $template * @param string $extensionKey * @param string $language * @throws \InvalidArgumentException * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException * @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException */ 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); } }