Passed
Push — master ( c89864...65eac8 )
by Jan
17:55 queued 12s
created

DashboardController::configureActions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 0
loc 19
rs 9.8666
c 0
b 0
f 0
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\BankAccount;
22
use App\Entity\Contracts\DBElementInterface;
23
use App\Entity\Department;
24
use App\Entity\PaymentOrder;
25
use App\Entity\User;
26
use App\Services\GitVersionInfo;
27
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
28
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
29
use EasyCorp\Bundle\EasyAdminBundle\Config\Assets;
30
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
31
use EasyCorp\Bundle\EasyAdminBundle\Config\Dashboard;
32
use EasyCorp\Bundle\EasyAdminBundle\Config\Menu\CrudMenuItem;
33
use EasyCorp\Bundle\EasyAdminBundle\Config\MenuItem;
34
use EasyCorp\Bundle\EasyAdminBundle\Config\UserMenu;
35
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractDashboardController;
36
use Symfony\Component\HttpFoundation\Response;
37
use Symfony\Component\Intl\Languages;
38
use Symfony\Component\Routing\Annotation\Route;
39
use Symfony\Component\Security\Core\User\UserInterface;
40
41
class DashboardController extends AbstractDashboardController
42
{
43
    private $app_version;
44
    private $gitVersionInfo;
45
46
    public function __construct(string $app_version, GitVersionInfo $gitVersionInfo)
47
    {
48
        $this->app_version = $app_version;
49
        $this->gitVersionInfo = $gitVersionInfo;
50
    }
51
52
    public function configureDashboard(): Dashboard
53
    {
54
        return Dashboard::new()
55
            ->setTitle('StuRa Finanzen');
56
    }
57
58
    /**
59
     * @Route("/admin", name="admin_dashboard", )
60
     */
61
    public function index(): Response
62
    {
63
        return $this->render('admin/dashboard.html.twig');
64
    }
65
66
    private function addFiltersToMenuItem(CrudMenuItem $menuItem, array $filters): CrudMenuItem
67
    {
68
        //Set referrer or we encounter errrors... (not needed in JB custom version))
69
70
        //$referrer = $this->crud_url_generator->build()->currentPageReferrer;
71
        //$menuItem->setQueryParameter('referrer', $referrer);
72
73
        foreach ($filters as $filter => $value) {
74
            $menuItem->setQueryParameter('filters['.$filter.']', $value);
75
        }
76
77
        $menuItem->setQueryParameter('crudAction', 'index');
78
79
        return $menuItem;
80
    }
81
82
    public function configureActions(): Actions
83
    {
84
        $actions = parent::configureActions();
85
86
        $showLog = Action::new('showLog', 'action.show_logs', 'fas fa-binoculars')
87
            ->displayIf(function (DBElementInterface $entity) {
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

87
            ->displayIf(function (/** @scrutinizer ignore-unused */ DBElementInterface $entity) {

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...
88
                return $this->isGranted('ROLE_VIEW_AUDITS');
89
            })
90
            ->setCssClass('ml-2 text-dark')
91
            ->linkToRoute('dh_auditor_show_entity_history', function(DBElementInterface $entity) {
92
                return [
93
                    'entity' => str_replace('\\', '-', get_class($entity)),
94
                    'id' => $entity->getId(),
95
                ];
96
            });
97
98
        return $actions
99
            ->add(Crud::PAGE_DETAIL, $showLog)
100
            ->add(Crud::PAGE_EDIT, $showLog);
101
    }
102
103
    public function configureMenuItems(): iterable
104
    {
105
        $mathematically_checking = MenuItem::linkToCrud('payment_order.mathematically_checking_needed', '', PaymentOrder::class)
106
            ->setDefaultSort([
107
                'creation_date' => 'ASC',
108
            ]);
109
        $this->addFiltersToMenuItem($mathematically_checking, [
110
            'mathematically_correct' => 0,
111
            'confirmed' => 1,
112
        ]);
113
114
        $ready_for_export_section = MenuItem::linkToCrud('payment_order.ready_for_export.section', '', PaymentOrder::class)
115
            ->setDefaultSort([
116
                'creation_date' => 'ASC',
117
            ]);
118
        $this->addFiltersToMenuItem($ready_for_export_section, [
119
            'mathematically_correct' => 1,
120
            'exported' => 0,
121
            'confirmed' => 1,
122
        ]);
123
124
        $factually_checking_fsr = MenuItem::linkToCrud('payment_order.factually_checking_needed.fsr', '', PaymentOrder::class)
125
            ->setDefaultSort([
126
                'creation_date' => 'ASC',
127
            ]);
128
        $this->addFiltersToMenuItem($factually_checking_fsr, [
129
            'factually_correct' => 0,
130
            'department_type' => 'fsr',
131
            'exported' => 1,
132
            'confirmed' => 1,
133
        ]);
134
135
        $factually_checking_section = MenuItem::linkToCrud('payment_order.factually_checking_needed.section', '', PaymentOrder::class)
136
            ->setDefaultSort([
137
                'creation_date' => 'ASC',
138
            ]);
139
        $this->addFiltersToMenuItem($factually_checking_section, [
140
            'factually_correct' => 0,
141
            'department_type' => 'section_misc',
142
            'exported' => 1,
143
            'confirmed' => 1,
144
        ]);
145
146
        $finished = MenuItem::linkToCrud('payment_order.finished', '', PaymentOrder::class)
147
            ->setDefaultSort([
148
                'creation_date' => 'DESC',
149
            ]);
150
        $this->addFiltersToMenuItem($finished, [
151
            'factually_correct' => 1,
152
            'mathematically_correct' => 1,
153
            'exported' => 1,
154
            'confirmed' => 1,
155
        ]);
156
157
        $unconfirmed = MenuItem::linkToCrud('payment_order.unconfirmed', '', PaymentOrder::class)
158
            ->setDefaultSort([
159
                'creation_date' => 'ASC',
160
            ]);
161
        $this->addFiltersToMenuItem($unconfirmed, [
162
            'confirmed' => 0,
163
        ]);
164
165
        $items = [
166
            $mathematically_checking,
167
            $ready_for_export_section,
168
            $factually_checking_fsr,
169
            $factually_checking_section,
170
            $finished,
171
            $unconfirmed,
172
            MenuItem::linkToCrud('payment_order.all', '', PaymentOrder::class),
173
        ];
174
175
        yield MenuItem::subMenu('payment_order.labelp', 'fas fa-file-invoice-dollar')
176
            ->setPermission('ROLE_SHOW_PAYMENT_ORDERS')
177
            ->setSubItems($items);
178
179
        yield MenuItem::linkToCrud('department.labelp', 'fas fa-sitemap', Department::class)
180
            ->setPermission('ROLE_READ_ORGANISATIONS');
181
        yield MenuItem::linkToCrud('bank_account.labelp', 'fas fa-university', BankAccount::class)
182
            ->setPermission('ROLE_READ_BANK_ACCOUNTS');
183
        yield MenuItem::linkToCrud('user.labelp', 'fas fa-user', User::class)
184
            ->setPermission('ROLE_READ_USER');
185
186
        $version = $this->app_version.'-'.$this->gitVersionInfo->getGitCommitHash() ?? '';
187
        yield MenuItem::section('Version '.$version, 'fas fa-info');
188
        yield MenuItem::linktoRoute('dashboard.menu.audits', 'fas fa-binoculars', 'dh_auditor_list_audits')
189
            ->setPermission('ROLE_VIEW_AUDITS');
190
        yield MenuItem::linktoRoute('dashboard.menu.homepage', 'fas fa-home', 'homepage');
191
        yield MenuItem::linkToUrl('dashboard.menu.stura', 'fab fa-rebel', 'https://www.stura.uni-jena.de/');
192
        yield MenuItem::linkToUrl('dashboard.menu.github', 'fab fa-github', 'https://github.com/jbtronics/StuRa-Finanzsoftware');
193
    }
194
195
    public function configureUserMenu(UserInterface $user): UserMenu
196
    {
197
        /** @var User $user */
198
199
        return parent::configureUserMenu($user)
200
            ->setName((string) $user)
201
            ->displayUserName(true)
202
            ->addMenuItems([
203
                MenuItem::linktoRoute('user.settings.title', 'fas fa-user-cog', 'user_settings'),
204
                MenuItem::linktoRoute(Languages::getName('de', 'de').' (DE)', '', 'admin_dashboard.de'),
205
                MenuItem::linktoRoute(Languages::getName('en', 'en').' (EN)', '', 'admin_dashboard.en'),
206
            ]);
207
    }
208
209
    public function configureCrud(): Crud
210
    {
211
        return parent::configureCrud()
212
            ->setPaginatorPageSize(40);
213
    }
214
215
    public function configureAssets(): Assets
216
    {
217
        return Assets::new()->addCssFile('admin_styles.css');
218
    }
219
}
220