xref: /webtrees/app/Module/SearchMenuModule.php (revision 7413816e6dd2d50e569034fb804f3dce7471bb94)
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    public function description(): string
52    {
53        /* I18N: Description of the “Search” module */
54        return I18N::translate('The search menu.');
55    }
56
57    /**
58     * The default position for this menu.  It can be changed in the control panel.
59     *
60     * @return int
61     */
62    public function defaultMenuOrder(): int
63    {
64        return 6;
65    }
66
67    /**
68     * A menu, to be added to the main application menu.
69     *
70     * @param Tree $tree
71     *
72     * @return Menu|null
73     */
74    public function getMenu(Tree $tree): Menu|null
75    {
76        $submenu = [
77            $this->menuSearchGeneral($tree),
78            $this->menuSearchPhonetic($tree),
79            $this->menuSearchAdvanced($tree),
80            $this->menuSearchAndReplace($tree),
81        ];
82
83        $submenu = array_filter($submenu);
84
85        return new Menu(I18N::translate('Search'), '#', 'menu-search', ['rel' => 'nofollow'], $submenu);
86    }
87
88    /**
89     * @param Tree $tree
90     *
91     * @return Menu
92     */
93    protected function menuSearchGeneral(Tree $tree): Menu
94    {
95        return new Menu(I18N::translate('General search'), route(SearchGeneralPage::class, ['tree' => $tree->name()]), 'menu-search-general', ['rel' => 'nofollow']);
96    }
97
98    /**
99     * @param Tree $tree
100     *
101     * @return Menu
102     */
103    protected function menuSearchPhonetic(Tree $tree): Menu
104    {
105        $url = route(SearchPhoneticPage::class, ['tree' => $tree->name()]);
106
107        /* I18N: search using “sounds like”, rather than exact spelling */
108        return new Menu(I18N::translate('Phonetic search'), $url, 'menu-search-soundex', ['rel' => 'nofollow']);
109    }
110
111    /**
112     * @param Tree $tree
113     *
114     * @return Menu
115     */
116    protected function menuSearchAdvanced(Tree $tree): Menu
117    {
118        $url = route(SearchAdvancedPage::class, ['tree' => $tree->name()]);
119
120        return new Menu(I18N::translate('Advanced search'), $url, 'menu-search-advanced', ['rel' => 'nofollow']);
121    }
122
123    /**
124     * @param Tree $tree
125     *
126     * @return Menu|null
127     */
128    protected function menuSearchAndReplace(Tree $tree): Menu|null
129    {
130        if (Auth::isEditor($tree)) {
131            $url = route(SearchReplacePage::class, ['tree' => $tree->name()]);
132
133            return new Menu(I18N::translate('Search and replace'), $url, 'menu-search-replace');
134        }
135
136        return null;
137    }
138}
139