[TASK] Improve ajax and mail sending handling

This commit is contained in:
Philipp Dieter 2021-10-26 23:50:04 +02:00
parent 46f7b519b7
commit f206c8fb0e
4 changed files with 293 additions and 18 deletions

View File

@ -24,6 +24,7 @@ use TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper;
use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager; use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager;
use TYPO3\CMS\Extbase\Property\PropertyMapper; use TYPO3\CMS\Extbase\Property\PropertyMapper;
use TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationBuilder; use TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationBuilder;
use TYPO3\CMS\Extbase\Service\EnvironmentService;
use TYPO3\CMS\Extbase\Service\ExtensionService; use TYPO3\CMS\Extbase\Service\ExtensionService;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility; use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
@ -148,6 +149,22 @@ class ActionController extends BaseController
$this->extensionService = $extensionService; $this->extensionService = $extensionService;
} }
/**
* environmentService
*
* @var EnvironmentService
*/
protected $environmentService;
/**
* @param
*/
public function injectEnvironmentService(
EnvironmentService $environmentService
) {
$this->environmentService = $environmentService;
}
/** /**
* propertyMapper * propertyMapper
* *
@ -253,7 +270,7 @@ class ActionController extends BaseController
} }
/** /**
* returns an instance of uribuilder *
*/ */
public function persistAll() public function persistAll()
{ {
@ -596,7 +613,10 @@ class ActionController extends BaseController
->setCreateAbsoluteUri(true) ->setCreateAbsoluteUri(true)
->setAddQueryString(true) ->setAddQueryString(true)
->setTargetPageType($this->ajaxPageType) ->setTargetPageType($this->ajaxPageType)
->setArguments(['cid' => $this->contentObjectUid]) ->setArguments([
'cid' => $this->contentObjectUid,
'type' => $this->ajaxPageType,
])
->uriFor($this->request->getControllerActionName()); ->uriFor($this->request->getControllerActionName());
$this->ajaxEnv = [ $this->ajaxEnv = [
'uri' => $uri, 'uri' => $uri,
@ -744,8 +764,12 @@ class ActionController extends BaseController
$this->response->setStatus($this->responseStatus); $this->response->setStatus($this->responseStatus);
} }
if ($this->pageType == $this->ajaxPageType) { if ($this->pageType == $this->ajaxPageType) {
if ($this->environmentService->isEnvironmentInBackendMode()) {
header('Content-Type: application/json');
} else {
$GLOBALS['TSFE']->setContentType('application/json'); $GLOBALS['TSFE']->setContentType('application/json');
} }
}
unset($result['data']); unset($result['data']);
if ($this->redirect) { if ($this->redirect) {
$result['redirect'] = $this->redirect; $result['redirect'] = $this->redirect;
@ -769,5 +793,4 @@ class ActionController extends BaseController
} }
$this->view->assignMultiple($result); $this->view->assignMultiple($result);
} }
} }

View File

@ -36,9 +36,10 @@ class MailUtility
*/ */
public static function parseContentTemplate( public static function parseContentTemplate(
$text, $text,
$markers = [] $markers = [],
$lineEnd = "\r\n"
) { ) {
$textParts = explode("\r\n\r\n", $text); $textParts = explode($lineEnd . $lineEnd, $text);
$result = []; $result = [];
foreach ($textParts as $textPart) { foreach ($textParts as $textPart) {
$type = 'text'; $type = 'text';
@ -54,6 +55,22 @@ class MailUtility
$type = 'headline3'; $type = 'headline3';
$textPart = substr($textPart, 4); $textPart = substr($textPart, 4);
} }
if (substr($textPart, 0, 2) === '- ') {
$type = 'list';
$textPart = substr($textPart, 2);
}
if (substr($textPart, 0, 2) === '| ') {
$type = 'table';
$textPart = substr($textPart, 2);
}
if (substr($textPart, 0, 3) === '|| ') {
$type = 'tableLayout';
$textPart = substr($textPart, 3);
}
if (substr($textPart, 0, 9) === '%subject ') {
$type = 'subject';
$textPart = substr($textPart, 9);
}
foreach ($markers as $markerName => $markerContent) { foreach ($markers as $markerName => $markerContent) {
$textPart = str_replace( $textPart = str_replace(
'###' . $markerName . '###', '###' . $markerName . '###',
@ -61,10 +78,40 @@ class MailUtility
$textPart $textPart
); );
} }
switch($type) {
case 'table':
case 'tableLayout':
if (
$result[count($result) - 1]['type'] == $type
&& count($result[count($result) - 1]['data']) == 1
) {
$result[count($result) - 1]['data'][] = $textPart;
} else {
$result[] = [
'type' => $type,
'data' => [$textPart],
];
}
break;
case 'list':
if (
$result[count($result) - 1]['type'] == 'list'
) {
$result[count($result) - 1]['data'][] = $textPart;
} else {
$result[] = [
'type' => 'list',
'data' => [$textPart],
];
}
break;
default:
$result[] = [ $result[] = [
'type' => $type, 'type' => $type,
'data' => $textPart, 'data' => $textPart,
]; ];
break;
}
} }
return $result; return $result;
} }
@ -107,12 +154,22 @@ class MailUtility
$htmlView->setTemplate($templateNameHtml); $htmlView->setTemplate($templateNameHtml);
$textView = $objectManager->get(StandaloneView::class); $textView = $objectManager->get(StandaloneView::class);
if ($templatePaths) { if ($templatePaths) {
$partialRootPaths = $htmlView->getPartialRootPaths();
$partialRootPaths[] = GeneralUtility::getFileAbsFileName(
'EXT:templates_aide/Resources/Private/Partials/'
);
$htmlView->setTemplateRootPaths( $htmlView->setTemplateRootPaths(
$templatePaths->getTemplateRootPaths() $templatePaths->getTemplateRootPaths()
); );
$htmlView->setPartialRootPaths(
$partialRootPaths
);
$textView->setTemplateRootPaths( $textView->setTemplateRootPaths(
$templatePaths->getTemplateRootPaths() $templatePaths->getTemplateRootPaths()
); );
$textView->setPartialRootPaths(
$partialRootPaths
);
} else { } else {
$htmlView->getTemplatePaths()->fillDefaultsByPackageName( $htmlView->getTemplatePaths()->fillDefaultsByPackageName(
'templates_aide' 'templates_aide'
@ -130,6 +187,9 @@ class MailUtility
foreach ($data as $row) { foreach ($data as $row) {
switch($row['type']) { switch($row['type']) {
case 'text': case 'text':
case 'table':
case 'tableLayout':
case 'list':
case 'textbold': case 'textbold':
case 'headline': case 'headline':
case 'headline2': case 'headline2':

View File

@ -1,23 +1,34 @@
<f:if condition="{width}">
<f:else>
<f:variable name="width" value="640" />
</f:else>
</f:if>
<f:if condition="{padding}">
<f:else>
<f:variable name="padding" value="20" />
</f:else>
</f:if>
<f:variable name="widthPadded" value="{width - padding - padding}" />
<f:variable name="widthTableColumn" value="{widthPadded / 2}" />
<f:for each="{content}" as="row" key="rowKey" iteration="rowI" > <f:for each="{content}" as="row" key="rowKey" iteration="rowI" >
<v:condition.type.isArray value="{row.data}"> <v:condition.type.isArray value="{row.data}">
<f:then>
</f:then>
<f:else> <f:else>
<f:if condition="{v:condition.string.contains(haystack: '{row.type}', needle: 'headline', then: '1')} || {row.type} == 'text'"> <f:if condition="{v:condition.string.contains(haystack: '{row.type}', needle: 'headline', then: '1')} || {row.type} == 'text'">
<!--[if mso | IE]> <!--[if mso | IE]>
<table align="center" border="0" cellpadding="0" cellspacing="0" class="" style="width:640px;" width="640" > <table align="center" border="0" cellpadding="0" cellspacing="0" class="" style="width:{width}px;" width="{width}" >
<tr> <tr>
<td style="line-height:0px;font-size:0px;mso-line-height-rule:exactly;"> <td style="line-height:0px;font-size:0px;mso-line-height-rule:exactly;">
<![endif]--> <![endif]-->
<div style="background:#ffffff;background-color:#ffffff;margin:0px auto;max-width:640px;"> <div style="background:#ffffff;background-color:#ffffff;margin:0px auto;max-width:{width}px;">
<table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="background:#ffffff;background-color:#ffffff;width:100%;"> <table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="background:#ffffff;background-color:#ffffff;width:100%;">
<tbody> <tbody>
<tr> <tr>
<td style="direction:ltr;font-size:0px;padding:0 20px;text-align:center;"> <td style="direction:ltr;font-size:0px;padding:0 {padding}px;text-align:center;">
<!--[if mso | IE]> <!--[if mso | IE]>
<table role="presentation" border="0" cellpadding="0" cellspacing="0"> <table role="presentation" border="0" cellpadding="0" cellspacing="0">
<tr> <tr>
<td class="" style="vertical-align:top;width:600px;" > <td class="" style="vertical-align:top;width:{widthPadded};" >
<![endif]--> <![endif]-->
<div class="mj-column-per-100 mj-outlook-group-fix" style="font-size:0px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%;"> <div class="mj-column-per-100 mj-outlook-group-fix" style="font-size:0px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%;">
<table border="0" cellpadding="0" cellspacing="0" role="presentation" width="100%"> <table border="0" cellpadding="0" cellspacing="0" role="presentation" width="100%">
@ -68,5 +79,136 @@
<![endif]--> <![endif]-->
</f:if> </f:if>
</f:else> </f:else>
<f:then>
<f:comment>Data is array, can be table or list</f:comment>
<f:switch expression="{row.type}">
<f:case value="table">
<f:variable name="type" value="table" />
</f:case>
<f:case value="tableLayout">
<f:variable name="type" value="table" />
</f:case>
<f:defaultCase>
<f:variable name="type" value="list" />
</f:defaultCase>
</f:switch>
<f:switch expression="{type}">
<f:case value="table">
<!--[if mso | IE]>
<table
align="center" border="0" cellpadding="0" cellspacing="0" class="" style="width:{width}px;" width="{width}"
>
<tr>
<td style="line-height:0px;font-size:0px;mso-line-height-rule:exactly;">
<![endif]-->
<div style="background:#ffffff;background-color:#ffffff;Margin:0px auto;max-width:{width}px;">
<table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="background:#ffffff;background-color:#ffffff;width:100%;">
<tbody>
<tr>
<td style="direction:ltr;font-size:0px;padding:2px {padding}px;text-align:center;vertical-align:top;">
<!--[if mso | IE]>
<table role="presentation" border="0" cellpadding="0" cellspacing="0">
<tr>
<td class="" style="vertical-align:top;width:{widthColumn}px;" >
<![endif]-->
<div class="mj-column-per-50 outlook-group-fix" style="font-size:13px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%;">
<table border="0" cellpadding="0" cellspacing="0" role="presentation" style="vertical-align:top;" width="100%">
<tr>
<td align="left" style="font-size:0px;padding:0px 4px;word-break:break-word;">
<div style="font-family:Arial, sans-serif;font-size:16px;line-height:1.4;text-align:left;color:#000000;">
<div>{row.data.0 -> f:format.nl2br() -> f:format.raw()}</div>
</div>
</td>
</tr>
</table>
</div>
<!--[if mso | IE]>
</td>
<td class="" style="vertical-align:top;width:{widthTableColumn}px;">
<![endif]-->
<div class="mj-column-per-50 outlook-group-fix" style="font-size:13px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%;">
<table border="0" cellpadding="0" cellspacing="0" role="presentation" style="vertical-align:top;" width="100%">
<tr>
<td align="left" style="font-size:0px;padding:0px 4px;word-break:break-word;">
<div style="font-family:Arial, sans-serif;font-size:16px;line-height:1.4;text-align:left;color:#000000;">
<div>{row.data.1 -> f:format.nl2br() -> f:format.raw()}</div>
</div>
</td>
</tr>
</table>
</div>
<!--[if mso | IE]>
</td>
</tr>
</table>
<![endif]-->
</td>
</tr>
</tbody>
</table>
</div>
<!--[if mso | IE]>
</td>
</tr>
</table>
<![endif]-->
</f:case>
<f:case value="list">
<!--[if mso | IE]>
<table align="center" border="0" cellpadding="0" cellspacing="0" class="" style="width:{width}px;" width="{width}" >
<tr>
<td style="line-height:0px;font-size:0px;mso-line-height-rule:exactly;">
<![endif]-->
<div style="background:#ffffff;background-color:#ffffff;margin:0px auto;max-width:{width}px;">
<table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="background:#ffffff;background-color:#ffffff;width:100%;">
<tbody>
<tr>
<td style="direction:ltr;font-size:0px;padding:0 {padding}px;text-align:center;">
<!--[if mso | IE]>
<table role="presentation" border="0" cellpadding="0" cellspacing="0">
<tr>
<td class="" style="vertical-align:top;width:{widthPadded};" >
<![endif]-->
<div class="mj-column-per-100 mj-outlook-group-fix" style="font-size:0px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%;">
<table border="0" cellpadding="0" cellspacing="0" role="presentation" width="100%">
<tbody>
<tr>
<td style="vertical-align:top;padding:0;">
<table border="0" cellpadding="0" cellspacing="0" role="presentation" style width="100%">
<tr>
<td align="left" style="font-size:0px;padding:0;word-break:break-word;">
<div style="font-family:Arial, sans-serif;font-size:16px;line-height:1.4;text-align:left;color:#000000;">
<ul style="padding-left: 14px;">
<f:for each="{row.data}" as="dataRow" key="dataRowKey" iteration="dataRowI">
<li>{dataRow -> f:format.nl2br() -> f:format.raw()}</li>
</f:for>
</ul>
</div>
</td>
</tr>
</table>
</td>
</tr>
</tbody>
</table>
</div>
<!--[if mso | IE]>
</td>
</tr>
</table>
<![endif]-->
</td>
</tr>
</tbody>
</table>
</div>
<!--[if mso | IE]>
</td>
</tr>
</table>
<![endif]-->
</f:case>
</f:switch>
</f:then>
</v:condition.type.isArray> </v:condition.type.isArray>
</f:for> </f:for>

View File

@ -0,0 +1,50 @@
<v:variable.set name="br">
</v:variable.set>
<f:for
each="{content}"
as="row"
key="rowKey"
iteration="rowI"
><f:spaceless>
<v:condition.type.isArray value="{row.data}">
<f:else>
<f:switch expression="{row.type}">
<f:case value="headline">
# {row.data -> f:format.nl2br() -> f:format.raw()}
<f:spaceless></f:spaceless>
</f:case>
<f:case value="headline2">
## {row.data -> f:format.nl2br() -> f:format.raw()}
</f:case>
<f:case value="headline3">
### {row.data -> f:format.nl2br() -> f:format.raw()}
</f:case>
<f:defaultCase>
<f:spaceless><v:format.wordWrap limit="76" glue="{br}">
{row.data -> f:format.raw()}
</v:format.wordWrap>
</f:spaceless>{br}{br}
</f:defaultCase>
</f:switch>
</f:else>
<f:then>
<f:switch expression="{row.type}">
<f:case value="tableLayout">
{row.data.0 -> f:format.raw()}{br}{br}{row.data.1 -> f:format.raw()}{br}
</f:case>
<f:case value="table">
{row.data.0 -> f:format.raw()}: {row.data.1 -> f:format.raw()}
</f:case>
<f:case value="list">
<f:for each="{row.data}" as="dataRow" key="dataRowKey" iteration="dataRowI">- <f:spaceless><v:format.wordWrap limit="76" glue="{br} " >
{dataRow -> f:format.raw()}
</v:format.wordWrap>
</f:spaceless>{br}{br}</f:for>
</f:case>
<f:defaultCase>
</f:defaultCase>
</f:switch>
</f:then>
</v:condition.type.isArray>
</f:spaceless><f:if condition="{row.type} == 'table'"><f:then>{br}<f:if condition="{content.{rowI.cycle}.type} == 'table'"><f:else>{br}</f:else></f:if></f:then><f:else><f:if condition="{content.{rowI.cycle}}"><f:then>{br}{br}</f:then></f:if></f:else></f:if></f:for>