xref: /webtrees/app/Module/ReportsMenuModule.php (revision d11be7027e34e3121be11cc025421873364403f9)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
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 General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Module;
21
22use Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Menu;
25use Fisharebest\Webtrees\Services\ModuleService;
26use Fisharebest\Webtrees\Tree;
27use Fisharebest\Webtrees\Validator;
28use Psr\Http\Message\ServerRequestInterface;
29
30use function app;
31use function assert;
32
33/**
34 * Class ReportsMenuModule - provide a menu option for the reports
35 */
36class ReportsMenuModule extends AbstractModule implements ModuleMenuInterface
37{
38    use ModuleMenuTrait;
39
40    private ModuleService $module_service;
41
42    /**
43     * ChartsMenuModule constructor.
44     *
45     * @param ModuleService $module_service
46     */
47    public function __construct(ModuleService $module_service)
48    {
49        $this->module_service = $module_service;
50    }
51
52    /**
53     * How should this module be identified in the control panel, etc.?
54     *
55     * @return string
56     */
57    public function title(): string
58    {
59        /* I18N: Name of a module */
60        return I18N::translate('Reports');
61    }
62
63    /**
64     * A sentence describing what this module does.
65     *
66     * @return string
67     */
68    public function description(): string
69    {
70        /* I18N: Description of the “Reports” module */
71        return I18N::translate('The reports menu.');
72    }
73
74    /**
75     * The default position for this menu.  It can be changed in the control panel.
76     *
77     * @return int
78     */
79    public function defaultMenuOrder(): int
80    {
81        return 5;
82    }
83
84    /**
85     * A menu, to be added to the main application menu.
86     *
87     * @param Tree $tree
88     *
89     * @return Menu|null
90     */
91    public function getMenu(Tree $tree): ?Menu
92    {
93        $request = app(ServerRequestInterface::class);
94        assert($request instanceof ServerRequestInterface);
95
96        $xref       = Validator::attributes($request)->isXref()->string('xref', '');
97        $individual = $tree->significantIndividual(Auth::user(), $xref);
98        $submenus   = $this->module_service->findByComponent(ModuleReportInterface::class, $tree, Auth::user())
99            ->map(static function (ModuleReportInterface $module) use ($individual): Menu {
100                return $module->getReportMenu($individual);
101            })
102            ->sort(static function (Menu $x, Menu $y): int {
103                return $x->getLabel() <=> $y->getLabel();
104            });
105
106        if ($submenus->isEmpty()) {
107            return null;
108        }
109
110        return new Menu(I18N::translate('Reports'), '#', 'menu-report', ['rel' => 'nofollow'], $submenus->all());
111    }
112}
113