xref: /webtrees/app/Module/ColorsTheme.php (revision 91c514e5e3b4273fec62c3ff012bcd0a10cfaca1)
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\I18N;
24use Fisharebest\Webtrees\Menu;
25use Fisharebest\Webtrees\Session;
26use Fisharebest\Webtrees\Site;
27use Fisharebest\Webtrees\Tree;
28use Psr\Http\Message\ServerRequestInterface;
29
30use function app;
31use function uasort;
32
33/**
34 * The colors theme.
35 */
36class ColorsTheme extends CloudsTheme
37{
38    /**
39     * How should this module be identified in the control panel, etc.?
40     *
41     * @return string
42     */
43    public function title(): string
44    {
45        /* I18N: Name of a theme. */
46        return I18N::translate('colors');
47    }
48
49    /**
50     * Generate a list of items for the user menu.
51     *
52     * @param Tree|null $tree
53     *
54     * @return Menu[]
55     */
56    public function userMenu(?Tree $tree): array
57    {
58        return array_filter([
59            $this->menuPendingChanges($tree),
60            $this->menuMyPages($tree),
61            $this->menuThemes(),
62            $this->menuPalette(),
63            $this->menuLanguages(),
64            $this->menuLogin(),
65            $this->menuLogout(),
66        ]);
67    }
68
69    /**
70     * Create a menu of palette options
71     *
72     * @return Menu
73     */
74    public function menuPalette(): Menu
75    {
76        $request = app(ServerRequestInterface::class);
77
78        /* I18N: A colour scheme */
79        $menu = new Menu(I18N::translate('Palette'), '#', 'menu-color');
80
81        foreach ($this->palettes() as $palette_id => $palette_name) {
82            $url = $request->getAttribute((string) $request->getUri());
83            $url = preg_replace('/&themecolor=[a-z]+/', '', $url);
84            $url .= '&themecolor=' . $palette_id;
85
86            $menu->addSubmenu(new Menu(
87                $palette_name,
88                '#',
89                'menu-color-' . $palette_id . ($this->palette() === $palette_id ? ' active' : ''),
90                [
91                    'onclick' => 'document.location=\'' . $url . '\'',
92                ]
93            ));
94        }
95
96        return $menu;
97    }
98
99    /**
100     * @return string[]
101     */
102    private function palettes(): array
103    {
104        $palettes = [
105            /* I18N: The name of a colour-scheme */
106            'aquamarine'       => I18N::translate('Aqua Marine'),
107            /* I18N: The name of a colour-scheme */
108            'ash'              => I18N::translate('Ash'),
109            /* I18N: The name of a colour-scheme */
110            'belgianchocolate' => I18N::translate('Belgian Chocolate'),
111            /* I18N: The name of a colour-scheme */
112            'bluelagoon'       => I18N::translate('Blue Lagoon'),
113            /* I18N: The name of a colour-scheme */
114            'bluemarine'       => I18N::translate('Blue Marine'),
115            /* I18N: The name of a colour-scheme */
116            'coffeeandcream'   => I18N::translate('Coffee and Cream'),
117            /* I18N: The name of a colour-scheme */
118            'coldday'          => I18N::translate('Cold Day'),
119            /* I18N: The name of a colour-scheme */
120            'greenbeam'        => I18N::translate('Green Beam'),
121            /* I18N: The name of a colour-scheme */
122            'mediterranio'     => I18N::translate('Mediterranio'),
123            /* I18N: The name of a colour-scheme */
124            'mercury'          => I18N::translate('Mercury'),
125            /* I18N: The name of a colour-scheme */
126            'nocturnal'        => I18N::translate('Nocturnal'),
127            /* I18N: The name of a colour-scheme */
128            'olivia'           => I18N::translate('Olivia'),
129            /* I18N: The name of a colour-scheme */
130            'pinkplastic'      => I18N::translate('Pink Plastic'),
131            /* I18N: The name of a colour-scheme */
132            'sage'             => I18N::translate('Sage'),
133            /* I18N: The name of a colour-scheme */
134            'shinytomato'      => I18N::translate('Shiny Tomato'),
135            /* I18N: The name of a colour-scheme */
136            'tealtop'          => I18N::translate('Teal Top'),
137        ];
138
139        uasort($palettes, '\Fisharebest\Webtrees\I18N::strcasecmp');
140
141        return $palettes;
142    }
143
144    /**
145     * @return string
146     */
147    private function palette(): string
148    {
149        $palettes = $this->palettes();
150
151        // If we've selected a new palette, and we are logged in, set this value as a default.
152        if (isset($_GET['themecolor'])) {
153            // Request to change color
154            $palette = $_GET['themecolor'];
155            Auth::user()->setPreference('themecolor', $palette);
156            if (Auth::isAdmin()) {
157                Site::setPreference('DEFAULT_COLOR_PALETTE', $palette);
158            }
159            unset($_GET['themecolor']);
160            // Rember that we have selected a value
161            Session::put('subColor', $palette);
162        }
163
164        // If we are logged in, use our preference
165        $palette = Auth::user()->getPreference('themecolor');
166
167        // If not logged in or no preference, use one we selected earlier in the session?
168        if (!$palette) {
169            $palette = Session::get('subColor');
170        }
171
172        // We haven't selected one this session? Use the site default
173        if (!$palette) {
174            $palette = Site::getPreference('DEFAULT_COLOR_PALETTE');
175        }
176
177        // Make sure our selected palette actually exists
178        if (!array_key_exists($palette, $palettes)) {
179            $palette = 'ash';
180        }
181
182        return $palette;
183    }
184
185    /**
186     * A list of CSS files to include for this page.
187     *
188     * @return string[]
189     */
190    public function stylesheets(): array
191    {
192        return [
193            asset('css/colors.min.css'),
194            asset('css/colors/' . $this->palette() . '.min.css'),
195        ];
196    }
197}
198