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