#39 - PHP Class to construct pages
Data: 2018-12-15 12:00 - PHP
Yet another class to help construct pages in PHP
<?php
class Page {
private static $page;
private static $constructsList = array();
private static $navigationList = array();
private static $title;
private static $description;
private static $header;
public static function getPage() {
return self::$page;
}
public static function setTitle($title) {
self::$title = $title;
}
public static function setDescription($description) {
self::$description = $description;
}
public static function setHeader($header) {
self::$header = $header;
}
public static function getFullTitle() {
$pageTitle = Settings::get('Website-Title');
return self::$title == null ? $pageTitle : ($pageTitle . ' - ' . self::$title);
}
public static function getDescription() {
return isset(self::$description) ? self::$description : Settings::get('Website-Description');
}
public static function getHeader() {
return isset(self::$header) ? self::$header : Settings::get('Website-Header');
}
public static function construct() {
$uri = $_SERVER['REQUEST_URI'];
if (($getStart = strpos($uri, '?')) !== false)
$uri = substr($uri, 0, $getStart);
$uri = trim($uri, '/');
if ((self::$page = $uri) == '')
self::$page = 'index';
if (file_exists('pages/' . self::$page . '.php'))
include ('pages/' . self::$page . '.php');
else if (array_key_exists('REDIRECT_STATUS', $_SERVER) && $_SERVER['REDIRECT_STATUS'] == 403)
Page::setErrorPage("403", "Forbidden");
else
Page::setErrorPage("404", "Not Found");
echo '<!DOCTYPE html>', "\n";
echo'<html lang="en"><head>';
self::dumpHead();
echo '</head><body>';
self::dumpBody();
self::constructSection('scripts');
echo '</body>';
echo '</html>';
}
private static function dumpHead() {
echo '<title>', self::getFullTitle(), '</title>';
echo '<meta charset="UTF-8">';
self::constructSection('head');
self::constructSection('metas');
self::constructSection('styles');
}
private static function dumpBody() {
self::constructSection('body');
}
public static function addConstruct($section, $func) {
if(!array_key_exists($section, self::$constructsList))
self::$constructsList[$section] = array();
self::$constructsList[$section][] = $func;
}
public static function constructSection($section) {
if(!array_key_exists($section, self::$constructsList))
return;
foreach(self::$constructsList[$section] as $func)
$func();
}
public static function addStylesheet($link, $attrs = '') {
self::addConstruct('styles', function() use ($link, $attrs) {
echo '<link rel="stylesheet" href="' . $link . '" type="text/css" ' . $attrs . '/>';
});
}
public static function addScript($link) {
self::addConstruct('scripts', function() use ($link) {
echo '<script src="' . $link . '"></script>';
});
}
public static function setErrorPage($error, $errorDesc) {
header($_SERVER['SERVER_PROTOCOL'] . " $error $errorDesc");
if (file_exists("pages/errors/$error.php"))
include ("pages/errors/$error.php");
else {
Page::setTitle("$error $errorDesc");
}
}
public static function addNavigation($section, $text, $link) {
if(!array_key_exists($section, self::$navigationList))
self::$navigationList[$section] = array();
self::$navigationList[$section][] = array($text, $link);
}
public static function getNavigation($section) {
if(!array_key_exists($section, self::$navigationList))
return array();
return self::$navigationList[$section];
}
}
?>