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 */
16namespace Fisharebest\Webtrees\Theme;
17
18use Fisharebest\Webtrees\Auth;
19use Fisharebest\Webtrees\Functions\Functions;
20use Fisharebest\Webtrees\I18N;
21use Fisharebest\Webtrees\Menu;
22use Fisharebest\Webtrees\Session;
23use Fisharebest\Webtrees\Site;
24
25/**
26 * The colors theme.
27 */
28class ColorsTheme extends CloudsTheme implements ThemeInterface
29{
30    /** @var string[] A list of color palettes */
31    protected $palettes;
32
33    /** @var string Which of the color palettes to use on this page */
34    protected $palette;
35
36    /**
37     * Where are our CSS, JS and other assets?
38     *
39     * @return string A relative path, such as "themes/foo/"
40     */
41    public function assetUrl()
42    {
43        return 'themes/colors/css-1.7.8/';
44    }
45
46    /**
47     * Add markup to the secondary menu.
48     *
49     * @return string
50     */
51    protected function formatSecondaryMenu()
52    {
53        return
54            '<ul class="secondary-menu">' .
55            implode('', $this->secondaryMenu()) .
56            '<li>' .
57            $this->formQuickSearch() .
58            '</li>' .
59            '</ul>';
60    }
61
62    /**
63     * Create the contents of the <header> tag.
64     *
65     * @return string
66     */
67    protected function headerContent()
68    {
69        return
70            //$this->accessibilityLinks() .
71            $this->formatTreeTitle() .
72            $this->formatSecondaryMenu();
73    }
74
75    /**
76     * Create resources for the colors theme.
77     */
78    public function hookAfterInit()
79    {
80        $this->palettes = array(
81            'aquamarine'       => /* I18N: The name of a colour-scheme */ I18N::translate('Aqua Marine'),
82            'ash'              => /* I18N: The name of a colour-scheme */ I18N::translate('Ash'),
83            'belgianchocolate' => /* I18N: The name of a colour-scheme */ I18N::translate('Belgian Chocolate'),
84            'bluelagoon'       => /* I18N: The name of a colour-scheme */ I18N::translate('Blue Lagoon'),
85            'bluemarine'       => /* I18N: The name of a colour-scheme */ I18N::translate('Blue Marine'),
86            'coffeeandcream'   => /* I18N: The name of a colour-scheme */ I18N::translate('Coffee and Cream'),
87            'coldday'          => /* I18N: The name of a colour-scheme */ I18N::translate('Cold Day'),
88            'greenbeam'        => /* I18N: The name of a colour-scheme */ I18N::translate('Green Beam'),
89            'mediterranio'     => /* I18N: The name of a colour-scheme */ I18N::translate('Mediterranio'),
90            'mercury'          => /* I18N: The name of a colour-scheme */ I18N::translate('Mercury'),
91            'nocturnal'        => /* I18N: The name of a colour-scheme */ I18N::translate('Nocturnal'),
92            'olivia'           => /* I18N: The name of a colour-scheme */ I18N::translate('Olivia'),
93            'pinkplastic'      => /* I18N: The name of a colour-scheme */ I18N::translate('Pink Plastic'),
94            'sage'             => /* I18N: The name of a colour-scheme */ I18N::translate('Sage'),
95            'shinytomato'      => /* I18N: The name of a colour-scheme */ I18N::translate('Shiny Tomato'),
96            'tealtop'          => /* I18N: The name of a colour-scheme */ I18N::translate('Teal Top'),
97        );
98        uasort($this->palettes, '\Fisharebest\Webtrees\I18N::strcasecmp');
99
100        // If we've selected a new palette, and we are logged in, set this value as a default.
101        if (isset($_GET['themecolor']) && array_key_exists($_GET['themecolor'], $this->palettes)) {
102            // Request to change color
103            $this->palette = $_GET['themecolor'];
104            Auth::user()->setPreference('themecolor', $this->palette);
105            if (Auth::isAdmin()) {
106                Site::setPreference('DEFAULT_COLOR_PALETTE', $this->palette);
107            }
108            unset($_GET['themecolor']);
109            // Rember that we have selected a value
110            Session::put('subColor', $this->palette);
111        }
112        // If we are logged in, use our preference
113        $this->palette = Auth::user()->getPreference('themecolor');
114        // If not logged in or no preference, use one we selected earlier in the session?
115        if (!$this->palette) {
116            $this->palette = Session::get('subColor');
117        }
118        // We haven't selected one this session? Use the site default
119        if (!$this->palette) {
120            $this->palette = Site::getPreference('DEFAULT_COLOR_PALETTE');
121        }
122        // Make sure our selected palette actually exists
123        if (!array_key_exists($this->palette, $this->palettes)) {
124            $this->palette = 'ash';
125        }
126    }
127
128    /**
129     * Generate a list of items for the user menu.
130     *
131     * @return Menu[]
132     */
133    protected function secondaryMenu()
134    {
135        return array_filter(array(
136            $this->menuPendingChanges(),
137            $this->menuMyPages(),
138            $this->menuFavorites(),
139            $this->menuThemes(),
140            $this->menuPalette(),
141            $this->menuLanguages(),
142            $this->menuLogin(),
143            $this->menuLogout(),
144        ));
145    }
146
147    /**
148     * Create a menu of palette options
149     *
150     * @return Menu
151     */
152    protected function menuPalette()
153    {
154        if ($this->tree && Site::getPreference('ALLOW_USER_THEMES') && $this->tree->getPreference('ALLOW_THEME_DROPDOWN')) {
155            $menu = new Menu(/* I18N: A colour scheme */
156                I18N::translate('Palette'), '#', 'menu-color');
157
158            foreach ($this->palettes as $palette_id => $palette_name) {
159                $menu->addSubmenu(new Menu(
160                    $palette_name,
161                    '#',
162                    'menu-color-' . $palette_id . ($this->palette === $palette_id ? ' active' : ''),
163                    array(
164                        'onclick' => 'document.location=\'' . Functions::getQueryUrl(array('themecolor' => $palette_id), '&') . '\'',
165                    )
166                ));
167            }
168
169            return $menu;
170        } else {
171            return null;
172        }
173    }
174
175    /**
176     * A list of CSS files to include for this page.
177     *
178     * @return string[]
179     */
180    protected function stylesheets()
181    {
182        return array(
183            'themes/colors/jquery-ui-1.11.2/jquery-ui.css',
184            $this->assetUrl() . 'style.css',
185            $this->assetUrl() . 'palette/' . $this->palette . '.css',
186        );
187    }
188
189    /**
190     * A fixed string to identify this theme, in settings, etc.
191     *
192     * @return string
193     */
194    public function themeId()
195    {
196        return 'colors';
197    }
198
199    /**
200     * What is this theme called?
201     *
202     * @return string
203     */
204    public function themeName()
205    {
206        return /* I18N: Name of a theme. */ I18N::translate('colors');
207    }
208}
209