1. <?php
  2. /******************************************************************************
  3.  * Master Page Class
  4.  * (C) Paul Clement 2007
  5.  *
  6.  * Usage: $Master = new MasterPage('Filename');
  7.  ******************************************************************************/
  8.  
  9. class MasterPage
  10. {
  11.         // Variables
  12.         public $HTML;
  13.         public $JavaScript = array();
  14.         public $CSS = array();
  15.         public $RSS = array();
  16.         public $HeadData = array();
  17.  
  18.         // Initialise Connection       
  19.         function __construct($FileName)
  20.         {
  21.                 // Check if Master Page exists
  22.                 if ( !file_exists( $FileName ) )
  23.                 {
  24.                         // Throw Error
  25.                         trigger_error("Master Page Error: File does not exist", E_USER_ERROR);
  26.                         return false;
  27.                         exit();
  28.                 }
  29.                
  30.                 // Get contents of Master File
  31.                 ob_start();
  32.                 include $FileName;
  33.                 $this->HTML = ob_get_contents()
  34.                         or trigger_error("Master Page Error: Cannot get contents", E_USER_WARNING);
  35.                 ob_end_clean();
  36.                
  37.                 // Return true
  38.                 return true;
  39.         }
  40.        
  41.         // Set Page Title
  42.         function SetPageTitle($PageTitle, $Seperator = "&bull;")
  43.         {
  44.                 // Build page title
  45.                 $TitleString = " " . $Seperator . " " . $PageTitle;
  46.                
  47.                 // Replace title
  48.                 $this->HTML = str_replace('<master:PageTitle />', $TitleString, $this->HTML);
  49.                
  50.                 // Return true
  51.                 return true;
  52.         }
  53.        
  54.         // Fill Content Area
  55.         function FillContentArea($ContentAreaID, $Content)
  56.         {
  57.                 // If ContentArea Exists
  58.                 if ( strpos($this->HTML, '<master:ContentArea id="' . $ContentAreaID . '" />') )
  59.                 {
  60.                         // Fill ContentArea
  61.                         $this->HTML = str_replace('<master:ContentArea id="' . $ContentAreaID . '" />', $Content, $this->HTML);
  62.                 }
  63.                 else
  64.                 {
  65.                         // Trigger Error
  66.                         trigger_error("Master Page Error: ContentArea not found", E_USER_WARNING);
  67.                         return false;
  68.                         exit();
  69.                 }
  70.                
  71.                 // Return true
  72.                 return true;   
  73.         }
  74.        
  75.         // Append data to head
  76.         function HeadAppend($HTML)
  77.         {
  78.                 $this->HeadData[] = $HTML;     
  79.         }
  80.        
  81.         // Add RSS Feed
  82.         function AddRSS($URI, $Title)
  83.         {
  84.                 $this->RSS[$Title] = $URI;     
  85.         }
  86.        
  87.         // Add CSS File
  88.         function AddCSS($URI)
  89.         {
  90.                 $this->CSS[] = $URI;   
  91.         }
  92.        
  93.         // Add JavaScript
  94.         function AddJavaScript($URI)
  95.         {
  96.                 $this->JavaScript[] = $URI;
  97.         }
  98.        
  99.         // Write to String
  100.         function ToString()
  101.         {
  102.                 // Load Output
  103.                 $Output = $this->HTML;
  104.                
  105.                 // Remove Empty ContentAreas
  106.                 $Output = preg_replace('<\<master:ContentArea id="([a-zA-Z0-9_-]*)" /\>>', "", $Output);
  107.                
  108.                 // Remove any Blank Title Elements
  109.                 $Output = str_replace('<master:PageTitle />', "", $Output);
  110.                
  111.                 // Initialise HeadData String
  112.                 $HeadData = "";
  113.  
  114.                 // Build JavaScript String
  115.                 foreach ( $this->JavaScript as $URI )
  116.                 {
  117.                         $HeadData .= '<script src="' . $URI . '" type="text/javascript"></script>' . "\n";
  118.                 }
  119.                
  120.                 // Build CSS String
  121.                 foreach ( $this->CSS as $URI )
  122.                 {
  123.                         $HeadData .= '<link rel="stylesheet" type="text/css" href="'. $URI .'" />' . "\n";     
  124.                 }
  125.                
  126.                 // Build RSS String
  127.                 foreach ( $this->RSS as $Title => $URI )
  128.                 {
  129.                         $HeadData .= '<link rel="alternate" href="' . $URI .'" type="application/rss+xml" title="' . $Title . '" />' . "\n";   
  130.                 }
  131.                
  132.                 // Add remaining HeadData
  133.                 foreach ( $this->HeadData as $HeadHTML )
  134.                 {
  135.                         $HeadData .= $HeadHTML . "\n"; 
  136.                 }
  137.                
  138.                 // Replace Placeholder
  139.                 $Output = str_replace('<master:HeadData />', $HeadData, $Output);
  140.                
  141.                 // Return output
  142.                 return $Output;
  143.         }
  144.        
  145.         // Show Page
  146.         function Display()
  147.         {
  148.                 // Clear Output
  149.                 ob_clean();
  150.                
  151.                 // Display HTML
  152.                 echo $this->ToString();
  153.                
  154.                 // Return true
  155.                 return true;   
  156.         }
  157.        
  158.         function FillFields($data) {
  159.                 foreach($data as $key => $value) {
  160.                         $this->HTML = preg_replace('/<input type="text" name="' . $key . '\" ([^>]+)>/', '<input type="text" name="' . $key . '" value="' . $value . '"$1>', $this->HTML);
  161.                         $this->HTML = preg_replace('/<textarea name="' . $key . '\"([^>]*)><\/textarea>/', '<textarea name="' . $key . '"$1>' . $value . '</textarea>', $this->HTML);
  162.                        
  163.                         if ( isset($value) ) {
  164.                                 $this->HTML = preg_replace('/<input type="checkbox" name="' . $key . '\" ([^>]+)>/', '<input type="checkbox" name="' . $key . '" checked="checked"$1>', $this->HTML);
  165.                                
  166.                                 if ( preg_match('/<input type="radio" name="' . $key .'" value="' . $value . '" ([^>]+)>/', $this->HTML)) {
  167.                                         $this->HTML = preg_replace('/<input type="radio" name="' . $key .'" value="' . $value . '" ([^>]+)>/', '<input type="radio" name="' . $key . '" value="' . $value . '" checked="checked"$1>', $this->HTML);
  168.                                 }
  169.                         }
  170.                        
  171.                         if ( preg_match('/<select name="' . $key . '"([^>]*)>(.*)<option value="' . $value . '"(.*)<\/select>/s', $this->HTML) ) {
  172.                                 $this->HTML = preg_replace('/<select name="' . $key . '"([^>]*)>(.*)<option value="' . $value . '"(.*)<\/select>/s',
  173.                                 '<select name="' . $key . '"$1>$2<option value="' . $value . '" selected="selected"$3</select>', $this->HTML);
  174.                         }
  175.                 }
  176.         }
  177. }
  178. ?>