Skip to content
Snippets Groups Projects
Commit 387c9b3f authored by Torsten Oppermann's avatar Torsten Oppermann
Browse files

[TASK] Added formeditor xclass, formerly in sg_forms

parent d8f67e4f
No related branches found
No related tags found
1 merge request!10Feature improved usability
<?php
namespace SGalinski\SgMail\XClass\Form;
/***************************************************************
* Copyright notice
*
* (c) sgalinski Internet Services (http://www.sgalinski.de)
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use SGalinski\SgMail\Service\MailTemplateService;
use SGalinski\SgMail\Service\TypoScriptSettingsService;
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Form\Type\FormDefinitionArray;
/** @noinspection LongInheritanceChainInspection */
/**
* XClass for the form editor controller in ext:form
*/
class FormEditorController extends \TYPO3\CMS\Form\Controller\FormEditorController {
/**
* This contains type names of fields we dont care about in sgmail
*
* @var array
*/
const IGNORE_FIELDS = [
'StaticText', 'Hidden', 'GridRow'
];
/**
* This contains the names of sgmail finishers, so we can identify if we need to generate a template registration
*
* @var array
*/
const MAIL_FINISHER = [
'MailToSenderFinisher', 'MailToReceiverFinisher'
];
/**
* Add automatic SgMail Configuration
*
* @param string $formPersistenceIdentifier
* @param FormDefinitionArray $formDefinition
* @internal
* @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
*/
public function saveFormAction(string $formPersistenceIdentifier, FormDefinitionArray $formDefinition) {
/** @noinspection PhpInternalEntityUsedInspection */
parent::saveFormAction($formPersistenceIdentifier, $formDefinition);
// immediately exit when no finisher is defined (that means no sgmail integration anyway)
if (!$formDefinition['finishers']) {
return;
}
/** @var array $finishers */
$finishers = $formDefinition['finishers'];
$extensionKey = '';
$templateKey = '';
foreach ($finishers as $finisher) {
// if one finisher prevents automatic registration, exit this function
if (!$finisher['options']['automaticRegistration']) {
return;
}
if (!\in_array($finisher['identifier'], self::MAIL_FINISHER, TRUE)) {
continue;
}
// retrieve the extension and template key and jump out of loop
$extensionKey = $finisher['options']['extension'];
$templateKey = $finisher['options']['template'];
break;
}
// if no template key was explicitly set, use the form identifier as template key
if ($templateKey === '') {
$templateKey = $formDefinition['identifier'];
}
// if there was no sg mail finisher or an missing key then simply exit the function
if ($extensionKey === '' || $templateKey === '') {
return;
}
// parse yaml for form fields
$absoluteFilePath = GeneralUtility::getFileAbsFileName($formPersistenceIdentifier);
$parsedYaml = \yaml_parse_file($absoluteFilePath);
$renderables = [];
foreach ($parsedYaml['renderables'] as $formPage) {
if (\is_array($formPage['renderables'])) {
foreach ($formPage['renderables'] as $row) {
if (!\is_array($row) || !$row['type']) {
continue;
}
if ($row['type'] === 'GridRow') {
foreach ($row['renderables'] as $renderableInsideGridrow) {
$renderables[] = $renderableInsideGridrow;
}
} elseif (!\in_array($row['type'], self::IGNORE_FIELDS, TRUE)) {
$renderables[] = $row;
}
}
} else {
return;
}
}
// write the new Register.php file
$this->writeRegisterFile($renderables, $extensionKey, $templateKey);
// call register function in mail template service class
MailTemplateService::registerExtensions();
// clear caches
$this->clearCaches();
}
/**
* @param array $renderables
* @param string $extensionKey
* @param string $templateKey
*/
private function writeRegisterFile(array $renderables, $extensionKey, $templateKey) {
// get the location where automatic registrations should be stored
$configurationLocation = $this->getRegistrationPath();
$registerFolder = GeneralUtility::getFileAbsFileName(
$configurationLocation
);
// create folder if it does not exist
if (!is_dir($registerFolder) && !mkdir($registerFolder) && !is_dir($registerFolder)) {
throw new \RuntimeException(sprintf('Directory "%s" was not created', $registerFolder));
}
$registerFile = GeneralUtility::getFileAbsFileName(
$registerFolder . '/' . $templateKey . '.php'
);
// build the register array
$newRegisterArray = [
'extension_key' => $extensionKey,
'template_key' => $templateKey,
'description' => $templateKey,
'subject' => $templateKey,
'markers' => []
];
// add the markers for this template
foreach ($renderables as $element) {
$markerName = $element['identifier'];
// if markerName is explicitly set, override the registered identifier
if(isset($element['properties']['markerName']) && $element['properties']['markerName'] !== '') {
$markerName = $element['properties']['markerName'];
}
$newRegisterArray['markers'][] = [
'marker' => $markerName,
'type' => MailTemplateService::MARKER_TYPE_STRING,
'value' => $element['label'],
'description' => $element['label']
];
}
file_put_contents($registerFile, '<?php return ' . var_export($newRegisterArray, TRUE) . ';');
}
/**
* Clear the sgmail register cache
*
* @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
*/
private function clearCaches() {
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
$cacheManager = $objectManager->get(CacheManager::class);
/** @var FrontendInterface $cache */
$cache = $cacheManager->getCache(MailTemplateService::CACHE_NAME);
/** @var FrontendInterface $cache */
$cache->flush();
}
/**
* Returns the path to the configured location where automatic mail template registrations should be
*
* @return string
*/
private function getRegistrationPath(): string {
// get typoscript settings from sg mail
/** @var TypoScriptSettingsService $typoScriptSettingsService */
$typoScriptSettingsService = $this->objectManager->get(TypoScriptSettingsService::class);
$tsSettings = $typoScriptSettingsService->getSettings(0, 'tx_sgmail');
// get the location where automatic registrations should be stored
return 'EXT:' . $tsSettings['mail']['configurationLocation'] . '/' . MailTemplateService::CONFIG_PATH;
}
}
...@@ -47,6 +47,11 @@ if (TYPO3_MODE === 'BE') { ...@@ -47,6 +47,11 @@ if (TYPO3_MODE === 'BE') {
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTypoScriptSetup(file_get_contents($tsPath . 'setup.ts')); \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTypoScriptSetup(file_get_contents($tsPath . 'setup.ts'));
} }
// register xclasses
$GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][\TYPO3\CMS\Form\Controller\FormEditorController::class] = [
'className' => \SGalinski\SgMail\XClass\Form\FormEditorController::class,
];
// Cache registration // Cache registration
$cacheName = \SGalinski\SgMail\Service\MailTemplateService::CACHE_NAME; $cacheName = \SGalinski\SgMail\Service\MailTemplateService::CACHE_NAME;
if (!is_array($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'][$cacheName])) { if (!is_array($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'][$cacheName])) {
......
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