1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Themes class file |
4
|
|
|
* |
5
|
|
|
* @package EBloodBank |
6
|
|
|
* @since 1.3 |
7
|
|
|
*/ |
8
|
|
|
namespace EBloodBank; |
9
|
|
|
|
10
|
|
|
use DirectoryIterator; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Themes class |
14
|
|
|
* |
15
|
|
|
* @since 1.3 |
16
|
|
|
*/ |
17
|
|
|
class Themes |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* The default theme. |
21
|
|
|
* |
22
|
|
|
* @var Theme |
23
|
|
|
* @since 1.3 |
24
|
|
|
* @static |
25
|
|
|
*/ |
26
|
|
|
protected static $defaultTheme; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The current theme. |
30
|
|
|
* |
31
|
|
|
* @var Theme |
32
|
|
|
* @since 1.3 |
33
|
|
|
* @static |
34
|
|
|
*/ |
35
|
|
|
protected static $currentTheme; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @access private |
39
|
|
|
* @since 1.3 |
40
|
|
|
*/ |
41
|
|
|
private function __construct() |
42
|
|
|
{ |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get the default theme. |
47
|
|
|
* |
48
|
|
|
* @return Theme |
49
|
|
|
* @since 1.3 |
50
|
|
|
* @static |
51
|
|
|
*/ |
52
|
|
|
public static function getDefaultTheme() |
53
|
|
|
{ |
54
|
|
|
return self::$defaultTheme; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get the current theme, or fallback to the default theme if no theme is active. |
59
|
|
|
* |
60
|
|
|
* @return Theme |
61
|
|
|
* @since 1.3 |
62
|
|
|
* @static |
63
|
|
|
*/ |
64
|
|
|
public static function getCurrentTheme() |
65
|
|
|
{ |
66
|
|
|
if (empty(self::$currentTheme)) { |
67
|
|
|
return self::$defaultTheme; |
68
|
|
|
} else { |
69
|
|
|
return self::$currentTheme; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Whether the given theme is the default. |
75
|
|
|
* |
76
|
|
|
* @return bool |
77
|
|
|
* @since 1.3 |
78
|
|
|
* @static |
79
|
|
|
*/ |
80
|
|
|
public static function isDefaultTheme(Theme $theme) |
81
|
|
|
{ |
82
|
|
|
$defaultTheme = self::getDefaultTheme(); |
83
|
|
|
|
84
|
|
|
if (empty($defaultTheme)) { |
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return ($defaultTheme->getName() === $theme->getName()); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Whether the given theme is the current. |
93
|
|
|
* |
94
|
|
|
* @return bool |
95
|
|
|
* @since 1.3 |
96
|
|
|
* @static |
97
|
|
|
*/ |
98
|
|
|
public static function isCurrentTheme(Theme $theme) |
99
|
|
|
{ |
100
|
|
|
$currentTheme = self::getCurrentTheme(); |
101
|
|
|
|
102
|
|
|
if (empty($currentTheme)) { |
103
|
|
|
return false; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return ($currentTheme->getName() === $theme->getName()); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Set the default theme. |
111
|
|
|
* |
112
|
|
|
* @return void |
113
|
|
|
* @since 1.3 |
114
|
|
|
* @static |
115
|
|
|
*/ |
116
|
|
|
public static function setDefaultTheme(Theme $theme) |
117
|
|
|
{ |
118
|
|
|
self::$defaultTheme = $theme; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Set the current theme. |
123
|
|
|
* |
124
|
|
|
* @return void |
125
|
|
|
* @since 1.3 |
126
|
|
|
* @static |
127
|
|
|
*/ |
128
|
|
|
public static function setCurrentTheme(Theme $theme) |
129
|
|
|
{ |
130
|
|
|
self::$currentTheme = $theme; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Find a theme. |
135
|
|
|
* |
136
|
|
|
* @return Theme |
137
|
|
|
* @since 1.3 |
138
|
|
|
* @static |
139
|
|
|
*/ |
140
|
|
|
public static function findTheme($name) |
141
|
|
|
{ |
142
|
|
|
if (empty($name)) { |
143
|
|
|
return; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$themesDir = EBB_THEMES_DIR; |
147
|
|
|
$themePath = realpath("{$themesDir}/{$name}"); |
148
|
|
|
|
149
|
|
|
if (! $themePath || ! is_dir($themePath)) { |
150
|
|
|
return; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
if (dirname($themePath) !== realpath($themesDir)) { |
154
|
|
|
return; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return new Theme($name, $themePath); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Get the available themes. |
162
|
|
|
* |
163
|
|
|
* @return Theme[] |
164
|
|
|
* @since 1.3 |
165
|
|
|
* @static |
166
|
|
|
*/ |
167
|
|
|
public static function getAvailableThemes() |
168
|
|
|
{ |
169
|
|
|
$themes = []; |
170
|
|
|
|
171
|
|
|
foreach (new DirectoryIterator(EBB_THEMES_DIR) as $themeDir) { |
172
|
|
|
if (! $themeDir->isDot() && $themeDir->isDir()) { |
173
|
|
|
$themeName = $themeDir->getFilename(); |
174
|
|
|
$themeDirPath = $themeDir->getRealPath(); |
175
|
|
|
$themes[$themeName] = new Theme($themeName, $themeDirPath); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return $themes; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|