forked from forkcms/forkcms
/
page.php
429 lines (355 loc) · 9.6 KB
/
page.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
<?php
/*
* This file is part of Fork CMS.
*
* For the full copyright and license information, please view the license
* file that was distributed with this source code.
*/
/**
* Frontend page class, this class will handle everything on a page
*
* @author Tijs Verkoyen <tijs@sumocoders.be>
* @author Matthias Mullie <matthias@mullie.eu>
*/
class FrontendPage extends FrontendBaseObject
{
/**
* Breadcrumb instance
*
* @var FrontendBreadcrumb
*/
protected $breadcrumb;
/**
* Array of extras linked to this page
*
* @var array
*/
protected $extras = array();
/**
* Footer instance
*
* @var FrontendFooter
*/
protected $footer;
/**
* Header instance
*
* @var FrontendHeader
*/
protected $header;
/**
* The current pageId
*
* @var int
*/
protected $pageId;
/**
* Content of the page
*
* @var array
*/
protected $record = array();
/**
* The path of the template to show
*
* @var string
*/
protected $templatePath;
/**
* The statuscode
*
* @var int
*/
protected $statusCode = 200;
public function __construct($pageId = null)
{
parent::__construct();
// set tracking cookie
FrontendModel::getVisitorId();
// add reference
Spoon::set('page', $this);
// get pageId for requested URL
$this->pageId = ($pageId === null)
? FrontendNavigation::getPageId(implode('/', $this->URL->getPages()))
: (int) $pageId;
// set headers if this is a 404 page
if($this->pageId == 404) $this->statusCode = 404;
// create breadcrumb instance
$this->breadcrumb = new FrontendBreadcrumb();
// create header instance
$this->header = new FrontendHeader();
// new footer instance
$this->footer = new FrontendFooter();
// get pagecontent
$this->getPageContent();
// process page
$this->processPage();
// execute all extras linked to the page
$this->processExtras();
// store statistics
$this->storeStatistics();
// display
$this->display();
// trigger event
FrontendModel::triggerEvent(
'core',
'after_page_processed',
array(
'id' => $this->getId(),
'record' => $this->getRecord(),
'statusCode' => $this->getStatusCode(),
'sessionId' => SpoonSession::getSessionId(),
'visitorId' => FrontendModel::getVisitorId(),
'SESSION' => $_SESSION,
'COOKIE' => $_COOKIE,
'GET' => $_GET,
'POST' => $_POST,
'SERVER' => $_SERVER
)
);
}
/**
* Display the page
*/
public function display()
{
// parse header
$this->header->parse();
// parse breadcrumb
$this->breadcrumb->parse();
// parse languages
$this->parseLanguages();
// parse footer
$this->footer->parse();
// assign the id so we can use it as an option
$this->tpl->assign('isPage' . $this->pageId, true);
// fetch variables from main template
$mainVariables = $this->tpl->getAssignedVariables();
// loop all positions
foreach($this->record['positions'] as $position => &$blocks)
{
// loop all blocks in this position
foreach($blocks as &$block)
{
// check for extra's that need to be reparsed
if(isset($block['extra']))
{
// fetch extra-specific variables
$extraVariables = $block['extra']->getTemplate()->getAssignedVariables();
// assign all main variables
$block['extra']->getTemplate()->assignArray($mainVariables);
// overwrite with all specific variables
$block['extra']->getTemplate()->assignArray($extraVariables);
// parse extra
$block = array('blockIsHTML' => false,
'blockContent' => $block['extra']->getContent());
}
}
// assign position to template
$this->tpl->assign('position' . SpoonFilter::ucfirst($position), $blocks);
}
// assign empty positions
$unusedPositions = array_diff($this->record['template_data']['names'], array_keys($this->record['positions']));
foreach($unusedPositions as $position) $this->tpl->assign('position' . SpoonFilter::ucfirst($position), array());
// only overwrite when status code is 404
if($this->statusCode == 404) SpoonHTTP::setHeadersByCode(404);
// output
$this->tpl->display($this->templatePath, false, true);
}
/**
* Get the extras linked to this page
*
* @return array
*/
public function getExtras()
{
return $this->extras;
}
/**
* Get the current page id
*
* @return int
*/
public function getId()
{
return $this->pageId;
}
/**
* Get page content
*/
protected function getPageContent()
{
// load revision
if($this->URL->getParameter('page_revision', 'int') != 0)
{
// get data
$this->record = FrontendModel::getPageRevision($this->URL->getParameter('page_revision', 'int'));
// add no-index to meta-custom, so the draft won't get accidentally indexed
$this->header->addMetaData(array('name' => 'robots', 'content' => 'noindex, nofollow'), true);
}
// get page record
else $this->record = (array) FrontendModel::getPage($this->pageId);
// empty record (pageId doesn't exists, hope this line is never used)
if(empty($this->record) && $this->pageId != 404) SpoonHTTP::redirect(FrontendNavigation::getURL(404), 404);
// init var
$redirect = true;
// loop blocks, if all are empty we should redirect to the first child
foreach($this->record['positions'] as $position => $blocks)
{
// loop blocks in position
foreach($blocks as $block)
{
// HTML provided?
if($block['html'] != '') $redirect = false;
// an decent extra provided?
if($block['extra_type'] == 'block') $redirect = false;
// a widget provided
if($block['extra_type'] == 'widget') $redirect = false;
}
}
// should we redirect?
if($redirect)
{
// get first child
$firstChildId = FrontendNavigation::getFirstChildId($this->record['id']);
// validate the child
if($firstChildId !== false)
{
// build URL
$URL = FrontendNavigation::getURL($firstChildId);
// redirect
SpoonHTTP::redirect($URL, 301);
}
}
}
/**
* Get the content of the page
*
* @var array
*/
public function getRecord()
{
return $this->record;
}
/**
* Fetch the statuscode for the current page.
*
* @return int
*/
public function getStatusCode()
{
return $this->statusCode;
}
/**
* Parse the languages
*/
protected function parseLanguages()
{
// just execute if the site is multi-language
if(SITE_MULTILANGUAGE)
{
// get languages
$activeLanguages = FrontendLanguage::getActiveLanguages();
// init var
$languages = array();
// loop active languages
foreach($activeLanguages as $language)
{
// build temp array
$temp = array();
$temp['url'] = '/' . $language;
$temp['label'] = $language;
$temp['name'] = FL::msg(strtoupper($language));
$temp['current'] = (bool) ($language == FRONTEND_LANGUAGE);
// add
$languages[] = $temp;
}
// assign
if(count($languages) > 1) $this->tpl->assign('languages', $languages);
}
}
/**
* Processes the extras linked to the page
*/
protected function processExtras()
{
// loop all extras
foreach($this->extras as $extra)
{
// execute
$extra->execute();
// overwrite the template
if(is_callable(array($extra, 'getOverwrite')) && $extra->getOverwrite()) $this->templatePath = $extra->getTemplatePath();
// assign the variables from this extra to the main template
$this->tpl->assignArray((array) $extra->getTemplate()->getAssignedVariables());
}
}
/**
* Processes the page
*/
protected function processPage()
{
// set pageTitle
$this->header->setPageTitle($this->record['meta_title'], (bool) ($this->record['meta_title_overwrite'] == 'Y'));
// set meta-data
$this->header->addMetaDescription($this->record['meta_description'], (bool) ($this->record['meta_description_overwrite'] == 'Y'));
$this->header->addMetaKeywords($this->record['meta_keywords'], ($this->record['meta_keywords_overwrite'] == 'Y'));
$this->header->setMetaCustom($this->record['meta_custom']);
// advanced SEO-attributes
if(isset($this->record['meta_data']['seo_index'])) $this->header->addMetaData(array('name' => 'robots', 'content' => $this->record['meta_data']['seo_index']));
if(isset($this->record['meta_data']['seo_follow'])) $this->header->addMetaData(array('name' => 'robots', 'content' => $this->record['meta_data']['seo_follow']));
// create navigation instance
new FrontendNavigation();
// assign content
$this->tpl->assign('page', $this->record);
// set template path
$this->templatePath = FRONTEND_PATH . '/' . $this->record['template_path'];
// loop blocks
foreach($this->record['positions'] as $position => &$blocks)
{
// position not known in template = skip it
if(!in_array($position, $this->record['template_data']['names'])) continue;
// loop blocks in position
foreach($blocks as $index => &$block)
{
// build templateVariable
$templateVariable = 'block' . ($index + 1);
// an extra
if($block['extra_id'] !== null)
{
// block
if($block['extra_type'] == 'block')
{
// create new instance
$extra = new FrontendBlockExtra($block['extra_module'], $block['extra_action'], $block['extra_data']);
}
// widget
else
{
// create new instance
$extra = new FrontendBlockWidget($block['extra_module'], $block['extra_action'], $block['extra_data']);
}
// add to list of extras
$block = array('extra' => $extra);
// add to list of extras to parse
$this->extras[] = $extra;
}
// the block only contains HTML
else
{
$block = array(
'blockIsHTML' => true,
'blockContent' => $block['html']
);
}
}
}
}
/**
* Store the data for statistics
*/
protected function storeStatistics()
{
// @later save temp statistics data here.
}
}