Completed
Push — develop ( c314ec...310a97 )
by Greg
13:42
created

ColorsTheme::menuPalette()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 18
rs 8.8571
c 1
b 1
f 0
cc 6
eloc 12
nc 3
nop 0
1
<?php
2
/**
3
 * webtrees: online genealogy
4
 * Copyright (C) 2015 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
 */
16
namespace Fisharebest\Webtrees\Theme;
17
18
use Fisharebest\Webtrees\Auth;
19
use Fisharebest\Webtrees\Functions\Functions;
20
use Fisharebest\Webtrees\I18N;
21
use Fisharebest\Webtrees\Menu;
22
use Fisharebest\Webtrees\Session;
23
use Fisharebest\Webtrees\Site;
24
25
/**
26
 * The colors theme.
27
 */
28
class ColorsTheme extends CloudsTheme implements ThemeInterface {
29
	/** @var string[] A list of color palettes */
30
	protected $palettes;
31
32
	/** @var string Which of the color palettes to use on this page */
33
	protected $palette;
34
35
	/**
36
	 * Where are our CSS, JS and other assets?
37
	 *
38
	 * @return string A relative path, such as "themes/foo/"
39
	 */
40
	public function assetUrl() {
41
		return 'themes/colors/css-1.7.0/';
42
	}
43
44
	/**
45
	 * Add markup to the secondary menu.
46
	 *
47
	 * @return string
48
	 */
49 View Code Duplication
	protected function formatSecondaryMenu() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
		return
51
			'<ul class="secondary-menu">' .
52
			implode('', $this->secondaryMenu()) .
53
			'<li>' .
54
			$this->formQuickSearch() .
55
			'</li>' .
56
			'</ul>';
57
	}
58
59
	/**
60
	 * Create the contents of the <header> tag.
61
	 *
62
	 * @return string
63
	 */
64
	protected function headerContent() {
65
		return
66
			//$this->accessibilityLinks() .
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
67
			$this->formatTreeTitle() .
68
			$this->formatSecondaryMenu();
69
	}
70
71
	/**
72
	 * Create resources for the colors theme.
73
	 */
74
	public function hookAfterInit() {
0 ignored issues
show
Coding Style introduced by
hookAfterInit uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
75
		$this->palettes = array(
0 ignored issues
show
Documentation Bug introduced by
It seems like array('aquamarine' => \F...:translate('Teal Top')) of type array<string,string,{"aq...g","tealtop":"string"}> is incompatible with the declared type array<integer,string> of property $palettes.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
76
			'aquamarine'       => /* I18N: The name of a colour-scheme */ I18N::translate('Aqua Marine'),
77
			'ash'              => /* I18N: The name of a colour-scheme */ I18N::translate('Ash'),
78
			'belgianchocolate' => /* I18N: The name of a colour-scheme */ I18N::translate('Belgian Chocolate'),
79
			'bluelagoon'       => /* I18N: The name of a colour-scheme */ I18N::translate('Blue Lagoon'),
80
			'bluemarine'       => /* I18N: The name of a colour-scheme */ I18N::translate('Blue Marine'),
81
			'coffeeandcream'   => /* I18N: The name of a colour-scheme */ I18N::translate('Coffee and Cream'),
82
			'coldday'          => /* I18N: The name of a colour-scheme */ I18N::translate('Cold Day'),
83
			'greenbeam'        => /* I18N: The name of a colour-scheme */ I18N::translate('Green Beam'),
84
			'mediterranio'     => /* I18N: The name of a colour-scheme */ I18N::translate('Mediterranio'),
85
			'mercury'          => /* I18N: The name of a colour-scheme */ I18N::translate('Mercury'),
86
			'nocturnal'        => /* I18N: The name of a colour-scheme */ I18N::translate('Nocturnal'),
87
			'olivia'           => /* I18N: The name of a colour-scheme */ I18N::translate('Olivia'),
88
			'pinkplastic'      => /* I18N: The name of a colour-scheme */ I18N::translate('Pink Plastic'),
89
			'sage'             => /* I18N: The name of a colour-scheme */ I18N::translate('Sage'),
90
			'shinytomato'      => /* I18N: The name of a colour-scheme */ I18N::translate('Shiny Tomato'),
91
			'tealtop'          => /* I18N: The name of a colour-scheme */ I18N::translate('Teal Top'),
92
		);
93
		uasort($this->palettes, '\Fisharebest\Webtrees\I18N::strcasecmp');
94
95
		// If we've selected a new palette, and we are logged in, set this value as a default.
96
		if (isset($_GET['themecolor']) && array_key_exists($_GET['themecolor'], $this->palettes)) {
97
			// Request to change color
98
			$this->palette = $_GET['themecolor'];
99
			Auth::user()->setPreference('themecolor', $this->palette);
100
			if (Auth::isAdmin()) {
101
				Site::setPreference('DEFAULT_COLOR_PALETTE', $this->palette);
102
			}
103
			unset($_GET['themecolor']);
104
			// Rember that we have selected a value
105
			Session::put('subColor', $this->palette);
106
		}
107
		// If we are logged in, use our preference
108
		$this->palette = Auth::user()->getPreference('themecolor');
109
		// If not logged in or no preference, use one we selected earlier in the session?
110
		if (!$this->palette) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->palette of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
111
			$this->palette = Session::get('subColor');
112
		}
113
		// We haven't selected one this session? Use the site default
114
		if (!$this->palette) {
115
			$this->palette = Site::getPreference('DEFAULT_COLOR_PALETTE');
116
		}
117
		// Make sure our selected palette actually exists
118
		if (!array_key_exists($this->palette, $this->palettes)) {
119
			$this->palette = 'ash';
120
		}
121
	}
122
123
	/**
124
	 * Generate a list of items for the user menu.
125
	 *
126
	 * @return Menu[]
127
	 */
128 View Code Duplication
	protected function secondaryMenu() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
129
		return array_filter(array(
130
			$this->menuPendingChanges(),
131
			$this->menuMyPages(),
132
			$this->menuFavorites(),
133
			$this->menuThemes(),
134
			$this->menuPalette(),
135
			$this->menuLanguages(),
136
			$this->menuLogin(),
137
			$this->menuLogout(),
138
		));
139
	}
140
141
	/**
142
	 * Create a menu of palette options
143
	 *
144
	 * @return Menu
0 ignored issues
show
Documentation introduced by
Should the return type not be Menu|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
145
	 */
146
	protected function menuPalette() {
147
		if ($this->tree && Site::getPreference('ALLOW_USER_THEMES') && $this->tree->getPreference('ALLOW_THEME_DROPDOWN')) {
0 ignored issues
show
Bug Best Practice introduced by
The expression \Fisharebest\Webtrees\Si...ce('ALLOW_USER_THEMES') of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
Bug Best Practice introduced by
The expression $this->tree->getPreferen...'ALLOW_THEME_DROPDOWN') of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
148
			$menu = new Menu(/* I18N: A colour scheme */
149
				I18N::translate('Palette'), '#', 'menu-color');
150
151
			foreach ($this->palettes as $palette_id => $palette_name) {
152
				$menu->addSubmenu(new Menu(
153
					$palette_name,
154
					Functions::getQueryUrl(array('themecolor' => $palette_id), '&amp;'),
155
					'menu-color-' . $palette_id . ($this->palette === $palette_id ? ' active' : '')
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison === seems to always evaluate to false as the types of $this->palette (string) and $palette_id (integer) can never be identical. Maybe you want to use a loose comparison == instead?
Loading history...
156
				));
157
			}
158
159
			return $menu;
160
		} else {
161
			return null;
162
		}
163
	}
164
165
	/**
166
	 * A list of CSS files to include for this page.
167
	 *
168
	 * @return string[]
169
	 */
170
	protected function stylesheets() {
171
		return array(
172
			'themes/colors/jquery-ui-1.11.2/jquery-ui.css',
173
			$this->assetUrl() . 'style.css',
174
			$this->assetUrl() . 'palette/' . $this->palette . '.css',
175
		);
176
	}
177
178
	/**
179
	 * A fixed string to identify this theme, in settings, etc.
180
	 *
181
	 * @return string
182
	 */
183
	public function themeId() {
184
		return 'colors';
185
	}
186
187
	/**
188
	 * What is this theme called?
189
	 *
190
	 * @return string
191
	 */
192
	public function themeName() {
193
		return /* I18N: Name of a theme. */ I18N::translate('colors');
194
	}
195
}
196