xref: /webtrees/app/Module/ReportsMenuModule.php (revision 8121b9bec19818120092699199161a1357bb8f3f)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Module;
19
20use Fisharebest\Webtrees\Auth;
21use Fisharebest\Webtrees\I18N;
22use Fisharebest\Webtrees\Individual;
23use Fisharebest\Webtrees\Menu;
24use Fisharebest\Webtrees\Module;
25use Fisharebest\Webtrees\Tree;
26use Symfony\Component\HttpFoundation\Request;
27
28/**
29 * Class ReportsMenuModule - provide a menu option for the reports
30 */
31class ReportsMenuModule extends AbstractModule implements ModuleMenuInterface
32{
33    use ModuleMenuTrait;
34
35    /**
36     * How should this module be labelled on tabs, menus, etc.?
37     *
38     * @return string
39     */
40    /** {@inheritdoc} */
41    public function title(): string
42    {
43        /* I18N: Name of a module */
44        return I18N::translate('Reports');
45    }
46
47    /**
48     * A sentence describing what this module does.
49     *
50     * @return string
51     */
52    public function description(): string
53    {
54        /* I18N: Description of the “Reports” module */
55        return I18N::translate('The reports menu.');
56    }
57
58    /**
59     * The default position for this menu.  It can be changed in the control panel.
60     *
61     * @return int
62     */
63    public function defaultMenuOrder(): int
64    {
65        return 5;
66    }
67
68    /**
69     * A menu, to be added to the main application menu.
70     *
71     * @param Tree $tree
72     *
73     * @return Menu|null
74     */
75    public function getMenu(Tree $tree): ?Menu
76    {
77        $request    = Request::createFromGlobals();
78        $xref       = $request->get('xref', '');
79        $individual = Individual::getInstance($xref, $tree) ?? $tree->significantIndividual(Auth::user());
80        $submenus   = Module::findByComponent('report', $tree, Auth::user())
81            ->map(function (ModuleReportInterface $module) use ($individual): Menu {
82                return $module->getReportMenu($individual);
83            });
84
85        if ($submenus->isEmpty()) {
86            return null;
87        }
88
89        return new Menu(I18N::translate('Reports'), '#', 'menu-report', ['rel' => 'nofollow'], $submenus->all());
90    }
91}
92