xref: /webtrees/app/Module/ColorsTheme.php (revision d11be7027e34e3121be11cc025421873364403f9)
1ade503dfSGreg Roach<?php
23976b470SGreg Roach
3ade503dfSGreg Roach/**
4ade503dfSGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 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;
23ade503dfSGreg Roachuse Fisharebest\Webtrees\I18N;
24ade503dfSGreg Roachuse Fisharebest\Webtrees\Menu;
25ade503dfSGreg Roachuse Fisharebest\Webtrees\Session;
26ade503dfSGreg Roachuse Fisharebest\Webtrees\Site;
27ade503dfSGreg Roachuse Fisharebest\Webtrees\Tree;
28b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
297619f373SGreg Roachuse Psr\Http\Message\ResponseInterface;
306ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
313976b470SGreg Roach
327619f373SGreg Roachuse function asset;
33d8809d62SGreg Roachuse function is_string;
347619f373SGreg Roachuse function response;
351b40e7bcSGreg Roachuse function uasort;
36ade503dfSGreg Roach
37ade503dfSGreg Roach/**
38ade503dfSGreg Roach * The colors theme.
39ade503dfSGreg Roach */
40ade503dfSGreg Roachclass ColorsTheme extends CloudsTheme
41ade503dfSGreg Roach{
427619f373SGreg Roach    // If no valid palette has been selected, use this one.
437619f373SGreg Roach    private const DEFAULT_PALETTE = 'ash';
447619f373SGreg Roach
45ade503dfSGreg Roach    /**
460cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
47ade503dfSGreg Roach     *
48ade503dfSGreg Roach     * @return string
49ade503dfSGreg Roach     */
50ade503dfSGreg Roach    public function title(): string
51ade503dfSGreg Roach    {
52ade503dfSGreg Roach        /* I18N: Name of a theme. */
53ade503dfSGreg Roach        return I18N::translate('colors');
54ade503dfSGreg Roach    }
55ade503dfSGreg Roach
56ade503dfSGreg Roach    /**
57ade503dfSGreg Roach     * Generate a list of items for the user menu.
58ade503dfSGreg Roach     *
590c8c69d4SGreg Roach     * @param Tree|null $tree
600c8c69d4SGreg Roach     *
6109482a55SGreg Roach     * @return array<Menu>
62ade503dfSGreg Roach     */
630c8c69d4SGreg Roach    public function userMenu(?Tree $tree): array
64ade503dfSGreg Roach    {
65ade503dfSGreg Roach        return array_filter([
660c8c69d4SGreg Roach            $this->menuPendingChanges($tree),
670c8c69d4SGreg Roach            $this->menuMyPages($tree),
68ade503dfSGreg Roach            $this->menuThemes(),
69ade503dfSGreg Roach            $this->menuPalette(),
70ade503dfSGreg Roach            $this->menuLanguages(),
71ade503dfSGreg Roach            $this->menuLogin(),
72ade503dfSGreg Roach            $this->menuLogout(),
73ade503dfSGreg Roach        ]);
74ade503dfSGreg Roach    }
75ade503dfSGreg Roach
76ade503dfSGreg Roach    /**
777619f373SGreg Roach     * Switch to a new palette
787619f373SGreg Roach     *
797619f373SGreg Roach     * @param ServerRequestInterface $request
807619f373SGreg Roach     *
817619f373SGreg Roach     * @return ResponseInterface
827619f373SGreg Roach     */
837619f373SGreg Roach    public function postPaletteAction(ServerRequestInterface $request): ResponseInterface
847619f373SGreg Roach    {
85b55cbc6bSGreg Roach        $user    = Validator::attributes($request)->user();
86748dbe15SGreg Roach        $palette = Validator::queryParams($request)->isInArrayKeys($this->palettes())->string('palette');
877619f373SGreg Roach
887619f373SGreg Roach        $user->setPreference('themecolor', $palette);
897619f373SGreg Roach
907619f373SGreg Roach        // If we are the admin, then use our selection as the site default.
917619f373SGreg Roach        if (Auth::isAdmin($user)) {
927619f373SGreg Roach            Site::setPreference('DEFAULT_COLOR_PALETTE', $palette);
937619f373SGreg Roach        }
947619f373SGreg Roach
957619f373SGreg Roach        Session::put('palette', $palette);
967619f373SGreg Roach
977619f373SGreg Roach        return response();
987619f373SGreg Roach    }
997619f373SGreg Roach
1007619f373SGreg Roach    /**
1017619f373SGreg Roach     * A list of CSS files to include for this page.
1027619f373SGreg Roach     *
10324f2a3afSGreg Roach     * @return array<string>
1047619f373SGreg Roach     */
1057619f373SGreg Roach    public function stylesheets(): array
1067619f373SGreg Roach    {
1077619f373SGreg Roach        return [
1087619f373SGreg Roach            asset('css/colors.min.css'),
1097619f373SGreg Roach            asset('css/colors/' . $this->palette() . '.min.css'),
1107619f373SGreg Roach        ];
1117619f373SGreg Roach    }
1127619f373SGreg Roach
1137619f373SGreg Roach    /**
114ade503dfSGreg Roach     * Create a menu of palette options
115ade503dfSGreg Roach     *
11677a20107SGreg Roach     * @return Menu
117ade503dfSGreg Roach     */
1187619f373SGreg Roach    protected function menuPalette(): Menu
119ade503dfSGreg Roach    {
120ade503dfSGreg Roach        /* I18N: A colour scheme */
121ade503dfSGreg Roach        $menu = new Menu(I18N::translate('Palette'), '#', 'menu-color');
122ade503dfSGreg Roach
1237619f373SGreg Roach        $palette = $this->palette();
124ade503dfSGreg Roach
1257619f373SGreg Roach        foreach ($this->palettes() as $palette_id => $palette_name) {
1267619f373SGreg Roach            $url = route('module', ['module' => $this->name(), 'action' => 'Palette', 'palette' => $palette_id]);
1277619f373SGreg Roach
1287619f373SGreg Roach            $submenu = new Menu(
129ade503dfSGreg Roach                $palette_name,
130ade503dfSGreg Roach                '#',
1317619f373SGreg Roach                'menu-color-' . $palette_id . ($palette === $palette_id ? ' active' : ''),
132ade503dfSGreg Roach                [
133d4786c66SGreg Roach                    'data-wt-post-url' => $url,
134ade503dfSGreg Roach                ]
1357619f373SGreg Roach            );
1367619f373SGreg Roach
1377619f373SGreg Roach            $menu->addSubmenu($submenu);
138ade503dfSGreg Roach        }
139ade503dfSGreg Roach
140ade503dfSGreg Roach        return $menu;
141ade503dfSGreg Roach    }
142ade503dfSGreg Roach
143ade503dfSGreg Roach    /**
14424f2a3afSGreg Roach     * @return array<string>
1451b40e7bcSGreg Roach     */
146f6f7bcffSGreg Roach    private function palettes(): array
147f6f7bcffSGreg Roach    {
1481b40e7bcSGreg Roach        $palettes = [
1491b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1501b40e7bcSGreg Roach            'aquamarine'       => I18N::translate('Aqua Marine'),
1511b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1521b40e7bcSGreg Roach            'ash'              => I18N::translate('Ash'),
1531b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1541b40e7bcSGreg Roach            'belgianchocolate' => I18N::translate('Belgian Chocolate'),
1551b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1561b40e7bcSGreg Roach            'bluelagoon'       => I18N::translate('Blue Lagoon'),
1571b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1581b40e7bcSGreg Roach            'bluemarine'       => I18N::translate('Blue Marine'),
1591b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1601b40e7bcSGreg Roach            'coffeeandcream'   => I18N::translate('Coffee and Cream'),
1611b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1621b40e7bcSGreg Roach            'coldday'          => I18N::translate('Cold Day'),
1631b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1641b40e7bcSGreg Roach            'greenbeam'        => I18N::translate('Green Beam'),
1651b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1661b40e7bcSGreg Roach            'mediterranio'     => I18N::translate('Mediterranio'),
1671b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1681b40e7bcSGreg Roach            'mercury'          => I18N::translate('Mercury'),
1691b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1701b40e7bcSGreg Roach            'nocturnal'        => I18N::translate('Nocturnal'),
1711b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1721b40e7bcSGreg Roach            'olivia'           => I18N::translate('Olivia'),
1731b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1741b40e7bcSGreg Roach            'pinkplastic'      => I18N::translate('Pink Plastic'),
1751b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1761b40e7bcSGreg Roach            'sage'             => I18N::translate('Sage'),
1771b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1781b40e7bcSGreg Roach            'shinytomato'      => I18N::translate('Shiny Tomato'),
1791b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1801b40e7bcSGreg Roach            'tealtop'          => I18N::translate('Teal Top'),
1811b40e7bcSGreg Roach        ];
1821b40e7bcSGreg Roach
18337646143SGreg Roach        uasort($palettes, I18N::comparator());
1841b40e7bcSGreg Roach
1851b40e7bcSGreg Roach        return $palettes;
1861b40e7bcSGreg Roach    }
1876ccdf4f0SGreg Roach
1886ccdf4f0SGreg Roach    /**
1896ccdf4f0SGreg Roach     * @return string
1906ccdf4f0SGreg Roach     */
1916ccdf4f0SGreg Roach    private function palette(): string
1926ccdf4f0SGreg Roach    {
1936ccdf4f0SGreg Roach        // If we are logged in, use our preference
194a9866bf2SGreg Roach        $palette = Auth::user()->getPreference('themecolor');
1956ccdf4f0SGreg Roach
1967619f373SGreg Roach        // If not logged in or no preference, use one we selected earlier in the session.
1977619f373SGreg Roach        if ($palette === '') {
198d8809d62SGreg Roach            $palette = Session::get('palette');
199d8809d62SGreg Roach            $palette = is_string($palette) ? $palette : '';
2006ccdf4f0SGreg Roach        }
2016ccdf4f0SGreg Roach
2026ccdf4f0SGreg Roach        // We haven't selected one this session? Use the site default
2037619f373SGreg Roach        if ($palette === '') {
2048a07c98eSGreg Roach            $palette = Site::getPreference('DEFAULT_COLOR_PALETTE');
2058a07c98eSGreg Roach        }
2068a07c98eSGreg Roach
2078a07c98eSGreg Roach        if ($palette === '') {
2088a07c98eSGreg Roach            $palette = self::DEFAULT_PALETTE;
2096ccdf4f0SGreg Roach        }
2106ccdf4f0SGreg Roach
2116ccdf4f0SGreg Roach        return $palette;
2126ccdf4f0SGreg Roach    }
213ade503dfSGreg Roach}
214