xref: /webtrees/app/Module/ModuleThemeTrait.php (revision f5402f3dbd759ec94d88460e5ae9815f9b823dae)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Module;
21
22use Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\Fact;
24use Fisharebest\Webtrees\Gedcom;
25use Fisharebest\Webtrees\GedcomTag;
26use Fisharebest\Webtrees\Http\RequestHandlers\ControlPanel;
27use Fisharebest\Webtrees\Http\RequestHandlers\HomePage;
28use Fisharebest\Webtrees\Http\RequestHandlers\LoginPage;
29use Fisharebest\Webtrees\Http\RequestHandlers\Logout;
30use Fisharebest\Webtrees\Http\RequestHandlers\SelectLanguage;
31use Fisharebest\Webtrees\Http\RequestHandlers\SelectTheme;
32use Fisharebest\Webtrees\I18N;
33use Fisharebest\Webtrees\Individual;
34use Fisharebest\Webtrees\Menu;
35use Fisharebest\Webtrees\Services\ModuleService;
36use Fisharebest\Webtrees\Tree;
37use Fisharebest\Webtrees\Webtrees;
38use InvalidArgumentException;
39use Psr\Http\Message\ServerRequestInterface;
40
41use function app;
42use function route;
43
44/**
45 * Trait ModuleThemeTrait - default implementation of ModuleThemeInterface
46 */
47trait ModuleThemeTrait
48{
49    /**
50     * @return string
51     */
52    abstract public function name(): string;
53
54    /**
55     * @return string
56     */
57    abstract public function title(): string;
58
59    /**
60     * A sentence describing what this module does.
61     *
62     * @return string
63     */
64    public function description(): string
65    {
66        return I18N::translate('Theme') . ' — ' . $this->title();
67    }
68
69    /**
70     * Display an icon for this fact.
71     *
72     * @TODO use CSS for this
73     *
74     * @param Fact $fact
75     *
76     * @return string
77     */
78    public function icon(Fact $fact): string
79    {
80        $asset = 'public/css/' . $this->name() . '/images/facts/' . $fact->getTag() . '.png';
81        if (file_exists(Webtrees::ROOT_DIR . 'public' . $asset)) {
82            return '<img src="' . e(asset($asset)) . '" title="' . GedcomTag::getLabel($fact->getTag()) . '">';
83        }
84
85        // Spacer image - for alignment - until we move to a sprite.
86        $asset = 'public/css/' . $this->name() . '/images/facts/NULL.png';
87        if (file_exists(Webtrees::ROOT_DIR . 'public' . $asset)) {
88            return '<img src="' . e(asset($asset)) . '">';
89        }
90
91        return '';
92    }
93
94    /**
95     * Generate the facts, for display in charts.
96     *
97     * @param Individual $individual
98     *
99     * @return string
100     */
101    public function individualBoxFacts(Individual $individual): string
102    {
103        $html = '';
104
105        $opt_tags = preg_split('/\W/', $individual->tree()->getPreference('CHART_BOX_TAGS'), 0, PREG_SPLIT_NO_EMPTY);
106        // Show BIRT or equivalent event
107        foreach (Gedcom::BIRTH_EVENTS as $birttag) {
108            if (!in_array($birttag, $opt_tags, true)) {
109                $event = $individual->facts([$birttag])->first();
110                if ($event instanceof Fact) {
111                    $html .= $event->summary();
112                    break;
113                }
114            }
115        }
116        // Show optional events (before death)
117        foreach ($opt_tags as $key => $tag) {
118            if (!in_array($tag, Gedcom::DEATH_EVENTS, true)) {
119                $event = $individual->facts([$tag])->first();
120                if ($event instanceof Fact) {
121                    $html .= $event->summary();
122                    unset($opt_tags[$key]);
123                }
124            }
125        }
126        // Show DEAT or equivalent event
127        foreach (Gedcom::DEATH_EVENTS as $deattag) {
128            $event = $individual->facts([$deattag])->first();
129            if ($event instanceof Fact) {
130                $html .= $event->summary();
131                if (in_array($deattag, $opt_tags, true)) {
132                    unset($opt_tags[array_search($deattag, $opt_tags, true)]);
133                }
134                break;
135            }
136        }
137        // Show remaining optional events (after death)
138        foreach ($opt_tags as $tag) {
139            $event = $individual->facts([$tag])->first();
140            if ($event instanceof Fact) {
141                $html .= $event->summary();
142            }
143        }
144
145        return $html;
146    }
147
148    /**
149     * Links, to show in chart boxes;
150     *
151     * @param Individual $individual
152     *
153     * @return Menu[]
154     */
155    public function individualBoxMenu(Individual $individual): array
156    {
157        $menus = array_merge(
158            $this->individualBoxMenuCharts($individual),
159            $this->individualBoxMenuFamilyLinks($individual)
160        );
161
162        return $menus;
163    }
164
165    /**
166     * Chart links, to show in chart boxes;
167     *
168     * @param Individual $individual
169     *
170     * @return Menu[]
171     */
172    public function individualBoxMenuCharts(Individual $individual): array
173    {
174        $menus = [];
175        foreach (app(ModuleService::class)->findByComponent(ModuleChartInterface::class, $individual->tree(), Auth::user()) as $chart) {
176            $menu = $chart->chartBoxMenu($individual);
177            if ($menu) {
178                $menus[] = $menu;
179            }
180        }
181
182        usort($menus, static function (Menu $x, Menu $y): int {
183            return I18N::strcasecmp($x->getLabel(), $y->getLabel());
184        });
185
186        return $menus;
187    }
188
189    /**
190     * Family links, to show in chart boxes.
191     *
192     * @param Individual $individual
193     *
194     * @return Menu[]
195     */
196    public function individualBoxMenuFamilyLinks(Individual $individual): array
197    {
198        $menus = [];
199
200        foreach ($individual->spouseFamilies() as $family) {
201            $menus[] = new Menu('<strong>' . I18N::translate('Family with spouse') . '</strong>', $family->url());
202            $spouse  = $family->spouse($individual);
203            if ($spouse && $spouse->canShowName()) {
204                $menus[] = new Menu($spouse->fullName(), $spouse->url());
205            }
206            foreach ($family->children() as $child) {
207                if ($child->canShowName()) {
208                    $menus[] = new Menu($child->fullName(), $child->url());
209                }
210            }
211        }
212
213        return $menus;
214    }
215
216    /**
217     * Generate a menu item to change the blocks on the current tree/user page.
218     *
219     * @param Tree $tree
220     *
221     * @return Menu|null
222     */
223    public function menuChangeBlocks(Tree $tree): ?Menu
224    {
225        /** @var ServerRequestInterface $request */
226        $request = app(ServerRequestInterface::class);
227
228        $route = $request->getAttribute('route');
229
230        if (Auth::check() && $route === 'user-page') {
231            return new Menu(I18N::translate('Customize this page'), route('user-page-edit', ['tree' => $tree->name()]), 'menu-change-blocks');
232        }
233
234        if (Auth::isManager($tree) && $route === 'tree-page') {
235            return new Menu(I18N::translate('Customize this page'), route('tree-page-edit', ['tree' => $tree->name()]), 'menu-change-blocks');
236        }
237
238        return null;
239    }
240
241    /**
242     * Generate a menu item for the control panel.
243     *
244     * @param Tree $tree
245     *
246     * @return Menu|null
247     */
248    public function menuControlPanel(Tree $tree): ?Menu
249    {
250        if (Auth::isAdmin()) {
251            return new Menu(I18N::translate('Control panel'), route(ControlPanel::class), 'menu-admin');
252        }
253
254        if (Auth::isManager($tree)) {
255            return new Menu(I18N::translate('Control panel'), route('manage-trees'), 'menu-admin');
256        }
257
258        return null;
259    }
260
261    /**
262     * A menu to show a list of available languages.
263     *
264     * @return Menu|null
265     */
266    public function menuLanguages(): ?Menu
267    {
268        $menu = new Menu(I18N::translate('Language'), '#', 'menu-language');
269
270        foreach (I18N::activeLocales() as $locale) {
271            $language_tag = $locale->languageTag();
272            $class        = 'menu-language-' . $language_tag . (WT_LOCALE === $language_tag ? ' active' : '');
273            $menu->addSubmenu(new Menu($locale->endonym(), '#', $class, [
274                'data-post-url' => route(SelectLanguage::class, ['language' => $language_tag]),
275            ]));
276        }
277
278        if (count($menu->getSubmenus()) > 1) {
279            return $menu;
280        }
281
282        return null;
283    }
284
285    /**
286     * A login menu option (or null if we are already logged in).
287     *
288     * @return Menu|null
289     */
290    public function menuLogin(): ?Menu
291    {
292        if (Auth::check()) {
293            return null;
294        }
295
296        $request = app(ServerRequestInterface::class);
297
298        // Return to this page after login...
299        $redirect = (string) $request->getUri();
300
301        // ...but switch from the tree-page to the user-page
302        if ($request->getAttribute('route') === 'tree-page') {
303            $tree = $request->getAttribute('tree');
304            assert($tree instanceof Tree, new InvalidArgumentException());
305            $redirect  = route('user-page', ['tree' => $tree->name()]);
306        }
307
308        // Stay on the same tree page
309        $tree = $request->getAttribute('tree');
310        $url  = route(LoginPage::class, ['tree' => $tree instanceof Tree ? $tree->name() : null, 'url' => $redirect]);
311
312        return new Menu(I18N::translate('Sign in'), $url, 'menu-login', ['rel' => 'nofollow']);
313    }
314
315    /**
316     * A logout menu option (or null if we are already logged out).
317     *
318     * @return Menu|null
319     */
320    public function menuLogout(): ?Menu
321    {
322        if (Auth::check()) {
323            $parameters = [
324                'data-post-url'   => route(Logout::class),
325                'data-reload-url' => route(HomePage::class),
326            ];
327
328            return new Menu(I18N::translate('Sign out'), '#', 'menu-logout', $parameters);
329        }
330
331        return null;
332    }
333
334    /**
335     * A link to allow users to edit their account settings.
336     *
337     * @return Menu|null
338     */
339    public function menuMyAccount(): ?Menu
340    {
341        if (Auth::check()) {
342            return new Menu(I18N::translate('My account'), route('my-account'));
343        }
344
345        return null;
346    }
347
348    /**
349     * A link to the user's individual record (individual.php).
350     *
351     * @param Tree $tree
352     *
353     * @return Menu|null
354     */
355    public function menuMyIndividualRecord(Tree $tree): ?Menu
356    {
357        $record = Individual::getInstance($tree->getUserPreference(Auth::user(), 'gedcomid'), $tree);
358
359        if ($record) {
360            return new Menu(I18N::translate('My individual record'), $record->url(), 'menu-myrecord');
361        }
362
363        return null;
364    }
365
366    /**
367     * A link to the user's personal home page.
368     *
369     * @param Tree $tree
370     *
371     * @return Menu
372     */
373    public function menuMyPage(Tree $tree): Menu
374    {
375        return new Menu(I18N::translate('My page'), route('user-page', ['tree' => $tree->name()]), 'menu-mypage');
376    }
377
378    /**
379     * A menu for the user's personal pages.
380     *
381     * @param Tree|null $tree
382     *
383     * @return Menu|null
384     */
385    public function menuMyPages(?Tree $tree): ?Menu
386    {
387        if ($tree instanceof Tree && Auth::id()) {
388            return new Menu(I18N::translate('My pages'), '#', 'menu-mymenu', [], array_filter([
389                $this->menuMyPage($tree),
390                $this->menuMyIndividualRecord($tree),
391                $this->menuMyPedigree($tree),
392                $this->menuMyAccount(),
393                $this->menuControlPanel($tree),
394                $this->menuChangeBlocks($tree),
395            ]));
396        }
397
398        return null;
399    }
400
401    /**
402     * A link to the user's individual record.
403     *
404     * @param Tree $tree
405     *
406     * @return Menu|null
407     */
408    public function menuMyPedigree(Tree $tree): ?Menu
409    {
410        $gedcomid = $tree->getUserPreference(Auth::user(), 'gedcomid');
411
412        $pedigree_chart = app(ModuleService::class)->findByComponent(ModuleChartInterface::class, $tree, Auth::user())
413            ->filter(static function (ModuleInterface $module): bool {
414                return $module instanceof PedigreeChartModule;
415            });
416
417        if ($gedcomid !== '' && $pedigree_chart instanceof PedigreeChartModule) {
418            return new Menu(
419                I18N::translate('My pedigree'),
420                route('pedigree', [
421                    'xref' => $gedcomid,
422                    'tree'  => $tree->name(),
423                ]),
424                'menu-mypedigree'
425            );
426        }
427
428        return null;
429    }
430
431    /**
432     * Create a pending changes menu.
433     *
434     * @param Tree|null $tree
435     *
436     * @return Menu|null
437     */
438    public function menuPendingChanges(?Tree $tree): ?Menu
439    {
440        if ($tree instanceof Tree && $tree->hasPendingEdit() && Auth::isModerator($tree)) {
441            $url = route('show-pending', [
442                'tree' => $tree->name(),
443                'url' => (string) app(ServerRequestInterface::class)->getUri(),
444            ]);
445
446            return new Menu(I18N::translate('Pending changes'), $url, 'menu-pending');
447        }
448
449        return null;
450    }
451
452    /**
453     * Themes menu.
454     *
455     * @return Menu|null
456     */
457    public function menuThemes(): ?Menu
458    {
459        $themes = app(ModuleService::class)->findByInterface(ModuleThemeInterface::class, false, true);
460
461        $current_theme = app(ModuleThemeInterface::class);
462
463        if ($themes->count() > 1) {
464            $submenus = $themes->map(static function (ModuleThemeInterface $theme) use ($current_theme): Menu {
465                $active     = $theme->name() === $current_theme->name();
466                $class      = 'menu-theme-' . $theme->name() . ($active ? ' active' : '');
467
468                return new Menu($theme->title(), '#', $class, [
469                    'data-post-url' => route(SelectTheme::class, ['theme' => $theme->name()]),
470                ]);
471            });
472
473            return new Menu(I18N::translate('Theme'), '#', 'menu-theme', [], $submenus->all());
474        }
475
476        return null;
477    }
478
479    /**
480     * Misecellaneous dimensions, fonts, styles, etc.
481     *
482     * @param string $parameter_name
483     *
484     * @return string|int|float
485     */
486    public function parameter($parameter_name)
487    {
488        return '';
489    }
490
491    /**
492     * Generate a list of items for the main menu.
493     *
494     * @param Tree|null $tree
495     *
496     * @return Menu[]
497     */
498    public function genealogyMenu(?Tree $tree): array
499    {
500        if ($tree === null) {
501            return [];
502        }
503
504        return app(ModuleService::class)->findByComponent(ModuleMenuInterface::class, $tree, Auth::user())
505            ->map(static function (ModuleMenuInterface $menu) use ($tree): ?Menu {
506                return $menu->getMenu($tree);
507            })
508            ->filter()
509            ->all();
510    }
511
512    /**
513     * Create the genealogy menu.
514     *
515     * @param Menu[] $menus
516     *
517     * @return string
518     */
519    public function genealogyMenuContent(array $menus): string
520    {
521        return implode('', array_map(static function (Menu $menu): string {
522            return $menu->bootstrap4();
523        }, $menus));
524    }
525
526    /**
527     * Generate a list of items for the user menu.
528     *
529     * @param Tree|null $tree
530     *
531     * @return Menu[]
532     */
533    public function userMenu(?Tree $tree): array
534    {
535        return array_filter([
536            $this->menuPendingChanges($tree),
537            $this->menuMyPages($tree),
538            $this->menuThemes(),
539            $this->menuLanguages(),
540            $this->menuLogin(),
541            $this->menuLogout(),
542        ]);
543    }
544
545    /**
546     * A list of CSS files to include for this page.
547     *
548     * @return string[]
549     */
550    public function stylesheets(): array
551    {
552        return [];
553    }
554}
555