Skip to content

Commit

Permalink
feat: add multiple option to FileUpload element
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdmlln committed Nov 13, 2024
1 parent e7b2394 commit 54e2c1d
Show file tree
Hide file tree
Showing 14 changed files with 6,435 additions and 1 deletion.
58 changes: 58 additions & 0 deletions Classes/Domain/Finishers/EmailFinisher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace Remind\Form\Domain\Finishers;

use TYPO3\CMS\Core\Mail\FluidEmail;
use TYPO3\CMS\Extbase\Domain\Model\FileReference;
use TYPO3\CMS\Form\Domain\Finishers\EmailFinisher as BaseEmailFinisher;
use TYPO3\CMS\Form\Domain\Model\FormElements\FileUpload;
use TYPO3\CMS\Form\Domain\Runtime\FormRuntime;

class EmailFinisher extends BaseEmailFinisher
{
private bool $attachUploads = false;

protected function executeInternal(): string|null
{
// temporarily store attachUploads option to use in initializeFluidEmail
$this->attachUploads = (bool) $this->parseOption('attachUploads');

// set option to false so BaseEmailFinisher skips attaching uploads
$this->options['attachUploads'] = false;

parent::executeInternal();

return null;
}

protected function initializeFluidEmail(FormRuntime $formRuntime): FluidEmail
{
$mail = parent::initializeFluidEmail($formRuntime);

if ($this->attachUploads) {
foreach ($formRuntime->getFormDefinition()->getRenderablesRecursively() as $element) {
if (!$element instanceof FileUpload) {
continue;
}
$files = $formRuntime[$element->getIdentifier()];
if ($files) {
// $files may contain single file or multiple files
if (!is_array($files)) {
$files = [$files];
}

foreach ($files as $file) {
if ($file instanceof FileReference) {
$file = $file->getOriginalResource();
}
$mail->attach($file->getContents(), $file->getName(), $file->getMimeType());
}
}
}
}

return $mail;
}
}
28 changes: 28 additions & 0 deletions Classes/Mvc/Property/PropertyMappingConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Remind\Form\Mvc\Property;

use Remind\Form\Mvc\Property\TypeConverter\MultiUploadedFileReferenceConverter;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Form\Domain\Model\FormElements\FileUpload;
use TYPO3\CMS\Form\Domain\Model\Renderable\RenderableInterface;

class PropertyMappingConfiguration
{
public function afterBuildingFinished(RenderableInterface $renderable): void
{
if (
$renderable instanceof FileUpload &&
!empty($renderable->getProperties()['multiple'])
) {
$typeConverter = GeneralUtility::makeInstance(MultiUploadedFileReferenceConverter::class);
$renderable
->getRootForm()
->getProcessingRule($renderable->getIdentifier())
->getPropertyMappingConfiguration()
->setTypeConverter($typeConverter);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Remind\Form\Mvc\Property\TypeConverter;

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Error\Error;
use TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface;
use TYPO3\CMS\Extbase\Property\TypeConverter\AbstractTypeConverter;
use TYPO3\CMS\Form\Mvc\Property\TypeConverter\UploadedFileReferenceConverter;

class MultiUploadedFileReferenceConverter extends AbstractTypeConverter
{
private UploadedFileReferenceConverter $uploadedFileReferenceConverter;

public function __construct()
{
$this->uploadedFileReferenceConverter = GeneralUtility::makeInstance(UploadedFileReferenceConverter::class);
}

/**
* @param mixed[] $convertedChildProperties
* @return \TYPO3\CMS\Extbase\Domain\Model\AbstractFileFolder[]|Error|null
*/
public function convertFrom(
mixed $source,
mixed $targetType,
array $convertedChildProperties = [],
PropertyMappingConfigurationInterface $configuration = null
): array|Error|null {
if (is_array($source)) {
$resources = [];
foreach ($source as $file) {
$resource = $this->uploadedFileReferenceConverter->convertFrom($file, $targetType, $convertedChildProperties, $configuration);

if ($resource instanceof Error) {
return $resource;
}

if ($resource) {
$resources[] = $resource;
}
}
return $resources;
}

return null;
}
}
15 changes: 15 additions & 0 deletions Configuration/TypoScript/setup.typoscript
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
plugin.tx_form {
settings {
yamlConfigurations {
1731425366 = EXT:rmnd_form/Configuration/Yaml/FormSetup.yaml
}
}
}

module.tx_form {
settings {
yamlConfigurations {
1731425366 = EXT:rmnd_form/Configuration/Yaml/FormSetup.yaml
}
}
}
5 changes: 5 additions & 0 deletions Configuration/Yaml/Finishers/EmailToReceiver.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
prototypes:
standard:
finishersDefinition:
EmailToReceiver:
implementationClassName: Remind\Form\Domain\Finishers\EmailFinisher
5 changes: 5 additions & 0 deletions Configuration/Yaml/Finishers/EmailToSender.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
prototypes:
standard:
finishersDefinition:
EmailToSender:
implementationClassName: Remind\Form\Domain\Finishers\EmailFinisher
23 changes: 23 additions & 0 deletions Configuration/Yaml/FormElements/FileUpload.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
prototypes:
standard:
formElementsDefinition:
FileUpload:
formEditor:
editors:
299:
identifier: multiple
templateName: Inspector-CheckboxEditor
label: formEditor.elements.FileUpload.editor.multiple.label
propertyPath: properties.multiple
# Add Mime Types from ImageUpload Element
300:
selectOptions:
80:
value: image/jpeg
label: formEditor.elements.ImageUpload.editor.allowedMimeTypes.jpg
90:
value: image/png
label: formEditor.elements.ImageUpload.editor.allowedMimeTypes.png
100:
value: image/bmp
label: formEditor.elements.ImageUpload.editor.allowedMimeTypes.bmp
4 changes: 4 additions & 0 deletions Configuration/Yaml/FormElements/ImageUpload.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
prototypes:
standard:
formElementsDefinition:
ImageUpload: null
15 changes: 15 additions & 0 deletions Configuration/Yaml/FormSetup.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
imports:
- { resource: "./FormElements/FileUpload.yaml" }
- { resource: "./FormElements/ImageUpload.yaml" }

- { resource: "./Finishers/EmailToSender.yaml" }
- { resource: "./Finishers/EmailToReceiver.yaml" }

prototypes:
standard:
formEditor:
translationFiles:
1731478859: "EXT:rmnd_form/Resources/Private/Language/locallang.xlf"
formEngine:
translationFiles:
1731478859: "EXT:rmnd_form/Resources/Private/Language/locallang.xlf"
11 changes: 11 additions & 0 deletions Resources/Private/Language/de.locallang.xlf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<xliff version="1.0">
<file source-language="en" datatype="plaintext" original="EXT:rmnd_form/Resources/Private/Language/de.locallang.xlf" product-name="rmnd_form">
<header/>
<body>
<trans-unit id="formEditor.elements.FileUpload.editor.multiple.label">
<target>Mehrfach</target>
</trans-unit>
</body>
</file>
</xliff>
11 changes: 11 additions & 0 deletions Resources/Private/Language/locallang.xlf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<xliff version="1.0">
<file source-language="en" datatype="plaintext" original="EXT:rmnd_form/Resources/Private/Language/locallang.xlf" product-name="rmnd_form">
<header/>
<body>
<trans-unit id="formEditor.elements.FileUpload.editor.multiple.label">
<source>Multiple</source>
</trans-unit>
</body>
</file>
</xliff>
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
}
},
"require": {
"typo3/cms-core": "^12.4"
"typo3/cms-core": "^12.4",
"typo3/cms-extbase": "^12.4",
"typo3/cms-form": "^12.4"
},
"require-dev": {
"dealerdirect/phpcodesniffer-composer-installer": "^1.0",
Expand Down
Loading

0 comments on commit 54e2c1d

Please sign in to comment.