Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 74
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
QuestionType
0.00% covered (danger)
0.00%
0 / 74
0.00% covered (danger)
0.00%
0 / 2
20
0.00% covered (danger)
0.00%
0 / 1
 buildForm
0.00% covered (danger)
0.00%
0 / 67
0.00% covered (danger)
0.00%
0 / 1
12
 configureOptions
0.00% covered (danger)
0.00%
0 / 7
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\Question;
27use Symfony\Component\Form\AbstractType;
28use Symfony\Component\Form\Extension\Core\Type\ButtonType;
29use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
30use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
31use Symfony\Component\Form\Extension\Core\Type\CollectionType;
32use Symfony\Component\Form\Extension\Core\Type\HiddenType;
33use Symfony\Component\Form\Extension\Core\Type\TextType;
34use Symfony\Component\Form\FormBuilderInterface;
35use Symfony\Component\Form\FormEvent;
36use Symfony\Component\Form\FormEvents;
37use Symfony\Component\OptionsResolver\OptionsResolver;
38
39class QuestionType extends AbstractType
40{
41    /**
42     * Build type form.
43     */
44    public function buildForm(FormBuilderInterface $builder, array $options)
45    {
46        /* @var Question $question */
47        $builder
48            ->add('name', TextType::class, [
49                'label'    => 'maturity.referentiel.label.question',
50                'required' => true,
51                'attr'     => [
52                    'maxlength' => 1000,
53                ],
54                'purify_html' => true,
55            ])
56            ->add('weight', ChoiceType::class, [
57                'label'    => 'maturity.referentiel.label.weight',
58                'required' => true,
59                'multiple' => false,
60                'expanded' => false,
61                'choices'  => [0, 1, 2, 3, 4, 5],
62            ])
63            ->add('position', HiddenType::class, [
64                'required' => false,
65                'attr'     => [
66                    'class' => 'question-position',
67                ],
68            ])
69            ->add('optional', CheckboxType::class, [
70                'label'    => 'maturity.referentiel.label.not_concerned',
71                'required' => false,
72            ])
73            ->add('optionReason', TextType::class, [
74                'label'    => false,
75                'required' => false,
76                'attr'     => [
77                    'placeholder' => 'maturity.referentiel.placeholder.not_concerned',
78                    'maxlength'   => 1000,
79                    'aria-label'  => 'Changer l\'option par défaut : Non concerné',
80                ],
81                'purify_html' => true,
82            ])
83            ->add('answers', CollectionType::class, [
84                'label'          => false,
85                'entry_type'     => AnswerType::class,
86                'required'       => true,
87                'allow_add'      => true,
88                'allow_delete'   => true,
89                'by_reference'   => false,
90                'prototype_name' => '__answer_name__',
91            ]);
92
93        $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
94            $form = $event->getForm();
95            if (null !== $data = $event->getData()) {
96                $form->add('addAnswer', ButtonType::class, [
97                    'label'      => 'maturity.referentiel.action.add_answer',
98                    'label_html' => true,
99                    'attr'       => [
100                        'class'                        => 'add_answer btn btn-primary',
101                        'data-question'                => ($data ? $data->getId() : ''),
102                        'data-collection-holder-class' => 'referentielAnswers',
103                    ],
104                ]);
105            } else {
106                $form->add('addAnswer', ButtonType::class, [
107                    'label'      => 'maturity.referentiel.action.add_answer',
108                    'label_html' => true,
109                    'attr'       => [
110                        'class'                        => 'add_answer btn btn-primary',
111                        'data-question'                => '9999',
112                        'data-collection-holder-class' => 'referentielAnswers',
113                    ],
114                ]);
115            }
116        });
117    }
118
119    /**
120     * Provide type options.
121     */
122    public function configureOptions(OptionsResolver $resolver)
123    {
124        $resolver
125            ->setDefaults([
126                'data_class'        => Question::class,
127                'validation_groups' => [
128                    'default',
129                ],
130            ]);
131    }
132}