xref: /webtrees/app/Module/ColorsTheme.php (revision 77a2010793e07b8d01d1a336fa5967181694729d)
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;
27ade503dfSGreg Roach
28ade503dfSGreg Roach/**
29ade503dfSGreg Roach * The colors theme.
30ade503dfSGreg Roach */
31ade503dfSGreg Roachclass ColorsTheme extends CloudsTheme
32ade503dfSGreg Roach{
33ade503dfSGreg Roach    protected const PERSON_BOX_CLASSES = [
34ade503dfSGreg Roach        'M' => 'person_box',
35ade503dfSGreg Roach        'F' => 'person_boxF',
36ade503dfSGreg Roach        'U' => 'person_boxNN',
37ade503dfSGreg Roach    ];
38ade503dfSGreg Roach
39ade503dfSGreg Roach    /** @var string[] A list of color palettes */
40ade503dfSGreg Roach    protected $palettes;
41ade503dfSGreg Roach
42ade503dfSGreg Roach    /** @var string Which of the color palettes to use on this page */
43ade503dfSGreg Roach    protected $palette;
44ade503dfSGreg Roach
45ade503dfSGreg Roach    /**
46ade503dfSGreg Roach     * How should this module be labelled on tabs, menus, etc.?
47ade503dfSGreg Roach     *
48ade503dfSGreg Roach     * @return string
49ade503dfSGreg Roach     */
50ade503dfSGreg Roach    public function title(): string
51ade503dfSGreg Roach    {
52ade503dfSGreg Roach        /* I18N: Name of a theme. */
53ade503dfSGreg Roach        return I18N::translate('colors');
54ade503dfSGreg Roach    }
55ade503dfSGreg Roach
56ade503dfSGreg Roach    /**
57ade503dfSGreg Roach     * @param Request   $request
58ade503dfSGreg Roach     * @param Tree|null $tree The current tree (if there is one).
59ade503dfSGreg Roach     */
60ade503dfSGreg Roach    public function __construct(Request $request, ?Tree $tree)
61ade503dfSGreg Roach    {
62ade503dfSGreg Roach        parent::__construct($request, $tree);
63ade503dfSGreg Roach
64ade503dfSGreg Roach        $this->palettes = [
65ade503dfSGreg Roach            /* I18N: The name of a colour-scheme */
66ade503dfSGreg Roach            'aquamarine'       => I18N::translate('Aqua Marine'),
67ade503dfSGreg Roach            /* I18N: The name of a colour-scheme */
68ade503dfSGreg Roach            'ash'              => I18N::translate('Ash'),
69ade503dfSGreg Roach            /* I18N: The name of a colour-scheme */
70ade503dfSGreg Roach            'belgianchocolate' => I18N::translate('Belgian Chocolate'),
71ade503dfSGreg Roach            /* I18N: The name of a colour-scheme */
72ade503dfSGreg Roach            'bluelagoon'       => I18N::translate('Blue Lagoon'),
73ade503dfSGreg Roach            /* I18N: The name of a colour-scheme */
74ade503dfSGreg Roach            'bluemarine'       => I18N::translate('Blue Marine'),
75ade503dfSGreg Roach            /* I18N: The name of a colour-scheme */
76ade503dfSGreg Roach            'coffeeandcream'   => I18N::translate('Coffee and Cream'),
77ade503dfSGreg Roach            /* I18N: The name of a colour-scheme */
78ade503dfSGreg Roach            'coldday'          => I18N::translate('Cold Day'),
79ade503dfSGreg Roach            /* I18N: The name of a colour-scheme */
80ade503dfSGreg Roach            'greenbeam'        => I18N::translate('Green Beam'),
81ade503dfSGreg Roach            /* I18N: The name of a colour-scheme */
82ade503dfSGreg Roach            'mediterranio'     => I18N::translate('Mediterranio'),
83ade503dfSGreg Roach            /* I18N: The name of a colour-scheme */
84ade503dfSGreg Roach            'mercury'          => I18N::translate('Mercury'),
85ade503dfSGreg Roach            /* I18N: The name of a colour-scheme */
86ade503dfSGreg Roach            'nocturnal'        => I18N::translate('Nocturnal'),
87ade503dfSGreg Roach            /* I18N: The name of a colour-scheme */
88ade503dfSGreg Roach            /* I18N: The name of a colour-scheme */
89ade503dfSGreg Roach            'olivia'           => I18N::translate('Olivia'),
90ade503dfSGreg Roach            /* I18N: The name of a colour-scheme */
91ade503dfSGreg Roach            'pinkplastic'      => I18N::translate('Pink Plastic'),
92ade503dfSGreg Roach            /* I18N: The name of a colour-scheme */
93ade503dfSGreg Roach            'sage'             => I18N::translate('Sage'),
94ade503dfSGreg Roach            /* I18N: The name of a colour-scheme */
95ade503dfSGreg Roach            'shinytomato'      => I18N::translate('Shiny Tomato'),
96ade503dfSGreg Roach            /* I18N: The name of a colour-scheme */
97ade503dfSGreg Roach            'tealtop'          => I18N::translate('Teal Top'),
98ade503dfSGreg Roach        ];
99ade503dfSGreg Roach        uasort($this->palettes, '\Fisharebest\Webtrees\I18N::strcasecmp');
100ade503dfSGreg Roach
101ade503dfSGreg Roach        // If we've selected a new palette, and we are logged in, set this value as a default.
102ade503dfSGreg Roach        if (isset($_GET['themecolor']) && array_key_exists($_GET['themecolor'], $this->palettes)) {
103ade503dfSGreg Roach            // Request to change color
104ade503dfSGreg Roach            $this->palette = $_GET['themecolor'];
105ade503dfSGreg Roach            Auth::user()->setPreference('themecolor', $this->palette);
106ade503dfSGreg Roach            if (Auth::isAdmin()) {
107ade503dfSGreg Roach                Site::setPreference('DEFAULT_COLOR_PALETTE', $this->palette);
108ade503dfSGreg Roach            }
109ade503dfSGreg Roach            unset($_GET['themecolor']);
110ade503dfSGreg Roach            // Rember that we have selected a value
111ade503dfSGreg Roach            Session::put('subColor', $this->palette);
112ade503dfSGreg Roach        }
113ade503dfSGreg Roach        // If we are logged in, use our preference
114ade503dfSGreg Roach        $this->palette = Auth::user()->getPreference('themecolor');
115ade503dfSGreg Roach        // If not logged in or no preference, use one we selected earlier in the session?
116ade503dfSGreg Roach        if (!$this->palette) {
117ade503dfSGreg Roach            $this->palette = Session::get('subColor');
118ade503dfSGreg Roach        }
119ade503dfSGreg Roach        // We haven't selected one this session? Use the site default
120ade503dfSGreg Roach        if (!$this->palette) {
121ade503dfSGreg Roach            $this->palette = Site::getPreference('DEFAULT_COLOR_PALETTE');
122ade503dfSGreg Roach        }
123ade503dfSGreg Roach        // Make sure our selected palette actually exists
124ade503dfSGreg Roach        if (!array_key_exists($this->palette, $this->palettes)) {
125ade503dfSGreg Roach            $this->palette = 'ash';
126ade503dfSGreg Roach        }
127ade503dfSGreg Roach    }
128ade503dfSGreg Roach
129ade503dfSGreg Roach    /**
130ade503dfSGreg Roach     * Generate a list of items for the user menu.
131ade503dfSGreg Roach     *
132ade503dfSGreg Roach     * @return Menu[]
133ade503dfSGreg Roach     */
134ade503dfSGreg Roach    public function secondaryMenu(): array
135ade503dfSGreg Roach    {
136ade503dfSGreg Roach        return array_filter([
137ade503dfSGreg Roach            $this->menuPendingChanges(),
138ade503dfSGreg Roach            $this->menuMyPages(),
139ade503dfSGreg Roach            $this->menuThemes(),
140ade503dfSGreg Roach            $this->menuPalette(),
141ade503dfSGreg Roach            $this->menuLanguages(),
142ade503dfSGreg Roach            $this->menuLogin(),
143ade503dfSGreg Roach            $this->menuLogout(),
144ade503dfSGreg Roach        ]);
145ade503dfSGreg Roach    }
146ade503dfSGreg Roach
147ade503dfSGreg Roach    /**
148ade503dfSGreg Roach     * Create a menu of palette options
149ade503dfSGreg Roach     *
150*77a20107SGreg Roach     * @return Menu
151ade503dfSGreg Roach     */
152*77a20107SGreg Roach    public function menuPalette(): Menu
153ade503dfSGreg Roach    {
154ade503dfSGreg Roach        /* I18N: A colour scheme */
155ade503dfSGreg Roach        $menu = new Menu(I18N::translate('Palette'), '#', 'menu-color');
156ade503dfSGreg Roach
157ade503dfSGreg Roach        foreach ($this->palettes as $palette_id => $palette_name) {
158ade503dfSGreg Roach            $url = $this->request->getRequestUri();
159ade503dfSGreg Roach            $url = preg_replace('/&themecolor=[a-z]+/', '', $url);
160ade503dfSGreg Roach            $url .= '&themecolor=' . $palette_id;
161ade503dfSGreg Roach
162ade503dfSGreg Roach            $menu->addSubmenu(new Menu(
163ade503dfSGreg Roach                $palette_name,
164ade503dfSGreg Roach                '#',
165ade503dfSGreg Roach                'menu-color-' . $palette_id . ($this->palette === $palette_id ? ' active' : ''),
166ade503dfSGreg Roach                [
167ade503dfSGreg Roach                    'onclick' => 'document.location=\'' . $url . '\'',
168ade503dfSGreg Roach                ]
169ade503dfSGreg Roach            ));
170ade503dfSGreg Roach        }
171ade503dfSGreg Roach
172ade503dfSGreg Roach        return $menu;
173ade503dfSGreg Roach    }
174ade503dfSGreg Roach
175ade503dfSGreg Roach    /**
176ade503dfSGreg Roach     * A list of CSS files to include for this page.
177ade503dfSGreg Roach     *
178ade503dfSGreg Roach     * @return string[]
179ade503dfSGreg Roach     */
180ade503dfSGreg Roach    public function stylesheets(): array
181ade503dfSGreg Roach    {
182ade503dfSGreg Roach        return [
183ade503dfSGreg Roach            'themes/_common/css-2.0.0/style.css',
184ade503dfSGreg Roach            'themes/clouds/css-2.0.0/style.css',
185ade503dfSGreg Roach            'themes/colors/css-2.0.0/style.css',
186ade503dfSGreg Roach            'themes/colors/css-2.0.0/palette/' . $this->palette . '.css',
187ade503dfSGreg Roach        ];
188ade503dfSGreg Roach    }
189ade503dfSGreg Roach}
190