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 { 111 $palettes = $this->palettes(); 112 113 // If we've selected a new palette, and we are logged in, set this value as a default. 114 if (isset($_GET['themecolor'])) { 115 // Request to change color 116 $palette = $_GET['themecolor']; 117 Auth::user()->setPreference('themecolor', $palette); 118 if (Auth::isAdmin()) { 119 Site::setPreference('DEFAULT_COLOR_PALETTE', $palette); 120 } 121 unset($_GET['themecolor']); 122 // Rember that we have selected a value 123 Session::put('subColor', $palette); 124 } 125 126 // If we are logged in, use our preference 127 $palette = Auth::user()->getPreference('themecolor'); 128 129 // If not logged in or no preference, use one we selected earlier in the session? 130 if (!$palette) { 131 $palette = Session::get('subColor'); 132 } 133 134 // We haven't selected one this session? Use the site default 135 if (!$palette) { 136 $palette = Site::getPreference('DEFAULT_COLOR_PALETTE'); 137 } 138 139 // Make sure our selected palette actually exists 140 if (!array_key_exists($palette, $palettes)) { 141 $palette = 'ash'; 142 } 143 144 return $palette; 145 } 146 147 /** 148 * @return string[] 149 */ 150 private function palettes(): array 151 { 152 $palettes = [ 153 /* I18N: The name of a colour-scheme */ 154 'aquamarine' => I18N::translate('Aqua Marine'), 155 /* I18N: The name of a colour-scheme */ 156 'ash' => I18N::translate('Ash'), 157 /* I18N: The name of a colour-scheme */ 158 'belgianchocolate' => I18N::translate('Belgian Chocolate'), 159 /* I18N: The name of a colour-scheme */ 160 'bluelagoon' => I18N::translate('Blue Lagoon'), 161 /* I18N: The name of a colour-scheme */ 162 'bluemarine' => I18N::translate('Blue Marine'), 163 /* I18N: The name of a colour-scheme */ 164 'coffeeandcream' => I18N::translate('Coffee and Cream'), 165 /* I18N: The name of a colour-scheme */ 166 'coldday' => I18N::translate('Cold Day'), 167 /* I18N: The name of a colour-scheme */ 168 'greenbeam' => I18N::translate('Green Beam'), 169 /* I18N: The name of a colour-scheme */ 170 'mediterranio' => I18N::translate('Mediterranio'), 171 /* I18N: The name of a colour-scheme */ 172 'mercury' => I18N::translate('Mercury'), 173 /* I18N: The name of a colour-scheme */ 174 'nocturnal' => I18N::translate('Nocturnal'), 175 /* I18N: The name of a colour-scheme */ 176 'olivia' => I18N::translate('Olivia'), 177 /* I18N: The name of a colour-scheme */ 178 'pinkplastic' => I18N::translate('Pink Plastic'), 179 /* I18N: The name of a colour-scheme */ 180 'sage' => I18N::translate('Sage'), 181 /* I18N: The name of a colour-scheme */ 182 'shinytomato' => I18N::translate('Shiny Tomato'), 183 /* I18N: The name of a colour-scheme */ 184 'tealtop' => I18N::translate('Teal Top'), 185 ]; 186 187 uasort($palettes, '\Fisharebest\Webtrees\I18N::strcasecmp'); 188 189 return $palettes; 190 } 191} 192