xref: /webtrees/app/Module/ColorsTheme.php (revision d8809d62a7e3fb7260ce3624ee766b7cb2dd1a21)
1ade503dfSGreg Roach<?php
23976b470SGreg Roach
3ade503dfSGreg Roach/**
4ade503dfSGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 webtrees development team
6ade503dfSGreg Roach * This program is free software: you can redistribute it and/or modify
7ade503dfSGreg Roach * it under the terms of the GNU General Public License as published by
8ade503dfSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9ade503dfSGreg Roach * (at your option) any later version.
10ade503dfSGreg Roach * This program is distributed in the hope that it will be useful,
11ade503dfSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12ade503dfSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13ade503dfSGreg Roach * GNU General Public License for more details.
14ade503dfSGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16ade503dfSGreg Roach */
17fcfa147eSGreg Roach
18ade503dfSGreg Roachdeclare(strict_types=1);
19ade503dfSGreg Roach
20ade503dfSGreg Roachnamespace Fisharebest\Webtrees\Module;
21ade503dfSGreg Roach
22ade503dfSGreg Roachuse Fisharebest\Webtrees\Auth;
237619f373SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
24ade503dfSGreg Roachuse Fisharebest\Webtrees\I18N;
25ade503dfSGreg Roachuse Fisharebest\Webtrees\Menu;
26ade503dfSGreg Roachuse Fisharebest\Webtrees\Session;
27ade503dfSGreg Roachuse Fisharebest\Webtrees\Site;
28ade503dfSGreg Roachuse Fisharebest\Webtrees\Tree;
297619f373SGreg Roachuse Psr\Http\Message\ResponseInterface;
306ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
313976b470SGreg Roach
327619f373SGreg Roachuse function assert;
337619f373SGreg Roachuse function asset;
34*d8809d62SGreg Roachuse function is_string;
357619f373SGreg Roachuse function response;
361b40e7bcSGreg Roachuse function uasort;
37ade503dfSGreg Roach
38ade503dfSGreg Roach/**
39ade503dfSGreg Roach * The colors theme.
40ade503dfSGreg Roach */
41ade503dfSGreg Roachclass ColorsTheme extends CloudsTheme
42ade503dfSGreg Roach{
437619f373SGreg Roach    // If no valid palette has been selected, use this one.
447619f373SGreg Roach    private const DEFAULT_PALETTE = 'ash';
457619f373SGreg Roach
46ade503dfSGreg Roach    /**
470cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
48ade503dfSGreg Roach     *
49ade503dfSGreg Roach     * @return string
50ade503dfSGreg Roach     */
51ade503dfSGreg Roach    public function title(): string
52ade503dfSGreg Roach    {
53ade503dfSGreg Roach        /* I18N: Name of a theme. */
54ade503dfSGreg Roach        return I18N::translate('colors');
55ade503dfSGreg Roach    }
56ade503dfSGreg Roach
57ade503dfSGreg Roach    /**
58ade503dfSGreg Roach     * Generate a list of items for the user menu.
59ade503dfSGreg Roach     *
600c8c69d4SGreg Roach     * @param Tree|null $tree
610c8c69d4SGreg Roach     *
6209482a55SGreg Roach     * @return array<Menu>
63ade503dfSGreg Roach     */
640c8c69d4SGreg Roach    public function userMenu(?Tree $tree): array
65ade503dfSGreg Roach    {
66ade503dfSGreg Roach        return array_filter([
670c8c69d4SGreg Roach            $this->menuPendingChanges($tree),
680c8c69d4SGreg Roach            $this->menuMyPages($tree),
69ade503dfSGreg Roach            $this->menuThemes(),
70ade503dfSGreg Roach            $this->menuPalette(),
71ade503dfSGreg Roach            $this->menuLanguages(),
72ade503dfSGreg Roach            $this->menuLogin(),
73ade503dfSGreg Roach            $this->menuLogout(),
74ade503dfSGreg Roach        ]);
75ade503dfSGreg Roach    }
76ade503dfSGreg Roach
77ade503dfSGreg Roach    /**
787619f373SGreg Roach     * Switch to a new palette
797619f373SGreg Roach     *
807619f373SGreg Roach     * @param ServerRequestInterface $request
817619f373SGreg Roach     *
827619f373SGreg Roach     * @return ResponseInterface
837619f373SGreg Roach     */
847619f373SGreg Roach    public function postPaletteAction(ServerRequestInterface $request): ResponseInterface
857619f373SGreg Roach    {
867619f373SGreg Roach        $user = $request->getAttribute('user');
8775964c75SGreg Roach        assert($user instanceof UserInterface);
887619f373SGreg Roach
897619f373SGreg Roach        $palette = $request->getQueryParams()['palette'];
9075964c75SGreg Roach        assert(array_key_exists($palette, $this->palettes()));
917619f373SGreg Roach
927619f373SGreg Roach        $user->setPreference('themecolor', $palette);
937619f373SGreg Roach
947619f373SGreg Roach        // If we are the admin, then use our selection as the site default.
957619f373SGreg Roach        if (Auth::isAdmin($user)) {
967619f373SGreg Roach            Site::setPreference('DEFAULT_COLOR_PALETTE', $palette);
977619f373SGreg Roach        }
987619f373SGreg Roach
997619f373SGreg Roach        Session::put('palette', $palette);
1007619f373SGreg Roach
1017619f373SGreg Roach        return response();
1027619f373SGreg Roach    }
1037619f373SGreg Roach
1047619f373SGreg Roach    /**
1057619f373SGreg Roach     * A list of CSS files to include for this page.
1067619f373SGreg Roach     *
10724f2a3afSGreg Roach     * @return array<string>
1087619f373SGreg Roach     */
1097619f373SGreg Roach    public function stylesheets(): array
1107619f373SGreg Roach    {
1117619f373SGreg Roach        return [
1127619f373SGreg Roach            asset('css/colors.min.css'),
1137619f373SGreg Roach            asset('css/colors/' . $this->palette() . '.min.css'),
1147619f373SGreg Roach        ];
1157619f373SGreg Roach    }
1167619f373SGreg Roach
1177619f373SGreg Roach    /**
118ade503dfSGreg Roach     * Create a menu of palette options
119ade503dfSGreg Roach     *
12077a20107SGreg Roach     * @return Menu
121ade503dfSGreg Roach     */
1227619f373SGreg Roach    protected function menuPalette(): Menu
123ade503dfSGreg Roach    {
124ade503dfSGreg Roach        /* I18N: A colour scheme */
125ade503dfSGreg Roach        $menu = new Menu(I18N::translate('Palette'), '#', 'menu-color');
126ade503dfSGreg Roach
1277619f373SGreg Roach        $palette = $this->palette();
128ade503dfSGreg Roach
1297619f373SGreg Roach        foreach ($this->palettes() as $palette_id => $palette_name) {
1307619f373SGreg Roach            $url = route('module', ['module' => $this->name(), 'action' => 'Palette', 'palette' => $palette_id]);
1317619f373SGreg Roach
1327619f373SGreg Roach            $submenu = new Menu(
133ade503dfSGreg Roach                $palette_name,
134ade503dfSGreg Roach                '#',
1357619f373SGreg Roach                'menu-color-' . $palette_id . ($palette === $palette_id ? ' active' : ''),
136ade503dfSGreg Roach                [
137d4786c66SGreg Roach                    'data-wt-post-url' => $url,
138ade503dfSGreg Roach                ]
1397619f373SGreg Roach            );
1407619f373SGreg Roach
1417619f373SGreg Roach            $menu->addSubmenu($submenu);
142ade503dfSGreg Roach        }
143ade503dfSGreg Roach
144ade503dfSGreg Roach        return $menu;
145ade503dfSGreg Roach    }
146ade503dfSGreg Roach
147ade503dfSGreg Roach    /**
14824f2a3afSGreg Roach     * @return array<string>
1491b40e7bcSGreg Roach     */
150f6f7bcffSGreg Roach    private function palettes(): array
151f6f7bcffSGreg Roach    {
1521b40e7bcSGreg Roach        $palettes = [
1531b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1541b40e7bcSGreg Roach            'aquamarine'       => I18N::translate('Aqua Marine'),
1551b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1561b40e7bcSGreg Roach            'ash'              => I18N::translate('Ash'),
1571b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1581b40e7bcSGreg Roach            'belgianchocolate' => I18N::translate('Belgian Chocolate'),
1591b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1601b40e7bcSGreg Roach            'bluelagoon'       => I18N::translate('Blue Lagoon'),
1611b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1621b40e7bcSGreg Roach            'bluemarine'       => I18N::translate('Blue Marine'),
1631b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1641b40e7bcSGreg Roach            'coffeeandcream'   => I18N::translate('Coffee and Cream'),
1651b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1661b40e7bcSGreg Roach            'coldday'          => I18N::translate('Cold Day'),
1671b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1681b40e7bcSGreg Roach            'greenbeam'        => I18N::translate('Green Beam'),
1691b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1701b40e7bcSGreg Roach            'mediterranio'     => I18N::translate('Mediterranio'),
1711b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1721b40e7bcSGreg Roach            'mercury'          => I18N::translate('Mercury'),
1731b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1741b40e7bcSGreg Roach            'nocturnal'        => I18N::translate('Nocturnal'),
1751b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1761b40e7bcSGreg Roach            'olivia'           => I18N::translate('Olivia'),
1771b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1781b40e7bcSGreg Roach            'pinkplastic'      => I18N::translate('Pink Plastic'),
1791b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1801b40e7bcSGreg Roach            'sage'             => I18N::translate('Sage'),
1811b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1821b40e7bcSGreg Roach            'shinytomato'      => I18N::translate('Shiny Tomato'),
1831b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1841b40e7bcSGreg Roach            'tealtop'          => I18N::translate('Teal Top'),
1851b40e7bcSGreg Roach        ];
1861b40e7bcSGreg Roach
18737646143SGreg Roach        uasort($palettes, I18N::comparator());
1881b40e7bcSGreg Roach
1891b40e7bcSGreg Roach        return $palettes;
1901b40e7bcSGreg Roach    }
1916ccdf4f0SGreg Roach
1926ccdf4f0SGreg Roach    /**
1936ccdf4f0SGreg Roach     * @return string
1946ccdf4f0SGreg Roach     */
1956ccdf4f0SGreg Roach    private function palette(): string
1966ccdf4f0SGreg Roach    {
1976ccdf4f0SGreg Roach        // If we are logged in, use our preference
198a9866bf2SGreg Roach        $palette = Auth::user()->getPreference('themecolor');
1996ccdf4f0SGreg Roach
2007619f373SGreg Roach        // If not logged in or no preference, use one we selected earlier in the session.
2017619f373SGreg Roach        if ($palette === '') {
202*d8809d62SGreg Roach            $palette = Session::get('palette');
203*d8809d62SGreg Roach            $palette = is_string($palette) ? $palette : '';
2046ccdf4f0SGreg Roach        }
2056ccdf4f0SGreg Roach
2066ccdf4f0SGreg Roach        // We haven't selected one this session? Use the site default
2077619f373SGreg Roach        if ($palette === '') {
2088a07c98eSGreg Roach            $palette = Site::getPreference('DEFAULT_COLOR_PALETTE');
2098a07c98eSGreg Roach        }
2108a07c98eSGreg Roach
2118a07c98eSGreg Roach        if ($palette === '') {
2128a07c98eSGreg Roach            $palette = self::DEFAULT_PALETTE;
2136ccdf4f0SGreg Roach        }
2146ccdf4f0SGreg Roach
2156ccdf4f0SGreg Roach        return $palette;
2166ccdf4f0SGreg Roach    }
217ade503dfSGreg Roach}
218