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