xref: /webtrees/app/Module/ColorsTheme.php (revision 77a2010793e07b8d01d1a336fa5967181694729d)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Module;
19
20use Fisharebest\Webtrees\Auth;
21use Fisharebest\Webtrees\I18N;
22use Fisharebest\Webtrees\Menu;
23use Fisharebest\Webtrees\Session;
24use Fisharebest\Webtrees\Site;
25use Fisharebest\Webtrees\Tree;
26use Symfony\Component\HttpFoundation\Request;
27
28/**
29 * The colors theme.
30 */
31class ColorsTheme extends CloudsTheme
32{
33    protected const PERSON_BOX_CLASSES = [
34        'M' => 'person_box',
35        'F' => 'person_boxF',
36        'U' => 'person_boxNN',
37    ];
38
39    /** @var string[] A list of color palettes */
40    protected $palettes;
41
42    /** @var string Which of the color palettes to use on this page */
43    protected $palette;
44
45    /**
46     * How should this module be labelled on tabs, menus, etc.?
47     *
48     * @return string
49     */
50    public function title(): string
51    {
52        /* I18N: Name of a theme. */
53        return I18N::translate('colors');
54    }
55
56    /**
57     * @param Request   $request
58     * @param Tree|null $tree The current tree (if there is one).
59     */
60    public function __construct(Request $request, ?Tree $tree)
61    {
62        parent::__construct($request, $tree);
63
64        $this->palettes = [
65            /* I18N: The name of a colour-scheme */
66            'aquamarine'       => I18N::translate('Aqua Marine'),
67            /* I18N: The name of a colour-scheme */
68            'ash'              => I18N::translate('Ash'),
69            /* I18N: The name of a colour-scheme */
70            'belgianchocolate' => I18N::translate('Belgian Chocolate'),
71            /* I18N: The name of a colour-scheme */
72            'bluelagoon'       => I18N::translate('Blue Lagoon'),
73            /* I18N: The name of a colour-scheme */
74            'bluemarine'       => I18N::translate('Blue Marine'),
75            /* I18N: The name of a colour-scheme */
76            'coffeeandcream'   => I18N::translate('Coffee and Cream'),
77            /* I18N: The name of a colour-scheme */
78            'coldday'          => I18N::translate('Cold Day'),
79            /* I18N: The name of a colour-scheme */
80            'greenbeam'        => I18N::translate('Green Beam'),
81            /* I18N: The name of a colour-scheme */
82            'mediterranio'     => I18N::translate('Mediterranio'),
83            /* I18N: The name of a colour-scheme */
84            'mercury'          => I18N::translate('Mercury'),
85            /* I18N: The name of a colour-scheme */
86            'nocturnal'        => I18N::translate('Nocturnal'),
87            /* I18N: The name of a colour-scheme */
88            /* I18N: The name of a colour-scheme */
89            'olivia'           => I18N::translate('Olivia'),
90            /* I18N: The name of a colour-scheme */
91            'pinkplastic'      => I18N::translate('Pink Plastic'),
92            /* I18N: The name of a colour-scheme */
93            'sage'             => I18N::translate('Sage'),
94            /* I18N: The name of a colour-scheme */
95            'shinytomato'      => I18N::translate('Shiny Tomato'),
96            /* I18N: The name of a colour-scheme */
97            'tealtop'          => I18N::translate('Teal Top'),
98        ];
99        uasort($this->palettes, '\Fisharebest\Webtrees\I18N::strcasecmp');
100
101        // If we've selected a new palette, and we are logged in, set this value as a default.
102        if (isset($_GET['themecolor']) && array_key_exists($_GET['themecolor'], $this->palettes)) {
103            // Request to change color
104            $this->palette = $_GET['themecolor'];
105            Auth::user()->setPreference('themecolor', $this->palette);
106            if (Auth::isAdmin()) {
107                Site::setPreference('DEFAULT_COLOR_PALETTE', $this->palette);
108            }
109            unset($_GET['themecolor']);
110            // Rember that we have selected a value
111            Session::put('subColor', $this->palette);
112        }
113        // If we are logged in, use our preference
114        $this->palette = Auth::user()->getPreference('themecolor');
115        // If not logged in or no preference, use one we selected earlier in the session?
116        if (!$this->palette) {
117            $this->palette = Session::get('subColor');
118        }
119        // We haven't selected one this session? Use the site default
120        if (!$this->palette) {
121            $this->palette = Site::getPreference('DEFAULT_COLOR_PALETTE');
122        }
123        // Make sure our selected palette actually exists
124        if (!array_key_exists($this->palette, $this->palettes)) {
125            $this->palette = 'ash';
126        }
127    }
128
129    /**
130     * Generate a list of items for the user menu.
131     *
132     * @return Menu[]
133     */
134    public function secondaryMenu(): array
135    {
136        return array_filter([
137            $this->menuPendingChanges(),
138            $this->menuMyPages(),
139            $this->menuThemes(),
140            $this->menuPalette(),
141            $this->menuLanguages(),
142            $this->menuLogin(),
143            $this->menuLogout(),
144        ]);
145    }
146
147    /**
148     * Create a menu of palette options
149     *
150     * @return Menu
151     */
152    public function menuPalette(): Menu
153    {
154        /* I18N: A colour scheme */
155        $menu = new Menu(I18N::translate('Palette'), '#', 'menu-color');
156
157        foreach ($this->palettes as $palette_id => $palette_name) {
158            $url = $this->request->getRequestUri();
159            $url = preg_replace('/&themecolor=[a-z]+/', '', $url);
160            $url .= '&themecolor=' . $palette_id;
161
162            $menu->addSubmenu(new Menu(
163                $palette_name,
164                '#',
165                'menu-color-' . $palette_id . ($this->palette === $palette_id ? ' active' : ''),
166                [
167                    'onclick' => 'document.location=\'' . $url . '\'',
168                ]
169            ));
170        }
171
172        return $menu;
173    }
174
175    /**
176     * A list of CSS files to include for this page.
177     *
178     * @return string[]
179     */
180    public function stylesheets(): array
181    {
182        return [
183            'themes/_common/css-2.0.0/style.css',
184            'themes/clouds/css-2.0.0/style.css',
185            'themes/colors/css-2.0.0/style.css',
186            'themes/colors/css-2.0.0/palette/' . $this->palette . '.css',
187        ];
188    }
189}
190