xref: /webtrees/app/Module/ColorsTheme.php (revision b55cbc6b43247e8b2ad14af6f6d24dc6747195ff)
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;
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;
28*b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
297619f373SGreg Roachuse Psr\Http\Message\ResponseInterface;
306ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
313976b470SGreg Roach
327619f373SGreg Roachuse function assert;
337619f373SGreg Roachuse function asset;
34d8809d62SGreg 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    {
86*b55cbc6bSGreg Roach        $user = Validator::attributes($request)->user();
877619f373SGreg Roach
887619f373SGreg Roach        $palette = $request->getQueryParams()['palette'];
8975964c75SGreg Roach        assert(array_key_exists($palette, $this->palettes()));
907619f373SGreg Roach
917619f373SGreg Roach        $user->setPreference('themecolor', $palette);
927619f373SGreg Roach
937619f373SGreg Roach        // If we are the admin, then use our selection as the site default.
947619f373SGreg Roach        if (Auth::isAdmin($user)) {
957619f373SGreg Roach            Site::setPreference('DEFAULT_COLOR_PALETTE', $palette);
967619f373SGreg Roach        }
977619f373SGreg Roach
987619f373SGreg Roach        Session::put('palette', $palette);
997619f373SGreg Roach
1007619f373SGreg Roach        return response();
1017619f373SGreg Roach    }
1027619f373SGreg Roach
1037619f373SGreg Roach    /**
1047619f373SGreg Roach     * A list of CSS files to include for this page.
1057619f373SGreg Roach     *
10624f2a3afSGreg Roach     * @return array<string>
1077619f373SGreg Roach     */
1087619f373SGreg Roach    public function stylesheets(): array
1097619f373SGreg Roach    {
1107619f373SGreg Roach        return [
1117619f373SGreg Roach            asset('css/colors.min.css'),
1127619f373SGreg Roach            asset('css/colors/' . $this->palette() . '.min.css'),
1137619f373SGreg Roach        ];
1147619f373SGreg Roach    }
1157619f373SGreg Roach
1167619f373SGreg Roach    /**
117ade503dfSGreg Roach     * Create a menu of palette options
118ade503dfSGreg Roach     *
11977a20107SGreg Roach     * @return Menu
120ade503dfSGreg Roach     */
1217619f373SGreg Roach    protected function menuPalette(): Menu
122ade503dfSGreg Roach    {
123ade503dfSGreg Roach        /* I18N: A colour scheme */
124ade503dfSGreg Roach        $menu = new Menu(I18N::translate('Palette'), '#', 'menu-color');
125ade503dfSGreg Roach
1267619f373SGreg Roach        $palette = $this->palette();
127ade503dfSGreg Roach
1287619f373SGreg Roach        foreach ($this->palettes() as $palette_id => $palette_name) {
1297619f373SGreg Roach            $url = route('module', ['module' => $this->name(), 'action' => 'Palette', 'palette' => $palette_id]);
1307619f373SGreg Roach
1317619f373SGreg Roach            $submenu = new Menu(
132ade503dfSGreg Roach                $palette_name,
133ade503dfSGreg Roach                '#',
1347619f373SGreg Roach                'menu-color-' . $palette_id . ($palette === $palette_id ? ' active' : ''),
135ade503dfSGreg Roach                [
136d4786c66SGreg Roach                    'data-wt-post-url' => $url,
137ade503dfSGreg Roach                ]
1387619f373SGreg Roach            );
1397619f373SGreg Roach
1407619f373SGreg Roach            $menu->addSubmenu($submenu);
141ade503dfSGreg Roach        }
142ade503dfSGreg Roach
143ade503dfSGreg Roach        return $menu;
144ade503dfSGreg Roach    }
145ade503dfSGreg Roach
146ade503dfSGreg Roach    /**
14724f2a3afSGreg Roach     * @return array<string>
1481b40e7bcSGreg Roach     */
149f6f7bcffSGreg Roach    private function palettes(): array
150f6f7bcffSGreg Roach    {
1511b40e7bcSGreg Roach        $palettes = [
1521b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1531b40e7bcSGreg Roach            'aquamarine'       => I18N::translate('Aqua Marine'),
1541b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1551b40e7bcSGreg Roach            'ash'              => I18N::translate('Ash'),
1561b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1571b40e7bcSGreg Roach            'belgianchocolate' => I18N::translate('Belgian Chocolate'),
1581b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1591b40e7bcSGreg Roach            'bluelagoon'       => I18N::translate('Blue Lagoon'),
1601b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1611b40e7bcSGreg Roach            'bluemarine'       => I18N::translate('Blue Marine'),
1621b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1631b40e7bcSGreg Roach            'coffeeandcream'   => I18N::translate('Coffee and Cream'),
1641b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1651b40e7bcSGreg Roach            'coldday'          => I18N::translate('Cold Day'),
1661b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1671b40e7bcSGreg Roach            'greenbeam'        => I18N::translate('Green Beam'),
1681b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1691b40e7bcSGreg Roach            'mediterranio'     => I18N::translate('Mediterranio'),
1701b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1711b40e7bcSGreg Roach            'mercury'          => I18N::translate('Mercury'),
1721b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1731b40e7bcSGreg Roach            'nocturnal'        => I18N::translate('Nocturnal'),
1741b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1751b40e7bcSGreg Roach            'olivia'           => I18N::translate('Olivia'),
1761b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1771b40e7bcSGreg Roach            'pinkplastic'      => I18N::translate('Pink Plastic'),
1781b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1791b40e7bcSGreg Roach            'sage'             => I18N::translate('Sage'),
1801b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1811b40e7bcSGreg Roach            'shinytomato'      => I18N::translate('Shiny Tomato'),
1821b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1831b40e7bcSGreg Roach            'tealtop'          => I18N::translate('Teal Top'),
1841b40e7bcSGreg Roach        ];
1851b40e7bcSGreg Roach
18637646143SGreg Roach        uasort($palettes, I18N::comparator());
1871b40e7bcSGreg Roach
1881b40e7bcSGreg Roach        return $palettes;
1891b40e7bcSGreg Roach    }
1906ccdf4f0SGreg Roach
1916ccdf4f0SGreg Roach    /**
1926ccdf4f0SGreg Roach     * @return string
1936ccdf4f0SGreg Roach     */
1946ccdf4f0SGreg Roach    private function palette(): string
1956ccdf4f0SGreg Roach    {
1966ccdf4f0SGreg Roach        // If we are logged in, use our preference
197a9866bf2SGreg Roach        $palette = Auth::user()->getPreference('themecolor');
1986ccdf4f0SGreg Roach
1997619f373SGreg Roach        // If not logged in or no preference, use one we selected earlier in the session.
2007619f373SGreg Roach        if ($palette === '') {
201d8809d62SGreg Roach            $palette = Session::get('palette');
202d8809d62SGreg Roach            $palette = is_string($palette) ? $palette : '';
2036ccdf4f0SGreg Roach        }
2046ccdf4f0SGreg Roach
2056ccdf4f0SGreg Roach        // We haven't selected one this session? Use the site default
2067619f373SGreg Roach        if ($palette === '') {
2078a07c98eSGreg Roach            $palette = Site::getPreference('DEFAULT_COLOR_PALETTE');
2088a07c98eSGreg Roach        }
2098a07c98eSGreg Roach
2108a07c98eSGreg Roach        if ($palette === '') {
2118a07c98eSGreg Roach            $palette = self::DEFAULT_PALETTE;
2126ccdf4f0SGreg Roach        }
2136ccdf4f0SGreg Roach
2146ccdf4f0SGreg Roach        return $palette;
2156ccdf4f0SGreg Roach    }
216ade503dfSGreg Roach}
217