Skip to content
Snippets Groups Projects
Commit 16e4cf73 authored by Torsten Oppermann's avatar Torsten Oppermann
Browse files

[TASK] Code refactorings, cgl, moving code to service

parent 05edfc9b
No related branches found
Tags 3.5.5
1 merge request!3New version 4 1
......@@ -190,53 +190,6 @@ class MailController extends ActionController {
$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 array $templateData
* @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, $templateData) {
$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($templateData['content']);
$template->setSubject($templateData['subject']);
$template->setFromName($templateData['fromName']);
$template->setFromMail($templateData['fromMail']);
$template->setCc($templateData['cc']);
$template->setBcc($templateData['bcc']);
$template->setReplyTo($templateData['replyTo']);
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
......@@ -250,7 +203,10 @@ class MailController extends ActionController {
*/
public function sendTestMailAction(array $parameters = []) {
foreach ($parameters['templates'] as $key => $template) {
$this->saveTemplate($parameters['selectedExtension'], $parameters['selectedTemplate'], $key, $template);
BackendService::saveTemplate(
(int) GeneralUtility::_GP('id'), $parameters['selectedExtension'], $parameters['selectedTemplate'],
$key, $template
);
}
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
......
......@@ -26,6 +26,7 @@ namespace SGalinski\SgMail\Service;
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use SGalinski\SgMail\Domain\Model\Template;
use SGalinski\SgMail\Domain\Repository\TemplateRepository;
use TYPO3\CMS\Backend\Template\Components\ButtonBar;
use TYPO3\CMS\Backend\Template\Components\DocHeaderComponent;
......@@ -243,7 +244,7 @@ class BackendService {
}
/**
* Get the template keys in an array suitable for filtering
* Get the template keys in an array suitable for filtering
*
* @return array
*/
......@@ -259,4 +260,54 @@ class BackendService {
return $templates;
}
/**
* Save or update the template in the DB, depending if it already exists or not
*
* @param int $pid
* @param string $selectedExtension
* @param string $selectedTemplate
* @param string $language
* @param array $templateData
* @return Template $template
* @throws \InvalidArgumentException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException
*/
public static function saveTemplate($pid, $selectedExtension, $selectedTemplate, $language, $templateData) {
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
/** @var TemplateRepository $templateRepository */
$templateRepository = $objectManager->get(TemplateRepository::class);
/** @var Template $template */
$template = $templateRepository->findOneByTemplate(
$selectedExtension, $selectedTemplate, $language, $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($language);
$template->setContent($templateData['content']);
$template->setSubject($templateData['subject']);
$template->setFromName($templateData['fromName']);
$template->setFromMail($templateData['fromMail']);
$template->setCc($templateData['cc']);
$template->setBcc($templateData['bcc']);
$template->setReplyTo($templateData['replyTo']);
if ($templateAlreadyExists) {
$templateRepository->update($template);
} else {
$templateRepository->add($template);
}
return $template;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment