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

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 Doctrine\ORM\EntityRepository;
  25. use Doctrine\ORM\QueryBuilder;
  26. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  27. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  28. use Symfony\Component\Form\AbstractType;
  29. use Symfony\Component\Form\Exception\TransformationFailedException;
  30. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  31. use Symfony\Component\Form\Extension\Core\Type\FileType;
  32. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  33. use Symfony\Component\Form\Extension\Core\Type\TextType;
  34. use Symfony\Component\Form\Extension\Core\Type\UrlType;
  35. use Symfony\Component\Form\FormBuilderInterface;
  36. use Symfony\Component\Form\FormEvent;
  37. use Symfony\Component\Form\FormEvents;
  38. use Symfony\Component\HttpFoundation\RequestStack;
  39. use Symfony\Component\OptionsResolver\OptionsResolver;
  40. use Symfony\Component\Validator\Constraints\File;
  41. use Symfony\Component\Validator\Constraints\Image;
  42. use Symfony\Contracts\Translation\TranslatorInterface;
  43. class DocumentType extends AbstractType implements EventSubscriberInterface
  44. {
  45.     private RequestStack $requestStack;
  46.     private string $maxSize;
  47.     private TranslatorInterface $translator;
  48.     public function __construct(RequestStack $requestStackstring $maxSizeTranslatorInterface $translator)
  49.     {
  50.         $this->requestStack $requestStack;
  51.         $this->maxSize      $maxSize;
  52.         $this->translator   $translator;
  53.     }
  54.     /**
  55.      * Build type form.
  56.      */
  57.     public function buildForm(FormBuilderInterface $builder, array $options)
  58.     {
  59.         $request $this->requestStack->getCurrentRequest();
  60.         $builder
  61.             ->add('isLink'HiddenType::class, [
  62.                 'label'      => false,
  63.                 'required'   => false,
  64.                 'empty_data' => '0',
  65.             ])
  66.             ->add('name'TextType::class, [
  67.                 'label'       => 'documentation.document.label.name',
  68.                 'purify_html' => true,
  69.             ])
  70.             ->add('categories'EntityType::class, [
  71.                 'label'         => 'documentation.document.label.categories',
  72.                 'class'         => 'App\Domain\Documentation\Model\Category',
  73.                 'query_builder' => function (EntityRepository $er): QueryBuilder {
  74.                     return $er->createQueryBuilder('c')
  75.                         ->orderBy('c.name''ASC');
  76.                 },
  77.                 'choice_label' => 'name',
  78.                 'multiple'     => true,
  79.                 'required'     => false,
  80.                 'expanded'     => false,
  81.                 'attr'         => [
  82.                     'class'            => 'selectpicker',
  83.                     'data-live-search' => 'true',
  84.                     'title'            => 'global.placeholder.multiple_select',
  85.                     'aria-label'       => 'Catégories',
  86.                 ],
  87.             ])
  88.             ->add('thumbUploadedFile'FileType::class, [
  89.                 'label'       => 'documentation.document.label.thumbnail',
  90.                 'required'    => false,
  91.                 'constraints' => [
  92.                     // Élément suivant commenté, car il génère un message d'erreur en plus de l'autre message
  93.                     // new Image(['groups' => ['default']]),
  94.                     new File([
  95.                         'maxSize'   => $this->maxSize,
  96.                         'groups'    => ['default'],
  97.                         'mimeTypes' => [
  98.                             'image/png'// .png
  99.                             'image/jpeg'// .jpg, .jpeg
  100.                         ],
  101.                         'mimeTypesMessage' => 'document_validator.document_file.thumbnail',
  102.                     ]),
  103.                 ],
  104.                 'attr' => [
  105.                     'accept' => 'image/*',
  106.                 ],
  107.             ])
  108.             ->add('pinned'CheckboxType::class, [
  109.                 'label'    => 'documentation.document.label.pinned',
  110.                 'required' => false,
  111.             ])
  112.         ;
  113.         $builder->addEventSubscriber($this);
  114.     }
  115.     /**
  116.      * Provide type options.
  117.      */
  118.     public function configureOptions(OptionsResolver $resolver)
  119.     {
  120.         $resolver
  121.             ->setDefaults([
  122.                 'data_class'        => Model\Document::class,
  123.                 'validation_groups' => [
  124.                     'default',
  125.                     'document',
  126.                 ],
  127.             ]);
  128.     }
  129.     public static function getSubscribedEvents()
  130.     {
  131.         return [
  132.             FormEvents::SUBMIT       => 'ensureOneFieldIsSubmitted',
  133.             FormEvents::PRE_SET_DATA => 'setIsLink',
  134.         ];
  135.     }
  136.     public function setIsLink(FormEvent $event)
  137.     {
  138.         $isLink = (bool) $this->requestStack->getCurrentRequest()->get('isLink');
  139.         $data   $event->getData();
  140.         if (!$data->getId()) {
  141.             $data->setIsLink($isLink);
  142.         }
  143.         // $data->setIsLink($isLink);
  144.         $event->setData($data);
  145.         $form $event->getForm();
  146.         if ($data->getThumbUrl()) {
  147.             $form->add('removeThumb'HiddenType::class, [
  148.                 'label'    => 'documentation.document.action.removeThumb',
  149.                 'required' => false,
  150.             ]);
  151.         }
  152.         if ($isLink || (true === $data->getIsLink())) {
  153.             $form->add('url'UrlType::class, [
  154.                 'label'    => 'documentation.document.label.url',
  155.                 'required' => true,
  156.             ]);
  157.             $form->add('isLink'HiddenType::class, [
  158.                 'data' => 1,
  159.             ]);
  160.         } else {
  161.             $form->add('isLink'HiddenType::class, [
  162.                 'data' => 0,
  163.             ]);
  164.             $form->add('uploadedFile'FileType::class, [
  165.                 'label'       => 'documentation.document.label.file',
  166.                 'required'    => !$data->getId(),
  167.                 'constraints' => [
  168.                     new File([
  169.                         'maxSize'   => $this->maxSize,
  170.                         'groups'    => ['default'],
  171.                         'mimeTypes' => [
  172.                             'image/png'// .png
  173.                             'image/jpeg'// .jpg, .jpeg
  174.                             'audio/mpeg'// .mp3
  175.                             'audio/ogg'// .ogg
  176.                             'audio/wav'// .wav
  177.                             'audio/m4a'// .m4a
  178.                             'video/mp4'// .mp4
  179.                             'video/quicktime'// .mov
  180.                             'video/x-msvideo'// .avi
  181.                             'video/mpeg'// .mpg
  182.                             'video/x-ms-wmv'// .wmv
  183.                             'video/ogg'// .ogv, .ogg
  184.                             'video/webm'// .webm
  185.                             'application/pdf'// .pdf
  186.                             'application/msword'// .doc
  187.                             'application/vnd.openxmlformats-officedocument.wordprocessingml.document'// .docx
  188.                             'application/vnd.oasis.opendocument.text'// .odt
  189.                             'application/vnd.ms-powerpoint'// .ppt
  190.                             'application/vnd.openxmlformats-officedocument.presentationml.presentation'// .pptx
  191.                             'application/vnd.oasis.opendocument.presentation'// .odp
  192.                             'application/vnd.ms-excel'// .xls
  193.                             'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'// .xlsx
  194.                             'application/vnd.ms-excel.sheet.macroEnabled.12'// .xlsm
  195.                             'application/vnd.oasis.opendocument.spreadsheet'// .ods
  196.                         ],
  197.                         'mimeTypesMessage' => 'document_validator.document_file.file',
  198.                     ]),
  199.                 ],
  200.             ]);
  201.         }
  202.     }
  203.     public function ensureOneFieldIsSubmitted(FormEvent $event)
  204.     {
  205.         $submittedData $event->getData();
  206.         if (!$submittedData->getUploadedFile() && !$submittedData->getUrl()) {
  207.             $error $this->translator->trans('documentation.document.form.error.fileorurl');
  208.             throw new TransformationFailedException($error400/* code */ null/* previous */ $error/* user message */ ['{{ what }}' => 'aa'/* message context for the translater */);
  209.         }
  210.         if (true === $submittedData->getIsLink() && !$submittedData->getUrl()) {
  211.             $error $this->translator->trans('documentation.document.form.error.missingurl');
  212.             throw new TransformationFailedException($error400/* code */ null/* previous */ $error/* user message */ ['{{ what }}' => 'aa'/* message context for the translater */);
  213.         }
  214.     }
  215. }