Use the name of an uploaded file for an attachment when no explicit name was set.

This commit is contained in:
Jan Böhmer 2023-03-05 23:47:45 +01:00
parent 4a30819ea5
commit 52e459ec60
3 changed files with 40 additions and 6 deletions

View file

@ -25,6 +25,7 @@ namespace App\Entity\Attachments;
use App\Entity\Base\AbstractNamedDBElement; use App\Entity\Base\AbstractNamedDBElement;
use App\Validator\Constraints\Selectable; use App\Validator\Constraints\Selectable;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\Validator\Constraints as Assert;
use function in_array; use function in_array;
use InvalidArgumentException; use InvalidArgumentException;
@ -95,6 +96,14 @@ abstract class Attachment extends AbstractNamedDBElement
*/ */
protected string $path = ''; protected string $path = '';
/**
* @var string the name of this element
* @ORM\Column(type="string")
* @Assert\NotBlank(message="validator.attachment.name_not_blank")
* @Groups({"simple", "extended", "full"})
*/
protected string $name = '';
/** /**
* ORM mapping is done in sub classes (like PartAttachment). * ORM mapping is done in sub classes (like PartAttachment).
*/ */

View file

@ -44,6 +44,7 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Security; use Symfony\Component\Security\Core\Security;
use Symfony\Component\Validator\Constraints\File; use Symfony\Component\Validator\Constraints\File;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Url; use Symfony\Component\Validator\Constraints\Url;
use Symfony\Contracts\Translation\TranslatorInterface; use Symfony\Contracts\Translation\TranslatorInterface;
@ -72,8 +73,11 @@ class AttachmentFormType extends AbstractType
public function buildForm(FormBuilderInterface $builder, array $options): void public function buildForm(FormBuilderInterface $builder, array $options): void
{ {
$builder->add('name', TextType::class, [ $builder
->add('name', TextType::class, [
'label' => 'attachment.edit.name', 'label' => 'attachment.edit.name',
'required' => false,
'empty_data' => '',
]) ])
->add('attachment_type', StructuralEntityType::class, [ ->add('attachment_type', StructuralEntityType::class, [
'label' => 'attachment.edit.attachment_type', 'label' => 'attachment.edit.attachment_type',
@ -134,6 +138,7 @@ class AttachmentFormType extends AbstractType
], ],
]); ]);
$builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event): void { $builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event): void {
$form = $event->getForm(); $form = $event->getForm();
$attachment = $form->getData(); $attachment = $form->getData();
@ -141,13 +146,27 @@ class AttachmentFormType extends AbstractType
$file_form = $form->get('file'); $file_form = $form->get('file');
$file = $file_form->getData(); $file = $file_form->getData();
if ($attachment instanceof Attachment && $file instanceof UploadedFile && $attachment->getAttachmentType( if (!$attachment instanceof Attachment) {
) && !$this->submitHandler->isValidFileExtension($attachment->getAttachmentType(), $file)) { return;
}
if (!$file instanceof UploadedFile) {
return;
}
//Ensure that the file extension is allowed for the selected attachment type
if ($attachment->getAttachmentType()
&& !$this->submitHandler->isValidFileExtension($attachment->getAttachmentType(), $file)) {
$event->getForm()->get('file')->addError( $event->getForm()->get('file')->addError(
new FormError($this->translator->trans('validator.file_ext_not_allowed')) new FormError($this->translator->trans('validator.file_ext_not_allowed'))
); );
} }
});
//If the name is empty, use the original file name as attachment name
if (empty($attachment->getName())) {
$attachment->setName($file->getClientOriginalName());
}
}, 100000);
//Check the secure file checkbox, if file is in securefile location //Check the secure file checkbox, if file is in securefile location
$builder->get('secureFile')->addEventListener( $builder->get('secureFile')->addEventListener(

View file

@ -299,5 +299,11 @@
<target>The part name does not match the regular expression stated by the category: %regex%</target> <target>The part name does not match the regular expression stated by the category: %regex%</target>
</segment> </segment>
</unit> </unit>
<unit id="m8kMFhf" name="validator.attachment.name_not_blank">
<segment>
<source>validator.attachment.name_not_blank</source>
<target>Set a value here, or upload a file to automatically use its filename as name for the attachment.</target>
</segment>
</unit>
</file> </file>
</xliff> </xliff>