xref: /webtrees/app/Module/ChartsMenuModule.php (revision 7413816e6dd2d50e569034fb804f3dce7471bb94)
146295629SGreg Roach<?php
23976b470SGreg Roach
346295629SGreg Roach/**
446295629SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
646295629SGreg Roach * This program is free software: you can redistribute it and/or modify
746295629SGreg Roach * it under the terms of the GNU General Public License as published by
846295629SGreg Roach * the Free Software Foundation, either version 3 of the License, or
946295629SGreg Roach * (at your option) any later version.
1046295629SGreg Roach * This program is distributed in the hope that it will be useful,
1146295629SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
1246295629SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1346295629SGreg Roach * GNU General Public License for more details.
1446295629SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
1646295629SGreg Roach */
17fcfa147eSGreg Roach
1846295629SGreg Roachdeclare(strict_types=1);
1946295629SGreg Roach
2046295629SGreg Roachnamespace Fisharebest\Webtrees\Module;
2146295629SGreg Roach
2246295629SGreg Roachuse Fisharebest\Webtrees\Auth;
2346295629SGreg Roachuse Fisharebest\Webtrees\I18N;
2446295629SGreg Roachuse Fisharebest\Webtrees\Menu;
25d35568b4SGreg Roachuse Fisharebest\Webtrees\Registry;
264ca7e03cSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
2746295629SGreg Roachuse Fisharebest\Webtrees\Tree;
28b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
296ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
3046295629SGreg Roach
3146295629SGreg Roach/**
3246295629SGreg Roach * Class ChartsMenuModule - provide a menu option for the charts
3346295629SGreg Roach */
3437eb8894SGreg Roachclass ChartsMenuModule extends AbstractModule implements ModuleMenuInterface
3546295629SGreg Roach{
3646295629SGreg Roach    use ModuleMenuTrait;
3746295629SGreg Roach
3843f2f523SGreg Roach    private ModuleService $module_service;
394ca7e03cSGreg Roach
404ca7e03cSGreg Roach    /**
414ca7e03cSGreg Roach     * @param ModuleService $module_service
424ca7e03cSGreg Roach     */
434ca7e03cSGreg Roach    public function __construct(ModuleService $module_service)
444ca7e03cSGreg Roach    {
454ca7e03cSGreg Roach        $this->module_service = $module_service;
464ca7e03cSGreg Roach    }
474ca7e03cSGreg Roach
484ca7e03cSGreg Roach    /**
490cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
50961ec755SGreg Roach     *
51961ec755SGreg Roach     * @return string
52961ec755SGreg Roach     */
5346295629SGreg Roach    public function title(): string
5446295629SGreg Roach    {
5546295629SGreg Roach        /* I18N: Name of a module */
5646295629SGreg Roach        return I18N::translate('Charts');
5746295629SGreg Roach    }
5846295629SGreg Roach
5946295629SGreg Roach    public function description(): string
6046295629SGreg Roach    {
619e0868cbSGreg Roach        /* I18N: Description of the “Charts” module */
6246295629SGreg Roach        return I18N::translate('The charts menu.');
6346295629SGreg Roach    }
6446295629SGreg Roach
6546295629SGreg Roach    /**
6646295629SGreg Roach     * The default position for this menu.  It can be changed in the control panel.
6746295629SGreg Roach     *
6846295629SGreg Roach     * @return int
6946295629SGreg Roach     */
7046295629SGreg Roach    public function defaultMenuOrder(): int
7146295629SGreg Roach    {
72353b36abSGreg Roach        return 2;
7346295629SGreg Roach    }
7446295629SGreg Roach
7546295629SGreg Roach    /**
7646295629SGreg Roach     * A menu, to be added to the main application menu.
7746295629SGreg Roach     */
78*1ff45046SGreg Roach    public function getMenu(Tree $tree): Menu|null
7946295629SGreg Roach    {
80d35568b4SGreg Roach        $request    = Registry::container()->get(ServerRequestInterface::class);
81b55cbc6bSGreg Roach        $xref       = Validator::attributes($request)->isXref()->string('xref', '');
823370567dSGreg Roach        $individual = $tree->significantIndividual(Auth::user(), $xref);
8387cca37cSGreg Roach        $submenus   = $this->module_service->findByComponent(ModuleChartInterface::class, $tree, Auth::user())
84f25fc0f9SGreg Roach            ->map(static fn (ModuleChartInterface $module): Menu => $module->chartMenu($individual))
85f25fc0f9SGreg Roach            ->sort(static fn (Menu $x, Menu $y): int => I18N::comparator()($x->getLabel(), $y->getLabel()));
8646295629SGreg Roach
8746295629SGreg Roach        if ($submenus->isEmpty()) {
8846295629SGreg Roach            return null;
8946295629SGreg Roach        }
9046295629SGreg Roach
9146295629SGreg Roach        return new Menu(I18N::translate('Charts'), '#', 'menu-chart', ['rel' => 'nofollow'], $submenus->all());
9246295629SGreg Roach    }
9346295629SGreg Roach}
94