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 */ 17fcfa147eSGreg Roach 18ade503dfSGreg Roachdeclare(strict_types=1); 19ade503dfSGreg Roach 20ade503dfSGreg Roachnamespace Fisharebest\Webtrees\Module; 21ade503dfSGreg Roach 22ade503dfSGreg Roachuse Fisharebest\Webtrees\Auth; 23*7619f373SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface; 24ade503dfSGreg Roachuse Fisharebest\Webtrees\I18N; 25ade503dfSGreg Roachuse Fisharebest\Webtrees\Menu; 26ade503dfSGreg Roachuse Fisharebest\Webtrees\Session; 27ade503dfSGreg Roachuse Fisharebest\Webtrees\Site; 28ade503dfSGreg Roachuse Fisharebest\Webtrees\Tree; 29*7619f373SGreg Roachuse InvalidArgumentException; 30*7619f373SGreg Roachuse Psr\Http\Message\ResponseInterface; 316ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 323976b470SGreg Roach 33*7619f373SGreg Roachuse function assert; 34*7619f373SGreg Roachuse function asset; 35*7619f373SGreg Roachuse function response; 361b40e7bcSGreg Roachuse function uasort; 37ade503dfSGreg Roach 38ade503dfSGreg Roach/** 39ade503dfSGreg Roach * The colors theme. 40ade503dfSGreg Roach */ 41ade503dfSGreg Roachclass ColorsTheme extends CloudsTheme 42ade503dfSGreg Roach{ 43*7619f373SGreg Roach // If no valid palette has been selected, use this one. 44*7619f373SGreg Roach private const DEFAULT_PALETTE = 'ash'; 45*7619f373SGreg Roach 46ade503dfSGreg Roach /** 470cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 48ade503dfSGreg Roach * 49ade503dfSGreg Roach * @return string 50ade503dfSGreg Roach */ 51ade503dfSGreg Roach public function title(): string 52ade503dfSGreg Roach { 53ade503dfSGreg Roach /* I18N: Name of a theme. */ 54ade503dfSGreg Roach return I18N::translate('colors'); 55ade503dfSGreg Roach } 56ade503dfSGreg Roach 57ade503dfSGreg Roach /** 58ade503dfSGreg Roach * Generate a list of items for the user menu. 59ade503dfSGreg Roach * 600c8c69d4SGreg Roach * @param Tree|null $tree 610c8c69d4SGreg Roach * 62ade503dfSGreg Roach * @return Menu[] 63ade503dfSGreg Roach */ 640c8c69d4SGreg Roach public function userMenu(?Tree $tree): array 65ade503dfSGreg Roach { 66ade503dfSGreg Roach return array_filter([ 670c8c69d4SGreg Roach $this->menuPendingChanges($tree), 680c8c69d4SGreg Roach $this->menuMyPages($tree), 69ade503dfSGreg Roach $this->menuThemes(), 70ade503dfSGreg Roach $this->menuPalette(), 71ade503dfSGreg Roach $this->menuLanguages(), 72ade503dfSGreg Roach $this->menuLogin(), 73ade503dfSGreg Roach $this->menuLogout(), 74ade503dfSGreg Roach ]); 75ade503dfSGreg Roach } 76ade503dfSGreg Roach 77ade503dfSGreg Roach /** 78*7619f373SGreg Roach * Switch to a new palette 79*7619f373SGreg Roach * 80*7619f373SGreg Roach * @param ServerRequestInterface $request 81*7619f373SGreg Roach * 82*7619f373SGreg Roach * @return ResponseInterface 83*7619f373SGreg Roach */ 84*7619f373SGreg Roach public function postPaletteAction(ServerRequestInterface $request): ResponseInterface 85*7619f373SGreg Roach { 86*7619f373SGreg Roach $user = $request->getAttribute('user'); 87*7619f373SGreg Roach assert($user instanceof UserInterface, new InvalidArgumentException()); 88*7619f373SGreg Roach 89*7619f373SGreg Roach $palette = $request->getQueryParams()['palette']; 90*7619f373SGreg Roach assert(array_key_exists($palette, $this->palettes()), new InvalidArgumentException()); 91*7619f373SGreg Roach 92*7619f373SGreg Roach $user->setPreference('themecolor', $palette); 93*7619f373SGreg Roach 94*7619f373SGreg Roach // If we are the admin, then use our selection as the site default. 95*7619f373SGreg Roach if (Auth::isAdmin($user)) { 96*7619f373SGreg Roach Site::setPreference('DEFAULT_COLOR_PALETTE', $palette); 97*7619f373SGreg Roach } 98*7619f373SGreg Roach 99*7619f373SGreg Roach Session::put('palette', $palette); 100*7619f373SGreg Roach 101*7619f373SGreg Roach return response(); 102*7619f373SGreg Roach } 103*7619f373SGreg Roach 104*7619f373SGreg Roach /** 105*7619f373SGreg Roach * A list of CSS files to include for this page. 106*7619f373SGreg Roach * 107*7619f373SGreg Roach * @return string[] 108*7619f373SGreg Roach */ 109*7619f373SGreg Roach public function stylesheets(): array 110*7619f373SGreg Roach { 111*7619f373SGreg Roach return [ 112*7619f373SGreg Roach asset('css/colors.min.css'), 113*7619f373SGreg Roach asset('css/colors/' . $this->palette() . '.min.css'), 114*7619f373SGreg Roach ]; 115*7619f373SGreg Roach } 116*7619f373SGreg Roach 117*7619f373SGreg Roach /** 118ade503dfSGreg Roach * Create a menu of palette options 119ade503dfSGreg Roach * 12077a20107SGreg Roach * @return Menu 121ade503dfSGreg Roach */ 122*7619f373SGreg Roach protected function menuPalette(): Menu 123ade503dfSGreg Roach { 124ade503dfSGreg Roach /* I18N: A colour scheme */ 125ade503dfSGreg Roach $menu = new Menu(I18N::translate('Palette'), '#', 'menu-color'); 126ade503dfSGreg Roach 127*7619f373SGreg Roach $palette = $this->palette(); 128ade503dfSGreg Roach 129*7619f373SGreg Roach foreach ($this->palettes() as $palette_id => $palette_name) { 130*7619f373SGreg Roach $url = route('module', ['module' => $this->name(), 'action' => 'Palette', 'palette' => $palette_id]); 131*7619f373SGreg Roach 132*7619f373SGreg Roach $submenu = new Menu( 133ade503dfSGreg Roach $palette_name, 134ade503dfSGreg Roach '#', 135*7619f373SGreg Roach 'menu-color-' . $palette_id . ($palette === $palette_id ? ' active' : ''), 136ade503dfSGreg Roach [ 137*7619f373SGreg Roach 'data-post-url' => $url, 138ade503dfSGreg Roach ] 139*7619f373SGreg Roach ); 140*7619f373SGreg Roach 141*7619f373SGreg Roach $menu->addSubmenu($submenu); 142ade503dfSGreg Roach } 143ade503dfSGreg Roach 144ade503dfSGreg Roach return $menu; 145ade503dfSGreg Roach } 146ade503dfSGreg Roach 147ade503dfSGreg Roach /** 1481b40e7bcSGreg Roach * @return string[] 1491b40e7bcSGreg Roach */ 150f6f7bcffSGreg Roach private function palettes(): array 151f6f7bcffSGreg Roach { 1521b40e7bcSGreg Roach $palettes = [ 1531b40e7bcSGreg Roach /* I18N: The name of a colour-scheme */ 1541b40e7bcSGreg Roach 'aquamarine' => I18N::translate('Aqua Marine'), 1551b40e7bcSGreg Roach /* I18N: The name of a colour-scheme */ 1561b40e7bcSGreg Roach 'ash' => I18N::translate('Ash'), 1571b40e7bcSGreg Roach /* I18N: The name of a colour-scheme */ 1581b40e7bcSGreg Roach 'belgianchocolate' => I18N::translate('Belgian Chocolate'), 1591b40e7bcSGreg Roach /* I18N: The name of a colour-scheme */ 1601b40e7bcSGreg Roach 'bluelagoon' => I18N::translate('Blue Lagoon'), 1611b40e7bcSGreg Roach /* I18N: The name of a colour-scheme */ 1621b40e7bcSGreg Roach 'bluemarine' => I18N::translate('Blue Marine'), 1631b40e7bcSGreg Roach /* I18N: The name of a colour-scheme */ 1641b40e7bcSGreg Roach 'coffeeandcream' => I18N::translate('Coffee and Cream'), 1651b40e7bcSGreg Roach /* I18N: The name of a colour-scheme */ 1661b40e7bcSGreg Roach 'coldday' => I18N::translate('Cold Day'), 1671b40e7bcSGreg Roach /* I18N: The name of a colour-scheme */ 1681b40e7bcSGreg Roach 'greenbeam' => I18N::translate('Green Beam'), 1691b40e7bcSGreg Roach /* I18N: The name of a colour-scheme */ 1701b40e7bcSGreg Roach 'mediterranio' => I18N::translate('Mediterranio'), 1711b40e7bcSGreg Roach /* I18N: The name of a colour-scheme */ 1721b40e7bcSGreg Roach 'mercury' => I18N::translate('Mercury'), 1731b40e7bcSGreg Roach /* I18N: The name of a colour-scheme */ 1741b40e7bcSGreg Roach 'nocturnal' => I18N::translate('Nocturnal'), 1751b40e7bcSGreg Roach /* I18N: The name of a colour-scheme */ 1761b40e7bcSGreg Roach 'olivia' => I18N::translate('Olivia'), 1771b40e7bcSGreg Roach /* I18N: The name of a colour-scheme */ 1781b40e7bcSGreg Roach 'pinkplastic' => I18N::translate('Pink Plastic'), 1791b40e7bcSGreg Roach /* I18N: The name of a colour-scheme */ 1801b40e7bcSGreg Roach 'sage' => I18N::translate('Sage'), 1811b40e7bcSGreg Roach /* I18N: The name of a colour-scheme */ 1821b40e7bcSGreg Roach 'shinytomato' => I18N::translate('Shiny Tomato'), 1831b40e7bcSGreg Roach /* I18N: The name of a colour-scheme */ 1841b40e7bcSGreg Roach 'tealtop' => I18N::translate('Teal Top'), 1851b40e7bcSGreg Roach ]; 1861b40e7bcSGreg Roach 1871b40e7bcSGreg Roach uasort($palettes, '\Fisharebest\Webtrees\I18N::strcasecmp'); 1881b40e7bcSGreg Roach 1891b40e7bcSGreg Roach return $palettes; 1901b40e7bcSGreg Roach } 1916ccdf4f0SGreg Roach 1926ccdf4f0SGreg Roach /** 1936ccdf4f0SGreg Roach * @return string 1946ccdf4f0SGreg Roach */ 1956ccdf4f0SGreg Roach private function palette(): string 1966ccdf4f0SGreg Roach { 1976ccdf4f0SGreg Roach // If we are logged in, use our preference 198*7619f373SGreg Roach $palette = Auth::user()->getPreference('themecolor', ''); 1996ccdf4f0SGreg Roach 200*7619f373SGreg Roach // If not logged in or no preference, use one we selected earlier in the session. 201*7619f373SGreg Roach if ($palette === '') { 202*7619f373SGreg Roach $palette = Session::get('palette', ''); 2036ccdf4f0SGreg Roach } 2046ccdf4f0SGreg Roach 2056ccdf4f0SGreg Roach // We haven't selected one this session? Use the site default 206*7619f373SGreg Roach if ($palette === '') { 207*7619f373SGreg Roach $palette = Site::getPreference('DEFAULT_COLOR_PALETTE', self::DEFAULT_PALETTE); 2086ccdf4f0SGreg Roach } 2096ccdf4f0SGreg Roach 2106ccdf4f0SGreg Roach return $palette; 2116ccdf4f0SGreg Roach } 212ade503dfSGreg Roach} 213