From 315033d97bf6d6bbb2a98bcc1bc73f79c8cdae6b Mon Sep 17 00:00:00 2001
From: Torsten Oppermann <torsten@sgalinski.de>
Date: Mon, 26 Mar 2018 16:45:12 +0200
Subject: [PATCH] [TASK] Initial creation of new finisher, wip

---
 Classes/Finisher/Forms/FormsFinisher.php      | 129 ++++++++++++++++++
 Configuration/TypoScript/constants.ts         |   2 +-
 Configuration/TypoScript/setup.ts             |  12 ++
 Configuration/Yaml/Forms/FormEditorSetup.yaml |  52 +++++++
 Configuration/Yaml/Forms/FormEngineSetup.yaml |  12 ++
 5 files changed, 206 insertions(+), 1 deletion(-)
 create mode 100644 Classes/Finisher/Forms/FormsFinisher.php
 create mode 100644 Configuration/Yaml/Forms/FormEditorSetup.yaml
 create mode 100644 Configuration/Yaml/Forms/FormEngineSetup.yaml

diff --git a/Classes/Finisher/Forms/FormsFinisher.php b/Classes/Finisher/Forms/FormsFinisher.php
new file mode 100644
index 00000000..ab569563
--- /dev/null
+++ b/Classes/Finisher/Forms/FormsFinisher.php
@@ -0,0 +1,129 @@
+<?php
+declare(strict_types=1);
+
+namespace SGalinski\SgMail\Finisher\Forms;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use SGalinski\SgMail\Service\MailTemplateService;
+use TYPO3\CMS\Core\Mail\MailMessage;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Extbase\Domain\Model\FileReference;
+use TYPO3\CMS\Extbase\Object\ObjectManager;
+use TYPO3\CMS\Fluid\View\StandaloneView;
+use TYPO3\CMS\Form\Domain\Finishers\AbstractFinisher;
+use TYPO3\CMS\Form\Domain\Finishers\Exception\FinisherException;
+use TYPO3\CMS\Form\Domain\Model\FormElements\FileUpload;
+use TYPO3\CMS\Form\Domain\Runtime\FormRuntime;
+use TYPO3\CMS\Form\Service\TranslationService;
+use TYPO3\CMS\Form\ViewHelpers\RenderRenderableViewHelper;
+
+/***************************************************************
+ *  Copyright notice
+ *
+ *  (c) sgalinski Internet Services (https://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 3 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!
+ ***************************************************************/
+
+/**
+ * This finisher sends an email to one recipient
+ *
+ * Options:
+ *
+ * - templatePathAndFilename (mandatory): Template path and filename for the mail body
+ * - layoutRootPath: root path for the layouts
+ * - partialRootPath: root path for the partials
+ * - variables: associative array of variables which are available inside the Fluid template
+ *
+ * The following options control the mail sending. In all of them, placeholders in the form
+ * of {...} are replaced with the corresponding form value; i.e. {email} as recipientAddress
+ * makes the recipient address configurable.
+ *
+ * - subject (mandatory): Subject of the email
+ * - recipientAddress (mandatory): Email address of the recipient
+ * - recipientName: Human-readable name of the recipient
+ * - senderAddress (mandatory): Email address of the sender
+ * - senderName: Human-readable name of the sender
+ * - replyToAddress: Email address of to be used as reply-to email (use multiple addresses with an array)
+ * - carbonCopyAddress: Email address of the copy recipient (use multiple addresses with an array)
+ * - blindCarbonCopyAddress: Email address of the blind copy recipient (use multiple addresses with an array)
+ * - format: format of the email (one of the FORMAT_* constants). By default mails are sent as HTML
+ *
+ * Scope: frontend
+ */
+class FormsFinisher extends AbstractFinisher {
+	const FORMAT_PLAINTEXT = 'plaintext';
+	const FORMAT_HTML = 'html';
+
+	/**
+	 * @var array
+	 */
+	protected $defaultOptions = [
+		'recipientName' => '',
+		'senderName' => '',
+		'format' => self::FORMAT_HTML,
+		'attachUploads' => TRUE
+	];
+
+	/**
+	 * Executes this finisher
+	 *
+	 * @see AbstractFinisher::execute()
+	 *
+	 * @throws \InvalidArgumentException
+	 * @throws FinisherException
+	 */
+	protected function executeInternal() {
+
+		$formValues = $this->finisherContext->getFormValues();
+		$formRuntime = $this->finisherContext->getFormRuntime();
+		debug($formRuntime);
+		debug($formValues);
+
+		$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
+		/** @var \SGalinski\SgMail\Service\MailTemplateService $mailTemplateService */
+		$mailTemplateService = $objectManager->get(MailTemplateService::class);
+
+		// parse options
+		$extensionKey = $this->parseOption('extensionKey');
+		$templateName = $this->parseOption('templateName');
+		$recipientAddress = $this->parseOption('recipientAddress');
+		$senderAddress = $this->parseOption('senderAddress');
+		$senderName = $this->parseOption('senderName');
+
+		$mailTemplateService->setExtensionKey($extensionKey);
+		$mailTemplateService->setTemplateName($templateName);
+		$mailTemplateService->setToAddresses($recipientAddress);
+		$mailTemplateService->setFromAddress($senderAddress);
+		$mailTemplateService->setFromName($senderName);
+		$mailTemplateService->setLanguage($GLOBALS['TSFE']->config['config']['language']);
+	}
+}
diff --git a/Configuration/TypoScript/constants.ts b/Configuration/TypoScript/constants.ts
index e3c15db4..9685bb4d 100644
--- a/Configuration/TypoScript/constants.ts
+++ b/Configuration/TypoScript/constants.ts
@@ -4,4 +4,4 @@ plugin.tx_sgmail {
 		partialRootPath = EXT:sg_mail/Resources/Private/Partials/
 		layoutRootPath = EXT:sg_mail/Resources/Private/Layouts/
 	}
-}
+}
\ No newline at end of file
diff --git a/Configuration/TypoScript/setup.ts b/Configuration/TypoScript/setup.ts
index b992c265..227a3c8a 100644
--- a/Configuration/TypoScript/setup.ts
+++ b/Configuration/TypoScript/setup.ts
@@ -21,3 +21,15 @@ module.tx_sgmail {
 		templateDefaultLanguage = en
 	}
 }
+
+# frontend configuration for ext:forms
+plugin.tx_form.settings.yamlConfigurations {
+	1499086867 = EXT:sg_mail/Configuration/Yaml/Forms/FormEngineSetup.yaml
+}
+
+# Backend configuration for ext:forms
+debug = 1
+module.tx_form.settings.yamlConfigurations {
+	1499086867 = EXT:sg_mail/Configuration/Yaml/Forms/FormEditorSetup.yaml
+	1499088215 = EXT:sg_mail/Configuration/Yaml/Forms/FormEngineSetup.yaml
+}
\ No newline at end of file
diff --git a/Configuration/Yaml/Forms/FormEditorSetup.yaml b/Configuration/Yaml/Forms/FormEditorSetup.yaml
new file mode 100644
index 00000000..a407bd0e
--- /dev/null
+++ b/Configuration/Yaml/Forms/FormEditorSetup.yaml
@@ -0,0 +1,52 @@
+TYPO3:
+  CMS:
+    Form:
+      prototypes:
+        standard:
+          formElementsDefinition:
+            Form:
+              formEditor:
+                editors:
+                  900:
+                    # Extend the finisher dropdown in the menu
+                    selectOptions:
+                      35:
+                        value: 'SgMailFinisher'
+                        label: 'SgMail Finisher'
+                propertyCollections:
+                  finishers:
+                    # define the finishers field
+                    25:
+                      identifier: 'SgMailFinisher'
+                      editors:
+                        __inheritances:
+                          10: 'TYPO3.CMS.Form.mixins.formElementMixins.BaseCollectionEditorsMixin'
+                        100:
+                          label: "SgMail - E-Mail to sender(form submitter)"
+                        110:
+                          identifier: 'extensionKey'
+                          templateName: 'Inspector-TextEditor'
+                          label: 'Extension key'
+                          propertyPath: 'options.extensionKey'
+                          propertyValidators:
+                            10: 'NotEmpty'
+                        140:
+                          identifier: 'templateName'
+                          templateName: 'Inspector-TextEditor'
+                          label: 'Template name'
+                          propertyPath: 'options.templateName'
+#                          enableFormelementSelectionButton: true
+                          propertyValidators:
+                            10: 'NotEmpty'
+#                            20: 'FormElementIdentifierWithinCurlyBracesInclusive'
+          # Hier ist definiert welche optionen das javascript im backend beim hinzufügen lädt.
+          # Am besten sollten es die selben sein wie in finisher.
+          finishersDefinition:
+            SgMailFinisher:
+              formEditor:
+                iconIdentifier: 't3-form-icon-finisher'
+                label: 'A Label that seems to be never used...'
+                predefinedDefaults:
+                  options:
+                    apiKey: ''
+                    email: ''
\ No newline at end of file
diff --git a/Configuration/Yaml/Forms/FormEngineSetup.yaml b/Configuration/Yaml/Forms/FormEngineSetup.yaml
new file mode 100644
index 00000000..171f9c7c
--- /dev/null
+++ b/Configuration/Yaml/Forms/FormEngineSetup.yaml
@@ -0,0 +1,12 @@
+TYPO3:
+  CMS:
+    Form:
+      prototypes:
+        standard:
+          finishersDefinition:
+            FormsFinisher:
+              FormEngine:
+                label: "When is this label used? And for what?"
+                elements:
+                  extensionKey: {label: extensionKey, config: {type: input}}
+                  templateName:  {label: templateName , config: {type: input}}
\ No newline at end of file
-- 
GitLab