src/Domain/Documentation/Form/Type/DocumentType.php line 213

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of the MADIS - RGPD Management application.
  4.  *
  5.  * @copyright Copyright (c) 2018-2019 Soluris - Solutions Numériques Territoriales Innovantes
  6.  * @author Donovan Bourlard <donovan@awkan.fr>
  7.  *
  8.  * This program is free software: you can redistribute it and/or modify
  9.  * it under the terms of the GNU Affero General Public License as published by
  10.  * the Free Software Foundation, either version 3 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU Affero General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Affero General Public License
  19.  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  20.  */
  21. declare(strict_types=1);
  22. namespace App\Domain\Documentation\Form\Type;
  23. use App\Domain\Documentation\Model;
  24. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  25. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  26. use Symfony\Component\Form\AbstractType;
  27. use Symfony\Component\Form\Exception\TransformationFailedException;
  28. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  29. use Symfony\Component\Form\Extension\Core\Type\FileType;
  30. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  31. use Symfony\Component\Form\Extension\Core\Type\TextType;
  32. use Symfony\Component\Form\Extension\Core\Type\UrlType;
  33. use Symfony\Component\Form\FormBuilderInterface;
  34. use Symfony\Component\Form\FormEvent;
  35. use Symfony\Component\Form\FormEvents;
  36. use Symfony\Component\HttpFoundation\RequestStack;
  37. use Symfony\Component\OptionsResolver\OptionsResolver;
  38. use Symfony\Component\Validator\Constraints\File;
  39. use Symfony\Component\Validator\Constraints\Image;
  40. use Symfony\Contracts\Translation\TranslatorInterface;
  41. class DocumentType extends AbstractType implements EventSubscriberInterface
  42. {
  43.     private RequestStack $requestStack;
  44.     private string $maxSize;
  45.     private TranslatorInterface $translator;
  46.     public function __construct(RequestStack $requestStackstring $maxSizeTranslatorInterface $translator)
  47.     {
  48.         $this->requestStack $requestStack;
  49.         $this->maxSize      $maxSize;
  50.         $this->translator   $translator;
  51.     }
  52.     /**
  53.      * Build type form.
  54.      */
  55.     public function buildForm(FormBuilderInterface $builder, array $options)
  56.     {
  57.         $request $this->requestStack->getCurrentRequest();
  58.         $builder
  59.             ->add('isLink'HiddenType::class, [
  60.                 'label'      => false,
  61.                 'required'   => false,
  62.                 'empty_data' => '0',
  63.             ])
  64.             ->add('name'TextType::class, [
  65.                 'label' => 'documentation.document.label.name',
  66.             ])
  67.             ->add('categories'EntityType::class, [
  68.                 'label'        => 'documentation.document.label.categories',
  69.                 'class'        => 'App\Domain\Documentation\Model\Category',
  70.                 'choice_label' => 'name',
  71.                 'multiple'     => true,
  72.                 'required'     => false,
  73.                 'expanded'     => false,
  74.                 'attr'         => [
  75.                     'class'            => 'selectpicker',
  76.                     'data-live-search' => 'true',
  77.                     'title'            => 'global.placeholder.multiple_select',
  78.                     'aria-label'       => 'Catégories',
  79.                 ],
  80.             ])
  81.             ->add('thumbUploadedFile'FileType::class, [
  82.                 'label'       => 'documentation.document.label.thumbnail',
  83.                 'required'    => false,
  84.                 'constraints' => [
  85.                     // Élément suivant commenté, car il génère un message d'erreur en plus de l'autre message
  86.                     // new Image(['groups' => ['default']]),
  87.                     new File([
  88.                         'maxSize'   => $this->maxSize,
  89.                         'groups'    => ['default'],
  90.                         'mimeTypes' => [
  91.                             'image/png'// .png
  92.                             'image/jpg'// .jpg
  93.                             'image/jpeg'// .jpeg
  94.                         ],
  95.                         'mimeTypesMessage' => 'document_validator.document_file.thumbnail',
  96.                     ]),
  97.                 ],
  98.                 'attr' => [
  99.                     'accept' => 'image/*',
  100.                 ],
  101.             ])
  102.             ->add('pinned'CheckboxType::class, [
  103.                 'label'    => 'documentation.document.label.pinned',
  104.                 'required' => false,
  105.             ])
  106.         ;
  107.         $builder->addEventSubscriber($this);
  108.     }
  109.     /**
  110.      * Provide type options.
  111.      */
  112.     public function configureOptions(OptionsResolver $resolver)
  113.     {
  114.         $resolver
  115.             ->setDefaults([
  116.                 'data_class'        => Model\Document::class,
  117.                 'validation_groups' => [
  118.                     'default',
  119.                     'document',
  120.                 ],
  121.             ]);
  122.     }
  123.     public static function getSubscribedEvents()
  124.     {
  125.         return [
  126.             FormEvents::SUBMIT       => 'ensureOneFieldIsSubmitted',
  127.             FormEvents::PRE_SET_DATA => 'setIsLink',
  128.         ];
  129.     }
  130.     public function setIsLink(FormEvent $event)
  131.     {
  132.         $isLink = (bool) $this->requestStack->getCurrentRequest()->get('isLink');
  133.         $data   $event->getData();
  134.         if (!$data->getId()) {
  135.             $data->setIsLink($isLink);
  136.         }
  137.         // $data->setIsLink($isLink);
  138.         $event->setData($data);
  139.         $form $event->getForm();
  140.         if ($data->getThumbUrl()) {
  141.             $form->add('removeThumb'HiddenType::class, [
  142.                 'label'    => 'documentation.document.action.removeThumb',
  143.                 'required' => false,
  144.             ]);
  145.         }
  146.         if ($isLink || (true === $data->getIsLink())) {
  147.             $form->add('url'UrlType::class, [
  148.                 'label'    => 'documentation.document.label.url',
  149.                 'required' => true,
  150.             ]);
  151.             $form->add('isLink'HiddenType::class, [
  152.                 'data' => 1,
  153.             ]);
  154.         } else {
  155.             $form->add('isLink'HiddenType::class, [
  156.                 'data' => 0,
  157.             ]);
  158.             $form->add('uploadedFile'FileType::class, [
  159.                 'label'       => 'documentation.document.label.file',
  160.                 'required'    => !$data->getId(),
  161.                 'constraints' => [
  162.                     new File([
  163.                         'maxSize'   => $this->maxSize,
  164.                         'groups'    => ['default'],
  165.                         'mimeTypes' => [
  166.                             'image/png'// .png
  167.                             'image/jpg'// .jpg
  168.                             'image/jpeg'// .jpeg
  169.                             'audio/mpeg'// .mp3
  170.                             'audio/ogg'// .ogg
  171.                             'audio/wav'// .wav
  172.                             'audio/m4a'// .m4a
  173.                             'video/mp4'// .mp4
  174.                             'video/quicktime'// .mov
  175.                             'video/avi'// .avi
  176.                             'video/mpeg'// .mpg
  177.                             'video/x-ms-wmv'// .wmv
  178.                             'video/ogg'// .ogv, .ogg
  179.                             'video/webm'// .webm
  180.                             'application/pdf'// .pdf
  181.                             'application/msword'// .doc
  182.                             'application/vnd.openxmlformats-officedocument.wordprocessingml.document'// .docx
  183.                             'application/vnd.oasis.opendocument.text'// .odt
  184.                             'application/vnd.ms-powerpoint'// .ppt
  185.                             'application/vnd.openxmlformats-officedocument.presentationml.presentation'// .pptx
  186.                             'application/vnd.oasis.opendocument.presentation'// .odp
  187.                             'application/vnd.ms-excel'// .xls
  188.                             'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'// .xlsx
  189.                             'application/vnd.ms-excel.sheet.macroEnabled.12'// .xlsm
  190.                             'application/vnd.oasis.opendocument.spreadsheet'// .ods
  191.                         ],
  192.                         'mimeTypesMessage' => 'document_validator.document_file.file',
  193.                     ]),
  194.                 ],
  195.             ]);
  196.         }
  197.     }
  198.     public function ensureOneFieldIsSubmitted(FormEvent $event)
  199.     {
  200.         $submittedData $event->getData();
  201.         if (!$submittedData->getUploadedFile() && !$submittedData->getUrl()) {
  202.             $error $this->translator->trans('documentation.document.form.error.fileorurl');
  203.             throw new TransformationFailedException($error400/* code */ null/* previous */ $error/* user message */ ['{{ what }}' => 'aa'/* message context for the translater */);
  204.         }
  205.         if (true === $submittedData->getIsLink() && !$submittedData->getUrl()) {
  206.             $error $this->translator->trans('documentation.document.form.error.missingurl');
  207.             throw new TransformationFailedException($error400/* code */ null/* previous */ $error/* user message */ ['{{ what }}' => 'aa'/* message context for the translater */);
  208.         }
  209.     }
  210. }