Skip to content
Snippets Groups Projects
Commit 72369dd2 authored by Georgi Mateev's avatar Georgi Mateev
Browse files

[BUGFIX] 1604 Fixed bugs after review

- Fixed PHPDoc
- Fixed the runtime template cache
- Fixed coding styles
- Changed preg_match to strpos for better performance
parent 27572acf
No related branches found
No related tags found
1 merge request!201584 add more placeholders to template editor fields
......@@ -332,7 +332,9 @@ class MailController extends ActionController {
if (\count($ccAddresses) > 0) {
foreach ($ccAddresses as $ccAddress) {
if (!preg_match('/{.*}/', $ccAddress) && !filter_var($ccAddress, FILTER_VALIDATE_EMAIL) && trim($ccAddress) !== '') {
if (!(strpos($ccAddress, '{') !== FALSE
&& strpos($ccAddress, '{') < strpos($ccAddress, '}'))
&& !filter_var($ccAddress, FILTER_VALIDATE_EMAIL) && trim($ccAddress) !== '') {
$message = LocalizationUtility::translate('backend.error_cc', 'sg_mail');
$this->addFlashMessage($message, '', FlashMessage::WARNING);
......@@ -344,7 +346,9 @@ class MailController extends ActionController {
$bccAddresses = GeneralUtility::trimExplode(',', $parameter['bcc']);
if (\count($bccAddresses) > 0) {
foreach ($bccAddresses as $bccAddress) {
if (!preg_match('/{.*}/', $bccAddress) && !filter_var($bccAddress, FILTER_VALIDATE_EMAIL) && trim($bccAddress) !== '') {
if (!(strpos($bccAddress, '{') !== FALSE
&& strpos($ccAddress, '{') < strpos($bccAddress, '}'))
&& !filter_var($bccAddress, FILTER_VALIDATE_EMAIL) && trim($bccAddress) !== '') {
$message = LocalizationUtility::translate('backend.error_bcc', 'sg_mail');
$this->addFlashMessage($message, '', FlashMessage::WARNING);
......
......@@ -391,7 +391,9 @@ class NewsletterController extends ActionController {
if (\count($ccAddresses) > 0) {
foreach ($ccAddresses as $ccAddress) {
if (!preg_match('/{.*}/', $ccAddress) && !filter_var($ccAddress, FILTER_VALIDATE_EMAIL) && trim($ccAddress) !== '') {
if (!(strpos($ccAddress, '{') !== FALSE
&& strpos($ccAddress, '{') < strpos($ccAddress, '}'))
&& !filter_var($ccAddress, FILTER_VALIDATE_EMAIL) && trim($ccAddress) !== '') {
$message = LocalizationUtility::translate('backend.error_cc', 'sg_mail');
$this->addFlashMessage($message, '', FlashMessage::WARNING);
......@@ -403,7 +405,9 @@ class NewsletterController extends ActionController {
$bccAddresses = GeneralUtility::trimExplode(',', $parameter['bcc']);
if (\count($bccAddresses) > 0) {
foreach ($bccAddresses as $bccAddress) {
if (!preg_match('/{.*}/', $bccAddress) && !filter_var($bccAddress, FILTER_VALIDATE_EMAIL) && trim($bccAddress) !== '') {
if (!(strpos($bccAddress, '{') !== FALSE
&& strpos($bccAddress, '{') < strpos($bccAddress, '}'))
&& !filter_var($bccAddress, FILTER_VALIDATE_EMAIL) && trim($bccAddress) !== '') {
$message = LocalizationUtility::translate('backend.error_bcc', 'sg_mail');
$this->addFlashMessage($message, '', FlashMessage::WARNING);
......
......@@ -64,9 +64,9 @@ class MailTemplateService {
const CONFIG_PATH = 'Configuration/MailTemplates';
/**
* @var Template
* @var array
*/
private static $template;
private static $templateObjectCache = [];
/**
* @var array
......@@ -212,22 +212,17 @@ class MailTemplateService {
* @var TYPO3\CMS\Core\Resource\ResourceFactory
*/
protected $resourceFactory;
/**
* @var string
*/
private $mailBodyToSend;
/**
* @var string
*/
private $subjectToSend;
/**
* @param Template $template
*/
public static function setTemplate(Template $template): void {
self::$template = $template;
}
/**
* @return string
*/
......@@ -838,6 +833,19 @@ class MailTemplateService {
return $pageUid;
}
/**
* Get the hash for the object cache
*
* @param $extensionKey
* @param $templateName
* @param $siteRootId
* @param $languageId
* @return string
*/
private function getTemplateHash($extensionKey, $templateName, $siteRootId, $languageId) {
return md5($extensionKey . '_' . $templateName . '_' . $siteRootId . '_' . $languageId);
}
/**
* Get the template object
*
......@@ -845,11 +853,17 @@ class MailTemplateService {
* @return null|object|Template|FALSE
* @throws \Exception
*/
public function getTemplate($siteRootId) {
private function getTemplate($siteRootId) {
$isTemplateBlacklisted = self::isTemplateBlacklisted($this->extensionKey, $this->templateName, $siteRootId);
if ($isTemplateBlacklisted) {
throw new \Exception('The template is blacklisted');
}
$templateHash = $this->getTemplateHash($this->extensionKey, $this->templateName, $siteRootId, $this->language);
if (isset(self::$templateObjectCache[$templateHash])
&& self::$templateObjectCache[$templateHash] instanceof Template) {
return self::$templateObjectCache[$templateHash];
}
/** @var Template $template */
$template = $this->templateRepository->findOneByTemplate(
$this->extensionKey, $this->templateName, $this->language, $siteRootId
......@@ -860,22 +874,24 @@ class MailTemplateService {
$this->extensionKey, $this->templateName, 'default', $siteRootId
);
}
self::$templateObjectCache[$templateHash] = $template;
return $template;
}
/**
* Get the default content for this template
*
* @param $template
* @param $registerService
* @param Template|null $template
* @param RegisterService $registerService
* @return bool|false|string
* @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
*/
protected function getDefaultTemplateContent($template, $registerService) {
$defaultTemplateContent =
$registerService->getRegisterArray()[$this->extensionKey][$this->templateName]['templateContent'];
// If there is no template for this language, use the default template
if (($template === NULL) && $defaultTemplateContent === NULL) {
if ($template === NULL && $defaultTemplateContent === NULL) {
$templatePath =
$registerService->getRegisterArray()[$this->extensionKey][$this->templateName]['templatePath'];
......@@ -906,11 +922,12 @@ class MailTemplateService {
* Sets the values to send the mail with from the template or register service
*
* @param Template|null $template
* @param $registerService
* @param $defaultTemplateContent
* @param $siteRootId
* @param RegisterService $registerService
* @param string $defaultTemplateContent
* @param int $siteRootId
* @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
*/
protected function extractValuesForMail($template, $registerService, $defaultTemplateContent, $siteRootId): void {
protected function extractValuesForMail($template, RegisterService $registerService, $defaultTemplateContent, $siteRootId): void {
/** @var StandaloneView $emailView */
$emailView = $this->objectManager->get(StandaloneView::class);
$emailView->assignMultiple($this->markers);
......@@ -1044,29 +1061,28 @@ class MailTemplateService {
// Get page ID
// TODO: this doesn't belong here. The API user needs to provide the UID
$siteRootId = $this->getSiteRootId();
if (self::$template === NULL) {
// Get template
try {
self::$template = $this->getTemplate($siteRootId);
} catch (\Exception $e) {
return FALSE;
}
try {
$template = $this->getTemplate($siteRootId);
} catch (\Exception $e) {
return FALSE;
}
$registerService = GeneralUtility::makeInstance(RegisterService::class);
// get default template content from register array
$defaultTemplateContent = $this->getDefaultTemplateContent(self::$template, $registerService);
$defaultTemplateContent = $this->getDefaultTemplateContent($template, $registerService);
// set the ToAddress if there are no placeholders in it
// TODO: does this belong here?
if (self::$template !== NULL && \filter_var(self::$template->getToAddress(), FILTER_VALIDATE_EMAIL)) {
$this->setToAddresses(\trim(self::$template->getToAddress()));
if ($template !== NULL && \filter_var($template->getToAddress(), FILTER_VALIDATE_EMAIL)) {
$this->setToAddresses(\trim($template->getToAddress()));
}
$this->extractValuesForMail(self::$template, $registerService, $defaultTemplateContent, $siteRootId);
$this->extractValuesForMail($template, $registerService, $defaultTemplateContent, $siteRootId);
$mail = $this->addMailToMailQueue(
$this->extensionKey, $this->templateName, $this->getSubjectToSend(), $this->getMailBodyToSend(), $this->priority,
$this->extensionKey, $this->templateName, $this->getSubjectToSend(), $this->getMailBodyToSend(),
$this->priority,
0, 0, $this->language, $siteRootId
);
self::$mailObjectCache[$mail->getUid()] = $mail; // add it to cache to avoid extra DB queries
......@@ -1211,7 +1227,9 @@ class MailTemplateService {
/**
* Get the mail object by uid and check if it's blacklisted
* @param $uid
*
* @param int $uid
* @return bool|object
*/
private function getMailObjectByUid($uid) {
$mailRepository = $this->objectManager->get(MailRepository::class);
......
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