xref: /webtrees/app/Module/ModuleThemeTrait.php (revision 10e0649788c8d7d4974d81c048ca2b225df8f22e)
149a243cbSGreg Roach<?php
23976b470SGreg Roach
349a243cbSGreg Roach/**
449a243cbSGreg Roach * webtrees: online genealogy
55bfc6897SGreg Roach * Copyright (C) 2022 webtrees development team
649a243cbSGreg Roach * This program is free software: you can redistribute it and/or modify
749a243cbSGreg Roach * it under the terms of the GNU General Public License as published by
849a243cbSGreg Roach * the Free Software Foundation, either version 3 of the License, or
949a243cbSGreg Roach * (at your option) any later version.
1049a243cbSGreg Roach * This program is distributed in the hope that it will be useful,
1149a243cbSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
1249a243cbSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1349a243cbSGreg Roach * GNU General Public License for more details.
1449a243cbSGreg 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/>.
1649a243cbSGreg Roach */
17fcfa147eSGreg Roach
1849a243cbSGreg Roachdeclare(strict_types=1);
1949a243cbSGreg Roach
2049a243cbSGreg Roachnamespace Fisharebest\Webtrees\Module;
2149a243cbSGreg Roach
22ade503dfSGreg Roachuse Fisharebest\Webtrees\Auth;
231fe542e9SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
24ade503dfSGreg Roachuse Fisharebest\Webtrees\Fact;
25ade503dfSGreg Roachuse Fisharebest\Webtrees\Gedcom;
26a49d0e3fSGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\AccountEdit;
270c0910bfSGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\ControlPanel;
28ea101122SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\HomePage;
2956f9a9c1SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\LoginPage;
3056f9a9c1SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\Logout;
316fd01894SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\ManageTrees;
3222e73debSGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\PendingChanges;
337adfb8e5SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\SelectLanguage;
347adfb8e5SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\SelectTheme;
358e0e1b25SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\TreePage;
368e0e1b25SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\TreePageEdit;
378e0e1b25SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\UserPage;
388e0e1b25SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\UserPageEdit;
39ade503dfSGreg Roachuse Fisharebest\Webtrees\I18N;
40ade503dfSGreg Roachuse Fisharebest\Webtrees\Individual;
41ade503dfSGreg Roachuse Fisharebest\Webtrees\Menu;
42c72a9801SGreg Roachuse Fisharebest\Webtrees\Registry;
434ca7e03cSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
44ade503dfSGreg Roachuse Fisharebest\Webtrees\Tree;
45b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
466ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
473976b470SGreg Roach
486ccdf4f0SGreg Roachuse function app;
49de2aa325SGreg Roachuse function assert;
50*10e06497SGreg Roachuse function count;
51*10e06497SGreg Roachuse function in_array;
527adfb8e5SGreg Roachuse function route;
53f78837dcSGreg Roachuse function view;
54ade503dfSGreg Roach
5549a243cbSGreg Roach/**
5649a243cbSGreg Roach * Trait ModuleThemeTrait - default implementation of ModuleThemeInterface
5749a243cbSGreg Roach */
5849a243cbSGreg Roachtrait ModuleThemeTrait
5949a243cbSGreg Roach{
60ade503dfSGreg Roach    /**
61bfed30e4SGreg Roach     * How should this module be identified in the control panel, etc.?
62bfed30e4SGreg Roach     *
63bfed30e4SGreg Roach     * @return string
64bfed30e4SGreg Roach     */
65bfed30e4SGreg Roach    abstract public function title(): string;
66bfed30e4SGreg Roach
67bfed30e4SGreg Roach    /**
683d8b2a8eSGreg Roach     * A sentence describing what this module does.
693d8b2a8eSGreg Roach     *
703d8b2a8eSGreg Roach     * @return string
713d8b2a8eSGreg Roach     */
723d8b2a8eSGreg Roach    public function description(): string
733d8b2a8eSGreg Roach    {
743d8b2a8eSGreg Roach        return I18N::translate('Theme') . ' — ' . $this->title();
753d8b2a8eSGreg Roach    }
763d8b2a8eSGreg Roach
773d8b2a8eSGreg Roach    /**
78ade503dfSGreg Roach     * Generate the facts, for display in charts.
79ade503dfSGreg Roach     *
80ade503dfSGreg Roach     * @param Individual $individual
81ade503dfSGreg Roach     *
82ade503dfSGreg Roach     * @return string
83ade503dfSGreg Roach     */
84ade503dfSGreg Roach    public function individualBoxFacts(Individual $individual): string
85ade503dfSGreg Roach    {
86ade503dfSGreg Roach        $html = '';
87ade503dfSGreg Roach
88ade503dfSGreg Roach        $opt_tags = preg_split('/\W/', $individual->tree()->getPreference('CHART_BOX_TAGS'), 0, PREG_SPLIT_NO_EMPTY);
89ade503dfSGreg Roach        // Show BIRT or equivalent event
90ade503dfSGreg Roach        foreach (Gedcom::BIRTH_EVENTS as $birttag) {
9122d65e5aSGreg Roach            if (!in_array($birttag, $opt_tags, true)) {
92820b62dfSGreg Roach                $event = $individual->facts([$birttag])->first();
93820b62dfSGreg Roach                if ($event instanceof Fact) {
94ade503dfSGreg Roach                    $html .= $event->summary();
95ade503dfSGreg Roach                    break;
96ade503dfSGreg Roach                }
97ade503dfSGreg Roach            }
98ade503dfSGreg Roach        }
99ade503dfSGreg Roach        // Show optional events (before death)
100ade503dfSGreg Roach        foreach ($opt_tags as $key => $tag) {
10122d65e5aSGreg Roach            if (!in_array($tag, Gedcom::DEATH_EVENTS, true)) {
102820b62dfSGreg Roach                $event = $individual->facts([$tag])->first();
103820b62dfSGreg Roach                if ($event instanceof Fact) {
104ade503dfSGreg Roach                    $html .= $event->summary();
105ade503dfSGreg Roach                    unset($opt_tags[$key]);
106ade503dfSGreg Roach                }
107ade503dfSGreg Roach            }
108ade503dfSGreg Roach        }
109ade503dfSGreg Roach        // Show DEAT or equivalent event
110ade503dfSGreg Roach        foreach (Gedcom::DEATH_EVENTS as $deattag) {
111820b62dfSGreg Roach            $event = $individual->facts([$deattag])->first();
112820b62dfSGreg Roach            if ($event instanceof Fact) {
113ade503dfSGreg Roach                $html .= $event->summary();
11422d65e5aSGreg Roach                if (in_array($deattag, $opt_tags, true)) {
11522d65e5aSGreg Roach                    unset($opt_tags[array_search($deattag, $opt_tags, true)]);
116ade503dfSGreg Roach                }
117ade503dfSGreg Roach                break;
118ade503dfSGreg Roach            }
119ade503dfSGreg Roach        }
120ade503dfSGreg Roach        // Show remaining optional events (after death)
121ade503dfSGreg Roach        foreach ($opt_tags as $tag) {
122820b62dfSGreg Roach            $event = $individual->facts([$tag])->first();
123820b62dfSGreg Roach            if ($event instanceof Fact) {
124ade503dfSGreg Roach                $html .= $event->summary();
125ade503dfSGreg Roach            }
126ade503dfSGreg Roach        }
127ade503dfSGreg Roach
128ade503dfSGreg Roach        return $html;
129ade503dfSGreg Roach    }
130ade503dfSGreg Roach
131ade503dfSGreg Roach    /**
132ade503dfSGreg Roach     * Links, to show in chart boxes;
133ade503dfSGreg Roach     *
134ade503dfSGreg Roach     * @param Individual $individual
135ade503dfSGreg Roach     *
13609482a55SGreg Roach     * @return array<Menu>
137ade503dfSGreg Roach     */
138ade503dfSGreg Roach    public function individualBoxMenu(Individual $individual): array
139ade503dfSGreg Roach    {
140aa6311c7SGreg Roach        return array_merge(
141ade503dfSGreg Roach            $this->individualBoxMenuCharts($individual),
142ade503dfSGreg Roach            $this->individualBoxMenuFamilyLinks($individual)
143ade503dfSGreg Roach        );
144ade503dfSGreg Roach    }
145ade503dfSGreg Roach
146ade503dfSGreg Roach    /**
147ade503dfSGreg Roach     * Chart links, to show in chart boxes;
148ade503dfSGreg Roach     *
149ade503dfSGreg Roach     * @param Individual $individual
150ade503dfSGreg Roach     *
15109482a55SGreg Roach     * @return array<Menu>
152ade503dfSGreg Roach     */
153ade503dfSGreg Roach    public function individualBoxMenuCharts(Individual $individual): array
154ade503dfSGreg Roach    {
155ade503dfSGreg Roach        $menus = [];
156b55cbc6bSGreg Roach
157b55cbc6bSGreg Roach        $module_service = app(ModuleService::class);
158b55cbc6bSGreg Roach        assert($module_service instanceof ModuleService);
159b55cbc6bSGreg Roach
160b55cbc6bSGreg Roach        foreach ($module_service->findByComponent(ModuleChartInterface::class, $individual->tree(), Auth::user()) as $chart) {
161ade503dfSGreg Roach            $menu = $chart->chartBoxMenu($individual);
162ade503dfSGreg Roach            if ($menu) {
163ade503dfSGreg Roach                $menus[] = $menu;
164ade503dfSGreg Roach            }
165ade503dfSGreg Roach        }
166ade503dfSGreg Roach
1670b93976aSGreg Roach        usort($menus, static function (Menu $x, Menu $y): int {
16837646143SGreg Roach            return I18N::comparator()($x->getLabel(), $y->getLabel());
169ade503dfSGreg Roach        });
170ade503dfSGreg Roach
171ade503dfSGreg Roach        return $menus;
172ade503dfSGreg Roach    }
173ade503dfSGreg Roach
174ade503dfSGreg Roach    /**
175ade503dfSGreg Roach     * Family links, to show in chart boxes.
176ade503dfSGreg Roach     *
177ade503dfSGreg Roach     * @param Individual $individual
178ade503dfSGreg Roach     *
17909482a55SGreg Roach     * @return array<Menu>
180ade503dfSGreg Roach     */
181ade503dfSGreg Roach    public function individualBoxMenuFamilyLinks(Individual $individual): array
182ade503dfSGreg Roach    {
183ade503dfSGreg Roach        $menus = [];
184ade503dfSGreg Roach
18539ca88baSGreg Roach        foreach ($individual->spouseFamilies() as $family) {
186ade503dfSGreg Roach            $menus[] = new Menu('<strong>' . I18N::translate('Family with spouse') . '</strong>', $family->url());
18739ca88baSGreg Roach            $spouse  = $family->spouse($individual);
188ade503dfSGreg Roach            if ($spouse && $spouse->canShowName()) {
18939ca88baSGreg Roach                $menus[] = new Menu($spouse->fullName(), $spouse->url());
190ade503dfSGreg Roach            }
19139ca88baSGreg Roach            foreach ($family->children() as $child) {
192ade503dfSGreg Roach                if ($child->canShowName()) {
19339ca88baSGreg Roach                    $menus[] = new Menu($child->fullName(), $child->url());
194ade503dfSGreg Roach                }
195ade503dfSGreg Roach            }
196ade503dfSGreg Roach        }
197ade503dfSGreg Roach
198ade503dfSGreg Roach        return $menus;
199ade503dfSGreg Roach    }
200ade503dfSGreg Roach
201ade503dfSGreg Roach    /**
202f567c3d8SGreg Roach     * Generate a menu item to change the blocks on the current tree/user page.
203ade503dfSGreg Roach     *
2040c8c69d4SGreg Roach     * @param Tree $tree
2050c8c69d4SGreg Roach     *
206ade503dfSGreg Roach     * @return Menu|null
207ade503dfSGreg Roach     */
208e364afe4SGreg Roach    public function menuChangeBlocks(Tree $tree): ?Menu
209ade503dfSGreg Roach    {
2106ccdf4f0SGreg Roach        $request = app(ServerRequestInterface::class);
211b55cbc6bSGreg Roach        assert($request instanceof ServerRequestInterface);
212e6bcfa02SGreg Roach
213b55cbc6bSGreg Roach        $route = Validator::attributes($request)->route();
214eb235819SGreg Roach
215de2aa325SGreg Roach        if (Auth::check() && $route->name === UserPage::class) {
2168e0e1b25SGreg Roach            return new Menu(I18N::translate('Customize this page'), route(UserPageEdit::class, ['tree' => $tree->name()]), 'menu-change-blocks');
217ade503dfSGreg Roach        }
218ade503dfSGreg Roach
219de2aa325SGreg Roach        if (Auth::isManager($tree) && $route->name === TreePage::class) {
2208e0e1b25SGreg Roach            return new Menu(I18N::translate('Customize this page'), route(TreePageEdit::class, ['tree' => $tree->name()]), 'menu-change-blocks');
221ade503dfSGreg Roach        }
222ade503dfSGreg Roach
223ade503dfSGreg Roach        return null;
224ade503dfSGreg Roach    }
225ade503dfSGreg Roach
226ade503dfSGreg Roach    /**
227ade503dfSGreg Roach     * Generate a menu item for the control panel.
228ade503dfSGreg Roach     *
2290c8c69d4SGreg Roach     * @param Tree $tree
2300c8c69d4SGreg Roach     *
231ade503dfSGreg Roach     * @return Menu|null
232ade503dfSGreg Roach     */
233e364afe4SGreg Roach    public function menuControlPanel(Tree $tree): ?Menu
234ade503dfSGreg Roach    {
235ade503dfSGreg Roach        if (Auth::isAdmin()) {
2360c0910bfSGreg Roach            return new Menu(I18N::translate('Control panel'), route(ControlPanel::class), 'menu-admin');
237ade503dfSGreg Roach        }
238ade503dfSGreg Roach
2390c8c69d4SGreg Roach        if (Auth::isManager($tree)) {
2406fd01894SGreg Roach            return new Menu(I18N::translate('Control panel'), route(ManageTrees::class, ['tree' => $tree->name()]), 'menu-admin');
241ade503dfSGreg Roach        }
242ade503dfSGreg Roach
243ade503dfSGreg Roach        return null;
244ade503dfSGreg Roach    }
245ade503dfSGreg Roach
246ade503dfSGreg Roach    /**
247ade503dfSGreg Roach     * A menu to show a list of available languages.
248ade503dfSGreg Roach     *
249ade503dfSGreg Roach     * @return Menu|null
250ade503dfSGreg Roach     */
251e364afe4SGreg Roach    public function menuLanguages(): ?Menu
252ade503dfSGreg Roach    {
253ade503dfSGreg Roach        $menu = new Menu(I18N::translate('Language'), '#', 'menu-language');
254ade503dfSGreg Roach
25590a2f718SGreg Roach        foreach (I18N::activeLocales() as $active_locale) {
25690a2f718SGreg Roach            $language_tag = $active_locale->languageTag();
25765cf5706SGreg Roach            $class        = 'menu-language-' . $language_tag . (I18N::languageTag() === $language_tag ? ' active' : '');
25890a2f718SGreg Roach            $menu->addSubmenu(new Menu($active_locale->endonym(), '#', $class, [
259d4786c66SGreg Roach                'data-wt-post-url' => route(SelectLanguage::class, ['language' => $language_tag]),
260ade503dfSGreg Roach            ]));
261ade503dfSGreg Roach        }
262ade503dfSGreg Roach
263ade503dfSGreg Roach        if (count($menu->getSubmenus()) > 1) {
264ade503dfSGreg Roach            return $menu;
265ade503dfSGreg Roach        }
266ade503dfSGreg Roach
267ade503dfSGreg Roach        return null;
268ade503dfSGreg Roach    }
269ade503dfSGreg Roach
270ade503dfSGreg Roach    /**
271ade503dfSGreg Roach     * A login menu option (or null if we are already logged in).
272ade503dfSGreg Roach     *
273ade503dfSGreg Roach     * @return Menu|null
274ade503dfSGreg Roach     */
275e364afe4SGreg Roach    public function menuLogin(): ?Menu
276ade503dfSGreg Roach    {
277ade503dfSGreg Roach        if (Auth::check()) {
278ade503dfSGreg Roach            return null;
279ade503dfSGreg Roach        }
280ade503dfSGreg Roach
28186661454SGreg Roach        $request = app(ServerRequestInterface::class);
282b55cbc6bSGreg Roach        assert($request instanceof ServerRequestInterface);
28386661454SGreg Roach
284ade503dfSGreg Roach        // Return to this page after login...
2859f0bdfcdSGreg Roach        $redirect = Validator::queryParams($request)->string('url', (string) $request->getUri());
286b55cbc6bSGreg Roach        $tree     = Validator::attributes($request)->treeOptional();
287b55cbc6bSGreg Roach        $route    = Validator::attributes($request)->route();
288b45ccc4fSGreg Roach
289ade503dfSGreg Roach        // ...but switch from the tree-page to the user-page
290de2aa325SGreg Roach        if ($route->name === TreePage::class) {
2918e0e1b25SGreg Roach            $redirect = route(UserPage::class, ['tree' => $tree instanceof Tree ? $tree->name() : null]);
29291c514e5SGreg Roach        }
293ade503dfSGreg Roach
29486661454SGreg Roach        // Stay on the same tree page
29591c514e5SGreg Roach        $url = route(LoginPage::class, ['tree' => $tree instanceof Tree ? $tree->name() : null, 'url' => $redirect]);
29686661454SGreg Roach
29786661454SGreg Roach        return new Menu(I18N::translate('Sign in'), $url, 'menu-login', ['rel' => 'nofollow']);
298ade503dfSGreg Roach    }
299ade503dfSGreg Roach
300ade503dfSGreg Roach    /**
301ade503dfSGreg Roach     * A logout menu option (or null if we are already logged out).
302ade503dfSGreg Roach     *
303ade503dfSGreg Roach     * @return Menu|null
304ade503dfSGreg Roach     */
305e364afe4SGreg Roach    public function menuLogout(): ?Menu
306ade503dfSGreg Roach    {
307ade503dfSGreg Roach        if (Auth::check()) {
3089b541e4aSGreg Roach            $parameters = [
309d4786c66SGreg Roach                'data-wt-post-url'   => route(Logout::class),
310d4786c66SGreg Roach                'data-wt-reload-url' => route(HomePage::class)
3119b541e4aSGreg Roach            ];
3129b541e4aSGreg Roach
3139b541e4aSGreg Roach            return new Menu(I18N::translate('Sign out'), '#', 'menu-logout', $parameters);
314ade503dfSGreg Roach        }
315ade503dfSGreg Roach
316ade503dfSGreg Roach        return null;
317ade503dfSGreg Roach    }
318ade503dfSGreg Roach
319ade503dfSGreg Roach    /**
320ade503dfSGreg Roach     * A link to allow users to edit their account settings.
321ade503dfSGreg Roach     *
32265aec466SGreg Roach     * @param Tree|null $tree
323a49d0e3fSGreg Roach     *
324a49d0e3fSGreg Roach     * @return Menu
325ade503dfSGreg Roach     */
326a49d0e3fSGreg Roach    public function menuMyAccount(?Tree $tree): Menu
327ade503dfSGreg Roach    {
328a49d0e3fSGreg Roach        $url = route(AccountEdit::class, ['tree' => $tree instanceof Tree ? $tree->name() : null]);
329ade503dfSGreg Roach
330df314983SGreg Roach        return new Menu(I18N::translate('My account'), $url, 'menu-myaccount');
331ade503dfSGreg Roach    }
332ade503dfSGreg Roach
333ade503dfSGreg Roach    /**
334ade503dfSGreg Roach     * A link to the user's individual record (individual.php).
335ade503dfSGreg Roach     *
3360c8c69d4SGreg Roach     * @param Tree $tree
3370c8c69d4SGreg Roach     *
338ade503dfSGreg Roach     * @return Menu|null
339ade503dfSGreg Roach     */
340e364afe4SGreg Roach    public function menuMyIndividualRecord(Tree $tree): ?Menu
341ade503dfSGreg Roach    {
3421fe542e9SGreg Roach        $record = Registry::individualFactory()->make($tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_ACCOUNT_XREF), $tree);
343ade503dfSGreg Roach
344ade503dfSGreg Roach        if ($record) {
345ade503dfSGreg Roach            return new Menu(I18N::translate('My individual record'), $record->url(), 'menu-myrecord');
346ade503dfSGreg Roach        }
347ade503dfSGreg Roach
348ade503dfSGreg Roach        return null;
349ade503dfSGreg Roach    }
350ade503dfSGreg Roach
351ade503dfSGreg Roach    /**
352ade503dfSGreg Roach     * A link to the user's personal home page.
353ade503dfSGreg Roach     *
3540c8c69d4SGreg Roach     * @param Tree $tree
3550c8c69d4SGreg Roach     *
356ade503dfSGreg Roach     * @return Menu
357ade503dfSGreg Roach     */
3580c8c69d4SGreg Roach    public function menuMyPage(Tree $tree): Menu
359ade503dfSGreg Roach    {
3608e0e1b25SGreg Roach        return new Menu(I18N::translate('My page'), route(UserPage::class, ['tree' => $tree->name()]), 'menu-mypage');
361ade503dfSGreg Roach    }
362ade503dfSGreg Roach
363ade503dfSGreg Roach    /**
364ade503dfSGreg Roach     * A menu for the user's personal pages.
365ade503dfSGreg Roach     *
3660c8c69d4SGreg Roach     * @param Tree|null $tree
3670c8c69d4SGreg Roach     *
368ade503dfSGreg Roach     * @return Menu|null
369ade503dfSGreg Roach     */
370e364afe4SGreg Roach    public function menuMyPages(?Tree $tree): ?Menu
371ade503dfSGreg Roach    {
372047fd705SGreg Roach        if (Auth::check()) {
373a49d0e3fSGreg Roach            if ($tree instanceof Tree) {
374ade503dfSGreg Roach                return new Menu(I18N::translate('My pages'), '#', 'menu-mymenu', [], array_filter([
3750c8c69d4SGreg Roach                    $this->menuMyPage($tree),
3760c8c69d4SGreg Roach                    $this->menuMyIndividualRecord($tree),
3770c8c69d4SGreg Roach                    $this->menuMyPedigree($tree),
378a49d0e3fSGreg Roach                    $this->menuMyAccount($tree),
3790c8c69d4SGreg Roach                    $this->menuControlPanel($tree),
3800c8c69d4SGreg Roach                    $this->menuChangeBlocks($tree),
381ade503dfSGreg Roach                ]));
382ade503dfSGreg Roach            }
383ade503dfSGreg Roach
384a49d0e3fSGreg Roach            return $this->menuMyAccount($tree);
385a49d0e3fSGreg Roach        }
386a49d0e3fSGreg Roach
387ade503dfSGreg Roach        return null;
388ade503dfSGreg Roach    }
389ade503dfSGreg Roach
390ade503dfSGreg Roach    /**
391ade503dfSGreg Roach     * A link to the user's individual record.
392ade503dfSGreg Roach     *
3930c8c69d4SGreg Roach     * @param Tree $tree
3940c8c69d4SGreg Roach     *
395ade503dfSGreg Roach     * @return Menu|null
396ade503dfSGreg Roach     */
397e364afe4SGreg Roach    public function menuMyPedigree(Tree $tree): ?Menu
398ade503dfSGreg Roach    {
399c72a9801SGreg Roach        $my_xref = $tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_ACCOUNT_XREF);
400ade503dfSGreg Roach
401b55cbc6bSGreg Roach        $module_service = app(ModuleService::class);
402b55cbc6bSGreg Roach        assert($module_service instanceof ModuleService);
403b55cbc6bSGreg Roach
404b55cbc6bSGreg Roach        $pedigree_chart = $module_service
405b55cbc6bSGreg Roach            ->findByComponent(ModuleChartInterface::class, $tree, Auth::user())
406b55cbc6bSGreg Roach            ->first(static fn (ModuleInterface $module): bool => $module instanceof PedigreeChartModule);
407ade503dfSGreg Roach
408c72a9801SGreg Roach        if ($my_xref !== '' && $pedigree_chart instanceof PedigreeChartModule) {
409c72a9801SGreg Roach            $individual = Registry::individualFactory()->make($my_xref, $tree);
410c72a9801SGreg Roach
411c72a9801SGreg Roach            if ($individual instanceof Individual) {
412ade503dfSGreg Roach                return new Menu(
413ade503dfSGreg Roach                    I18N::translate('My pedigree'),
414c72a9801SGreg Roach                    $pedigree_chart->chartUrl($individual),
415ade503dfSGreg Roach                    'menu-mypedigree'
416ade503dfSGreg Roach                );
417ade503dfSGreg Roach            }
418c72a9801SGreg Roach        }
419ade503dfSGreg Roach
420ade503dfSGreg Roach        return null;
421ade503dfSGreg Roach    }
422ade503dfSGreg Roach
423ade503dfSGreg Roach    /**
424ade503dfSGreg Roach     * Create a pending changes menu.
425ade503dfSGreg Roach     *
4260c8c69d4SGreg Roach     * @param Tree|null $tree
4270c8c69d4SGreg Roach     *
428ade503dfSGreg Roach     * @return Menu|null
429ade503dfSGreg Roach     */
430e364afe4SGreg Roach    public function menuPendingChanges(?Tree $tree): ?Menu
431ade503dfSGreg Roach    {
4320c8c69d4SGreg Roach        if ($tree instanceof Tree && $tree->hasPendingEdit() && Auth::isModerator($tree)) {
433b55cbc6bSGreg Roach            $request = app(ServerRequestInterface::class);
434b55cbc6bSGreg Roach            assert($request instanceof ServerRequestInterface);
435b55cbc6bSGreg Roach
43622e73debSGreg Roach            $url = route(PendingChanges::class, [
4379022ab66SGreg Roach                'tree' => $tree->name(),
438b55cbc6bSGreg Roach                'url' => (string) $request->getUri(),
439ade503dfSGreg Roach            ]);
440ade503dfSGreg Roach
441ade503dfSGreg Roach            return new Menu(I18N::translate('Pending changes'), $url, 'menu-pending');
442ade503dfSGreg Roach        }
443ade503dfSGreg Roach
444ade503dfSGreg Roach        return null;
445ade503dfSGreg Roach    }
446ade503dfSGreg Roach
447ade503dfSGreg Roach    /**
448ade503dfSGreg Roach     * Themes menu.
449ade503dfSGreg Roach     *
450ade503dfSGreg Roach     * @return Menu|null
451ade503dfSGreg Roach     */
452e364afe4SGreg Roach    public function menuThemes(): ?Menu
453ade503dfSGreg Roach    {
454b55cbc6bSGreg Roach        $module_service = app(ModuleService::class);
455b55cbc6bSGreg Roach        assert($module_service instanceof ModuleService);
456b55cbc6bSGreg Roach
457b55cbc6bSGreg Roach        $themes = $module_service->findByInterface(ModuleThemeInterface::class, false, true);
458df8baf00SGreg Roach
459cab242e7SGreg Roach        $current_theme = app(ModuleThemeInterface::class);
4608136679eSGreg Roach
4618136679eSGreg Roach        if ($themes->count() > 1) {
4620b5fd0a6SGreg Roach            $submenus = $themes->map(static function (ModuleThemeInterface $theme) use ($current_theme): Menu {
4638136679eSGreg Roach                $active     = $theme->name() === $current_theme->name();
4648136679eSGreg Roach                $class      = 'menu-theme-' . $theme->name() . ($active ? ' active' : '');
4658136679eSGreg Roach
4668136679eSGreg Roach                return new Menu($theme->title(), '#', $class, [
467d4786c66SGreg Roach                    'data-wt-post-url' => route(SelectTheme::class, ['theme' => $theme->name()]),
468ade503dfSGreg Roach                ]);
469ade503dfSGreg Roach            });
470ade503dfSGreg Roach
4718136679eSGreg Roach            return new Menu(I18N::translate('Theme'), '#', 'menu-theme', [], $submenus->all());
472ade503dfSGreg Roach        }
473ade503dfSGreg Roach
474ade503dfSGreg Roach        return null;
475ade503dfSGreg Roach    }
476ade503dfSGreg Roach
477ade503dfSGreg Roach    /**
478ade503dfSGreg Roach     * Generate a list of items for the main menu.
479ade503dfSGreg Roach     *
4800c8c69d4SGreg Roach     * @param Tree|null $tree
4810c8c69d4SGreg Roach     *
48209482a55SGreg Roach     * @return array<Menu>
483ade503dfSGreg Roach     */
4840c8c69d4SGreg Roach    public function genealogyMenu(?Tree $tree): array
485ade503dfSGreg Roach    {
4860c8c69d4SGreg Roach        if ($tree === null) {
4870c8c69d4SGreg Roach            return [];
4880c8c69d4SGreg Roach        }
4890c8c69d4SGreg Roach
490b55cbc6bSGreg Roach        $module_service = app(ModuleService::class);
491b55cbc6bSGreg Roach        assert($module_service instanceof ModuleService);
492b55cbc6bSGreg Roach
493b55cbc6bSGreg Roach        return $module_service
494b55cbc6bSGreg Roach            ->findByComponent(ModuleMenuInterface::class, $tree, Auth::user())
495b55cbc6bSGreg Roach            ->map(static fn (ModuleMenuInterface $menu): ?Menu => $menu->getMenu($tree))
496ade503dfSGreg Roach            ->filter()
497ade503dfSGreg Roach            ->all();
498ade503dfSGreg Roach    }
499ade503dfSGreg Roach
500ade503dfSGreg Roach    /**
5010c8c69d4SGreg Roach     * Create the genealogy menu.
502ade503dfSGreg Roach     *
50309482a55SGreg Roach     * @param array<Menu> $menus
504ade503dfSGreg Roach     *
505ade503dfSGreg Roach     * @return string
506ade503dfSGreg Roach     */
5070c8c69d4SGreg Roach    public function genealogyMenuContent(array $menus): string
508ade503dfSGreg Roach    {
5090b5fd0a6SGreg Roach        return implode('', array_map(static function (Menu $menu): string {
510f78837dcSGreg Roach            return view('components/menu-item', ['menu' => $menu]);
511ade503dfSGreg Roach        }, $menus));
512ade503dfSGreg Roach    }
513ade503dfSGreg Roach
514ade503dfSGreg Roach    /**
515ade503dfSGreg Roach     * Generate a list of items for the user menu.
516ade503dfSGreg Roach     *
5170c8c69d4SGreg Roach     * @param Tree|null $tree
5180c8c69d4SGreg Roach     *
51909482a55SGreg Roach     * @return array<Menu>
520ade503dfSGreg Roach     */
5210c8c69d4SGreg Roach    public function userMenu(?Tree $tree): array
522ade503dfSGreg Roach    {
523ade503dfSGreg Roach        return array_filter([
5240c8c69d4SGreg Roach            $this->menuPendingChanges($tree),
5250c8c69d4SGreg Roach            $this->menuMyPages($tree),
526ade503dfSGreg Roach            $this->menuThemes(),
527ade503dfSGreg Roach            $this->menuLanguages(),
528ade503dfSGreg Roach            $this->menuLogin(),
529ade503dfSGreg Roach            $this->menuLogout(),
530ade503dfSGreg Roach        ]);
531ade503dfSGreg Roach    }
532ade503dfSGreg Roach
533ade503dfSGreg Roach    /**
534ade503dfSGreg Roach     * A list of CSS files to include for this page.
535ade503dfSGreg Roach     *
53624f2a3afSGreg Roach     * @return array<string>
537ade503dfSGreg Roach     */
538ade503dfSGreg Roach    public function stylesheets(): array
539ade503dfSGreg Roach    {
540ade503dfSGreg Roach        return [];
541ade503dfSGreg Roach    }
54249a243cbSGreg Roach}
543