Commit c3e33b1d authored by Kevin Ditscheid's avatar Kevin Ditscheid
Browse files

[BUGFIX] Fix broken mail creation and sending

parent 35b38b3f
......@@ -125,7 +125,6 @@ class MailController extends AbstractController {
// if no templates are in the db, get the default from the files
$templates = [];
$subject = $registerArray[$parameters['selectedExtension']][$parameters['selectedTemplate']]['subject'];
foreach ($languages as $language) {
$template = NULL;
foreach ($templatesFromDb as $_template) {
......@@ -136,31 +135,21 @@ class MailController extends AbstractController {
}
if ($template === NULL) {
$defaultTemplatePath = $registerArray[$parameters['selectedExtension']][$parameters['selectedTemplate']]['templatePath'];
$fallbackTemplateFile = $defaultTemplatePath . 'template.html';
$templateFromFile = GeneralUtility::makeInstance(Template::class);
$templateFromFile->setExtensionKey($parameters['selectedExtension']);
$templateFromFile->setTemplateName($parameters['selectedTemplate']);
$templateFromFile->setSiteLanguage($language);
$templates[$language->getLanguageId()] = $templateFromFile;
if (\strpos($subject, 'LLL:') === 0) {
$subject = LocalizationUtility::translate($subject, NULL, NULL, $language->getTypo3Language());
}
$templateFromFile->setSubject($subject);
$subject = $registerArray[$parameters['selectedExtension']][$parameters['selectedTemplate']]['subject'];
if ($registerArray[$parameters['selectedExtension']][$parameters['selectedTemplate']]['templateContent']) {
$templateFromFile->setContent(
$registerArray[$parameters['selectedExtension']][$parameters['selectedTemplate']]['templateContent']
);
} elseif (file_exists($fallbackTemplateFile)) {
$templateFromFile->setContent(file_get_contents($fallbackTemplateFile));
}
$templateFromFile = $this->registerService->findTemplate(
$parameters['selectedExtension'],
$parameters['selectedTemplate'],
$language
);
$template = GeneralUtility::makeInstance(Template::class);
$this->templateRepository->fillTemplate(
$template,
$templateFromFile
);
} else {
$templates[$language->getLanguageId()] = $template;
$template->setIsOverwritten(TRUE);
}
$templates[$language->getLanguageId()] = $template;
}
// calculating optimal column width for the view
......
......@@ -157,34 +157,21 @@ class NewsletterController extends AbstractController {
// if no templates are in the db, get the default from the files
$templates = [];
$subject = $arguments['subject'] ?? $registerArray[$parameters['selectedExtension']][$parameters['selectedTemplate']]['subject'];
/** @var Template $template */
foreach ($templatesFromDb as $template) {
if ($template === NULL) {
$defaultTemplatePath = $registerArray[$parameters['selectedExtension']][$parameters['selectedTemplate']]['templatePath'];
$fallbackTemplateFile = $defaultTemplatePath . 'template.html';
$templateFromFile = new Template();
$templateFromFile->setSiteLanguage($selectedLanguage);
$templates[$selectedLanguage->getLanguageId()] = $templateFromFile;
if (\strpos($subject, 'LLL:') === 0) {
$subject = LocalizationUtility::translate($subject, NULL, NULL, $selectedLanguage->getTypo3Language());
}
$templateFromFile->setSubject($subject);
$subject = $arguments['subject'] ?? $registerArray[$parameters['selectedExtension']][$parameters['selectedTemplate']]['subject'];
if ($registerArray[$parameters['selectedExtension']][$parameters['selectedTemplate']]['templateContent']) {
$templateFromFile->setContent(
$registerArray[$parameters['selectedExtension']][$parameters['selectedTemplate']]['templateContent']
);
} elseif (file_exists($fallbackTemplateFile)) {
$templateFromFile->setContent(file_get_contents($fallbackTemplateFile));
}
$templateFromFile = $this->registerService->findTemplate(
$parameters['selectedExtension'],
$parameters['selectedTemplate'],
$selectedLanguage
);
$template = GeneralUtility::makeInstance(Template::class);
$this->templateRepository->fillTemplate($template, $templateFromFile);
} else {
$templates[$selectedLanguage->getLanguageId()] = $template;
$template->setIsOverwritten(TRUE);
}
$templates[$selectedLanguage->getLanguageId()] = $template;
if (!empty($arguments['content'])) {
$templates[$selectedLanguage->getLanguageId()]->setContent($arguments['content']);
}
......@@ -243,7 +230,6 @@ class NewsletterController extends AbstractController {
}
// create doc header component
$pageInfo = BackendUtility::readPageAccess($this->site->getRootPageId(), $GLOBALS['BE_USER']->getPagePermsClause(1));
$layouts = $this->layoutRepository->findByPidForModule($this->site->getRootPageId());
$layoutOptions = [
0 => LocalizationUtility::translate('backend.layout.default', 'SgMail'),
......
......@@ -153,7 +153,6 @@ class QueueController extends AbstractController {
$this->redirect('index', NULL, NULL, $this->request->getArguments());
}
if ($mail->getStatus() !== Mail::STATUS_ERROR) {
$this->addFlashMessage(
LocalizationUtility::translate('backend.success_mail_queue', 'sg_mail'),
......
......@@ -68,15 +68,14 @@ class LayoutRepository extends Repository {
* @param int $uid
* @param int $pid
* @param int $languageUid
* @return Layout
* @return null|Layout
*/
public function findByUidOrDefault(int $uid, int $pid, int $languageUid = 0): Layout {
public function findByUidOrDefault(int $uid, int $pid, int $languageUid = 0): ?Layout {
$query = $this->createQuery();
$querySettings = $query->getQuerySettings();
$querySettings->setLanguageUid($languageUid);
$query->setQuerySettings($querySettings);
$query->setLimit(1);
if ($uid > 0) {
$result = $query->matching($query->equals('uid', $uid))->execute();
} else {
......
......@@ -127,17 +127,42 @@ class TemplateRepository extends AbstractRepository {
* @param array $templateData
*/
public function fillTemplate(Template $template, array $templateData): void {
$template->setExtensionKey($templateData['extensionKey']);
if ($templateData['extensionKey']) {
$template->setExtensionKey($templateData['extensionKey']);
} else {
$template->setExtensionKey($templateData['extension']);
}
$template->setTemplateName($templateData['templateName']);
$template->setLanguageUid($templateData['sys_language_uid']);
$template->setLayout($templateData['layout']);
if ($templateData['layout']) {
$template->setLayout($templateData['layout']);
}
$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']);
$template->setToAddress($templateData['toAddress']);
if ($templateData['fromName']) {
$template->setFromName($templateData['fromName']);
}
if ($templateData['fromName']) {
$template->setFromMail($templateData['fromMail']);
}
if ($templateData['cc']) {
$template->setCc($templateData['cc']);
}
if ($templateData['bcc']) {
$template->setBcc($templateData['bcc']);
}
if ($templateData['replyTo']) {
$template->setReplyTo($templateData['replyTo']);
}
if ($templateData['toAddress']) {
$template->setToAddress($templateData['toAddress']);
}
}
}
<?php
namespace SGalinski\SgMail\Exceptions;
use TYPO3\CMS\Core\Exception;
/**
* Class TemplateNotFoundException
*
* @package SGalinski\SgMail\Exceptions
*/
class TemplateNotFoundException extends Exception {
}
......@@ -441,7 +441,7 @@ class MailTemplateService implements SingletonInterface {
$this->extensionKey, $this->templateName, [$this->siteLanguage], $this->pid
)->getFirst();
if ($template === NULL) {
if ($template === NULL && !empty($this->siteLanguage->getFallbackLanguageIds())) {
$site = GeneralUtility::makeInstance(SiteFinder::class)->getSiteByPageId($this->pid);
$fallbackLanguages = [];
foreach ($this->siteLanguage->getFallbackLanguageIds() as $fallbackLanguageId) {
......@@ -451,6 +451,13 @@ class MailTemplateService implements SingletonInterface {
$template = $this->getTemplateRepository()->findByTemplateProperties(
$this->extensionKey, $this->templateName, $fallbackLanguages, $this->pid
)->getFirst();
} elseif($template === NULL) {
$templateArray = $this->registerService->findTemplate(
$this->extensionKey,
$this->templateName,
$this->siteLanguage
);
$template = $this->getTemplateRepository()->create($templateArray);
}
self::$templateObjectCache[$templateHash] = $template;
......@@ -566,6 +573,10 @@ class MailTemplateService implements SingletonInterface {
return FALSE;
}
}
} elseif ($template !== NULL) {
$defaultTemplateContent = $template->getContent();
} else {
$defaultTemplateContent = '';
}
return $defaultTemplateContent;
......
......@@ -28,6 +28,8 @@ namespace SGalinski\SgMail\Service;
use BadFunctionCallException;
use InvalidArgumentException;
use SGalinski\SgMail\Domain\Model\Template;
use SGalinski\SgMail\Exceptions\TemplateNotFoundException;
use TYPO3\CMS\Core\Cache\Backend\Typo3DatabaseBackend;
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException;
......@@ -35,11 +37,13 @@ use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
use TYPO3\CMS\Core\Cache\Frontend\VariableFrontend;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\SingletonInterface;
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
use TYPO3\CMS\Extbase\Object\Exception;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
use function count;
use function file_exists;
use function file_put_contents;
......@@ -105,6 +109,46 @@ class RegisterService implements SingletonInterface {
return $registerArray;
}
/**
* Find a template from the registry
*
* @param string $extKey
* @param string $templateName
* @param SiteLanguage $siteLanguage
* @return array
* @throws NoSuchCacheException
* @throws TemplateNotFoundException
*/
public function findTemplate(string $extKey, string $templateName, SiteLanguage $siteLanguage): array {
$registerArray = $this->getRegisterArray();
if (!isset($registerArray[$extKey][$templateName])) {
throw new TemplateNotFoundException(
'The template "%s" is not registered',
1620111957029,
['extensionKey' => $extKey, 'templateName' => $templateName]
);
}
$template = $registerArray[$extKey][$templateName];
$template['sys_language_uid'] = $siteLanguage->getLanguageId();
if (\strpos($template['subject'], 'LLL:') === 0) {
$template['subject'] = LocalizationUtility::translate(
$template['subject'],
NULL,
NULL,
$siteLanguage->getTypo3Language()
);
}
if ($template['templateContent']) {
$template['content'] = $template['templateContent'];
} elseif (file_exists($template['templatePath'] . 'template.html')) {
$template['content'] = file_get_contents($template['templatePath'] . 'template.html');
}
return $template;
}
/**
* Filter the register array to have only whitelisted templates for this domain
*
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment