xref: /webtrees/app/Module/ColorsTheme.php (revision a9866bf217fcbf79695b119a127478d64260caff)
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;
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    {
857619f373SGreg Roach        $user = $request->getAttribute('user');
8675964c75SGreg Roach        assert($user instanceof UserInterface);
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
197*a9866bf2SGreg 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 === '') {
2017619f373SGreg Roach            $palette = Session::get('palette', '');
2026ccdf4f0SGreg Roach        }
2036ccdf4f0SGreg Roach
2046ccdf4f0SGreg Roach        // We haven't selected one this session? Use the site default
2057619f373SGreg Roach        if ($palette === '') {
2068a07c98eSGreg Roach            $palette = Site::getPreference('DEFAULT_COLOR_PALETTE');
2078a07c98eSGreg Roach        }
2088a07c98eSGreg Roach
2098a07c98eSGreg Roach        if ($palette === '') {
2108a07c98eSGreg Roach            $palette = self::DEFAULT_PALETTE;
2116ccdf4f0SGreg Roach        }
2126ccdf4f0SGreg Roach
2136ccdf4f0SGreg Roach        return $palette;
2146ccdf4f0SGreg Roach    }
215ade503dfSGreg Roach}
216