/
txplib_theme.php
300 lines (263 loc) · 6.41 KB
/
txplib_theme.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?php
/*
$HeadURL$
$LastChangedRevision$
*/
if (!defined ('THEME')) define('THEME', 'theme/');
class theme
{
var $name, $menu, $url, $is_popup, $message;
//----------------------------------------
// Theme engine methods
//----------------------------------------
/**
* Constructor
* @param string $name Theme name
*/
function theme($name)
{
$this->name = $name;
$this->menu = array();
$this->url = THEME.rawurlencode($name).'/';
$this->is_popup = false;
$this->message = '';
}
/**
* Get a theme's source path
* @param string $name Theme name
* @return string Source file path for named theme
*/
static function path($name)
{
return txpath.DS.THEME.$name.DS.$name.'.php';
}
/**
* Theme factory
* @param string $name Theme name
* @return object|boolean An initialised theme object, or false on failure
*/
static function factory($name)
{
$path = theme::path($name);
if (is_readable($path))
{
require_once($path);
}
else
{
return false;
}
$t = "{$name}_theme";
if (class_exists($t))
{
return new $t($name);
}
else
{
return false;
}
}
/**
* Initialise the theme singleton
* @param string $name Theme name
* @return object A valid theme object
*/
static function init($name = '')
{
static $instance;
if ($name === '')
{
$name = pluggable_ui('admin_side', 'theme_name', get_pref('theme_name', 'hive'));
}
if ($instance && is_object($instance) && ($name == $instance->name))
{
return $instance;
}
else
{
$instance = null;
}
$instance = theme::factory($name);
if (!$instance)
{
set_pref('theme_name', 'hive');
die(gTxt('cannot_instantiate_theme', array('{name}' => $name, '{class}' => "{$name}_theme", '{path}' => theme::path($name))));
}
return $instance;
}
/**
* Get a list of all theme names
* @return array Alphabetically sorted array of all available theme names
*/
static function names()
{
$dirs = glob(txpath.DS.THEME.'*');
if (is_array($dirs))
{
foreach ($dirs as $d)
{
// extract trailing directory name
preg_match('#(.*)[\\/]+(.*)$#', $d, $m);
$name = $m[2];
// accept directories containing an equally named .php file
if (is_dir($d) && ($d != '.') && ($d != '..') && isset($name) && is_file($d.DS.$name.'.php'))
{
$out[] = $name;
}
}
sort($out, SORT_STRING);
return $out;
}
else
return array();
}
/**
* Inherit from an ancestor theme
* @param string $name Name of ancestor theme
* @return boolean True on success, false on unavailable/invalid ancestor theme
*/
static function based_on($name)
{
global $production_status;
$theme = theme::factory($name);
if (!$theme)
{
set_pref('theme_name', 'hive');
if ($production_status === 'debug')
{
echo gTxt('cannot_instantiate_theme', array('{name}' => $name, '{class}' => "{$name}_theme", '{path}' => theme::path($name)));
}
return false;
}
return true;
}
/**
* Sets Textpatterns menu structure, message contents and other application states
* @param string $area Currently active top level menu
* @param string $event Currently active second level menu
* @param boolean $is_popup Just a popup window for tag builder et cetera
* @param array $message The contents of the notification message pane
* @return object This theme object
*/
function set_state($area, $event, $is_popup, $message)
{
$this->is_popup = $is_popup;
$this->message = $message;
if ($is_popup) return $this;
// use legacy areas() for b/c
$areas = areas();
$defaults = array(
'content' => 'article',
'presentation' => 'page',
'admin' => 'admin'
);
if(empty($areas['start']))
{
unset($areas['start']);
}
if(empty($areas['extensions']))
{
unset($areas['extensions']);
}
$dflt_tab = get_pref('default_event', '');
foreach ($areas as $ar => $items)
{
$l_ = gTxt('tab_'.$ar);
$e_ = (array_key_exists($ar,$defaults)) ? $defaults[$ar] : reset($areas[$ar]);
$i_ = array();
if (has_privs('tab.'.$ar))
{
if (!has_privs($e_))
{
$e_ = '';
}
foreach ($items as $a => $b)
{
if (has_privs($b))
{
if ($e_ === '') {
$e_ = $b;
}
if ($b == $dflt_tab)
{
$this->menu[$ar]['event'] = $dflt_tab;
}
$i_[] = array('label' => $a, 'event' => $b, 'active' => ($b == $event));
}
}
if ($e_)
{
$this->menu[$ar] = array(
'label' => $l_,
'event' => $e_,
'active' => ($ar == $area),
'items' => $i_,
);
}
}
}
return $this;
}
//----------------------------------------
// Overrideable methods for custom themes
//----------------------------------------
/**
* Output HEAD element contents. Returned value is rendered into the HEAD element of all admin side pages by core.
* @return string
*/
function html_head()
{
trigger_error(__FUNCTION__.' is abstract.', E_USER_ERROR);
}
/**
* Draw the theme's header
* @return string
*/
function header()
{
trigger_error(__FUNCTION__.' is abstract.', E_USER_ERROR);
}
/**
* Draw the theme's footer
* @return string
*/
function footer()
{
trigger_error(__FUNCTION__.' is abstract.', E_USER_ERROR);
}
/**
* Output notification message for synchronous HTML views
* @param array $thing Message text and status flag
* @param boolean $modal true: Immediate user interaction suggested
*/
function announce($thing=array('', 0), $modal = false)
{
trigger_error(__FUNCTION__.' is abstract.', E_USER_ERROR);
}
/**
* Output notification message for asynchronous Javascript views
* @param array $thing Message text and status flag
* @param boolean $modal true: Immediate user interaction suggested
* @since 4.5.0
*/
function announce_async($thing=array('', 0), $modal = false)
{
trigger_error(__FUNCTION__.' is abstract.', E_USER_ERROR);
}
/**
* Define bureaucratic details of this theme. All returned items are optional.
* @return array
*/
function manifest()
{
return array(
'title' => '', // Human-readable title of this theme. No HTML, keep it short.
'author' => '', // Name(s) of this theme's creator(s).
'author_uri' => '', // URI of the theme's site. Decent vanity is accepted.
'version' => '', // Version numbering. Mind version_compare().
'description' => '', // Human readable short description. No HTML.
'help' => '', // URI of the theme's help and docs. Strictly optional.
);
}
}
?>