/
Controller.php
330 lines (304 loc) · 9.44 KB
/
Controller.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
/**
* The plugin controller.
*
* PHP version 5
*
* @category CMSimple_XH
* @package Onepage
* @author Christoph M. Becker <cmbecker69@gmx.de>
* @copyright 2015-2017 Christoph M. Becker <http://3-magi.net>
* @license http://www.gnu.org/licenses/gpl-3.0.en.html GNU GPLv3
* @link http://3-magi.net/?CMSimple_XH/Onepage_XH
*/
/**
* The plugin controller.
*
* @category CMSimple_XH
* @package Onepage
* @author Christoph M. Becker <cmbecker69@gmx.de>
* @license http://www.gnu.org/licenses/gpl-3.0.en.html GNU GPLv3
* @link http://3-magi.net/?CMSimple_XH/Onepage_XH
*/
class Onepage_Controller
{
/**
* Handles plugin related requests.
*
* @return void
*
* @global bool Whether we're in edit mode.
* @global array The configuration of the plugins.
* @global int The requested page.
*/
public static function dispatch()
{
global $edit, $plugin_cf, $s;
if ((!XH_ADM || (!$edit && $s >= 0)) && $plugin_cf['onepage']['use_javascript']) {
self::emitJavaScript();
}
if (XH_ADM) {
if ($edit) {
$template = trim($plugin_cf['onepage']['admin_template']);
if ($template != '') {
self::setTemplate($template);
}
}
if (function_exists('XH_registerStandardPluginMenuItems')) {
XH_registerStandardPluginMenuItems(false);
}
if (self::isAdministrationRequested()) {
self::handleAdministration();
}
} else {
XH_afterPluginLoading(array('Onepage_Controller', 'evaluateScripting'));
}
}
/**
* Emits the JavaScript.
*
* @return void
*
* @global array The paths of system files and folders.
* @global array The URLs of the pages.
* @global string The (X)HTML to append to the body element.
* @global array The configuration of the plugins.
*/
protected static function emitJavaScript()
{
global $pth, $u, $bjs, $plugin_cf;
$pcf = $plugin_cf['onepage'];
$config = array(
'numericUrls' => $pcf['url_numeric'],
'scrollDuration' => $pcf['scroll_duration'],
'scrollEasing' => $pcf['scroll_easing']
);
if (XH_ADM && $pcf['url_numeric']) {
$config['urls'] = array_flip($u);
}
$bjs .= '<script type="text/javascript">/* <![CDATA[ */'
. 'var ONEPAGE = ' . json_encode($config)
. '/* ]]> */</script>'
. '<script type="text/javascript" src="' . $pth['folder']['plugins']
. 'onepage/onepage.js"></script>';
}
/**
* Sets the template.
*
* @param string $template A template name.
*
* @return void
*
* @global array The paths of system files and folders.
*/
protected static function setTemplate($template)
{
global $pth;
$pth['folder']['template'] = $pth['folder']['templates'] . $template . '/';
$pth['file']['template'] = $pth['folder']['template'] . 'template.htm';
$pth['file']['stylesheet'] = $pth['folder']['template'] . 'stylesheet.css';
$pth['folder']['menubuttons'] = $pth['folder']['template'] . 'menu/';
$pth['folder']['templateimages'] = $pth['folder']['template'] . 'images/';
}
/**
* Returns whether the plugin administration is requested.
*
* @return bool
*
* @global string Whether the plugin administration is requested.
*/
protected static function isAdministrationRequested()
{
global $onepage;
return function_exists('XH_wantsPluginAdministration')
&& XH_wantsPluginAdministration('onepage')
|| isset($onepage) && $onepage == 'true';
}
/**
* Handles the plugin administration.
*
* @return void
*
* @global string The value of the <var>admin</var> GP parameter.
* @global string The value of the <var>action</var> GP parameter.
* @global string The (X)HTML fragment to insert at the top of the content.
*/
protected static function handleAdministration()
{
global $admin, $action, $o;
$o .= print_plugin_admin('off');
switch ($admin) {
case '':
$o .= self::render('info');
break;
default:
$o .= plugin_admin_common($action, $admin, 'onepage');
}
}
/**
* Renders a view template.
*
* @param string $template The name of the template.
*
* @return string
*
* @global array The paths of system files and folders.
* @global array The configuration of the core.
*/
protected static function render($template)
{
global $pth, $cf;
$template = $pth['folder']['plugins'] . 'onepage/views/'
. $template . '.php';
ob_start();
include $template;
$o = ob_get_clean();
if (!$cf['xhtml']['endtags']) {
$o = str_replace('/>', '>', $o);
}
return $o;
}
/**
* Returns the localization of a string.
*
* @param string $key The key of the string.
*
* @return string
*
* @global array The localization of the plugins.
*/
protected static function l10n($key)
{
global $plugin_tx;
return $plugin_tx['onepage'][$key];
}
/**
* Returns the path of the plugin logo.
*
* @return string
*
* @global array The paths of system files and folders.
*/
protected static function logoPath()
{
global $pth;
return $pth['folder']['plugins'] . 'onepage/onepage.png';
}
/**
* Evaluates scripting of all visible pages.
*
* @return void
*
* @global array The contents of the pages.
* @global int The number of pages.
* @global int The requested page.
*/
static public function evaluateScripting()
{
global $c, $cl, $s;
$oldS = $s;
for ($i = 0; $i < $cl; $i++) {
if (hide($i)) {
continue;
}
$s = $i;
$c[$i] = evaluate_scripting($c[$i]);
}
$s = $oldS;
}
/**
* Returns the current page content.
*
* @return string (X)HTML.
*
* @global int The requested page.
* @global string The (X)HTML fragment to be prepended to the contents area.
* @global array The indexes of the visible pages.
* @global array The contents of the pages.
* @global array The URLs of the pages.
* @global bool Whether we're in edit mode.
* @global array The configuration of the plugins.
*/
public static function getContent()
{
global $s, $o, $hc, $c, $u, $edit, $plugin_cf, $pd_router;
if (!($edit && XH_ADM) && $s > -1) {
$contents = '';
foreach ($hc as $i) {
$url = $plugin_cf['onepage']['url_numeric']
? $i
: XH_hsc(urldecode($u[$i]));
$pageData = $pd_router->find_page($i);
$content = self::replaceAlternativeHeading($c[$i], $pageData);
$contents .= sprintf(
'<div id="%s" class="onepage_page">%s</div>',
$url,
sprintf(
'<div class="%s">%s</div>',
$plugin_cf['onepage']['inner_class'],
$content
)
);
}
return $o . preg_replace('/#CMSimple (.*?)#/is', '', $contents);
} else {
return $o;
}
}
/**
* Replaces the existing heading with an alternative heading if configured.
*
* @param string $content The page content.
* @param array $pageData The page data.
*
* @return string
*
* @global array The configuration of the core.
*
* @todo Use Pageparams_replaceAlternativeHeading() if available.
*/
protected static function replaceAlternativeHeading($content, $pageData)
{
global $cf;
if ($pageData['show_heading'] == '1') {
$pattern = '/(<h[1-' . $cf['menu']['levels'] . '].*>).+(<\/h[1-'
. $cf['menu']['levels'] . ']>)/isU';
if (trim($pageData['heading']) == '') {
return preg_replace($pattern, '', $content);
} else {
return preg_replace(
$pattern, '${1}' . addcslashes($pageData['heading'], '$\\') . '$2',
$content
);
}
} else {
return $content;
}
}
/**
* Renders the top link.
*
* @param string $id An (X)HTML id.
*
* @return string (X)HTML.
*
* @global array The paths of system files and folders.
* @global array The localization of the plugins.
*/
public static function renderTopLink($id)
{
global $pth, $plugin_tx;
if ($id != '' && $id[0] == '#') {
$id = substr($id, 1);
}
$image = $pth['folder']['templateimages'] . 'up.png';
if (!file_exists($image)) {
$image = $pth['folder']['plugins'] . 'onepage/images/up.png';
}
$alt = $plugin_tx['onepage']['alt_toplink'];
return '<a id="onepage_toplink" href="#' . $id . '">'
. tag('img src="' . $image . '" alt="' . $alt . '"')
. '</a>';
}
}
?>