Commit a5a994a1 authored by Tim Wagner's avatar Tim Wagner
Browse files

[TASK] Fix email template & layout rendering

parent 3c5058ba
...@@ -43,6 +43,11 @@ class Layout extends AbstractEntity { ...@@ -43,6 +43,11 @@ class Layout extends AbstractEntity {
*/ */
protected $content = ''; protected $content = '';
/**
* @var string
*/
protected $headContent = '';
/** /**
* @return string * @return string
*/ */
...@@ -70,4 +75,18 @@ class Layout extends AbstractEntity { ...@@ -70,4 +75,18 @@ class Layout extends AbstractEntity {
public function setContent($content) { public function setContent($content) {
$this->content = $content; $this->content = $content;
} }
/**
* @return string
*/
public function getHeadContent(): string {
return $this->headContent;
}
/**
* @param string $headContent
*/
public function setHeadContent(string $headContent) {
$this->headContent = $headContent;
}
} }
...@@ -26,6 +26,7 @@ namespace SGalinski\SgMail\Domain\Repository; ...@@ -26,6 +26,7 @@ namespace SGalinski\SgMail\Domain\Repository;
* This copyright notice MUST APPEAR in all copies of the script! * This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/ ***************************************************************/
use SGalinski\SgMail\Domain\Model\Layout;
use TYPO3\CMS\Core\Database\Connection; use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\ConnectionPool; use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction; use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
...@@ -60,7 +61,7 @@ class LayoutRepository extends Repository { ...@@ -60,7 +61,7 @@ class LayoutRepository extends Repository {
* @param int $uid * @param int $uid
* @param int $pid * @param int $pid
* @param int $languageUid * @param int $languageUid
* @return array|NULL * @return Layout
*/ */
public function findByUidOrDefault(int $uid, int $pid, int $languageUid = 0) { public function findByUidOrDefault(int $uid, int $pid, int $languageUid = 0) {
$query = $this->createQuery(); $query = $this->createQuery();
...@@ -70,18 +71,22 @@ class LayoutRepository extends Repository { ...@@ -70,18 +71,22 @@ class LayoutRepository extends Repository {
$query->setLimit(1); $query->setLimit(1);
if ($uid > 0) { if ($uid > 0) {
$result = $query->matching($query->equals('uid', $uid))->execute(TRUE)[0] ?? NULL; $result = $query->matching($query->equals('uid', $uid))->execute();
if ($result) { } else {
return $result; $result = $query->matching(
} $query->logicalAnd(
[
$query->equals('pid', $pid),
$query->equals('default', 1)
]
)
)->execute();
} }
return $query->matching( /** @var Layout $layout */
$query->logicalAnd([ $layout = $result->getFirst();
$query->equals('pid', $pid),
$query->equals('default', 1) return $layout;
])
)->execute(TRUE)[0] ?? NULL;
} }
/** /**
......
...@@ -27,6 +27,7 @@ namespace SGalinski\SgMail\Service; ...@@ -27,6 +27,7 @@ namespace SGalinski\SgMail\Service;
***************************************************************/ ***************************************************************/
use DateTime; use DateTime;
use SGalinski\SgMail\Domain\Model\Layout;
use SGalinski\SgMail\Domain\Model\Mail; use SGalinski\SgMail\Domain\Model\Mail;
use SGalinski\SgMail\Domain\Model\Template; use SGalinski\SgMail\Domain\Model\Template;
use SGalinski\SgMail\Domain\Repository\LayoutRepository; use SGalinski\SgMail\Domain\Repository\LayoutRepository;
...@@ -72,117 +73,94 @@ class MailTemplateService { ...@@ -72,117 +73,94 @@ class MailTemplateService {
* @var array * @var array
*/ */
private static $mailObjectCache = []; private static $mailObjectCache = [];
/**
* @var \SGalinski\SgMail\Domain\Repository\TemplateRepository
*/
protected $templateRepository;
/**
* @var \SGalinski\SgMail\Domain\Repository\LayoutRepository
*/
protected $layoutRepository;
/**
* @var \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager
*/
protected $persistenceManager;
/**
* @var \TYPO3\CMS\Extbase\Object\ObjectManager
*/
protected $objectManager;
/**
* @var TYPO3\CMS\Core\Resource\ResourceFactory
*/
protected $resourceFactory;
/** /**
* @var string $toAddresses * @var string $toAddresses
*/ */
private $toAddresses = ''; private $toAddresses = '';
/** /**
* @var string $fromAddress * @var string $fromAddress
*/ */
private $fromAddress = ''; private $fromAddress = '';
/** /**
* @var string $ccAddresses * @var string $ccAddresses
*/ */
private $ccAddresses; private $ccAddresses;
/** /**
* @var string $replyToAddress * @var string $replyToAddress
*/ */
private $replyToAddress = ''; private $replyToAddress = '';
/** /**
* @var string $language * @var string $language
*/ */
private $language = 'default'; private $language = 'default';
/** /**
* @var boolean $ignoreMailQueue * @var boolean $ignoreMailQueue
*/ */
private $ignoreMailQueue = FALSE; private $ignoreMailQueue = FALSE;
/** /**
* @var \TYPO3\CMS\Core\Mail\MailMessage $mailMessage * @var \TYPO3\CMS\Core\Mail\MailMessage $mailMessage
*/ */
private $mailMessage; private $mailMessage;
/** /**
* @var string $templateName * @var string $templateName
*/ */
private $templateName; private $templateName;
/** /**
* @var string $subject * @var string $subject
*/ */
private $subject; private $subject;
/** /**
* @var string $overwrittenEmailBody * @var string $overwrittenEmailBody
*/ */
private $overwrittenEmailBody = ''; private $overwrittenEmailBody = '';
/** /**
* @var string $extensionKey * @var string $extensionKey
*/ */
private $extensionKey; private $extensionKey;
/** /**
* @var array $markers * @var array $markers
*/ */
private $markers; private $markers;
/** /**
* @var array $markerLabels * @var array $markerLabels
*/ */
private $markerLabels; private $markerLabels;
/** /**
* @var string $bccAddresses * @var string $bccAddresses
*/ */
private $bccAddresses; private $bccAddresses;
/** /**
* @var int * @var int
*/ */
private $priority = Mail::PRIORITY_LOWEST; private $priority = Mail::PRIORITY_LOWEST;
/** /**
* @var int * @var int
*/ */
private $pid; private $pid;
/** /**
* @var string * @var string
*/ */
private $fromName = ''; private $fromName = '';
/**
* @var \SGalinski\SgMail\Domain\Repository\TemplateRepository
*/
protected $templateRepository;
/**
* @var \SGalinski\SgMail\Domain\Repository\LayoutRepository
*/
protected $layoutRepository;
/**
* @var \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager
*/
protected $persistenceManager;
/**
* @var \TYPO3\CMS\Extbase\Object\ObjectManager
*/
protected $objectManager;
/**
* @var TYPO3\CMS\Core\Resource\ResourceFactory
*/
protected $resourceFactory;
/** /**
* @var string * @var string
*/ */
...@@ -272,13 +250,6 @@ class MailTemplateService { ...@@ -272,13 +250,6 @@ class MailTemplateService {
} }
} }
/**
* @param string $fromName
*/
public function setFromName($fromName) {
$this->fromName = $fromName;
}
/** /**
* Provides translation for the marker data type * Provides translation for the marker data type
* *
...@@ -304,59 +275,32 @@ class MailTemplateService { ...@@ -304,59 +275,32 @@ class MailTemplateService {
} }
/** /**
* @param string $toAddresses * Return default markers for sg_mail
* @return MailTemplateService *
*/ * @param string $translationKey
public function setToAddresses($toAddresses): MailTemplateService { * @param array $marker
$normalizedToAddresses = trim(preg_replace('~\x{00a0}~iu', ' ', $toAddresses)); * @param string $extensionKey
$this->toAddresses = $normalizedToAddresses; * @return array
$addressesArray = GeneralUtility::trimExplode(',', $normalizedToAddresses, TRUE);
if (\count($addressesArray) > 1) {
$normalizedToAddresses = $addressesArray;
}
$this->mailMessage->setTo($normalizedToAddresses);
return $this;
}
/**
* @param string $fromAddress
* @param string $fromName
* @return MailTemplateService
*/
public function setFromAddress($fromAddress, $fromName = ''): MailTemplateService {
if ($fromAddress) {
$this->fromAddress = $fromAddress;
$this->mailMessage->setFrom($fromAddress, $fromName);
}
return $this;
}
/**
* @param string $ccAddresses
* @return MailTemplateService
*/ */
public function setCcAddresses($ccAddresses): MailTemplateService { public static function getDefaultTemplateMarker($translationKey, array $marker, $extensionKey = 'sg_mail'): array {
if ($ccAddresses) { $languagePath = 'LLL:EXT:' . $extensionKey . '/Resources/Private/Language/locallang.xlf:' . $translationKey;
$this->ccAddresses = $ccAddresses; // Need the key for translations
$this->mailMessage->setCc(GeneralUtility::trimExplode(',', $this->ccAddresses)); if (\trim($extensionKey) === '') {
return [];
} }
return $this; $generatedMarker = [];
} foreach ($marker as $markerName) {
$generatedMarker[] = [
/** 'marker' => $markerName,
* @param string $replyToAddress 'value' => $languagePath . '.example.' . $markerName,
* @return MailTemplateService 'description' => $languagePath . '.description.' . $markerName,
*/ 'backend_translation_key' => $translationKey . '.example.' . $markerName,
public function setReplyToAddress($replyToAddress): MailTemplateService { 'extension_key' => $extensionKey
if ($replyToAddress) { ];
$this->replyToAddress = $replyToAddress;
$this->mailMessage->setReplyTo($replyToAddress);
} }
return $this; return $generatedMarker;
} }
/** /**
...@@ -368,15 +312,6 @@ class MailTemplateService { ...@@ -368,15 +312,6 @@ class MailTemplateService {
return $this; return $this;
} }
/**
* @param boolean $ignoreMailQueue
* @return MailTemplateService
*/
public function setIgnoreMailQueue($ignoreMailQueue): MailTemplateService {
$this->ignoreMailQueue = $ignoreMailQueue;
return $this;
}
/** /**
* @param string $templateName * @param string $templateName
* @return MailTemplateService * @return MailTemplateService
...@@ -395,34 +330,6 @@ class MailTemplateService { ...@@ -395,34 +330,6 @@ class MailTemplateService {
return $this; return $this;
} }
/**
* @return string|string[]|null
*/
public function getMailBodyToSend() {
return $this->mailBodyToSend;
}
/**
* @param string|string[]|null $mailBodyToSend
*/
public function setMailBodyToSend($mailBodyToSend): void {
$this->mailBodyToSend = $mailBodyToSend;
}
/**
* @return string
*/
public function getSubjectToSend(): string {
return $this->subjectToSend;
}
/**
* @param string $subjectToSend
*/
public function setSubjectToSend(string $subjectToSend): void {
$this->subjectToSend = $subjectToSend;
}
/** /**
* @param array $markers * @param array $markers
* @return MailTemplateService * @return MailTemplateService
...@@ -448,19 +355,6 @@ class MailTemplateService { ...@@ -448,19 +355,6 @@ class MailTemplateService {
return $this; return $this;
} }
/**
* @param string $bccAddresses
* @return MailTemplateService
*/
public function setBccAddresses($bccAddresses): MailTemplateService {
if ($bccAddresses) {
$this->bccAddresses = $bccAddresses;
$this->mailMessage->setBcc(GeneralUtility::trimExplode(',', $this->bccAddresses));
}
return $this;
}
/** /**
* @param int $priority * @param int $priority
* @return MailTemplateService * @return MailTemplateService
...@@ -473,6 +367,7 @@ class MailTemplateService { ...@@ -473,6 +367,7 @@ class MailTemplateService {
/** /**
* IMPORTANT: make sure to set $this->>ignoreMailQueue(TRUE), if you're using this method to add an attachment! * IMPORTANT: make sure to set $this->>ignoreMailQueue(TRUE), if you're using this method to add an attachment!
* Otherwise the attachment is ignored when sending the mail via mail queue. * Otherwise the attachment is ignored when sending the mail via mail queue.
*
* @param Swift_OutputByteStream $data * @param Swift_OutputByteStream $data
* @param string $filename * @param string $filename
* @param string $contentType * @param string $contentType
...@@ -548,26 +443,6 @@ class MailTemplateService { ...@@ -548,26 +443,6 @@ class MailTemplateService {
return $this; return $this;
} }
/**
* Checks if a template is blacklisted for a given siteroot id
*
* @param string $extensionKey
* @param string $templateName
* @param int $siteRootId
* @return boolean
* @throws \InvalidArgumentException
* @throws \BadFunctionCallException
* @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
*/
public static function isTemplateBlacklisted($extensionKey, $templateName, $siteRootId): bool {
$nonBlacklistedTemplates = BackendService::getNonBlacklistedTemplates($siteRootId);
if ($nonBlacklistedTemplates[$extensionKey]) {
return $nonBlacklistedTemplates[$extensionKey][$templateName] ? FALSE : TRUE;
}
return TRUE;
}
/** /**
* @return string * @return string
*/ */
...@@ -582,49 +457,6 @@ class MailTemplateService { ...@@ -582,49 +457,6 @@ class MailTemplateService {
$this->subject = $subject; $this->subject = $subject;
} }
/**
* @return string
*/
public function getOverwrittenEmailBody(): string {
return $this->overwrittenEmailBody;
}
/**
* @param string $overwrittenEmailBody
*/
public function setOverwrittenEmailBody(string $overwrittenEmailBody) {
$this->overwrittenEmailBody = $overwrittenEmailBody;
}
/**
* Return default markers for sg_mail
*
* @param string $translationKey
* @param array $marker
* @param string $extensionKey
* @return array
*/
public static function getDefaultTemplateMarker($translationKey, array $marker, $extensionKey = 'sg_mail'): array {
$languagePath = 'LLL:EXT:' . $extensionKey . '/Resources/Private/Language/locallang.xlf:' . $translationKey;
// Need the key for translations
if (\trim($extensionKey) === '') {
return [];
}
$generatedMarker = [];
foreach ($marker as $markerName) {
$generatedMarker[] = [
'marker' => $markerName,
'value' => $languagePath . '.example.' . $markerName,
'description' => $languagePath . '.description.' . $markerName,
'backend_translation_key' => $translationKey . '.example.' . $markerName,
'extension_key' => $extensionKey
];
}
return $generatedMarker;
}
/** /**
* Set preview markers for the template editor * Set preview markers for the template editor
* *
...@@ -665,21 +497,86 @@ class MailTemplateService { ...@@ -665,21 +497,86 @@ class MailTemplateService {
} }
/** /**
* Parses markers in an email View. * Send the Email
* !!! CHANGES THE SOURCE PATH AND IT SHOULD BE RESET BACK TO THE ORIGINAL!!!
* *
* @param string $text * @param bool $isPreview
* @param StandaloneView $emailView * @param Template|null $template
* @return mixed * @return bool email was sent or added to mail queue successfully?
* @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
* @throws \TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException
*/ */
protected function parseMarkers($text, $emailView) { public function sendEmail($isPreview = FALSE, $isNewsletter = FALSE): bool {
$text = (string) $text; if ($isPreview) { //TODO: remove this from here
if (strpos($text, '{') !== FALSE) { $this->setIgnoreMailQueue(TRUE);
$emailView->setTemplateSource($text);
return $emailView->render();
} }
$success = FALSE;
return $text;