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