xref: /webtrees/app/Module/ReportsMenuModule.php (revision 1f1ffa65b3b51df2b95b5c68894525436855964a)
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\Services\ModuleService;
25use Fisharebest\Webtrees\Tree;
26use Psr\Http\Message\ServerRequestInterface;
27use function app;
28
29/**
30 * Class ReportsMenuModule - provide a menu option for the reports
31 */
32class ReportsMenuModule extends AbstractModule implements ModuleMenuInterface
33{
34    use ModuleMenuTrait;
35
36    /**
37     * @var ModuleService
38     */
39    private $module_service;
40
41    /**
42     * ChartsMenuModule constructor.
43     *
44     * @param ModuleService $module_service
45     */
46    public function __construct(ModuleService $module_service)
47    {
48        $this->module_service = $module_service;
49    }
50
51    /**
52     * How should this module be identified in the control panel, etc.?
53     *
54     * @return string
55     */
56    /** {@inheritdoc} */
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        $xref       = $request->getQueryParams()['xref'] ?? '';
95        $individual = Individual::getInstance($xref, $tree) ?? $tree->significantIndividual(Auth::user());
96        $submenus   = $this->module_service->findByComponent(ModuleReportInterface::class, $tree, Auth::user())
97            ->map(static function (ModuleReportInterface $module) use ($individual): Menu {
98                return $module->getReportMenu($individual);
99            });
100
101        if ($submenus->isEmpty()) {
102            return null;
103        }
104
105        return new Menu(I18N::translate('Reports'), '#', 'menu-report', ['rel' => 'nofollow'], $submenus->all());
106    }
107}
108