src/Form/Type/MaterielMouvementFormType.php line 45

Open in your IDE?
  1. <?php
  2. namespace App\Form\Type;
  3. use App\Entity\MaterielMouvement;
  4. use App\Entity\Stock;
  5. use Doctrine\ORM\EntityManager;
  6. use Doctrine\ORM\EntityRepository;
  7. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  8. use Symfony\Component\Form\AbstractType;
  9. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  10. use Symfony\Component\Form\Extension\Core\Type\IntegerType;
  11. use Symfony\Component\Form\FormBuilderInterface;
  12. use Symfony\Component\Form\FormEvent;
  13. use Symfony\Component\Form\FormEvents;
  14. use Symfony\Component\OptionsResolver\OptionsResolver;
  15. use Symfony\Component\Validator\Constraints\NotBlank;
  16. class MaterielMouvementFormType extends AbstractType
  17. {
  18.     private $em;
  19.     public function __construct(EntityManager $em)
  20.     {
  21.         $this->em $em;
  22.     }
  23.     public function buildForm(FormBuilderInterface $builder, array $options): void
  24.     {
  25.         $materielMouvement $builder->getData(); // instance de MaterielMouvement (si édition)
  26.         $selectedIds = [];
  27.         if ($materielMouvement && $materielMouvement->getIdentifiants()) {
  28.             $selectedIds $materielMouvement->getIdentifiants(); // ex: array d’IDs
  29.         }
  30.         $builder
  31.             ->add('stock'EntityType::class, [
  32.                 'class' => Stock::class,
  33.                 'choice_label' => function ($s) {
  34.                     return $s->getModele()->getIntitule();
  35.                 },
  36.                 'choice_attr' => function (Stock $stock) {
  37.                     return [
  38.                         'data-individuel' => $stock->getProduit()->getCategorie()->isIndividuel() ? 0
  39.                     ];
  40.                 },
  41.                 'placeholder' => 'Choisir',
  42.                 'attr' => [
  43.                     'class' => 'stock_select chosen-select',
  44.                 ],
  45.                 'query_builder' => function (EntityRepository $er) {
  46.                     $sub $er->createQueryBuilder('s2')
  47.                         ->select('MIN(s2.id)')
  48.                         ->innerJoin('s2.modele''m')
  49.                         ->where('m.nonDefini = false')
  50.                         // ->andWhere('s2.statut = :statut')
  51.                         ->groupBy('s2.modele');
  52.                     return $er->createQueryBuilder('s')
  53.                         ->where('s.id IN (' $sub->getDQL() . ')')
  54.                         //->setParameter('statut', 'disponible')
  55.                         ->orderBy('s.modele');
  56.                 },
  57.             ])
  58.             ->add('identifiants'ChoiceType::class, [
  59.                 'choices' => [], // rempli via Ajax
  60.                 'multiple' => true,
  61.                 'attr' => [
  62.                     'class' => 'identifiant_select chosen-select',
  63.                     'data-placeholder' => 'Choisir des identifiants',
  64.                 ],
  65.             ])
  66.             ->add('qte'IntegerType::class, [
  67.                 'constraints' => [new NotBlank()],
  68.                 'attr' => [
  69.                     'min' => 0,
  70.                 ],
  71.             ])
  72.             ->add('qteDisponible'IntegerType::class, [
  73.                 'disabled' => true,
  74.                 'mapped' => false
  75.             ]);
  76.         $builder->addEventListener(
  77.             FormEvents::PRE_SUBMIT,
  78.             function (FormEvent $event) {
  79.                 $form $event->getForm();
  80.                 $data $event->getData();
  81.                 $ids = [];
  82.                 if (!empty($data['identifiants'])) {
  83.                     foreach ($data['identifiants'] as $id) {
  84.                         $stocks $this->em->getRepository(Stock::class)->findBy(['identifiant' => $id]);
  85.                         foreach ($stocks as $stock) {
  86.                             $ids[$stock->getIdentifiant()] = $stock->getIdentifiant();
  87.                         }
  88.                     }
  89.                 }
  90.                 $form->add('identifiants'ChoiceType::class, [
  91.                     'choices' => $ids// rempli via Ajax
  92.                     'multiple' => true,
  93.                     'attr' => [
  94.                         'class' => 'identifiant_select chosen-select',
  95.                         'data-placeholder' => 'Choisir des identifiants',
  96.                     ],
  97.                 ]);
  98.             }
  99.         );
  100.     }
  101.     public function configureOptions(OptionsResolver $resolver): void
  102.     {
  103.         $resolver->setDefaults([
  104.             'data_class' => MaterielMouvement::class,
  105.         ]);
  106.     }
  107. }