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