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