Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
31.11% |
14 / 45 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
ConformiteType | |
31.11% |
14 / 45 |
|
50.00% |
1 / 2 |
5.94 | |
0.00% |
0 / 1 |
buildForm | |
18.42% |
7 / 38 |
|
0.00% |
0 / 1 |
4.17 | |||
configureOptions | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace App\Domain\Registry\Form\Type\ConformiteOrganisation; |
4 | |
5 | use App\Domain\Registry\Dictionary\MesurementStatusDictionary; |
6 | use App\Domain\Registry\Model\ConformiteOrganisation\Conformite; |
7 | use App\Domain\Registry\Model\Mesurement; |
8 | use Doctrine\ORM\EntityRepository; |
9 | use Symfony\Bridge\Doctrine\Form\Type\EntityType; |
10 | use Symfony\Component\Form\AbstractType; |
11 | use Symfony\Component\Form\Extension\Core\Type\CollectionType; |
12 | use Symfony\Component\Form\FormBuilderInterface; |
13 | use Symfony\Component\Form\FormEvent; |
14 | use Symfony\Component\Form\FormEvents; |
15 | use Symfony\Component\OptionsResolver\OptionsResolver; |
16 | |
17 | class ConformiteType extends AbstractType |
18 | { |
19 | /** |
20 | * Build type form. |
21 | */ |
22 | public function buildForm(FormBuilderInterface $builder, array $options) |
23 | { |
24 | $builder |
25 | ->add('reponses', CollectionType::class, [ |
26 | 'required' => false, |
27 | 'entry_type' => ReponseType::class, |
28 | ]); |
29 | |
30 | $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) { |
31 | $parentForm = $event->getForm()->getParent()->getParent(); |
32 | $collectivity = $parentForm->getData()->getCollectivity(); |
33 | $event->getForm()->add('actionProtections', EntityType::class, [ |
34 | 'required' => false, |
35 | 'label' => false, |
36 | 'class' => Mesurement::class, |
37 | 'query_builder' => function (EntityRepository $er) use ($collectivity) { |
38 | return $er->createQueryBuilder('m') |
39 | ->andWhere('m.collectivity = :collectivity') |
40 | ->setParameter('collectivity', $collectivity) |
41 | ->andWhere('m.status = :nonApplied') |
42 | ->setParameter('nonApplied', MesurementStatusDictionary::STATUS_NOT_APPLIED) |
43 | ->orderBy('m.name', 'ASC'); |
44 | }, |
45 | 'choice_label' => 'name', |
46 | 'expanded' => false, |
47 | 'multiple' => true, |
48 | 'attr' => [ |
49 | 'class' => 'selectpicker', |
50 | 'title' => 'global.placeholder.multiple_select', |
51 | 'data-live-search' => true, |
52 | 'aria-label' => 'Actions de protection', |
53 | 'data-width' => 'calc(100% - 40px)', |
54 | ], |
55 | 'choice_attr' => function (Mesurement $choice) { |
56 | $name = $choice->getName(); |
57 | if (\mb_strlen($name) > 85) { |
58 | $name = \mb_substr($name, 0, 85) . '...'; |
59 | } |
60 | |
61 | return ['data-content' => $name]; |
62 | }, |
63 | ]); |
64 | }); |
65 | } |
66 | |
67 | /** |
68 | * Provide type options. |
69 | */ |
70 | public function configureOptions(OptionsResolver $resolver) |
71 | { |
72 | $resolver |
73 | ->setDefaults([ |
74 | 'data_class' => Conformite::class, |
75 | 'validation_groups' => [ |
76 | 'default', |
77 | ], |
78 | ]); |
79 | } |
80 | } |