Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
81.51% covered (warning)
81.51%
119 / 146
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ProofType
81.51% covered (warning)
81.51%
119 / 146
66.67% covered (warning)
66.67%
2 / 3
11.77
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
80.43% covered (warning)
80.43%
111 / 138
0.00% covered (danger)
0.00%
0 / 1
9.61
 configureOptions
100.00% covered (success)
100.00%
5 / 5
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\FileType;
40use Symfony\Component\Form\Extension\Core\Type\TextType;
41use Symfony\Component\Form\FormBuilderInterface;
42use Symfony\Component\OptionsResolver\OptionsResolver;
43use Symfony\Component\Security\Core\Security;
44use Symfony\Component\Validator\Constraints\File;
45
46class ProofType extends LinkableType
47{
48    protected string $maxSize;
49
50    /**
51     * @var Security
52     */
53    private $security;
54
55    public function __construct(Security $security, string $maxSize)
56    {
57        $this->security = $security;
58        $this->maxSize  = $maxSize;
59        parent::__construct($security);
60    }
61
62    /**
63     * Build type form.
64     *
65     * @throws \Exception
66     */
67    public function buildForm(FormBuilderInterface $builder, array $options)
68    {
69        /** @var Proof $proof */
70        $proof = $options['data'];
71        if (!\is_null($proof->getCollectivity())) {
72            $collectivity = $proof->getCollectivity();
73        } else {
74            /** @var User $authenticatedUser */
75            $authenticatedUser = $this->security->getUser();
76            $collectivity      = $authenticatedUser->getCollectivity();
77        }
78
79        $builder
80            ->add('name', TextType::class, [
81                'label'    => 'registry.proof.label.name',
82                'required' => true,
83                'attr'     => [
84                    'maxlength' => 255,
85                ],
86                'purify_html' => true,
87            ])
88            ->add('type', DictionaryType::class, [
89                'label'    => 'registry.proof.label.type',
90                'name'     => 'registry_proof_type',
91                'required' => true,
92            ])
93            ->add('documentFile', FileType::class, [
94                'label'       => 'registry.proof.label.file',
95                'required'    => !$proof->getDocument(),
96                'constraints' => [
97                    new File([
98                        'maxSize'          => $this->maxSize,
99                        'mimeTypesMessage' => 'registry_proof.document_file.file',
100                        'mimeTypes'        => [
101                            // JPG / PNG
102                            'image/jpeg',
103                            'image/png',
104                            // PDF
105                            'application/pdf',
106                            // DOC
107                            'application/msword',
108                            // DOCX
109                            'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
110                            // Lors de la génération d'un fichier (Bilan) word son mimetype est doublé.
111                            // On conserve le mimetype suivant car il y avait des bugs avec iOS (ipad et iphone) lors du téléchargement
112                            'application/vnd.openxmlformats-officedocument.wordprocessingml.documentapplication/vnd.openxmlformats-officedocument.wordprocessingml.document',
113                            // ODT
114                            'application/vnd.oasis.opendocument.text',
115                            // XLS
116                            'application/vnd.ms-excel',
117                            // XLSX
118                            'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
119                            // ODS
120                            'application/vnd.oasis.opendocument.spreadsheet',
121                            // PPT / PPTX
122                            'application/vnd.ms-powerpoint',
123                            'application/vnd.openxmlformats-officedocument.presentationml.presentation',
124                            // TXT / LOG / CSV / MD
125                            'text/plain',
126                        ],
127                        'groups' => ['default'],
128                    ]),
129                ],
130            ])
131            ->add('comment', TextType::class, [
132                'label'    => 'registry.proof.label.comment',
133                'required' => false,
134                'attr'     => [
135                    'maxlength' => 255,
136                ],
137                'purify_html' => true,
138            ])
139            ->add('mesurements',
140                EntityType::class,
141                $this->getLinkedFormField(
142                    'global.label.linked_mesurement',
143                    Mesurement::class,
144                    $proof,
145                    'Actions de protection',
146                    ['status' => 'asc', 'name' => 'asc'],
147                ),
148            )
149            ->add('treatments', EntityType::class, $this->getLinkedFormField(
150                'global.label.linked_treatment',
151                Treatment::class,
152                $proof,
153                'Traitements',
154                ['active' => 'desc', 'name' => 'asc'],
155            ),
156            )
157            ->add('violations', EntityType::class, $this->getLinkedFormField(
158                'global.label.linked_violation',
159                Violation::class,
160                $proof,
161                'Violations',
162                ['deletedAt' => 'asc', 'date' => 'asc'],
163            ),
164            )
165            ->add('proofs', EntityType::class, $this->getLinkedFormField(
166                'global.label.linked_proof',
167                Proof::class,
168                $proof,
169                'Preuves',
170                ['deletedAt' => 'asc', 'name' => 'asc'],
171            ),
172            )
173            ->add('requests', EntityType::class, $this->getLinkedFormField(
174                'global.label.linked_request',
175                Request::class,
176                $proof,
177                'Demandes',
178                ['deletedAt' => 'asc', 'date' => 'asc'],
179            ),
180            )
181            ->add('contractors', EntityType::class, $this->getLinkedFormField(
182                'global.label.linked_contractor',
183                Contractor::class,
184                $proof,
185                'Sous-traitants',
186                ['name' => 'asc'],
187            ),
188            )
189        ;
190
191        if ($collectivity && $collectivity->isHasModuleTools()) {
192            $builder->add('tools', EntityType::class, $this->getLinkedFormField(
193                'global.label.linked_tool',
194                Tool::class,
195                $proof,
196                'Logiciels et supports',
197                'name',
198            ),
199            );
200        }
201
202        // Check if services are enabled for the collectivity's treatment
203        if ($collectivity && $collectivity->getIsServicesEnabled()) {
204            $builder->add('service', EntityType::class, [
205                'class'         => Service::class,
206                'label'         => 'registry.label.service',
207                'query_builder' => function (EntityRepository $er) use ($proof) {
208                    if ($proof->getCollectivity()) {
209                        /** @var User $authenticatedUser */
210                        $authenticatedUser = $this->security->getUser();
211                        $collectivity      = $proof->getCollectivity();
212
213                        $qb = $er->createQueryBuilder('s')
214                            ->where('s.collectivity = :collectivity')
215                            ->setParameter(':collectivity', $collectivity)
216                        ;
217
218                        if (!$this->security->isGranted('ROLE_ADMIN') && $authenticatedUser->getServices()->getValues()) {
219                            $qb->leftJoin('s.users', 'users')
220                                ->andWhere('users.id = :id')
221                                ->setParameter('id', $authenticatedUser->getId())
222                            ;
223                        }
224
225                        $qb
226                            ->orderBy('s.name', 'ASC');
227
228                        return $qb;
229                    }
230
231                    return $er->createQueryBuilder('s')
232                        ->orderBy('s.name', 'ASC');
233                },
234                'required' => false,
235            ]);
236        }
237    }
238
239    /**
240     * Provide type options.
241     */
242    public function configureOptions(OptionsResolver $resolver)
243    {
244        $resolver
245            ->setDefaults([
246                'data_class'        => Proof::class,
247                'validation_groups' => ['default', 'proof'],
248            ]);
249    }
250}