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

[TASK] Refactoring Mailcontroller for new db logic

parent 82153d5e
No related branches found
No related tags found
No related merge requests found
......@@ -26,6 +26,8 @@ namespace SGalinski\SgMail\Controller;
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use SGalinski\SgMail\Domain\Model\Template;
use SGalinski\SgMail\Domain\Repository\TemplateRepository;
use SGalinski\SgMail\Service\MailTemplateService;
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Core\Utility\GeneralUtility;
......@@ -44,6 +46,12 @@ class MailController extends ActionController {
*/
protected $languageRepository = NULL;
/**
* @var \SGalinski\SgMail\Domain\Repository\TemplateRepository
* @inject
*/
protected $templateRepository = NULL;
/**
* Show template Selection and enable content input + mail preview
*
......@@ -72,10 +80,6 @@ class MailController extends ActionController {
$languages[] = $language->getLocale();
}
foreach ($languages as $currentLanguage) {
$this->writeLanguageFile($currentLanguage, $templatePath);
}
$this->view->assign('languages', $languages);
$this->view->assign('templates', MailTemplateService::getRegisterArray());
......@@ -83,7 +87,6 @@ class MailController extends ActionController {
&& !isset($selectedLanguageLeft)
&& !isset($selectedLanguageRight)
) {
$selectedTemplateArray = $registerArray[$selectedExtension][$selectedTemplate];
$this->view->assign('selectedTemplate', $selectedTemplateArray);
......@@ -116,48 +119,72 @@ class MailController extends ActionController {
$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(
'contentLeft',
file_get_contents($templatePath . $selectedLanguageLeft . '.sg_mail.locallang.html')
$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(
'contentRight',
file_get_contents($templatePath . $selectedLanguageRight . '.sg_mail.locallang.html')
$templateRight !== NULL ? $templateRight->getContent() : ''
);
$this->view->assign(
'subjectRight',
$templateRight !== NULL ? $templateRight->getSubject() : ''
);
} else {
$this->view->assign('selectedLanguageLeft', 'en');
/** @var /SGalinski/SgMail/Domain/Model/Template $result */
$templateLeft = $this->templateRepository->findTemplate(
$firstEntry, $secondLevelFirstEntry, 'en'
)->getFirst();
$this->view->assign(
'contentLeft', file_get_contents($templatePath . 'en.sg_mail.locallang.html')
'contentLeft',
$templateLeft !== NULL ? $templateLeft->getContent() : ''
);
$this->view->assign('selectedLanguageRight', 'en');
$this->view->assign(
'contentRight', file_get_contents($templatePath . 'en.sg_mail.locallang.html')
'subjectLeft',
$templateLeft !== NULL ? $templateLeft->getSubject() : ''
);
}
}
/**
*
* creates a language File for this Template
*
* @param string $language
* @param string $path
*/
private function writeLanguageFile($language, $path) {
$currentLangFile = $path . $language . '.sg_mail.locallang.html';
/** @var /SGalinski/SgMail/Domain/Model/Template $result */
$templateRight = $this->templateRepository->findTemplate(
$firstEntry, $secondLevelFirstEntry, 'en'
)->getFirst();
if (file_exists($currentLangFile)) {
return;
}
$this->view->assign(
'contentRight',
$templateRight !== NULL ? $templateRight->getContent() : ''
);
$langFileContent = 'Auto Generated for Language: ' . $language;
file_put_contents($currentLangFile, $langFileContent);
$this->view->assign(
'subjectRight',
$templateRight !== NULL ? $templateRight->getSubject() : ''
);
}
}
/**
* Save content (left & right)
* if left & right is the same language, the right content will apply
* if left & right is the same language, the right content will apply only if its another language
*
* @param string $contentLeft
* @param string $contentRight
......@@ -165,19 +192,20 @@ class MailController extends ActionController {
* @param string $selectedTemplate
* @param string $selectedLanguageLeft
* @param string $selectedLanguageRight
* @param string $subjectLeft
* @param string $subjectRight
*/
public function saveAction(
$contentLeft = NULL, $contentRight = NULL, $selectedExtension = NULL, $selectedTemplate = NULL,
$selectedLanguageLeft = NULL, $selectedLanguageRight = NULL
$selectedLanguageLeft = NULL, $selectedLanguageRight = NULL, $subjectLeft = NULL, $subjectRight = NULL
) {
$this->saveTemplate($selectedExtension, $selectedTemplate, $selectedLanguageLeft, $contentLeft, $subjectLeft);
$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);
if ($selectedLanguageLeft !== $selectedLanguageRight) {
$this->saveTemplate(
$selectedExtension, $selectedTemplate, $selectedLanguageRight, $contentRight, $subjectRight
);
}
$message = LocalizationUtility::translate('backend.success', 'sg_mail');
$this->addFlashMessage($message, '', FlashMessage::OK);
......@@ -186,6 +214,46 @@ class MailController extends ActionController {
$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
* @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;
}
/**
* Returns the Path for this 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