Passed
Push — master ( ec0d02...c0f595 )
by Jan
06:31 queued 10s
created

BaseEntityAdminForm   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 87
c 1
b 0
f 0
dl 0
loc 142
rs 10
wmc 21

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configureOptions() 0 5 1
A __construct() 0 4 1
D buildForm() 0 119 18
A additionalFormElements() 0 2 1
1
<?php
2
/**
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 * Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as published
9
 * by the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
declare(strict_types=1);
22
23
/**
24
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
25
 *
26
 * Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
27
 *
28
 * This program is free software; you can redistribute it and/or
29
 * modify it under the terms of the GNU General Public License
30
 * as published by the Free Software Foundation; either version 2
31
 * of the License, or (at your option) any later version.
32
 *
33
 * This program is distributed in the hope that it will be useful,
34
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36
 * GNU General Public License for more details.
37
 *
38
 * You should have received a copy of the GNU General Public License
39
 * along with this program; if not, write to the Free Software
40
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
41
 */
42
43
namespace App\Form\AdminPages;
44
45
use App\Entity\Attachments\Attachment;
46
use App\Entity\Base\AbstractNamedDBElement;
47
use App\Entity\Base\AbstractStructuralDBElement;
48
use App\Entity\LabelSystem\LabelOptions;
49
use App\Entity\LabelSystem\LabelProfile;
50
use App\Form\AttachmentFormType;
51
use App\Form\ParameterType;
52
use App\Form\Type\MasterPictureAttachmentType;
53
use App\Form\Type\StructuralEntityType;
54
use FOS\CKEditorBundle\Form\Type\CKEditorType;
55
use function get_class;
56
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
57
use Symfony\Component\Form\AbstractType;
58
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
59
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
60
use Symfony\Component\Form\Extension\Core\Type\ResetType;
61
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
62
use Symfony\Component\Form\Extension\Core\Type\TextType;
63
use Symfony\Component\Form\FormBuilderInterface;
64
use Symfony\Component\OptionsResolver\OptionsResolver;
65
use Symfony\Component\Security\Core\Security;
66
67
class BaseEntityAdminForm extends AbstractType
68
{
69
    protected $security;
70
    protected $params;
71
72
    public function __construct(Security $security, ParameterBagInterface $params)
73
    {
74
        $this->security = $security;
75
        $this->params = $params;
76
    }
77
78
    public function configureOptions(OptionsResolver $resolver): void
79
    {
80
        parent::configureOptions($resolver);
81
        $resolver->setRequired('attachment_class');
82
        $resolver->setRequired('parameter_class');
83
    }
84
85
    public function buildForm(FormBuilderInterface $builder, array $options): void
86
    {
87
        /** @var AbstractStructuralDBElement $entity */
88
        $entity = $options['data'];
89
        $is_new = null === $entity->getID();
90
91
        $builder
92
            ->add('name', TextType::class, [
93
                'empty_data' => '',
94
                'label' => 'name.label',
95
                'attr' => [
96
                    'placeholder' => 'part.name.placeholder',
97
                ],
98
                'disabled' => ! $this->security->isGranted($is_new ? 'create' : 'edit', $entity),
99
            ]);
100
101
        if ($entity instanceof AbstractStructuralDBElement) {
0 ignored issues
show
introduced by
$entity is always a sub-type of App\Entity\Base\AbstractStructuralDBElement.
Loading history...
102
            $builder->add(
103
                'parent',
104
                StructuralEntityType::class,
105
                [
106
                    'class' => get_class($entity),
107
                    'required' => false,
108
                    'label' => 'parent.label',
109
                    'disabled' => !$this->security->isGranted($is_new ? 'create' : 'move', $entity),
110
                ]
111
            )
112
                ->add(
113
                    'not_selectable',
114
                    CheckboxType::class,
115
                    [
116
                        'required' => false,
117
                        'label' => 'entity.edit.not_selectable',
118
                        'help' => 'entity.edit.not_selectable.help',
119
                        'label_attr' => [
120
                            'class' => 'checkbox-custom',
121
                        ],
122
                        'disabled' => !$this->security->isGranted($is_new ? 'create' : 'edit', $entity),
123
                    ]
124
                );
125
        }
126
        if ($entity instanceof AbstractStructuralDBElement || $entity instanceof LabelProfile) {
0 ignored issues
show
introduced by
$entity is always a sub-type of App\Entity\Base\AbstractStructuralDBElement.
Loading history...
127
            $builder->add(
128
                'comment',
129
                CKEditorType::class,
130
                [
131
                    'required' => false,
132
                    'empty_data' => '',
133
                    'label' => 'comment.label',
134
                    'attr' => [
135
                        'rows' => 4,
136
                    ],
137
                    'help' => 'bbcode.hint',
138
                    'disabled' => !$this->security->isGranted($is_new ? 'create' : 'edit', $entity),
139
                ]
140
            );
141
        }
142
143
        $this->additionalFormElements($builder, $options, $entity);
144
145
        //Attachment section
146
        $builder->add('attachments', CollectionType::class, [
147
            'entry_type' => AttachmentFormType::class,
148
            'allow_add' => true,
149
            'allow_delete' => true,
150
            'label' => false,
151
            'reindex_enable' => true,
152
            'disabled' => ! $this->security->isGranted($is_new ? 'create' : 'edit', $entity),
153
            'entry_options' => [
154
                'data_class' => $options['attachment_class'],
155
            ],
156
            'by_reference' => false,
157
        ]);
158
159
        $builder->add('master_picture_attachment', MasterPictureAttachmentType::class, [
160
            'required' => false,
161
            'disabled' => ! $this->security->isGranted($is_new ? 'create' : 'edit', $entity),
162
            'label' => 'part.edit.master_attachment',
163
            'entity' => $entity,
164
        ]);
165
166
        $builder->add('log_comment', TextType::class, [
167
            'label' => 'edit.log_comment',
168
            'mapped' => false,
169
            'required' => false,
170
            'empty_data' => null,
171
        ]);
172
173
        if ($entity instanceof AbstractStructuralDBElement) {
0 ignored issues
show
introduced by
$entity is always a sub-type of App\Entity\Base\AbstractStructuralDBElement.
Loading history...
174
            $builder->add(
175
                'parameters',
176
                CollectionType::class,
177
                [
178
                    'entry_type' => ParameterType::class,
179
                    'allow_add' => $this->security->isGranted($is_new ? 'create' : 'edit', $entity),
180
                    'allow_delete' => $this->security->isGranted($is_new ? 'create' : 'edit', $entity),
181
                    'disabled' => !$this->security->isGranted($is_new ? 'create' : 'edit', $entity),
182
                    'reindex_enable' => true,
183
                    'label' => false,
184
                    'by_reference' => false,
185
                    'prototype_data' => new $options['parameter_class'](),
186
                    'entry_options' => [
187
                        'data_class' => $options['parameter_class'],
188
                    ],
189
                ]
190
            );
191
        }
192
193
        //Buttons
194
        $builder->add('save', SubmitType::class, [
195
            'label' => $is_new ? 'entity.create' : 'entity.edit.save',
196
            'attr' => [
197
                'class' => $is_new ? 'btn-success' : '',
198
            ],
199
            'disabled' => ! $this->security->isGranted($is_new ? 'create' : 'edit', $entity),
200
        ])
201
            ->add('reset', ResetType::class, [
202
                'label' => 'entity.edit.reset',
203
                'disabled' => ! $this->security->isGranted($is_new ? 'create' : 'edit', $entity),
204
            ]);
205
    }
206
207
    protected function additionalFormElements(FormBuilderInterface $builder, array $options, AbstractNamedDBElement $entity): void
0 ignored issues
show
Unused Code introduced by
The parameter $entity is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

207
    protected function additionalFormElements(FormBuilderInterface $builder, array $options, /** @scrutinizer ignore-unused */ AbstractNamedDBElement $entity): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
208
    {
209
        //Empty for Base
210
    }
211
}
212