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

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.form.label.name',
  66.             ])
  67.             ->add('categories'EntityType::class, [
  68.                 'label'        => 'documentation.document.form.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'            => 'placeholder.multiple_select',
  78.                     'aria-label'       => 'Catégories',
  79.                 ],
  80.             ])
  81.             ->add('thumbUploadedFile'FileType::class, [
  82.                 'label'       => 'documentation.document.form.label.thumbnail',
  83.                 'required'    => false,
  84.                 'constraints' => [
  85.                     new Image(['groups' => ['default']]),
  86.                     new File([
  87.                         'maxSize'   => $this->maxSize,
  88.                         'groups'    => ['default'],
  89.                         'mimeTypes' => [
  90.                             'image/png'// .png
  91.                             'image/jpg'// .jpg
  92.                             'image/jpeg'// .jpeg
  93.                         ],
  94.                         'mimeTypesMessage' => 'Les formats autorisés sont .png, .jpg, .jpeg.',
  95.                     ]),
  96.                 ],
  97.                 'attr' => [
  98.                     'accept' => 'image/*',
  99.                 ],
  100.             ])
  101.             ->add('pinned'CheckboxType::class, [
  102.                 'label'    => 'documentation.document.form.label.pinned',
  103.                 'required' => false,
  104.             ])
  105.         ;
  106.         $builder->addEventSubscriber($this);
  107.     }
  108.     /**
  109.      * Provide type options.
  110.      */
  111.     public function configureOptions(OptionsResolver $resolver)
  112.     {
  113.         $resolver
  114.             ->setDefaults([
  115.                 'data_class'        => Model\Document::class,
  116.                 'validation_groups' => [
  117.                     'default',
  118.                     'document',
  119.                 ],
  120.             ]);
  121.     }
  122.     public static function getSubscribedEvents()
  123.     {
  124.         return [
  125.             FormEvents::SUBMIT       => 'ensureOneFieldIsSubmitted',
  126.             FormEvents::PRE_SET_DATA => 'setIsLink',
  127.         ];
  128.     }
  129.     public function setIsLink(FormEvent $event)
  130.     {
  131.         $isLink = (bool) $this->requestStack->getCurrentRequest()->get('isLink');
  132.         $data   $event->getData();
  133.         if (!$data->getId()) {
  134.             $data->setIsLink($isLink);
  135.         }
  136.         // $data->setIsLink($isLink);
  137.         $event->setData($data);
  138.         $form $event->getForm();
  139.         if ($data->getThumbUrl()) {
  140.             $form->add('removeThumb'HiddenType::class, [
  141.                 'label'    => 'documentation.document.form.label.removeThumb',
  142.                 'required' => false,
  143.             ]);
  144.         }
  145.         if ($isLink || (true === $data->getIsLink())) {
  146.             $form->add('url'UrlType::class, [
  147.                 'label'    => 'documentation.document.form.label.url',
  148.                 'required' => true,
  149.             ]);
  150.             $form->add('isLink'HiddenType::class, [
  151.                 'data' => 1,
  152.             ]);
  153.         } else {
  154.             $form->add('isLink'HiddenType::class, [
  155.                 'data' => 0,
  156.             ]);
  157.             $form->add('uploadedFile'FileType::class, [
  158.                 'label'       => 'documentation.document.form.label.file',
  159.                 'required'    => !$data->getId(),
  160.                 'constraints' => [
  161.                     new File([
  162.                         'maxSize'   => $this->maxSize,
  163.                         'groups'    => ['default'],
  164.                         'mimeTypes' => [
  165.                             'image/png'// .png
  166.                             'image/jpg'// .jpg
  167.                             'image/jpeg'// .jpeg
  168.                             'audio/mpeg'// .mp3
  169.                             'audio/ogg'// .ogg
  170.                             'audio/wav'// .wav
  171.                             'audio/m4a'// .m4a
  172.                             'video/mp4'// .mp4
  173.                             'video/quicktime'// .mov
  174.                             'video/avi'// .avi
  175.                             'video/mpeg'// .mpg
  176.                             'video/x-ms-wmv'// .wmv
  177.                             'video/ogg'// .ogv, .ogg
  178.                             'video/webm'// .webm
  179.                             'application/pdf'// .pdf
  180.                             'application/msword'// .doc
  181.                             'application/vnd.openxmlformats-officedocument.wordprocessingml.document'// .docx
  182.                             'application/vnd.oasis.opendocument.text'// .odt
  183.                             'application/vnd.ms-powerpoint'// .ppt
  184.                             'application/vnd.openxmlformats-officedocument.presentationml.presentation'// .pptx
  185.                             'application/vnd.oasis.opendocument.presentation'// .odp
  186.                             'application/vnd.ms-excel'// .xls
  187.                             'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'// .xlsx
  188.                             'application/vnd.ms-excel.sheet.macroEnabled.12'// .xlsm
  189.                             'application/vnd.oasis.opendocument.spreadsheet'// .ods
  190.                         ],
  191.                         'mimeTypesMessage' => "Ce format de fichier n'est pas autorisé.",
  192.                     ]),
  193.                 ],
  194.             ]);
  195.         }
  196.     }
  197.     public function ensureOneFieldIsSubmitted(FormEvent $event)
  198.     {
  199.         $submittedData $event->getData();
  200.         if (!$submittedData->getUploadedFile() && !$submittedData->getUrl()) {
  201.             $error $this->translator->trans('documentation.document.form.error.fileorurl');
  202.             throw new TransformationFailedException($error400/* code */ null/* previous */ $error/* user message */ ['{{ what }}' => 'aa'/* message context for the translater */);
  203.         }
  204.         if (true === $submittedData->getIsLink() && !$submittedData->getUrl()) {
  205.             $error $this->translator->trans('documentation.document.form.error.missingurl');
  206.             throw new TransformationFailedException($error400/* code */ null/* previous */ $error/* user message */ ['{{ what }}' => 'aa'/* message context for the translater */);
  207.         }
  208.     }
  209. }