Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 44
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
SyntheseAnswerType
0.00% covered (danger)
0.00%
0 / 44
0.00% covered (danger)
0.00%
0 / 3
20
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 buildForm
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 1
6
 configureOptions
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * This file is part of the MADIS - RGPD Management application.
5 *
6 * @copyright Copyright (c) 2018-2019 Soluris - Solutions Numériques Territoriales Innovantes
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
22declare(strict_types=1);
23
24namespace App\Domain\Maturity\Form\Type;
25
26use App\Domain\Maturity\Model;
27use App\Domain\Registry\Dictionary\MesurementStatusDictionary;
28use App\Domain\Registry\Model\Mesurement;
29use App\Domain\User\Model\User;
30use Doctrine\ORM\EntityRepository;
31use Symfony\Bridge\Doctrine\Form\Type\EntityType;
32use Symfony\Component\Form\AbstractType;
33use Symfony\Component\Form\FormBuilderInterface;
34use Symfony\Component\Form\FormEvent;
35use Symfony\Component\Form\FormEvents;
36use Symfony\Component\OptionsResolver\OptionsResolver;
37use Symfony\Component\Security\Core\Security;
38
39class SyntheseAnswerType extends AbstractType
40{
41    protected Security $security;
42
43    public function __construct(Security $security)
44    {
45        $this->security = $security;
46    }
47
48    /**
49     * Build type form.
50     */
51    public function buildForm(FormBuilderInterface $builder, array $options)
52    {
53        $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
54            $parentForm   = $event->getForm()->getParent()->getParent();
55            $collectivity = $parentForm->getData()->getCollectivity();
56            $a            = $event->getData()->getAnswer()->getRecommendation();
57
58            $event->getForm()->add('mesurements', EntityType::class, [
59                'required'      => false,
60                'label'         => false,
61                'class'         => Mesurement::class,
62                'query_builder' => function (EntityRepository $er) use ($collectivity) {
63                    /** @var User $user */
64                    $user = $this->security->getUser();
65
66                    return $er->createQueryBuilder('m')
67                        ->andWhere('m.collectivity = :collectivity')
68                        ->setParameter('collectivity', $collectivity)
69                        ->andWhere('m.status = :nonApplied')
70                        ->setParameter('nonApplied', MesurementStatusDictionary::STATUS_NOT_APPLIED)
71                        ->orderBy('m.name', 'ASC');
72                },
73                'choice_label' => 'name',
74                'expanded'     => false,
75                'multiple'     => true,
76                'attr'         => [
77                    'class'            => 'selectpicker',
78                    'title'            => 'global.placeholder.multiple_select',
79                    'aria-label'       => $a,
80                    'data-live-search' => true,
81                    'data-width'       => 'calc(100% - 40px)',
82                ],
83                'choice_attr' => function (Mesurement $choice) {
84                    $name = $choice->getName();
85                    if (\mb_strlen($name) > 85) {
86                        $name = \mb_substr($name, 0, 85) . '...';
87                    }
88
89                    return ['data-content' => $name];
90                },
91            ]);
92        });
93    }
94
95    /**
96     * Provide type options.
97     */
98    public function configureOptions(OptionsResolver $resolver)
99    {
100        $resolver
101            ->setDefaults([
102                'data_class'        => Model\AnswerSurvey::class,
103                'validation_groups' => [
104                    'default',
105                    'answer',
106                ],
107            ]);
108    }
109}