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