Commit cba83a0c authored by Stefan Galinski's avatar Stefan Galinski 🎮
Browse files

[BUGFIX] Cleanups, Newly registered form mail templates missed the proper extension key

parent 4d24df58
......@@ -81,9 +81,9 @@ class FormsFinisher extends AbstractFinisher {
continue;
}
$formElemenProperties = $formElement->getProperties();
if (isset($formElemenProperties['markerName']) && \trim($formElemenProperties['markerName']) !== '') {
$markers[\trim($formElemenProperties['markerName'])] = $value;
$formElementProperties = $formElement->getProperties();
if (isset($formElementProperties['markerName']) && \trim($formElementProperties['markerName']) !== '') {
$markers[\trim($formElementProperties['markerName'])] = $value;
} else {
$markers[$identifier] = $value;
}
......@@ -94,19 +94,18 @@ class FormsFinisher extends AbstractFinisher {
$templateName = $formDefinition->getIdentifier();
}
$ignoreMailQueue = (boolean) $this->parseOption('ignoreMailQueue');
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
$mailTemplateService = $objectManager->get(
MailTemplateService::class, $templateName, 'sg_mail', $markers
);
$ignoreMailQueue = (boolean) $this->parseOption('ignoreMailQueue');
$mailTemplateService->setIgnoreMailQueue($ignoreMailQueue);
$mailTemplateService->setLanguage($GLOBALS['TSFE']->config['config']['language']);
$mailToAdresses = trim((string) $this->parseOption('mailTo'));
if ($mailToAdresses !== '') {
$mailTemplateService->setToAddresses($this->parseOption('mailTo'));
$mailToAddresses = trim((string) $this->parseOption('mailTo'));
if ($mailToAddresses !== '') {
$mailTemplateService->setToAddresses($mailToAddresses);
}
$fromAddress = trim((string) $this->parseOption('mailFrom'));
......
......@@ -431,7 +431,9 @@ class BackendService {
}
// filter out excluded templates from all domains
if (isset($extensionConfiguration['excludeTemplatesAllDomains']) && $extensionConfiguration['excludeTemplatesAllDomains'] !== '') {
if (isset($extensionConfiguration['excludeTemplatesAllDomains']) &&
$extensionConfiguration['excludeTemplatesAllDomains'] !== ''
) {
$excludedTemplates = GeneralUtility::trimExplode(
',', $extensionConfiguration['excludeTemplatesAllDomains'], TRUE
);
......
......@@ -62,24 +62,24 @@ class MailTemplateService {
const CONFIG_PATH = 'Configuration/MailTemplates';
/**
* @var array $toAddresses
* @var string $toAddresses
*/
private $toAddresses = [];
private $toAddresses = '';
/**
* @var string $fromAddress
*/
private $fromAddress;
private $fromAddress = '';
/**
* @var array $ccAddresses
* @var string $ccAddresses
*/
private $ccAddresses;
/**
* @var string $replyToAddress
*/
private $replyToAddress;
private $replyToAddress = '';
/**
* @var string $language
......@@ -117,7 +117,7 @@ class MailTemplateService {
private $markers;
/**
* @var array $bccAddresses
* @var string $bccAddresses
*/
private $bccAddresses;
......@@ -211,6 +211,254 @@ class MailTemplateService {
}
}
/**
* @param string $fromName
*/
public function setFromName($fromName) {
$this->fromName = $fromName;
}
/**
* Provides translation for the marker data type
*
* @param string $markerType
*/
public static function getReadableMarkerType($markerType) {
switch ($markerType) {
case self::MARKER_TYPE_STRING :
LocalizationUtility::translate('backend.marker.type.string', 'sg_mail');
break;
case self::MARKER_TYPE_ARRAY :
LocalizationUtility::translate('backend.marker.type.array', 'sg_mail');
break;
case self::MARKER_TYPE_OBJECT :
LocalizationUtility::translate('backend.marker.type.object', 'sg_mail');
break;
case self::MARKER_TYPE_FILE:
LocalizationUtility::translate('backend.marker.type.file', 'sg_mail');
break;
default:
LocalizationUtility::translate('backend.marker.type.mixed', 'sg_mail');
}
}
/**
* @param string $toAddresses
* @return MailTemplateService
*/
public function setToAddresses($toAddresses): MailTemplateService {
$normalizedToAddresses = trim(preg_replace('~\x{00a0}~iu', ' ', $toAddresses));
$this->toAddresses = $normalizedToAddresses;
$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 {
if ($ccAddresses) {
$this->ccAddresses = $ccAddresses;
$this->mailMessage->setCc(GeneralUtility::trimExplode(',', $this->ccAddresses));
}
return $this;
}
/**
* @param string $replyToAddress
* @return MailTemplateService
*/
public function setReplyToAddress($replyToAddress): MailTemplateService {
if ($replyToAddress) {
$this->replyToAddress = $replyToAddress;
$this->mailMessage->setReplyTo($replyToAddress);
}
return $this;
}
/**
* @param string $language
* @return MailTemplateService
*/
public function setLanguage($language): MailTemplateService {
$this->language = $language;
return $this;
}
/**
* @param boolean $ignoreMailQueue
* @return MailTemplateService
*/
public function setIgnoreMailQueue($ignoreMailQueue): MailTemplateService {
$this->ignoreMailQueue = $ignoreMailQueue;
return $this;
}
/**
* @param string $templateName
* @return MailTemplateService
*/
public function setTemplateName($templateName): MailTemplateService {
$this->templateName = $templateName;
return $this;
}
/**
* @param string $extensionKey
* @return MailTemplateService
*/
public function setExtensionKey($extensionKey): MailTemplateService {
$this->extensionKey = $extensionKey;
return $this;
}
/**
* @param array $markers
* @return MailTemplateService
*/
public function setMarkers(array $markers): MailTemplateService {
$this->markers = $markers;
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
* @return MailTemplateService
*/
public function setPriority($priority): MailTemplateService {
$this->priority = $priority;
return $this;
}
/**
* @param Swift_OutputByteStream $data
* @param string $filename
* @param string $contentType
* @return MailTemplateService
*/
public function addAttachment($data, $filename, $contentType): MailTemplateService {
$attachment = Swift_Attachment::newInstance()
->setFilename($filename)
->setContentType($contentType)
->setBody($data);
$this->mailMessage->attach($attachment);
return $this;
}
/**
* Add a file resource as attachment
*
* @param FileInterface|FileReference $file
* @return MailTemplateService
*/
public function addFileResourceAttachment($file): MailTemplateService {
if ($file instanceof FileReference) {
$file = $file->getOriginalResource()->getOriginalFile();
}
$fileReference = $this->objectManager->get(FileReference::class);
$resourceFactory = $this->objectManager->get(ResourceFactory::class);
$falFileReference = $resourceFactory->createFileReferenceObject(
[
'uid_local' => $file->getUid(),
'uid_foreign' => uniqid('NEW_', TRUE),
'uid' => uniqid('NEW_', TRUE),
'crop' => NULL,
]
);
$fileReference->setOriginalResource($falFileReference);
$this->markers[] = $fileReference;
/** @noinspection PhpParamsInspection */
$this->addAttachment($file->getContents(), $file->getName(), $file->getMimeType());
return $this;
}
/**
* @return MailMessage
*/
public function getMailMessage(): MailMessage {
return $this->mailMessage;
}
/**
* set the page id from which this was called
*
* @param int $pid
* @return MailTemplateService
*/
public function setPid($pid): MailTemplateService {
$this->pid = (int) $pid;
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
*/
public function getSubject(): string {
return $this->subject;
}
/**
* @param string $subject
*/
public function setSubject(string $subject) {
$this->subject = $subject;
}
/**
* Return default markers for sg_mail
*
......@@ -250,14 +498,15 @@ class MailTemplateService {
* @throws \InvalidArgumentException
* @throws \BadFunctionCallException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
* @throws \Exception
*/
public function sendEmail($isPreview = FALSE): bool {
$registerService = GeneralUtility::makeInstance(RegisterService::class);
if (TYPO3_MODE === 'FE') {
/** @var TypoScriptFrontendController $tsfe */
$tsfe = $GLOBALS['TSFE'];
$pageUid = (int) $tsfe->id;
/** @var TypoScriptFrontendController $typoscriptFrontendController */
$typoscriptFrontendController = $GLOBALS['TSFE'];
$pageUid = (int) $typoscriptFrontendController->id;
} else {
$pageUid = (int) GeneralUtility::_GP('id');
}
......@@ -349,6 +598,7 @@ class MailTemplateService {
$temporaryMarkerArray = [$markerPathSegment => $temporaryMarkerArray];
}
}
/** @noinspection SlowArrayOperationsInLoopInspection */
$previewMarker = array_merge_recursive($previewMarker, $temporaryMarkerArray);
}
$this->setIgnoreMailQueue(TRUE);
......@@ -389,15 +639,16 @@ class MailTemplateService {
$this->mailMessage->setSubject($subject);
$emailBody = $emailView->render();
// insert <br /> tags, but replace every instance of three or more successive breaks with just two.
$emailBody = nl2br($emailBody);
$emailBody = preg_replace('/(<br[\s]?[\/]?>[\s]*){3,}/', '<br /><br />', $emailBody);
$isTemplateBlacklisted = self::isTemplateBlacklisted(
$this->extensionKey, $this->templateName, $siteRootId
);
// insert <br> tags, but replace every instance of three or more successive breaks with just two.
$emailBody = $emailView->render();
$emailBody = nl2br($emailBody);
$emailBody = preg_replace('/(<br[\s]?[\/]?>[\s]*){3,}/', '<br><br>', $emailBody);
$currentTimestamp = 0;
if ($this->ignoreMailQueue && !$isTemplateBlacklisted) {
$this->mailMessage->setBody($emailBody, 'text/html');
$plaintextService = GeneralUtility::makeInstance(PlaintextService::class);
......@@ -406,21 +657,13 @@ class MailTemplateService {
$this->mailMessage->send();
$dateTime = new DateTime();
$currentTimestamp = $dateTime->getTimestamp();
}
if (!$isPreview) {
$this->addMailToMailQueue(
$this->extensionKey, $this->templateName, $subject, $emailBody, $this->priority,
$currentTimestamp, $currentTimestamp, $this->language, $siteRootId
);
}
} else {
if (!$isPreview) {
$this->addMailToMailQueue(
$this->extensionKey, $this->templateName, $subject, $emailBody, $this->priority, 0, 0,
$this->language, $siteRootId
);
}
if (!$isPreview) {
$this->addMailToMailQueue(
$this->extensionKey, $this->templateName, $subject, $emailBody, $this->priority,
$currentTimestamp, $currentTimestamp, $this->language, $siteRootId
);
}
return TRUE;
......@@ -483,6 +726,7 @@ class MailTemplateService {
* @param int $uid
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException
* @throws \Exception
*/
public function sendMailFromQueue($uid) {
$mailRepository = $this->objectManager->get(MailRepository::class);
......@@ -536,177 +780,6 @@ class MailTemplateService {
}
}
/**
* @param string $toAddresses
* @return MailTemplateService
*/
public function setToAddresses($toAddresses): MailTemplateService {
$toAddresses = trim(preg_replace('~\x{00a0}~siu', ' ', $toAddresses));
$this->toAddresses = $toAddresses;
$addressesArray = GeneralUtility::trimExplode(',', $toAddresses, TRUE);
if (\count($addressesArray) > 1) {
$toAddresses = $addressesArray;
}
$this->mailMessage->setTo($toAddresses);
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 {
if ($ccAddresses) {
$this->ccAddresses = $ccAddresses;
$this->mailMessage->setCc(GeneralUtility::trimExplode(',', $this->ccAddresses));
}
return $this;
}
/**
* @param string $replyToAddress
* @return MailTemplateService
*/
public function setReplyToAddress($replyToAddress): MailTemplateService {
if ($replyToAddress) {
$this->replyToAddress = $replyToAddress;
$this->mailMessage->setReplyTo($replyToAddress);
}
return $this;
}
/**
* @param string $language
* @return MailTemplateService
*/
public function setLanguage($language): MailTemplateService {
$this->language = $language;
return $this;
}
/**
* @param boolean $ignoreMailQueue
* @return MailTemplateService
*/
public function setIgnoreMailQueue($ignoreMailQueue): MailTemplateService {
$this->ignoreMailQueue = $ignoreMailQueue;
return $this;
}
/**
* @param string $templateName
* @return MailTemplateService
*/
public function setTemplateName($templateName): MailTemplateService {
$this->templateName = $templateName;
return $this;
}
/**
* @param string $extensionKey
* @return MailTemplateService
*/
public function setExtensionKey($extensionKey): MailTemplateService {
$this->extensionKey = $extensionKey;
return $this;
}
/**
* @param array $markers
* @return MailTemplateService
*/
public function setMarkers(array $markers): MailTemplateService {
$this->markers = $markers;
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
* @return MailTemplateService
*/
public function setPriority($priority): MailTemplateService {
$this->priority = $priority;
return $this;
}
/**
* @param Swift_OutputByteStream $data
* @param string $filename
* @param string $contentType
* @return MailTemplateService
*/
public function addAttachment($data, $filename, $contentType): MailTemplateService {
$attachment = Swift_Attachment::newInstance()
->setFilename($filename)
->setContentType($contentType)
->setBody($data);
$this->mailMessage->attach($attachment);
return $this;
}
/**
* Add a file resource as attachment
*
* @param FileInterface|FileReference $file
* @return MailTemplateService
*/
public function addFileResourceAttachment($file): MailTemplateService {
if ($file instanceof FileReference) {
$file = $file->getOriginalResource()->getOriginalFile();
}
$fileReference = $this->objectManager->get(FileReference::class);
$resourceFactory = $this->objectManager->get(ResourceFactory::class);
$falFileReference = $resourceFactory->createFileReferenceObject(
[
'uid_local' => $file->getUid(),
'uid_foreign' => uniqid('NEW_', TRUE),
'uid' => uniqid('NEW_', TRUE),
'crop' => NULL,
]
);
$fileReference->setOriginalResource($falFileReference);
$this->markers[] = $fileReference;
$this->addAttachment($file->getContents(), $file->getName(), $file->getMimeType());
return $this;
}
/**
* @return MailMessage
*/
public function getMailMessage(): MailMessage {
return $this->mailMessage;
}
/**
* use all values from the given template
*
......@@ -734,48 +807,6 @@ class MailTemplateService {
$this->setReplyToAddress($template->getReplyTo());
}
/**
* @param string $fromName
*/
public function setFromName($fromName) {
$this->fromName = $fromName;
}
/**
* Provides translation for the marker data type
*