From f206c8fb0e8cc79ed79fc53e82cf6ab4bb2e6b72 Mon Sep 17 00:00:00 2001 From: Philipp Dieter Date: Tue, 26 Oct 2021 23:50:04 +0200 Subject: [PATCH] [TASK] Improve ajax and mail sending handling --- Classes/Controller/ActionController.php | 35 +++- Classes/Utility/MailUtility.php | 72 +++++++- .../Private/Partials/Mails/DefaultHtml.html | 154 +++++++++++++++++- .../Private/Partials/Mails/DefaultText.html | 50 ++++++ 4 files changed, 293 insertions(+), 18 deletions(-) create mode 100644 Resources/Private/Partials/Mails/DefaultText.html diff --git a/Classes/Controller/ActionController.php b/Classes/Controller/ActionController.php index ed5070d..918e378 100644 --- a/Classes/Controller/ActionController.php +++ b/Classes/Controller/ActionController.php @@ -24,6 +24,7 @@ use TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper; use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager; use TYPO3\CMS\Extbase\Property\PropertyMapper; use TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationBuilder; +use TYPO3\CMS\Extbase\Service\EnvironmentService; use TYPO3\CMS\Extbase\Service\ExtensionService; use TYPO3\CMS\Extbase\Utility\LocalizationUtility; @@ -148,6 +149,22 @@ class ActionController extends BaseController $this->extensionService = $extensionService; } + /** + * environmentService + * + * @var EnvironmentService + */ + protected $environmentService; + + /** + * @param + */ + public function injectEnvironmentService( + EnvironmentService $environmentService + ) { + $this->environmentService = $environmentService; + } + /** * propertyMapper * @@ -253,7 +270,7 @@ class ActionController extends BaseController } /** - * returns an instance of uribuilder + * */ public function persistAll() { @@ -596,11 +613,14 @@ class ActionController extends BaseController ->setCreateAbsoluteUri(true) ->setAddQueryString(true) ->setTargetPageType($this->ajaxPageType) - ->setArguments(['cid' => $this->contentObjectUid]) + ->setArguments([ + 'cid' => $this->contentObjectUid, + 'type' => $this->ajaxPageType, + ]) ->uriFor($this->request->getControllerActionName()); $this->ajaxEnv = [ - 'uri' => $uri, - 'object' => $object, + 'uri' => $uri, + 'object' => $object, 'namespace' => $this->getPluginNamespace(), ]; } @@ -744,7 +764,11 @@ class ActionController extends BaseController $this->response->setStatus($this->responseStatus); } if ($this->pageType == $this->ajaxPageType) { - $GLOBALS['TSFE']->setContentType('application/json'); + if ($this->environmentService->isEnvironmentInBackendMode()) { + header('Content-Type: application/json'); + } else { + $GLOBALS['TSFE']->setContentType('application/json'); + } } unset($result['data']); if ($this->redirect) { @@ -769,5 +793,4 @@ class ActionController extends BaseController } $this->view->assignMultiple($result); } - } diff --git a/Classes/Utility/MailUtility.php b/Classes/Utility/MailUtility.php index 19129ce..6f16ebe 100644 --- a/Classes/Utility/MailUtility.php +++ b/Classes/Utility/MailUtility.php @@ -36,9 +36,10 @@ class MailUtility */ public static function parseContentTemplate( $text, - $markers = [] + $markers = [], + $lineEnd = "\r\n" ) { - $textParts = explode("\r\n\r\n", $text); + $textParts = explode($lineEnd . $lineEnd, $text); $result = []; foreach ($textParts as $textPart) { $type = 'text'; @@ -54,6 +55,22 @@ class MailUtility $type = 'headline3'; $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) { $textPart = str_replace( '###' . $markerName . '###', @@ -61,10 +78,40 @@ class MailUtility $textPart ); } - $result[] = [ - 'type' => $type, - 'data' => $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[] = [ + 'type' => $type, + 'data' => $textPart, + ]; + break; + } } return $result; } @@ -107,12 +154,22 @@ class MailUtility $htmlView->setTemplate($templateNameHtml); $textView = $objectManager->get(StandaloneView::class); if ($templatePaths) { + $partialRootPaths = $htmlView->getPartialRootPaths(); + $partialRootPaths[] = GeneralUtility::getFileAbsFileName( + 'EXT:templates_aide/Resources/Private/Partials/' + ); $htmlView->setTemplateRootPaths( $templatePaths->getTemplateRootPaths() ); + $htmlView->setPartialRootPaths( + $partialRootPaths + ); $textView->setTemplateRootPaths( $templatePaths->getTemplateRootPaths() ); + $textView->setPartialRootPaths( + $partialRootPaths + ); } else { $htmlView->getTemplatePaths()->fillDefaultsByPackageName( 'templates_aide' @@ -130,6 +187,9 @@ class MailUtility foreach ($data as $row) { switch($row['type']) { case 'text': + case 'table': + case 'tableLayout': + case 'list': case 'textbold': case 'headline': case 'headline2': diff --git a/Resources/Private/Partials/Mails/DefaultHtml.html b/Resources/Private/Partials/Mails/DefaultHtml.html index d30001a..464f7c9 100644 --- a/Resources/Private/Partials/Mails/DefaultHtml.html +++ b/Resources/Private/Partials/Mails/DefaultHtml.html @@ -1,23 +1,34 @@ + + + + + + + + + + + + + - - -
+
-
+
@@ -68,5 +79,136 @@ + + Data is array, can be table or list + + + + + + + + + + + + + + +
+
+ + + + + +
+ +
+ + + + +
+
+
{row.data.0 -> f:format.nl2br() -> f:format.raw()}
+
+
+
+ +
+ + + + +
+
+
{row.data.1 -> f:format.nl2br() -> f:format.raw()}
+
+
+
+ +
+
+ + + + +
+ + + + + + +
+ +
+ + + + + + +
+ + + + +
+
+
    + +
  • {dataRow -> f:format.nl2br() -> f:format.raw()}
  • +
    +
+
+
+
+
+ +
+
+ +
+ + diff --git a/Resources/Private/Partials/Mails/DefaultText.html b/Resources/Private/Partials/Mails/DefaultText.html new file mode 100644 index 0000000..ff9aa5b --- /dev/null +++ b/Resources/Private/Partials/Mails/DefaultText.html @@ -0,0 +1,50 @@ + + + + + + + + # {row.data -> f:format.nl2br() -> f:format.raw()} + + + + ## {row.data -> f:format.nl2br() -> f:format.raw()} + + + ### {row.data -> f:format.nl2br() -> f:format.raw()} + + + + {row.data -> f:format.raw()} + + {br}{br} + + + + + + + + {row.data.0 -> f:format.raw()}{br}{br}{row.data.1 -> f:format.raw()}{br} + + + {row.data.0 -> f:format.raw()}: {row.data.1 -> f:format.raw()} + + + - + {dataRow -> f:format.raw()} + + {br}{br} + + + + + + +{br}{br}{br}{br}