Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
91.78% |
201 / 219 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
ToolType | |
91.78% |
201 / 219 |
|
66.67% |
2 / 3 |
10.06 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
buildForm | |
91.39% |
191 / 209 |
|
0.00% |
0 / 1 |
8.04 | |||
configureOptions | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 |
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 | |
22 | declare(strict_types=1); |
23 | |
24 | namespace App\Domain\Registry\Form\Type; |
25 | |
26 | use App\Application\Form\Type\LinkableType; |
27 | use App\Domain\Registry\Form\Type\Embeddable\ComplexChoiceAreaType; |
28 | use App\Domain\Registry\Model\Contractor; |
29 | use App\Domain\Registry\Model\Mesurement; |
30 | use App\Domain\Registry\Model\Proof; |
31 | use App\Domain\Registry\Model\Request; |
32 | use App\Domain\Registry\Model\Tool; |
33 | use App\Domain\Registry\Model\Treatment; |
34 | use App\Domain\Registry\Model\Violation; |
35 | use App\Domain\User\Model\Service; |
36 | use App\Domain\User\Model\User; |
37 | use Doctrine\ORM\EntityRepository; |
38 | use Knp\DictionaryBundle\Form\Type\DictionaryType; |
39 | use Symfony\Bridge\Doctrine\Form\Type\EntityType; |
40 | use Symfony\Component\Form\Extension\Core\Type\ChoiceType; |
41 | use Symfony\Component\Form\Extension\Core\Type\DateType; |
42 | use Symfony\Component\Form\Extension\Core\Type\HiddenType; |
43 | use Symfony\Component\Form\Extension\Core\Type\TextareaType; |
44 | use Symfony\Component\Form\Extension\Core\Type\TextType; |
45 | use Symfony\Component\Form\FormBuilderInterface; |
46 | use Symfony\Component\OptionsResolver\OptionsResolver; |
47 | use Symfony\Component\Security\Core\Security; |
48 | use Symfony\Component\Validator\Constraints\Length; |
49 | use Symfony\Component\Validator\Constraints\NotBlank; |
50 | |
51 | class ToolType extends LinkableType |
52 | { |
53 | /** |
54 | * @var Security |
55 | */ |
56 | private $security; |
57 | |
58 | public function __construct(Security $security) |
59 | { |
60 | $this->security = $security; |
61 | parent::__construct($security); |
62 | } |
63 | |
64 | /** |
65 | * Build type form. |
66 | */ |
67 | public function buildForm(FormBuilderInterface $builder, array $options) |
68 | { |
69 | /** @var User $user */ |
70 | $user = $this->security->getUser(); |
71 | |
72 | /** @var Tool $tool */ |
73 | $tool = $options['data'] ?? null; |
74 | $collectivity = null; |
75 | if ($tool) { |
76 | $collectivity = $tool->getCollectivity(); |
77 | } |
78 | /** @var User $user */ |
79 | $user = $this->security->getUser(); |
80 | |
81 | if (!$collectivity) { |
82 | $collectivity = $user->getCollectivity(); |
83 | } |
84 | $builder |
85 | ->add('name', TextType::class, [ |
86 | 'label' => 'registry.tool.label.name', |
87 | 'required' => true, |
88 | 'attr' => [ |
89 | 'maxlength' => 255, |
90 | ], |
91 | 'purify_html' => true, |
92 | 'constraints' => [ |
93 | new NotBlank(), |
94 | new Length(['min' => 3]), |
95 | ], |
96 | ]) |
97 | |
98 | ->add('type', DictionaryType::class, [ |
99 | 'label' => 'registry.tool.label.type', |
100 | 'name' => 'registry_tool_type', |
101 | 'required' => true, |
102 | 'multiple' => false, |
103 | 'expanded' => true, |
104 | ]) |
105 | ->add('description', TextareaType::class, [ |
106 | 'label' => 'registry.tool.label.description', |
107 | 'required' => false, |
108 | 'attr' => [ |
109 | 'rows' => 2, |
110 | ], |
111 | 'purify_html' => true, |
112 | ]) |
113 | ->add('other_info', TextareaType::class, [ |
114 | 'label' => 'registry.tool.label.other_info', |
115 | 'required' => false, |
116 | 'attr' => [ |
117 | 'rows' => 2, |
118 | ], |
119 | 'purify_html' => true, |
120 | ]) |
121 | ->add('editor', TextType::class, [ |
122 | 'label' => 'registry.tool.label.editor', |
123 | 'required' => false, |
124 | 'attr' => [ |
125 | 'maxlength' => 255, |
126 | ], |
127 | 'purify_html' => true, |
128 | ]) |
129 | ->add('manager', TextType::class, [ |
130 | 'label' => 'registry.tool.label.manager', |
131 | 'required' => false, |
132 | 'attr' => [ |
133 | 'maxlength' => 255, |
134 | ], |
135 | 'purify_html' => true, |
136 | ]) |
137 | |
138 | ->add('mesurements', |
139 | EntityType::class, |
140 | $this->getLinkedFormField( |
141 | 'global.label.linked_mesurement', |
142 | Mesurement::class, |
143 | $tool, |
144 | 'Actions de protection', |
145 | ['status' => 'asc', 'name' => 'asc'], |
146 | ), |
147 | ) |
148 | ->add('treatments', EntityType::class, $this->getLinkedFormField( |
149 | 'global.label.linked_treatment', |
150 | Treatment::class, |
151 | $tool, |
152 | 'Traitements', |
153 | ['active' => 'desc', 'name' => 'asc'], |
154 | ), |
155 | ) |
156 | ->add('violations', EntityType::class, $this->getLinkedFormField( |
157 | 'global.label.linked_violation', |
158 | Violation::class, |
159 | $tool, |
160 | 'Violations', |
161 | ['deletedAt' => 'asc', 'date' => 'asc'], |
162 | ), |
163 | ) |
164 | ->add('proofs', EntityType::class, $this->getLinkedFormField( |
165 | 'global.label.linked_proof', |
166 | Proof::class, |
167 | $tool, |
168 | 'Preuves', |
169 | ['deletedAt' => 'asc', 'name' => 'asc'], |
170 | ), |
171 | ) |
172 | ->add('requests', EntityType::class, $this->getLinkedFormField( |
173 | 'global.label.linked_request', |
174 | Request::class, |
175 | $tool, |
176 | 'Demandes', |
177 | ['deletedAt' => 'asc', 'date' => 'asc'], |
178 | ), |
179 | ) |
180 | ->add('contractors', EntityType::class, $this->getLinkedFormField( |
181 | 'global.label.linked_contractor', |
182 | Contractor::class, |
183 | $tool, |
184 | 'Sous-traitants', |
185 | ['name' => 'asc'], |
186 | ), |
187 | ) |
188 | ->add('tools', EntityType::class, $this->getLinkedFormField( |
189 | 'global.label.linked_tool', |
190 | Tool::class, |
191 | $tool, |
192 | 'Logiciels et supports', |
193 | ['name' => 'asc'], |
194 | ), |
195 | ) |
196 | |
197 | ->add('prod_date', DateType::class, [ |
198 | 'label' => 'registry.tool.label.prod_date', |
199 | 'required' => false, |
200 | 'widget' => 'single_text', |
201 | 'format' => 'dd/MM/yyyy', |
202 | 'html5' => false, |
203 | 'attr' => [ |
204 | 'class' => 'datepicker', |
205 | ], |
206 | ]) |
207 | |
208 | ->add('country_type', ChoiceType::class, [ |
209 | 'label' => 'registry.tool.label.country_type', |
210 | 'choices' => Tool::COUNTRY_TYPES, |
211 | 'required' => false, |
212 | ]) |
213 | |
214 | ->add('country_name', TextType::class, [ |
215 | 'label' => 'registry.tool.label.country_name', |
216 | 'required' => false, |
217 | 'purify_html' => true, |
218 | ]) |
219 | |
220 | ->add('country_guarantees', TextType::class, [ |
221 | 'label' => 'registry.tool.label.country_guarantees', |
222 | 'required' => true, |
223 | 'purify_html' => true, |
224 | ]) |
225 | |
226 | ->add('archival', ComplexChoiceAreaType::class, [ |
227 | 'label' => 'registry.tool.label.archival', |
228 | 'required' => false, |
229 | ]) |
230 | ->add('encrypted', ComplexChoiceAreaType::class, [ |
231 | 'label' => 'registry.tool.label.encrypted', |
232 | 'required' => false, |
233 | ]) |
234 | ->add('access_control', ComplexChoiceAreaType::class, [ |
235 | 'label' => 'registry.tool.label.access_control', |
236 | 'required' => false, |
237 | ]) |
238 | ->add('update', ComplexChoiceAreaType::class, [ |
239 | 'label' => 'registry.tool.label.update', |
240 | 'required' => false, |
241 | ]) |
242 | ->add('backup', ComplexChoiceAreaType::class, [ |
243 | 'label' => 'registry.tool.label.backup', |
244 | 'required' => false, |
245 | ]) |
246 | ->add('deletion', ComplexChoiceAreaType::class, [ |
247 | 'label' => 'registry.tool.label.deletion', |
248 | 'required' => false, |
249 | ]) |
250 | ->add('tracking', ComplexChoiceAreaType::class, [ |
251 | 'label' => 'registry.tool.label.tracking', |
252 | 'required' => false, |
253 | ]) |
254 | ->add('has_comment', ComplexChoiceAreaType::class, [ |
255 | 'label' => 'registry.tool.label.has_comment', |
256 | 'required' => false, |
257 | ]) |
258 | ->add('other', ComplexChoiceAreaType::class, [ |
259 | 'label' => 'registry.tool.label.other', |
260 | 'required' => false, |
261 | ]) |
262 | ->add('updatedBy', HiddenType::class, [ |
263 | 'required' => false, |
264 | 'data' => $user ? $user->getFirstName() . ' ' . strtoupper($user->getLastName()) : '', |
265 | ]) |
266 | ; |
267 | // Check if services are enabled for the collectivity's treatment |
268 | if ($options['data']->getCollectivity()->getIsServicesEnabled()) { |
269 | $builder->add('service', EntityType::class, [ |
270 | 'class' => Service::class, |
271 | 'label' => 'registry.label.service', |
272 | 'query_builder' => function (EntityRepository $er) use ($tool) { |
273 | if ($tool->getCollectivity()) { |
274 | /** @var User $authenticatedUser */ |
275 | $authenticatedUser = $this->security->getUser(); |
276 | $collectivity = $tool->getCollectivity(); |
277 | |
278 | $qb = $er->createQueryBuilder('s') |
279 | ->where('s.collectivity = :collectivity') |
280 | ->setParameter(':collectivity', $collectivity) |
281 | ; |
282 | |
283 | if (!$this->security->isGranted('ROLE_ADMIN') && $authenticatedUser->getServices()->getValues()) { |
284 | $qb->leftJoin('s.users', 'users') |
285 | ->andWhere('users.id = :id') |
286 | ->setParameter('id', $authenticatedUser->getId()) |
287 | ; |
288 | } |
289 | |
290 | $qb |
291 | ->orderBy('s.name', 'ASC'); |
292 | |
293 | return $qb; |
294 | } |
295 | |
296 | return $er->createQueryBuilder('s') |
297 | ->orderBy('s.name', 'ASC'); |
298 | }, |
299 | 'required' => false, |
300 | ]); |
301 | } |
302 | } |
303 | |
304 | /** |
305 | * Provide type options. |
306 | */ |
307 | public function configureOptions(OptionsResolver $resolver) |
308 | { |
309 | $resolver |
310 | ->setDefaults([ |
311 | 'data_class' => Tool::class, |
312 | 'validation_groups' => [ |
313 | 'default', |
314 | 'tool', |
315 | ], |
316 | ]); |
317 | } |
318 | } |