Issues (50)

src/Controller/Admin/DepartmentCrudController.php (1 issue)

Severity
1
<?php
2
/*
3
 * Copyright (C) 2020  Jan Böhmer
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Affero General Public License as published
7
 * by the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU Affero General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Affero General Public License
16
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17
 */
18
19
namespace App\Controller\Admin;
20
21
use App\Entity\Department;
22
use App\Services\EmailConfirmation\ConfirmationTokenGenerator;
23
use Doctrine\ORM\EntityManagerInterface;
24
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
25
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
26
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
27
use EasyCorp\Bundle\EasyAdminBundle\Config\Filters;
28
use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
29
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
30
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
31
use EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField;
32
use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField;
33
use EasyCorp\Bundle\EasyAdminBundle\Field\CollectionField;
34
use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
35
use EasyCorp\Bundle\EasyAdminBundle\Field\FormField;
36
use EasyCorp\Bundle\EasyAdminBundle\Field\IntegerField;
37
use EasyCorp\Bundle\EasyAdminBundle\Field\TextEditorField;
38
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
39
use EasyCorp\Bundle\EasyAdminBundle\Filter\BooleanFilter;
40
use EasyCorp\Bundle\EasyAdminBundle\Filter\DateTimeFilter;
41
use Symfony\Component\Form\Extension\Core\Type\EmailType;
42
use Symfony\Component\HttpFoundation\Response;
43
44
class DepartmentCrudController extends AbstractCrudController
45
{
46
    private $tokenGenerator;
47
    private $entityManager;
48
49
    public function __construct(ConfirmationTokenGenerator $tokenGenerator, EntityManagerInterface $entityManager)
50
    {
51
        $this->tokenGenerator = $tokenGenerator;
52
        $this->entityManager = $entityManager;
53
    }
54
55
    public static function getEntityFqcn(): string
56
    {
57
        return Department::class;
58
    }
59
60
    public function configureCrud(Crud $crud): Crud
61
    {
62
        return $crud
63
            ->setEntityLabelInSingular('department.label')
64
            ->setEntityLabelInPlural('department.labelp')
65
            ->setSearchFields(['id', 'name', 'type', 'comment']);
66
    }
67
68
    public function generateSkipToken(AdminContext $context): Response
69
    {
70
        $this->denyAccessUnlessGranted('ROLE_EDIT_ORGANISATIONS');
71
72
        /** @var Department $department */
73
        $department = $context->getEntity()
74
            ->getInstance();
75
76
        $department->addSkipBlockedValidationToken($this->tokenGenerator->getToken());
77
        $this->entityManager->flush();
78
79
        return $this->redirect($context->getReferrer() ?? '/admin');
80
    }
81
82
    public function configureActions(Actions $actions): Actions
83
    {
84
        $actions->setPermissions([
85
            Action::EDIT => 'ROLE_EDIT_ORGANISATIONS',
86
            Action::DELETE => 'ROLE_EDIT_ORGANISATIONS',
87
            Action::NEW => 'ROLE_EDIT_ORGANISATIONS',
88
            Action::INDEX => 'ROLE_READ_ORGANISATIONS',
89
            Action::DETAIL => 'ROLE_READ_ORGANISATIONS',
90
        ]);
91
92
        $generateTokenAction = Action::new('generateSkipToken', 'department.action.generate_skip_token', 'fas fa-award')
93
            ->displayIf(function (Department $paymentOrder) {
0 ignored issues
show
The parameter $paymentOrder 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

93
            ->displayIf(function (/** @scrutinizer ignore-unused */ Department $paymentOrder) {

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...
94
                return $this->isGranted('ROLE_EDIT_ORGANISATIONS');
95
            })
96
            ->setCssClass('btn btn-secondary')
97
            ->linkToCrudAction('generateSkipToken');
98
99
        $actions->add(Crud::PAGE_DETAIL, $generateTokenAction);
100
        $actions->add(Crud::PAGE_EDIT, $generateTokenAction);
101
102
        return $actions->add(Crud::PAGE_INDEX, Action::DETAIL);
103
    }
104
105
    public function configureFilters(Filters $filters): Filters
106
    {
107
        return $filters
108
            ->add(BooleanFilter::new('blocked', 'department.blocked.label'))
109
            ->add(DateTimeFilter::new('creation_date', 'creation_date'))
110
            ->add(DateTimeFilter::new('last_modified', 'last_modified'));
111
    }
112
113
    public function configureFields(string $pageName): iterable
114
    {
115
        $choices = [];
116
        foreach (Department::ALLOWED_TYPES as $type) {
117
            $choices['department.type.'.$type] = $type;
118
        }
119
120
        return [
121
            //Basic informations
122
            TextField::new('name', 'department.name.label'),
123
            ChoiceField::new('type', 'department.type.label')
124
                ->setChoices($choices)
125
                ->autocomplete(),
126
            BooleanField::new('blocked', 'department.blocked.label')
127
                ->renderAsSwitch(true)
128
                ->setHelp('department.blocked.help'),
129
            TextEditorField::new('comment', 'department.comment.label')
130
                ->setRequired(false)
131
                ->setFormTypeOption('empty_data', '')
132
                ->hideOnIndex(),
133
            IntegerField::new('id', 'department.id.label')
134
                ->onlyOnDetail(),
135
            DateTimeField::new('last_modified', 'last_modified')
136
                ->onlyOnDetail(),
137
            DateTimeField::new('creation_date', 'creation_date')
138
                ->onlyOnDetail(),
139
140
            AssociationField::new('bank_account', 'department.bank_account.label')
141
                ->setHelp('department.bank_account.help')
142
                ->setRequired(false)
143
                ->hideOnDetail(),
144
145
            CollectionField::new('contact_emails', 'department.contact_emails.label')
146
                ->setHelp('department.contact_emails.help')
147
                ->setTemplatePath('admin/field/email_collection.html.twig')
148
                ->allowAdd()
149
                ->allowDelete()
150
                ->setFormTypeOption('delete_empty', true)
151
                ->setFormTypeOption('entry_options.required', false)
152
                ->setTemplatePath('admin/field/email_collection.html.twig')
153
                ->setEntryType(EmailType::class),
154
155
            TextField::new('references_export_prefix', 'department.references_export_prefix.label')
156
                ->setHelp('department.references_export_prefix.help')
157
                ->hideOnIndex(),
158
159
            //FSR contact info panel
160
            FormField::addPanel('department.fsr_email_panel.label')
161
                ->setHelp('department.fsr_email_panel.help'),
162
            CollectionField::new('email_hhv', 'department.email_hhv.label')
163
                ->setHelp('department.email_hhv.help')
164
                ->setTemplatePath('admin/field/email_collection.html.twig')
165
                ->allowAdd()
166
                ->allowDelete()
167
                ->setFormTypeOption('delete_empty', true)
168
                ->setFormTypeOption('entry_options.required', false)
169
                ->setFormTypeOption('entry_options.empty_data', '')
170
                ->setEntryType(EmailType::class)
171
                ->hideOnIndex(),
172
173
            CollectionField::new('email_treasurer', 'department.email_treasurer.label')
174
                ->setHelp('department.email_hhv.help')
175
                ->setTemplatePath('admin/field/email_collection.html.twig')
176
                ->allowAdd()
177
                ->allowDelete()
178
                ->setFormTypeOption('delete_empty', true)
179
                ->setFormTypeOption('entry_options.required', false)
180
                ->setFormTypeOption('entry_options.empty_data', '')
181
                ->setEntryType(EmailType::class)
182
                ->hideOnIndex(),
183
184
            FormField::addPanel('department.skip_blocked_validation_tokens.panel.label')
185
                ->setHelp('department.skip_blocked_validation_tokens.panel.help'),
186
187
            CollectionField::new('skip_blocked_validation_tokens', 'department.skip_blocked_validation_tokens.label')
188
                ->allowDelete()
189
                ->allowAdd(false)
190
                ->setFormTypeOption('delete_empty', false)
191
                ->setTemplatePath('admin/field/validation_token_collection.html.twig')
192
                ->hideOnIndex(),
193
        ];
194
    }
195
}
196