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