src/Admin/PieceJointeAdmin.php line 29

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Admin;
  4. use App\Entity\PieceJointe;
  5. use App\Form\BlockType;
  6. use App\Form\ImageAdminType;
  7. use App\Form\PieceJointeType;
  8. use App\Form\PlanLotType;
  9. use Sonata\AdminBundle\Form\Type\TemplateType;
  10. use Sonata\AdminBundle\Form\FormMapper;
  11. use Sonata\AdminBundle\Show\ShowMapper;
  12. use Sonata\AdminBundle\Admin\AbstractAdmin;
  13. use Sonata\AdminBundle\Datagrid\ListMapper;
  14. use Sonata\AdminBundle\Datagrid\DatagridMapper;
  15. use Symfony\Component\Validator\Constraints\File;
  16. use Sonata\AdminBundle\Datagrid\DatagridInterface;
  17. use Symfony\Component\Form\Extension\Core\Type\RadioType;
  18. use Symfony\Component\Form\Extension\Core\Type\FileType;
  19. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  20. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  21. final class PieceJointeAdmin extends AbstractAdmin
  22. {
  23.     protected $datagridValues = [
  24.         '_page' => 1,
  25.         '_sort_order' => 'DESC',
  26.         '_sort_by' => 'id',
  27.         '_per_page' => 250,
  28.     ];
  29.     protected function configureDefaultSortValues(array &$sortValues): void
  30.     {
  31.         $sortValues[DatagridInterface::PAGE] = 1;
  32.         $sortValues[DatagridInterface::SORT_ORDER] = 'DESC';
  33.         $sortValues[DatagridInterface::SORT_BY] = 'id';
  34.         $sortValues[DatagridInterface::PER_PAGE] = 250;
  35.     }
  36.     protected function configureDatagridFilters(DatagridMapper $filter): void
  37.     {
  38.         $filter
  39.             ->add('filename')
  40.             ->add('descriptionCourte')
  41.             ;
  42.     }
  43.     protected function configureListFields(ListMapper $list): void
  44.     {
  45.         $list
  46.             ->add('filename')
  47.             ->add('descriptionCourte')
  48.             ->add(ListMapper::NAME_ACTIONSnull, [
  49.                 'actions' => [
  50.                     'show' => [],
  51.                     'edit' => [],
  52.                     'delete' => [],
  53.                 ],
  54.             ]);
  55.     }
  56.     protected function configureFormFields(FormMapper $form): void
  57.     {
  58.         $lot $this->getSubject()->getLot();
  59.         $isInProgramme $lot === null;
  60.         $classDescriptionCourte 'pj-description-courte-field';
  61.         $pieceJointe $this->getSubject();
  62.         if($pieceJointe->getPieceJointeMere()){
  63.             $classDescriptionCourte .= ' pj-of-programme';
  64.         }
  65.         $isLotContext $this->hasParentFieldDescription() && $this->getParentFieldDescription()->getAdmin() instanceof LotAdmin;
  66.         if ($isLotContext) {
  67.             $form->add('showPublic'null, [
  68.                 'required' => false,
  69.                 'label' => 'Visibilité Client',
  70.                 'row_attr' => [
  71.                     'class' => 'showPublic'
  72.                 ]
  73.             ]);
  74.         }
  75.         $form
  76.             // ->add('_rotate', PieceJointeType::class, [
  77.             //     'mapped' => false,
  78.             //     'label' => 'Faire pivoter',
  79.             //     'required' => false
  80.             // ])
  81.             ->add('ordre'null, [
  82.                 'required' => false,
  83.                 'label' => 'Ordre'
  84.             ])
  85.             ->add('descriptionCourte'null, [
  86.                 'attr' => [
  87.                     'class' => $classDescriptionCourte,
  88.                     'oninput' => 'this.value = this.value.toUpperCase()'
  89.                 ]
  90.             ])
  91.             ->add('fichier'PieceJointeType::class, [// unmapped means that this field is not associated to any entity property
  92.                 'mapped' => false,
  93.                 'save_path' => PieceJointe::SAVE_PATH,
  94.                 'attr' => [
  95.                     'accept' => 'application/pdf,application/x-pdf,image/jpeg,image/gif,image/png',
  96.                 ],
  97.                 'label' => 'Fichier',
  98.                 // make it optional so you don't have to re-upload the PDF file
  99.                 // every time you edit the Product details
  100.                 'required' => false,
  101.                 // unmapped fields can't define their validation using annotations
  102.                 // in the associated entity, so you can use the PHP constraint classes
  103.                 'constraints' => [
  104.                     new File([
  105.                         'maxSize' => '32768k',
  106.                         'mimeTypes' => [
  107.                             'application/pdf',
  108.                             'application/x-pdf',
  109.                             'image/jpeg',
  110.                             'image/png',
  111.                             'image/gif',
  112.                         ],
  113.                         'mimeTypesMessage' => 'Vous devez choisir un fichier au format PDF ou image.',
  114.                     ])
  115.                 ]
  116.             ])
  117.             ;
  118.             if (!$isInProgramme) {
  119.             $form
  120.                 ->add('estUnPlan'PlanLotType::class, [
  121.                     'label' => 'Plan du lot',
  122.                     'attr' => [
  123.                         'class' => 'checkbox-plan',
  124.                         'data-sonata-icheck' => 'false'
  125.                     ],
  126.                     'help' => 'Cocher pour faire apparaitre ce doc dans recap',
  127.                     'required' => false
  128.                 ])
  129.                 ->add('estImageAdmin'ImageAdminType::class, [
  130.                     'label' => 'Image pour l\'administrateur du lot',
  131.                     'attr' => [
  132.                         'class' => 'checkbox-image-admin',
  133.                         'data-sonata-icheck' => 'false'
  134.                     ],
  135.                     'help' => 'Cocher pour faire apparaitre ce doc dans la fiche administrateur du lot',
  136.                     'required' => false
  137.                 ])
  138.                 ;
  139.             }
  140.     }
  141.     protected function configureShowFields(ShowMapper $show): void
  142.     {
  143.         $show
  144.             ->add('filename')
  145.             ->add('descriptionCourte')
  146.             ;
  147.     }
  148. }