xref: /webtrees/app/Module/ListsMenuModule.php (revision 961ec7555e8fa1828ea16f2a1fd6a74f140ce707)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Module;
19
20use Fisharebest\Webtrees\I18N;
21use Fisharebest\Webtrees\Menu;
22use Fisharebest\Webtrees\Tree;
23use Illuminate\Database\Capsule\Manager as DB;
24
25/**
26 * Class ListsMenuModule - provide a menu option for the lists
27 */
28class ListsMenuModule extends AbstractModule implements ModuleInterface, ModuleMenuInterface
29{
30    use ModuleMenuTrait;
31
32    /**
33     * How should this module be labelled on tabs, menus, etc.?
34     *
35     * @return string
36     */
37    public function title(): string
38    {
39        /* I18N: Name of a module */
40        return I18N::translate('Lists');
41    }
42
43    /**
44     * A sentence describing what this module does.
45     *
46     * @return string
47     */
48    public function description(): string
49    {
50        /* I18N: Description of the “Reports” module */
51        return I18N::translate('The lists menu.');
52    }
53
54    /**
55     * The default position for this menu.  It can be changed in the control panel.
56     *
57     * @return int
58     */
59    public function defaultMenuOrder(): int
60    {
61        return 30;
62    }
63
64    /**
65     * A menu, to be added to the main application menu.
66     *
67     * @param Tree $tree
68     *
69     * @return Menu|null
70     */
71    public function getMenu(Tree $tree): ?Menu
72    {
73        // Do not show empty lists
74        $sources_exist = DB::table('sources')
75            ->where('s_file', '=', $tree->id())
76            ->exists();
77
78        $repositories_exist = DB::table('other')
79            ->where('o_file', '=', $tree->id())
80            ->where('o_type', '=', 'REPO')
81            ->exists();
82
83        $notes_exist = DB::table('other')
84            ->where('o_file', '=', $tree->id())
85            ->where('o_type', '=', 'NOTE')
86            ->exists();
87
88        $media_exist = DB::table('media')
89            ->where('m_file', '=', $tree->id())
90            ->exists();
91
92        $submenus = [
93            new Menu(I18N::translate('Individuals'), route('individual-list', ['ged' => $tree->name()]), 'menu-list-indi'),
94            new Menu(I18N::translate('Families'), route('family-list', ['ged' => $tree->name()]), 'menu-list-fam'),
95            new Menu(I18N::translate('Branches'), route('branches', ['ged' => $tree->name()]), 'menu-branches', ['rel' => 'nofollow']),
96            new Menu(I18N::translate('Place hierarchy'), route('place-hierarchy', ['ged' => $tree->name()]), 'menu-list-plac', ['rel' => 'nofollow']),
97        ];
98
99        if ($media_exist) {
100            $submenus[] = new Menu(I18N::translate('Media objects'), route('media-list', ['ged' => $tree->name()]), 'menu-list-obje', ['rel' => 'nofollow']);
101        }
102        if ($repositories_exist) {
103            $submenus[] = new Menu(I18N::translate('Repositories'), route('repository-list', ['ged' => $tree->name()]), 'menu-list-repo', ['rel' => 'nofollow']);
104        }
105        if ($sources_exist) {
106            $submenus[] = new Menu(I18N::translate('Sources'), route('source-list', ['ged' => $tree->name()]), 'menu-list-sour', ['rel' => 'nofollow']);
107        }
108        if ($notes_exist) {
109            $submenus[] = new Menu(I18N::translate('Shared notes'), route('note-list', ['ged' => $tree->name()]), 'menu-list-note', ['rel' => 'nofollow']);
110        }
111
112        uasort($submenus, function (Menu $x, Menu $y) {
113            return I18N::strcasecmp($x->getLabel(), $y->getLabel());
114        });
115
116        return new Menu(I18N::translate('Lists'), '#', 'menu-list', [], $submenus);
117    }
118}
119