xref: /webtrees/app/Module/SearchMenuModule.php (revision 1ff45046fabc22237b5d0d8e489c96f031fc598d)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 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 <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Module;
21
22use Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\Http\RequestHandlers\SearchAdvancedPage;
24use Fisharebest\Webtrees\Http\RequestHandlers\SearchGeneralPage;
25use Fisharebest\Webtrees\Http\RequestHandlers\SearchPhoneticPage;
26use Fisharebest\Webtrees\Http\RequestHandlers\SearchReplacePage;
27use Fisharebest\Webtrees\I18N;
28use Fisharebest\Webtrees\Menu;
29use Fisharebest\Webtrees\Tree;
30
31use function route;
32
33/**
34 * Class SearchMenuModule - provide a menu option for the search options
35 */
36class SearchMenuModule extends AbstractModule implements ModuleMenuInterface
37{
38    use ModuleMenuTrait;
39
40    /**
41     * How should this module be identified in the control panel, etc.?
42     *
43     * @return string
44     */
45    public function title(): string
46    {
47        /* I18N: Name of a module */
48        return I18N::translate('Search');
49    }
50
51    /**
52     * A sentence describing what this module does.
53     *
54     * @return string
55     */
56    public function description(): string
57    {
58        /* I18N: Description of the “Search” module */
59        return I18N::translate('The search menu.');
60    }
61
62    /**
63     * The default position for this menu.  It can be changed in the control panel.
64     *
65     * @return int
66     */
67    public function defaultMenuOrder(): int
68    {
69        return 6;
70    }
71
72    /**
73     * A menu, to be added to the main application menu.
74     *
75     * @param Tree $tree
76     *
77     * @return Menu|null
78     */
79    public function getMenu(Tree $tree): Menu|null
80    {
81        $submenu = [
82            $this->menuSearchGeneral($tree),
83            $this->menuSearchPhonetic($tree),
84            $this->menuSearchAdvanced($tree),
85            $this->menuSearchAndReplace($tree),
86        ];
87
88        $submenu = array_filter($submenu);
89
90        return new Menu(I18N::translate('Search'), '#', 'menu-search', ['rel' => 'nofollow'], $submenu);
91    }
92
93    /**
94     * @param Tree $tree
95     *
96     * @return Menu
97     */
98    protected function menuSearchGeneral(Tree $tree): Menu
99    {
100        return new Menu(I18N::translate('General search'), route(SearchGeneralPage::class, ['tree' => $tree->name()]), 'menu-search-general', ['rel' => 'nofollow']);
101    }
102
103    /**
104     * @param Tree $tree
105     *
106     * @return Menu
107     */
108    protected function menuSearchPhonetic(Tree $tree): Menu
109    {
110        $url = route(SearchPhoneticPage::class, ['tree' => $tree->name()]);
111
112        /* I18N: search using “sounds like”, rather than exact spelling */
113        return new Menu(I18N::translate('Phonetic search'), $url, 'menu-search-soundex', ['rel' => 'nofollow']);
114    }
115
116    /**
117     * @param Tree $tree
118     *
119     * @return Menu
120     */
121    protected function menuSearchAdvanced(Tree $tree): Menu
122    {
123        $url = route(SearchAdvancedPage::class, ['tree' => $tree->name()]);
124
125        return new Menu(I18N::translate('Advanced search'), $url, 'menu-search-advanced', ['rel' => 'nofollow']);
126    }
127
128    /**
129     * @param Tree $tree
130     *
131     * @return Menu|null
132     */
133    protected function menuSearchAndReplace(Tree $tree): Menu|null
134    {
135        if (Auth::isEditor($tree)) {
136            $url = route(SearchReplacePage::class, ['tree' => $tree->name()]);
137
138            return new Menu(I18N::translate('Search and replace'), $url, 'menu-search-replace');
139        }
140
141        return null;
142    }
143}
144