xref: /webtrees/app/Module/ColorsTheme.php (revision 0c8c69d4fa633b3ee8e61f082e813991aabf9076)
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;
26ade503dfSGreg Roachuse Symfony\Component\HttpFoundation\Request;
271b40e7bcSGreg Roachuse function uasort;
28ade503dfSGreg Roach
29ade503dfSGreg Roach/**
30ade503dfSGreg Roach * The colors theme.
31ade503dfSGreg Roach */
32ade503dfSGreg Roachclass ColorsTheme extends CloudsTheme
33ade503dfSGreg Roach{
34ade503dfSGreg Roach    /**
35ade503dfSGreg Roach     * How should this module be labelled on tabs, menus, etc.?
36ade503dfSGreg Roach     *
37ade503dfSGreg Roach     * @return string
38ade503dfSGreg Roach     */
39ade503dfSGreg Roach    public function title(): string
40ade503dfSGreg Roach    {
41ade503dfSGreg Roach        /* I18N: Name of a theme. */
42ade503dfSGreg Roach        return I18N::translate('colors');
43ade503dfSGreg Roach    }
44ade503dfSGreg Roach
45ade503dfSGreg Roach    /**
46ade503dfSGreg Roach     * Generate a list of items for the user menu.
47ade503dfSGreg Roach     *
48*0c8c69d4SGreg Roach     * @param Tree|null $tree
49*0c8c69d4SGreg Roach     *
50ade503dfSGreg Roach     * @return Menu[]
51ade503dfSGreg Roach     */
52*0c8c69d4SGreg Roach    public function userMenu(?Tree $tree): array
53ade503dfSGreg Roach    {
54ade503dfSGreg Roach        return array_filter([
55*0c8c69d4SGreg Roach            $this->menuPendingChanges($tree),
56*0c8c69d4SGreg Roach            $this->menuMyPages($tree),
57ade503dfSGreg Roach            $this->menuThemes(),
58ade503dfSGreg Roach            $this->menuPalette(),
59ade503dfSGreg Roach            $this->menuLanguages(),
60ade503dfSGreg Roach            $this->menuLogin(),
61ade503dfSGreg Roach            $this->menuLogout(),
62ade503dfSGreg Roach        ]);
63ade503dfSGreg Roach    }
64ade503dfSGreg Roach
65ade503dfSGreg Roach    /**
66ade503dfSGreg Roach     * Create a menu of palette options
67ade503dfSGreg Roach     *
6877a20107SGreg Roach     * @return Menu
69ade503dfSGreg Roach     */
7077a20107SGreg Roach    public function menuPalette(): Menu
71ade503dfSGreg Roach    {
72ade503dfSGreg Roach        /* I18N: A colour scheme */
73ade503dfSGreg Roach        $menu = new Menu(I18N::translate('Palette'), '#', 'menu-color');
74ade503dfSGreg Roach
751b40e7bcSGreg Roach        foreach ($this->palettes() as $palette_id => $palette_name) {
76ade503dfSGreg Roach            $url = $this->request->getRequestUri();
77ade503dfSGreg Roach            $url = preg_replace('/&themecolor=[a-z]+/', '', $url);
78ade503dfSGreg Roach            $url .= '&themecolor=' . $palette_id;
79ade503dfSGreg Roach
80ade503dfSGreg Roach            $menu->addSubmenu(new Menu(
81ade503dfSGreg Roach                $palette_name,
82ade503dfSGreg Roach                '#',
831b40e7bcSGreg Roach                'menu-color-' . $palette_id . ($this->palette() === $palette_id ? ' active' : ''),
84ade503dfSGreg Roach                [
85ade503dfSGreg Roach                    'onclick' => 'document.location=\'' . $url . '\'',
86ade503dfSGreg Roach                ]
87ade503dfSGreg Roach            ));
88ade503dfSGreg Roach        }
89ade503dfSGreg Roach
90ade503dfSGreg Roach        return $menu;
91ade503dfSGreg Roach    }
92ade503dfSGreg Roach
93ade503dfSGreg Roach    /**
94ade503dfSGreg Roach     * A list of CSS files to include for this page.
95ade503dfSGreg Roach     *
96ade503dfSGreg Roach     * @return string[]
97ade503dfSGreg Roach     */
98ade503dfSGreg Roach    public function stylesheets(): array
99ade503dfSGreg Roach    {
100ade503dfSGreg Roach        return [
101e837ff07SGreg Roach            asset('css/colors.min.css'),
1021b40e7bcSGreg Roach            asset('css/colors/' . $this->palette() . '.min.css'),
103ade503dfSGreg Roach        ];
104ade503dfSGreg Roach    }
1051b40e7bcSGreg Roach
1061b40e7bcSGreg Roach    /**
1071b40e7bcSGreg Roach     * @return string
1081b40e7bcSGreg Roach     */
1091b40e7bcSGreg Roach    private function palette(): string {
1101b40e7bcSGreg Roach        $palettes = $this->palettes();
1111b40e7bcSGreg Roach
1121b40e7bcSGreg Roach        // If we've selected a new palette, and we are logged in, set this value as a default.
1131b40e7bcSGreg Roach        if (isset($_GET['themecolor'])) {
1141b40e7bcSGreg Roach            // Request to change color
1151b40e7bcSGreg Roach            $palette = $_GET['themecolor'];
1161b40e7bcSGreg Roach            Auth::user()->setPreference('themecolor', $palette);
1171b40e7bcSGreg Roach            if (Auth::isAdmin()) {
1181b40e7bcSGreg Roach                Site::setPreference('DEFAULT_COLOR_PALETTE', $palette);
1191b40e7bcSGreg Roach            }
1201b40e7bcSGreg Roach            unset($_GET['themecolor']);
1211b40e7bcSGreg Roach            // Rember that we have selected a value
1221b40e7bcSGreg Roach            Session::put('subColor', $palette);
1231b40e7bcSGreg Roach        }
1241b40e7bcSGreg Roach
1251b40e7bcSGreg Roach        // If we are logged in, use our preference
1261b40e7bcSGreg Roach        $palette = Auth::user()->getPreference('themecolor');
1271b40e7bcSGreg Roach
1281b40e7bcSGreg Roach        // If not logged in or no preference, use one we selected earlier in the session?
1291b40e7bcSGreg Roach        if (!$palette) {
1301b40e7bcSGreg Roach            $palette = Session::get('subColor');
1311b40e7bcSGreg Roach        }
1321b40e7bcSGreg Roach
1331b40e7bcSGreg Roach        // We haven't selected one this session? Use the site default
1341b40e7bcSGreg Roach        if (!$palette) {
1351b40e7bcSGreg Roach            $palette = Site::getPreference('DEFAULT_COLOR_PALETTE');
1361b40e7bcSGreg Roach        }
1371b40e7bcSGreg Roach
1381b40e7bcSGreg Roach        // Make sure our selected palette actually exists
1391b40e7bcSGreg Roach        if (!array_key_exists($palette, $palettes)) {
1401b40e7bcSGreg Roach            $palette = 'ash';
1411b40e7bcSGreg Roach        }
1421b40e7bcSGreg Roach
1431b40e7bcSGreg Roach        return $palette;
1441b40e7bcSGreg Roach    }
1451b40e7bcSGreg Roach
1461b40e7bcSGreg Roach    /**
1471b40e7bcSGreg Roach     * @return string[]
1481b40e7bcSGreg Roach     */
1491b40e7bcSGreg Roach    private function palettes(): array {
1501b40e7bcSGreg Roach        $palettes = [
1511b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1521b40e7bcSGreg Roach            'aquamarine'       => I18N::translate('Aqua Marine'),
1531b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1541b40e7bcSGreg Roach            'ash'              => I18N::translate('Ash'),
1551b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1561b40e7bcSGreg Roach            'belgianchocolate' => I18N::translate('Belgian Chocolate'),
1571b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1581b40e7bcSGreg Roach            'bluelagoon'       => I18N::translate('Blue Lagoon'),
1591b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1601b40e7bcSGreg Roach            'bluemarine'       => I18N::translate('Blue Marine'),
1611b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1621b40e7bcSGreg Roach            'coffeeandcream'   => I18N::translate('Coffee and Cream'),
1631b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1641b40e7bcSGreg Roach            'coldday'          => I18N::translate('Cold Day'),
1651b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1661b40e7bcSGreg Roach            'greenbeam'        => I18N::translate('Green Beam'),
1671b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1681b40e7bcSGreg Roach            'mediterranio'     => I18N::translate('Mediterranio'),
1691b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1701b40e7bcSGreg Roach            'mercury'          => I18N::translate('Mercury'),
1711b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1721b40e7bcSGreg Roach            'nocturnal'        => I18N::translate('Nocturnal'),
1731b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1741b40e7bcSGreg Roach            'olivia'           => I18N::translate('Olivia'),
1751b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1761b40e7bcSGreg Roach            'pinkplastic'      => I18N::translate('Pink Plastic'),
1771b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1781b40e7bcSGreg Roach            'sage'             => I18N::translate('Sage'),
1791b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1801b40e7bcSGreg Roach            'shinytomato'      => I18N::translate('Shiny Tomato'),
1811b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1821b40e7bcSGreg Roach            'tealtop'          => I18N::translate('Teal Top'),
1831b40e7bcSGreg Roach        ];
1841b40e7bcSGreg Roach
1851b40e7bcSGreg Roach        uasort($palettes, '\Fisharebest\Webtrees\I18N::strcasecmp');
1861b40e7bcSGreg Roach
1871b40e7bcSGreg Roach        return $palettes;
1881b40e7bcSGreg Roach    }
189ade503dfSGreg Roach}
190