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 labelled on tabs, menus, 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 /* I18N: A colour scheme */ 73 $menu = new Menu(I18N::translate('Palette'), '#', 'menu-color'); 74 75 foreach ($this->palettes() as $palette_id => $palette_name) { 76 $url = $this->request->getRequestUri(); 77 $url = preg_replace('/&themecolor=[a-z]+/', '', $url); 78 $url .= '&themecolor=' . $palette_id; 79 80 $menu->addSubmenu(new Menu( 81 $palette_name, 82 '#', 83 'menu-color-' . $palette_id . ($this->palette() === $palette_id ? ' active' : ''), 84 [ 85 'onclick' => 'document.location=\'' . $url . '\'', 86 ] 87 )); 88 } 89 90 return $menu; 91 } 92 93 /** 94 * A list of CSS files to include for this page. 95 * 96 * @return string[] 97 */ 98 public function stylesheets(): array 99 { 100 return [ 101 asset('css/colors.min.css'), 102 asset('css/colors/' . $this->palette() . '.min.css'), 103 ]; 104 } 105 106 /** 107 * @return string 108 */ 109 private function palette(): string { 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 $palettes = [ 151 /* I18N: The name of a colour-scheme */ 152 'aquamarine' => I18N::translate('Aqua Marine'), 153 /* I18N: The name of a colour-scheme */ 154 'ash' => I18N::translate('Ash'), 155 /* I18N: The name of a colour-scheme */ 156 'belgianchocolate' => I18N::translate('Belgian Chocolate'), 157 /* I18N: The name of a colour-scheme */ 158 'bluelagoon' => I18N::translate('Blue Lagoon'), 159 /* I18N: The name of a colour-scheme */ 160 'bluemarine' => I18N::translate('Blue Marine'), 161 /* I18N: The name of a colour-scheme */ 162 'coffeeandcream' => I18N::translate('Coffee and Cream'), 163 /* I18N: The name of a colour-scheme */ 164 'coldday' => I18N::translate('Cold Day'), 165 /* I18N: The name of a colour-scheme */ 166 'greenbeam' => I18N::translate('Green Beam'), 167 /* I18N: The name of a colour-scheme */ 168 'mediterranio' => I18N::translate('Mediterranio'), 169 /* I18N: The name of a colour-scheme */ 170 'mercury' => I18N::translate('Mercury'), 171 /* I18N: The name of a colour-scheme */ 172 'nocturnal' => I18N::translate('Nocturnal'), 173 /* I18N: The name of a colour-scheme */ 174 'olivia' => I18N::translate('Olivia'), 175 /* I18N: The name of a colour-scheme */ 176 'pinkplastic' => I18N::translate('Pink Plastic'), 177 /* I18N: The name of a colour-scheme */ 178 'sage' => I18N::translate('Sage'), 179 /* I18N: The name of a colour-scheme */ 180 'shinytomato' => I18N::translate('Shiny Tomato'), 181 /* I18N: The name of a colour-scheme */ 182 'tealtop' => I18N::translate('Teal Top'), 183 ]; 184 185 uasort($palettes, '\Fisharebest\Webtrees\I18N::strcasecmp'); 186 187 return $palettes; 188 } 189} 190