<?php
abstract class element
{
/**
*
* @var string The HTML ID of the field.
*/
protected $id;
/**
* @var string The HTML tag to use when printing the element.
*/
protected $tag;
/**
* @var boolean Whether to include the indents and new line character when displaying.
*/
public $format = false;
/**
* @var array Any associated CSS files.
*/
public $css = array ();
/**
* @var array Attributes specific to this element.
*/
public $attributes = array ();
/**
* Default element constructor.
*/
function __construct ($id = false)
{
$this->id = $id;
}
/**
* Increase the indents to be printed before the source code.
* @return void No return value, the function actual prints the HTML to the screen.
*/
function incIndent ()
{
global $indent;
$indent++;
}
/**
* Decrease the indents to be printed before the source code.
* @return void No return value, the function actual prints the HTML to the screen.
*/
function decIndent ()
{
global $indent;
$indent--;
}
/**
* Get the current number of indents.
* @return int The current number of indents.
*/
function returnIndent ()
{
global $indent;
$r = '';
for ($i = 0; $i < $indent; $i++)
$r .= " ";
return $r;
}
/**
* Get the string containing all the style information.
* @return string The style string for the current element.
*/
protected function returnStyleString ()
{
$result = '';
foreach ($this->css as $att => $val)
$result .= $att.':'.$val.';';
if ($result <> '')
return ' style="'.$result.'" ';
else
return ' ';
}
/**
* Get the string containing all the attributes and their values.
* @return string The string containing all the various attributes for the current element (excluding STYLE).
*/
protected function returnAttributeString ()
{
$result = '';
foreach ($this->attributes as $att => $val)
{
$result .= $att.'="'.$val.'" ';
}
return $result;
}
/**
* Get the opening tags for the element.
* @return string The opening tag for the current element.
*/
protected function returnStartTag ()
{
if (!isset ($this->tag))
throw new exception ('ERROR: element::tag is not set');
if (isset($this->id))
$id = 'id="'.$this->id.'"';
return sprintf ('<%s %s %s %s>', $this->tag, $id, $this->returnStyleString(), $this->returnAttributeString());
}
/**
* Get the closing tags for the element.
* @return string The closing tags for the current element.
*/
protected function returnEndTag ()
{
if (!isset ($this->tag))
throw new exception ('ERROR: element::tag is not set');
return sprintf ('</'.$this->tag.'>');
}
/**
* Display the opening tags for the element.
* @return void No return value, the function actual prints the HTML to the screen.
*/
protected function displayStartTag ()
{
if($this->format)
{
echo ($this->returnIndent () . $this->returnStartTag () . "\n");
$this->incIndent ();
}
else
echo ($this->returnStartTag ());
}
/**
* Display the closing tags for the element.
* @return void No return value, the function actual prints the HTML to the screen.
*/
protected function displayEndTag ()
{
if($this->format)
{
echo ($this->returnIndent () . $this->returnEndTag () . "\n");
$this->decIndent ();
}
else
echo ($this->returnEndTag ());
}
/**
* Display the element (including style and attributes).
* @return void No return value, the function actual prints the HTML to the screen.
*/
function display ()
{
if ($this->tag == '')
throw new exception ('ERROR: element::tag is not set');
echo ($this->returnIndent ());
if($this->format)
printf ("<%s%s%s />\n", $this->tag, $this->returnStyleString(), $this->returnAttributeString());
else
printf ("<%s%s%s />", $this->tag, $this->returnStyleString(), $this->returnAttributeString());
}
}
class Text extends element
{
/**
* @var string The text that the element will display.
*/
var $text;
/**
* Default constructor for the element.
* @param string $text The text to display in the text element.
*/
function __construct ($text)
{
$this->text = $text;
}
/**
* Display the text element.
* @return void No return value, the function actual prints the HTML to the screen.
*/
function display ()
{
echo ($this->text);
}
}
abstract class block extends element
{
/**
* @var array The elements that will be contained in the block.
*/
protected $elements = array ();
/**
* Add an element to the block.
* @param mixed &$element The element to add to the block.
* @return void No return value.
*/
function addElement (&$element, $position = -1)
{
if (is_a ($element, 'element'))
{
if(($position != -1) && ($position < count($this->elements)))
{
for($i = count($this->elements); $i > $position; $i--)
$this->elements[$i] = $this->elements[$i-1];
$this->elements[$position] = $element;
}
else
$this->elements[] = $element;
}
}
/**
* Display the block.
* @return void No return value.
*/
function display ()
{
$this->displayStartTag ();
foreach ($this->elements as $e)
$e->display ();
$this->displayEndTag ();
}
}
class div extends block
{
/**
* @var string The HTML tag for a div.
*/
var $tag = 'div';
}
$no_towns = new div ();
$no_towns->attributes ['id'] = 'no_towns';
$no_towns->addElement ( new text ( "<i>No towns are viewable with your current permissions.</i>" ) );
$no_towns->colspan = 8;
$no_towns->attributes ['style'] .= 'width:263px';
$no_towns->displayChanges = false;
$no_towns->css ['display'] = 'none';