xref: /webtrees/app/Module/ChartsMenuModule.php (revision 9b152ff9230017d2c03aa1bf603a98b18250446d)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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 <http://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\Individual;
25use Fisharebest\Webtrees\Menu;
26use Fisharebest\Webtrees\Services\ModuleService;
27use Fisharebest\Webtrees\Tree;
28use Psr\Http\Message\ServerRequestInterface;
29
30/**
31 * Class ChartsMenuModule - provide a menu option for the charts
32 */
33class ChartsMenuModule extends AbstractModule implements ModuleMenuInterface
34{
35    use ModuleMenuTrait;
36
37    /**
38     * @var ModuleService
39     */
40    private $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('Charts');
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 “Charts” module */
71        return I18N::translate('The charts 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 2;
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->getAttribute('xref', '');
95        $individual = Individual::getInstance($xref, $tree) ?? $tree->significantIndividual(Auth::user());
96        $submenus   = $this->module_service->findByComponent(ModuleChartInterface::class, $tree, Auth::user())
97            ->map(static function (ModuleChartInterface $module) use ($individual): Menu {
98                return $module->chartMenu($individual);
99            });
100
101        if ($submenus->isEmpty()) {
102            return null;
103        }
104
105        return new Menu(I18N::translate('Charts'), '#', 'menu-chart', ['rel' => 'nofollow'], $submenus->all());
106    }
107}
108