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