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