xref: /webtrees/app/Module/TreesMenuModule.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
228e0e1b25SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\TreePage;
2346295629SGreg Roachuse Fisharebest\Webtrees\I18N;
2446295629SGreg Roachuse Fisharebest\Webtrees\Menu;
253df1e584SGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
2646295629SGreg Roachuse Fisharebest\Webtrees\Site;
2746295629SGreg Roachuse Fisharebest\Webtrees\Tree;
2846295629SGreg Roach
293df1e584SGreg Roachuse function e;
303df1e584SGreg Roachuse function route;
313df1e584SGreg Roach
3246295629SGreg Roach/**
3346295629SGreg Roach * Class TreesMenuModule - provide a menu option for the trees options
3446295629SGreg Roach */
3537eb8894SGreg Roachclass TreesMenuModule extends AbstractModule implements ModuleMenuInterface
3646295629SGreg Roach{
3746295629SGreg Roach    use ModuleMenuTrait;
3846295629SGreg Roach
3943f2f523SGreg Roach    private TreeService $tree_service;
403df1e584SGreg Roach
413df1e584SGreg Roach    /**
423df1e584SGreg Roach     * @param TreeService $tree_service
433df1e584SGreg Roach     */
443df1e584SGreg Roach    public function __construct(TreeService $tree_service)
453df1e584SGreg Roach    {
463df1e584SGreg Roach        $this->tree_service = $tree_service;
473df1e584SGreg Roach    }
483df1e584SGreg Roach
49961ec755SGreg Roach    /**
500cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
51961ec755SGreg Roach     *
52961ec755SGreg Roach     * @return string
53961ec755SGreg Roach     */
5446295629SGreg Roach    public function title(): string
5546295629SGreg Roach    {
5646295629SGreg Roach        /* I18N: Name of a module */
5746295629SGreg Roach        return I18N::translate('Family trees');
5846295629SGreg Roach    }
5946295629SGreg Roach
6046295629SGreg Roach    public function description(): string
6146295629SGreg Roach    {
629e0868cbSGreg Roach        /* I18N: Description of the “Family trees” module */
6346295629SGreg Roach        return I18N::translate('The family trees menu.');
6446295629SGreg Roach    }
6546295629SGreg Roach
6646295629SGreg Roach    /**
6746295629SGreg Roach     * The default position for this menu.  It can be changed in the control panel.
6846295629SGreg Roach     *
6946295629SGreg Roach     * @return int
7046295629SGreg Roach     */
7146295629SGreg Roach    public function defaultMenuOrder(): int
7246295629SGreg Roach    {
73353b36abSGreg Roach        return 1;
7446295629SGreg Roach    }
7546295629SGreg Roach
7646295629SGreg Roach    /**
7746295629SGreg Roach     * A menu, to be added to the main application menu.
7846295629SGreg Roach     *
7946295629SGreg Roach     * @param Tree $tree
8046295629SGreg Roach     *
8146295629SGreg Roach     * @return Menu|null
8246295629SGreg Roach     */
83*1ff45046SGreg Roach    public function getMenu(Tree $tree): Menu|null
8446295629SGreg Roach    {
853df1e584SGreg Roach        $trees = $this->tree_service->all();
863df1e584SGreg Roach
873df1e584SGreg Roach        if ($trees->count() === 1 || Site::getPreference('ALLOW_CHANGE_GEDCOM') !== '1') {
888e0e1b25SGreg Roach            return new Menu(I18N::translate('Family tree'), route(TreePage::class, ['tree' => $tree->name()]), 'menu-tree');
8946295629SGreg Roach        }
9046295629SGreg Roach
9146295629SGreg Roach        $submenus = [];
923df1e584SGreg Roach        foreach ($trees as $menu_tree) {
9346295629SGreg Roach            if ($menu_tree->id() === $tree->id()) {
9446295629SGreg Roach                $active = 'active ';
9546295629SGreg Roach            } else {
9646295629SGreg Roach                $active = '';
9746295629SGreg Roach            }
988e0e1b25SGreg Roach            $submenus[] = new Menu(e($menu_tree->title()), route(TreePage::class, ['tree' => $menu_tree->name()]), $active . 'menu-tree-' . $menu_tree->id());
9946295629SGreg Roach        }
10046295629SGreg Roach
10146295629SGreg Roach        return new Menu(I18N::translate('Family trees'), '#', 'menu-tree', [], $submenus);
10246295629SGreg Roach    }
10346295629SGreg Roach}
104