xref: /webtrees/app/Module/ThemeSelectModule.php (revision 5bfc689774bb9a6401271c4ed15a6d50652c991b)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 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\I18N;
23use Fisharebest\Webtrees\Menu;
24use Fisharebest\Webtrees\Tree;
25use Illuminate\Support\Str;
26
27use function app;
28use function view;
29
30/**
31 * Class ThemeSelectModule
32 */
33class ThemeSelectModule extends AbstractModule implements ModuleBlockInterface
34{
35    use ModuleBlockTrait;
36
37    /**
38     * How should this module be identified in the control panel, etc.?
39     *
40     * @return string
41     */
42    public function title(): string
43    {
44        /* I18N: Name of a module */
45        return I18N::translate('Theme change');
46    }
47
48    /**
49     * A sentence describing what this module does.
50     *
51     * @return string
52     */
53    public function description(): string
54    {
55        /* I18N: Description of the “Theme change” module */
56        return I18N::translate('An alternative way to select a new theme.');
57    }
58
59    /**
60     * Generate the HTML content of this block.
61     *
62     * @param Tree                 $tree
63     * @param int                  $block_id
64     * @param string               $context
65     * @param array<string,string> $config
66     *
67     * @return string
68     */
69    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
70    {
71        $theme = app(ModuleThemeInterface::class);
72        assert($theme instanceof ModuleThemeInterface);
73
74        $menu = $theme->menuThemes();
75
76        if ($menu instanceof Menu) {
77            $content = '<ul class="nav text-justify" role="menu">' . view('components/menu-item', ['menu' => $menu]) . '</ul>';
78
79            if ($context !== self::CONTEXT_EMBED) {
80                return view('modules/block-template', [
81                    'block'      => Str::kebab($this->name()),
82                    'id'         => $block_id,
83                    'config_url' => '',
84                    'title'      => $this->title(),
85                    'content'    => $content,
86                ]);
87            }
88
89            return $content;
90        }
91
92        return '';
93    }
94
95    /**
96     * Should this block load asynchronously using AJAX?
97     *
98     * Simple blocks are faster in-line, more complex ones can be loaded later.
99     *
100     * @return bool
101     */
102    public function loadAjax(): bool
103    {
104        return false;
105    }
106
107    /**
108     * Can this block be shown on the user’s home page?
109     *
110     * @return bool
111     */
112    public function isUserBlock(): bool
113    {
114        return true;
115    }
116
117    /**
118     * Can this block be shown on the tree’s home page?
119     *
120     * @return bool
121     */
122    public function isTreeBlock(): bool
123    {
124        return true;
125    }
126}
127