<?php
/******************************************************************************
* Master Page Class
* (C) Paul Clement 2007
*
* Usage: $Master = new MasterPage('Filename');
******************************************************************************/
class MasterPage
{
// Variables
public $HTML;
public $JavaScript = array();
public $HeadData = array();
// Initialise Connection
function __construct($FileName)
{
// Check if Master Page exists
{
// Throw Error
trigger_error("Master Page Error: File does not exist", E_USER_ERROR);
return false;
}
// Get contents of Master File
include $FileName;
or
trigger_error("Master Page Error: Cannot get contents", E_USER_WARNING);
// Return true
return true;
}
// Set Page Title
function SetPageTitle($PageTitle, $Seperator = "•")
{
// Build page title
$TitleString = " " . $Seperator . " " . $PageTitle;
// Replace title
$this->HTML = str_replace('<master:PageTitle />', $TitleString, $this->HTML);
// Return true
return true;
}
// Fill Content Area
function FillContentArea($ContentAreaID, $Content)
{
// If ContentArea Exists
if ( strpos($this->HTML, '<master:ContentArea id="' . $ContentAreaID . '" />') )
{
// Fill ContentArea
$this->HTML = str_replace('<master:ContentArea id="' . $ContentAreaID . '" />', $Content, $this->HTML);
}
else
{
// Trigger Error
trigger_error("Master Page Error: ContentArea not found", E_USER_WARNING);
return false;
}
// Return true
return true;
}
// Append data to head
function HeadAppend($HTML)
{
$this->HeadData[] = $HTML;
}
// Add RSS Feed
function AddRSS($URI, $Title)
{
$this->RSS[$Title] = $URI;
}
// Add CSS File
function AddCSS($URI)
{
$this->CSS[] = $URI;
}
// Add JavaScript
function AddJavaScript($URI)
{
$this->JavaScript[] = $URI;
}
// Write to String
function ToString()
{
// Load Output
$Output = $this->HTML;
// Remove Empty ContentAreas
$Output = preg_replace('<\<master:ContentArea id="([a-zA-Z0-9_-]*)" /\>>', "", $Output);
// Remove any Blank Title Elements
$Output = str_replace('<master:PageTitle />', "", $Output);
// Initialise HeadData String
$HeadData = "";
// Build JavaScript String
foreach ( $this->JavaScript as $URI )
{
$HeadData .= '<script src="' . $URI . '" type="text/javascript"></script>' . "\n";
}
// Build CSS String
foreach ( $this->CSS as $URI )
{
$HeadData .= '<link rel="stylesheet" type="text/css" href="'. $URI .'" />' . "\n";
}
// Build RSS String
foreach ( $this->RSS as $Title => $URI )
{
$HeadData .= '<link rel="alternate" href="' . $URI .'" type="application/rss+xml" title="' . $Title . '" />' . "\n";
}
// Add remaining HeadData
foreach ( $this->HeadData as $HeadHTML )
{
$HeadData .= $HeadHTML . "\n";
}
// Replace Placeholder
$Output = str_replace('<master:HeadData />', $HeadData, $Output);
// Return output
return $Output;
}
// Show Page
function Display()
{
// Clear Output
// Display HTML
echo $this->ToString();
// Return true
return true;
}
}
?>