xref: /webtrees/app/Module/ColorsTheme.php (revision 6ccdf4f0fd1b65a5d54259c969912382ce49629d)
1ade503dfSGreg Roach<?php
2ade503dfSGreg Roach/**
3ade503dfSGreg Roach * webtrees: online genealogy
4ade503dfSGreg Roach * Copyright (C) 2019 webtrees development team
5ade503dfSGreg Roach * This program is free software: you can redistribute it and/or modify
6ade503dfSGreg Roach * it under the terms of the GNU General Public License as published by
7ade503dfSGreg Roach * the Free Software Foundation, either version 3 of the License, or
8ade503dfSGreg Roach * (at your option) any later version.
9ade503dfSGreg Roach * This program is distributed in the hope that it will be useful,
10ade503dfSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
11ade503dfSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12ade503dfSGreg Roach * GNU General Public License for more details.
13ade503dfSGreg Roach * You should have received a copy of the GNU General Public License
14ade503dfSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
15ade503dfSGreg Roach */
16ade503dfSGreg Roachdeclare(strict_types=1);
17ade503dfSGreg Roach
18ade503dfSGreg Roachnamespace Fisharebest\Webtrees\Module;
19ade503dfSGreg Roach
20ade503dfSGreg Roachuse Fisharebest\Webtrees\Auth;
21ade503dfSGreg Roachuse Fisharebest\Webtrees\I18N;
22ade503dfSGreg Roachuse Fisharebest\Webtrees\Menu;
23ade503dfSGreg Roachuse Fisharebest\Webtrees\Session;
24ade503dfSGreg Roachuse Fisharebest\Webtrees\Site;
25ade503dfSGreg Roachuse Fisharebest\Webtrees\Tree;
26*6ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
27*6ccdf4f0SGreg Roachuse function app;
281b40e7bcSGreg Roachuse function uasort;
29ade503dfSGreg Roach
30ade503dfSGreg Roach/**
31ade503dfSGreg Roach * The colors theme.
32ade503dfSGreg Roach */
33ade503dfSGreg Roachclass ColorsTheme extends CloudsTheme
34ade503dfSGreg Roach{
35ade503dfSGreg Roach    /**
360cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
37ade503dfSGreg Roach     *
38ade503dfSGreg Roach     * @return string
39ade503dfSGreg Roach     */
40ade503dfSGreg Roach    public function title(): string
41ade503dfSGreg Roach    {
42ade503dfSGreg Roach        /* I18N: Name of a theme. */
43ade503dfSGreg Roach        return I18N::translate('colors');
44ade503dfSGreg Roach    }
45ade503dfSGreg Roach
46ade503dfSGreg Roach    /**
47ade503dfSGreg Roach     * Generate a list of items for the user menu.
48ade503dfSGreg Roach     *
490c8c69d4SGreg Roach     * @param Tree|null $tree
500c8c69d4SGreg Roach     *
51ade503dfSGreg Roach     * @return Menu[]
52ade503dfSGreg Roach     */
530c8c69d4SGreg Roach    public function userMenu(?Tree $tree): array
54ade503dfSGreg Roach    {
55ade503dfSGreg Roach        return array_filter([
560c8c69d4SGreg Roach            $this->menuPendingChanges($tree),
570c8c69d4SGreg Roach            $this->menuMyPages($tree),
58ade503dfSGreg Roach            $this->menuThemes(),
59ade503dfSGreg Roach            $this->menuPalette(),
60ade503dfSGreg Roach            $this->menuLanguages(),
61ade503dfSGreg Roach            $this->menuLogin(),
62ade503dfSGreg Roach            $this->menuLogout(),
63ade503dfSGreg Roach        ]);
64ade503dfSGreg Roach    }
65ade503dfSGreg Roach
66ade503dfSGreg Roach    /**
67ade503dfSGreg Roach     * Create a menu of palette options
68ade503dfSGreg Roach     *
6977a20107SGreg Roach     * @return Menu
70ade503dfSGreg Roach     */
7177a20107SGreg Roach    public function menuPalette(): Menu
72ade503dfSGreg Roach    {
73*6ccdf4f0SGreg Roach        $request = app(ServerRequestInterface::class);
74e6bcfa02SGreg Roach
75ade503dfSGreg Roach        /* I18N: A colour scheme */
76ade503dfSGreg Roach        $menu = new Menu(I18N::translate('Palette'), '#', 'menu-color');
77ade503dfSGreg Roach
781b40e7bcSGreg Roach        foreach ($this->palettes() as $palette_id => $palette_name) {
79*6ccdf4f0SGreg Roach            $url = $request->getUri();
80ade503dfSGreg Roach            $url = preg_replace('/&themecolor=[a-z]+/', '', $url);
81ade503dfSGreg Roach            $url .= '&themecolor=' . $palette_id;
82ade503dfSGreg Roach
83ade503dfSGreg Roach            $menu->addSubmenu(new Menu(
84ade503dfSGreg Roach                $palette_name,
85ade503dfSGreg Roach                '#',
861b40e7bcSGreg Roach                'menu-color-' . $palette_id . ($this->palette() === $palette_id ? ' active' : ''),
87ade503dfSGreg Roach                [
88ade503dfSGreg Roach                    'onclick' => 'document.location=\'' . $url . '\'',
89ade503dfSGreg Roach                ]
90ade503dfSGreg Roach            ));
91ade503dfSGreg Roach        }
92ade503dfSGreg Roach
93ade503dfSGreg Roach        return $menu;
94ade503dfSGreg Roach    }
95ade503dfSGreg Roach
96ade503dfSGreg Roach    /**
971b40e7bcSGreg Roach     * @return string[]
981b40e7bcSGreg Roach     */
99f6f7bcffSGreg Roach    private function palettes(): array
100f6f7bcffSGreg Roach    {
1011b40e7bcSGreg Roach        $palettes = [
1021b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1031b40e7bcSGreg Roach            'aquamarine'       => I18N::translate('Aqua Marine'),
1041b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1051b40e7bcSGreg Roach            'ash'              => I18N::translate('Ash'),
1061b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1071b40e7bcSGreg Roach            'belgianchocolate' => I18N::translate('Belgian Chocolate'),
1081b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1091b40e7bcSGreg Roach            'bluelagoon'       => I18N::translate('Blue Lagoon'),
1101b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1111b40e7bcSGreg Roach            'bluemarine'       => I18N::translate('Blue Marine'),
1121b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1131b40e7bcSGreg Roach            'coffeeandcream'   => I18N::translate('Coffee and Cream'),
1141b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1151b40e7bcSGreg Roach            'coldday'          => I18N::translate('Cold Day'),
1161b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1171b40e7bcSGreg Roach            'greenbeam'        => I18N::translate('Green Beam'),
1181b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1191b40e7bcSGreg Roach            'mediterranio'     => I18N::translate('Mediterranio'),
1201b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1211b40e7bcSGreg Roach            'mercury'          => I18N::translate('Mercury'),
1221b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1231b40e7bcSGreg Roach            'nocturnal'        => I18N::translate('Nocturnal'),
1241b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1251b40e7bcSGreg Roach            'olivia'           => I18N::translate('Olivia'),
1261b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1271b40e7bcSGreg Roach            'pinkplastic'      => I18N::translate('Pink Plastic'),
1281b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1291b40e7bcSGreg Roach            'sage'             => I18N::translate('Sage'),
1301b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1311b40e7bcSGreg Roach            'shinytomato'      => I18N::translate('Shiny Tomato'),
1321b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1331b40e7bcSGreg Roach            'tealtop'          => I18N::translate('Teal Top'),
1341b40e7bcSGreg Roach        ];
1351b40e7bcSGreg Roach
1361b40e7bcSGreg Roach        uasort($palettes, '\Fisharebest\Webtrees\I18N::strcasecmp');
1371b40e7bcSGreg Roach
1381b40e7bcSGreg Roach        return $palettes;
1391b40e7bcSGreg Roach    }
140*6ccdf4f0SGreg Roach
141*6ccdf4f0SGreg Roach    /**
142*6ccdf4f0SGreg Roach     * @return string
143*6ccdf4f0SGreg Roach     */
144*6ccdf4f0SGreg Roach    private function palette(): string
145*6ccdf4f0SGreg Roach    {
146*6ccdf4f0SGreg Roach        $palettes = $this->palettes();
147*6ccdf4f0SGreg Roach
148*6ccdf4f0SGreg Roach        // If we've selected a new palette, and we are logged in, set this value as a default.
149*6ccdf4f0SGreg Roach        if (isset($_GET['themecolor'])) {
150*6ccdf4f0SGreg Roach            // Request to change color
151*6ccdf4f0SGreg Roach            $palette = $_GET['themecolor'];
152*6ccdf4f0SGreg Roach            Auth::user()->setPreference('themecolor', $palette);
153*6ccdf4f0SGreg Roach            if (Auth::isAdmin()) {
154*6ccdf4f0SGreg Roach                Site::setPreference('DEFAULT_COLOR_PALETTE', $palette);
155*6ccdf4f0SGreg Roach            }
156*6ccdf4f0SGreg Roach            unset($_GET['themecolor']);
157*6ccdf4f0SGreg Roach            // Rember that we have selected a value
158*6ccdf4f0SGreg Roach            Session::put('subColor', $palette);
159*6ccdf4f0SGreg Roach        }
160*6ccdf4f0SGreg Roach
161*6ccdf4f0SGreg Roach        // If we are logged in, use our preference
162*6ccdf4f0SGreg Roach        $palette = Auth::user()->getPreference('themecolor');
163*6ccdf4f0SGreg Roach
164*6ccdf4f0SGreg Roach        // If not logged in or no preference, use one we selected earlier in the session?
165*6ccdf4f0SGreg Roach        if (!$palette) {
166*6ccdf4f0SGreg Roach            $palette = Session::get('subColor');
167*6ccdf4f0SGreg Roach        }
168*6ccdf4f0SGreg Roach
169*6ccdf4f0SGreg Roach        // We haven't selected one this session? Use the site default
170*6ccdf4f0SGreg Roach        if (!$palette) {
171*6ccdf4f0SGreg Roach            $palette = Site::getPreference('DEFAULT_COLOR_PALETTE');
172*6ccdf4f0SGreg Roach        }
173*6ccdf4f0SGreg Roach
174*6ccdf4f0SGreg Roach        // Make sure our selected palette actually exists
175*6ccdf4f0SGreg Roach        if (!array_key_exists($palette, $palettes)) {
176*6ccdf4f0SGreg Roach            $palette = 'ash';
177*6ccdf4f0SGreg Roach        }
178*6ccdf4f0SGreg Roach
179*6ccdf4f0SGreg Roach        return $palette;
180*6ccdf4f0SGreg Roach    }
181*6ccdf4f0SGreg Roach
182*6ccdf4f0SGreg Roach    /**
183*6ccdf4f0SGreg Roach     * A list of CSS files to include for this page.
184*6ccdf4f0SGreg Roach     *
185*6ccdf4f0SGreg Roach     * @return string[]
186*6ccdf4f0SGreg Roach     */
187*6ccdf4f0SGreg Roach    public function stylesheets(): array
188*6ccdf4f0SGreg Roach    {
189*6ccdf4f0SGreg Roach        return [
190*6ccdf4f0SGreg Roach            asset('css/colors.min.css'),
191*6ccdf4f0SGreg Roach            asset('css/colors/' . $this->palette() . '.min.css'),
192*6ccdf4f0SGreg Roach        ];
193*6ccdf4f0SGreg Roach    }
194ade503dfSGreg Roach}
195