xref: /webtrees/app/Module/ColorsTheme.php (revision fcfa147e10aaa6c7ff580c29bd6e5b88666befc1)
1ade503dfSGreg Roach<?php
23976b470SGreg Roach
3ade503dfSGreg Roach/**
4ade503dfSGreg Roach * webtrees: online genealogy
5ade503dfSGreg Roach * Copyright (C) 2019 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
15ade503dfSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16ade503dfSGreg Roach */
17*fcfa147eSGreg 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;
286ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
293976b470SGreg Roach
306ccdf4f0SGreg Roachuse function app;
311b40e7bcSGreg Roachuse function uasort;
32ade503dfSGreg Roach
33ade503dfSGreg Roach/**
34ade503dfSGreg Roach * The colors theme.
35ade503dfSGreg Roach */
36ade503dfSGreg Roachclass ColorsTheme extends CloudsTheme
37ade503dfSGreg Roach{
38ade503dfSGreg Roach    /**
390cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
40ade503dfSGreg Roach     *
41ade503dfSGreg Roach     * @return string
42ade503dfSGreg Roach     */
43ade503dfSGreg Roach    public function title(): string
44ade503dfSGreg Roach    {
45ade503dfSGreg Roach        /* I18N: Name of a theme. */
46ade503dfSGreg Roach        return I18N::translate('colors');
47ade503dfSGreg Roach    }
48ade503dfSGreg Roach
49ade503dfSGreg Roach    /**
50ade503dfSGreg Roach     * Generate a list of items for the user menu.
51ade503dfSGreg Roach     *
520c8c69d4SGreg Roach     * @param Tree|null $tree
530c8c69d4SGreg Roach     *
54ade503dfSGreg Roach     * @return Menu[]
55ade503dfSGreg Roach     */
560c8c69d4SGreg Roach    public function userMenu(?Tree $tree): array
57ade503dfSGreg Roach    {
58ade503dfSGreg Roach        return array_filter([
590c8c69d4SGreg Roach            $this->menuPendingChanges($tree),
600c8c69d4SGreg Roach            $this->menuMyPages($tree),
61ade503dfSGreg Roach            $this->menuThemes(),
62ade503dfSGreg Roach            $this->menuPalette(),
63ade503dfSGreg Roach            $this->menuLanguages(),
64ade503dfSGreg Roach            $this->menuLogin(),
65ade503dfSGreg Roach            $this->menuLogout(),
66ade503dfSGreg Roach        ]);
67ade503dfSGreg Roach    }
68ade503dfSGreg Roach
69ade503dfSGreg Roach    /**
70ade503dfSGreg Roach     * Create a menu of palette options
71ade503dfSGreg Roach     *
7277a20107SGreg Roach     * @return Menu
73ade503dfSGreg Roach     */
7477a20107SGreg Roach    public function menuPalette(): Menu
75ade503dfSGreg Roach    {
766ccdf4f0SGreg Roach        $request = app(ServerRequestInterface::class);
77e6bcfa02SGreg Roach
78ade503dfSGreg Roach        /* I18N: A colour scheme */
79ade503dfSGreg Roach        $menu = new Menu(I18N::translate('Palette'), '#', 'menu-color');
80ade503dfSGreg Roach
811b40e7bcSGreg Roach        foreach ($this->palettes() as $palette_id => $palette_name) {
82f567c3d8SGreg Roach            $url = $request->getAttribute((string) $request->getUri());
83ade503dfSGreg Roach            $url = preg_replace('/&themecolor=[a-z]+/', '', $url);
84ade503dfSGreg Roach            $url .= '&themecolor=' . $palette_id;
85ade503dfSGreg Roach
86ade503dfSGreg Roach            $menu->addSubmenu(new Menu(
87ade503dfSGreg Roach                $palette_name,
88ade503dfSGreg Roach                '#',
891b40e7bcSGreg Roach                'menu-color-' . $palette_id . ($this->palette() === $palette_id ? ' active' : ''),
90ade503dfSGreg Roach                [
91ade503dfSGreg Roach                    'onclick' => 'document.location=\'' . $url . '\'',
92ade503dfSGreg Roach                ]
93ade503dfSGreg Roach            ));
94ade503dfSGreg Roach        }
95ade503dfSGreg Roach
96ade503dfSGreg Roach        return $menu;
97ade503dfSGreg Roach    }
98ade503dfSGreg Roach
99ade503dfSGreg Roach    /**
1001b40e7bcSGreg Roach     * @return string[]
1011b40e7bcSGreg Roach     */
102f6f7bcffSGreg Roach    private function palettes(): array
103f6f7bcffSGreg Roach    {
1041b40e7bcSGreg Roach        $palettes = [
1051b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1061b40e7bcSGreg Roach            'aquamarine'       => I18N::translate('Aqua Marine'),
1071b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1081b40e7bcSGreg Roach            'ash'              => I18N::translate('Ash'),
1091b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1101b40e7bcSGreg Roach            'belgianchocolate' => I18N::translate('Belgian Chocolate'),
1111b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1121b40e7bcSGreg Roach            'bluelagoon'       => I18N::translate('Blue Lagoon'),
1131b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1141b40e7bcSGreg Roach            'bluemarine'       => I18N::translate('Blue Marine'),
1151b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1161b40e7bcSGreg Roach            'coffeeandcream'   => I18N::translate('Coffee and Cream'),
1171b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1181b40e7bcSGreg Roach            'coldday'          => I18N::translate('Cold Day'),
1191b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1201b40e7bcSGreg Roach            'greenbeam'        => I18N::translate('Green Beam'),
1211b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1221b40e7bcSGreg Roach            'mediterranio'     => I18N::translate('Mediterranio'),
1231b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1241b40e7bcSGreg Roach            'mercury'          => I18N::translate('Mercury'),
1251b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1261b40e7bcSGreg Roach            'nocturnal'        => I18N::translate('Nocturnal'),
1271b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1281b40e7bcSGreg Roach            'olivia'           => I18N::translate('Olivia'),
1291b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1301b40e7bcSGreg Roach            'pinkplastic'      => I18N::translate('Pink Plastic'),
1311b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1321b40e7bcSGreg Roach            'sage'             => I18N::translate('Sage'),
1331b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1341b40e7bcSGreg Roach            'shinytomato'      => I18N::translate('Shiny Tomato'),
1351b40e7bcSGreg Roach            /* I18N: The name of a colour-scheme */
1361b40e7bcSGreg Roach            'tealtop'          => I18N::translate('Teal Top'),
1371b40e7bcSGreg Roach        ];
1381b40e7bcSGreg Roach
1391b40e7bcSGreg Roach        uasort($palettes, '\Fisharebest\Webtrees\I18N::strcasecmp');
1401b40e7bcSGreg Roach
1411b40e7bcSGreg Roach        return $palettes;
1421b40e7bcSGreg Roach    }
1436ccdf4f0SGreg Roach
1446ccdf4f0SGreg Roach    /**
1456ccdf4f0SGreg Roach     * @return string
1466ccdf4f0SGreg Roach     */
1476ccdf4f0SGreg Roach    private function palette(): string
1486ccdf4f0SGreg Roach    {
1496ccdf4f0SGreg Roach        $palettes = $this->palettes();
1506ccdf4f0SGreg Roach
1516ccdf4f0SGreg Roach        // If we've selected a new palette, and we are logged in, set this value as a default.
1526ccdf4f0SGreg Roach        if (isset($_GET['themecolor'])) {
1536ccdf4f0SGreg Roach            // Request to change color
1546ccdf4f0SGreg Roach            $palette = $_GET['themecolor'];
1556ccdf4f0SGreg Roach            Auth::user()->setPreference('themecolor', $palette);
1566ccdf4f0SGreg Roach            if (Auth::isAdmin()) {
1576ccdf4f0SGreg Roach                Site::setPreference('DEFAULT_COLOR_PALETTE', $palette);
1586ccdf4f0SGreg Roach            }
1596ccdf4f0SGreg Roach            unset($_GET['themecolor']);
1606ccdf4f0SGreg Roach            // Rember that we have selected a value
1616ccdf4f0SGreg Roach            Session::put('subColor', $palette);
1626ccdf4f0SGreg Roach        }
1636ccdf4f0SGreg Roach
1646ccdf4f0SGreg Roach        // If we are logged in, use our preference
1656ccdf4f0SGreg Roach        $palette = Auth::user()->getPreference('themecolor');
1666ccdf4f0SGreg Roach
1676ccdf4f0SGreg Roach        // If not logged in or no preference, use one we selected earlier in the session?
1686ccdf4f0SGreg Roach        if (!$palette) {
1696ccdf4f0SGreg Roach            $palette = Session::get('subColor');
1706ccdf4f0SGreg Roach        }
1716ccdf4f0SGreg Roach
1726ccdf4f0SGreg Roach        // We haven't selected one this session? Use the site default
1736ccdf4f0SGreg Roach        if (!$palette) {
1746ccdf4f0SGreg Roach            $palette = Site::getPreference('DEFAULT_COLOR_PALETTE');
1756ccdf4f0SGreg Roach        }
1766ccdf4f0SGreg Roach
1776ccdf4f0SGreg Roach        // Make sure our selected palette actually exists
1786ccdf4f0SGreg Roach        if (!array_key_exists($palette, $palettes)) {
1796ccdf4f0SGreg Roach            $palette = 'ash';
1806ccdf4f0SGreg Roach        }
1816ccdf4f0SGreg Roach
1826ccdf4f0SGreg Roach        return $palette;
1836ccdf4f0SGreg Roach    }
1846ccdf4f0SGreg Roach
1856ccdf4f0SGreg Roach    /**
1866ccdf4f0SGreg Roach     * A list of CSS files to include for this page.
1876ccdf4f0SGreg Roach     *
1886ccdf4f0SGreg Roach     * @return string[]
1896ccdf4f0SGreg Roach     */
1906ccdf4f0SGreg Roach    public function stylesheets(): array
1916ccdf4f0SGreg Roach    {
1926ccdf4f0SGreg Roach        return [
1936ccdf4f0SGreg Roach            asset('css/colors.min.css'),
1946ccdf4f0SGreg Roach            asset('css/colors/' . $this->palette() . '.min.css'),
1956ccdf4f0SGreg Roach        ];
1966ccdf4f0SGreg Roach    }
197ade503dfSGreg Roach}
198