/**
* The required files
*/
require_once(HTML_BASE_COMMON_PATH.'/Html.php');
require_once(HTML_BASE_UTIL_PATH.'/Raw.php');
require_once(HTML_BASE_UTIL_PATH.'/Htmlspecialchars.php');
if (defined('HTML_LOG_UTIL_PATH')) {
require_once(HTML_LOG_UTIL_PATH.'/Log.php');
}
/**
* Generates a Tag object, surronded by the specified html tag element
* <code>
* Usage:
* $text = 'Hello world';
* $element = 'h1';
* $tag = new Tag($text,$element,$class,$name);
* print $tag->getHtml();
* Or
* Tag::display($text,$element,$class,$name);
* </code>
* @package base
*/
class Tag extends Html {
var $text = ''; // The text to show
var $element = ''; // The html element
var $class = ''; // The class name
var $name = ''; // The name/id for a link identifier
/**
* Constructor
* @param String $text The text to show
* @param String $element The html element, if any
* @param String $class The css class name
* @param String $name The name/id attribute for a link identifier
*/
function Tag($text='',$element='',$class='',$name='') {
$this->Html();
if ($text!= '') {
if (is_object($text)) {
$this->add($text);
} else {
$this->add(new Raw($text)); // Plain text object
}
}
$this->element = $element;
$this->class = $class;
$this->name = $name;
$this->isValid();
}
/**
* Validates the Tag
*/
function isValid() {
if ($this->element!='') {
switch ($this->element) {
case 'pre':
case 'div':
case 'span':
case 'p':
case 'hr':
case 'br':
case 'ol':
case 'ul':
case 'li':
case 'b':
case 'h1':
case 'h2':
case 'h3':
case 'h4':
case 'h5':
case 'h6':
// Ok
break;
default:
$msg = $this->getClassName().' unsupported html element, found='.$this->element;
if (defined('HTML_LOG_UTIL_PATH')) {
Log::fatal(__FILE__,__LINE__,$msg);
} else {
// TODO what ?
die('File: '.__FILE__."<br />\r\nLine: ".__LINE__."<br />\r\n".$this->getMsg(LOG_LEVEL_FATAL, $msg));
}
}
}
}
/**
* Returns the start html for the text, optional surronded by the html element
* @return String the complete html
*/
function getStart() {
$html = '';
if ($this->element!='') {
if ($this->element=='br' || $this->element=='hr') {
// <br />, <hr /> See getEnd()
} else {
$html .= $this->html;
$html .= '<'.$this->element;
$html .= $this->getAttribute('class');
$html .= ">";
switch ($this->element) {
case 'h1':
case 'h2':
case 'h3':
case 'h4':
case 'h5':
case 'h6':
case 'p' :
case 'li': // Ignore \r\n
break;
default:
$html .= "\r\n";
break;
}
}
}
$html .= $this->getContent();
return $html;
}
/**
* Returns the content html for the text, optional surronded by the html element
* @return String the complete html
*/
function getContent() {
$html = '';
if ($this->text!='') {
if ($this->name!='') {
switch ($this->element) {
case 'h1':
case 'h2':
case 'h3':
case 'h4':
case 'h5':
case 'h6':
// i.e. <h1><a name=""name>text</a></h1>
$link = new Link($this->text,'','','','','',$this->name);
$html .= $link->getHtml();
break;
default:
// Ignore, Use standard
$html .= Htmlspecialchars::encode($this->text); // Translate html special chars
break;
}
} else {
$html .= Htmlspecialchars::encode($this->text); // Translate html special chars
}
}
$html .= $this->getElements();
return $html;
}
/**
* Returns the end html for the text, optional surronded by the html element
* @return String the complete html
*/
function getEnd() {
$html = '';
if ($this->element!='') {
if ($this->element=='br' || $this->element=='hr') {
$html .= '<'.$this->element." />\r\n"; // <br /> OR <hr />
} else {
$html .= '</'.$this->element.">\r\n";
}
}
return $html;
}
/**
* Returns the html for the text, optional surronded by the html element
* @return String the complete html
*/
function getHtml() {
$html = '';
if ($this->text=='' && $this->element=='' && $this->getSizeof()==0) {
$msg = $this->getClassName().'->getHtml(), no text or elements found';
if (defined('HTML_LOG_UTIL_PATH')) {
Log::warn(__FILE__,__LINE__,$msg);
$html .= TEXT_NO_DATA.'<!-- '.$this->getMsg(LOG_LEVEL_ERROR, $msg)." -->\r\n";
} else {
$html .= TEXT_NO_DATA.'<!-- '.$msg." -->\r\n";
}
} else {
$html .= $this->getStart();
$html .= $this->getEnd();
}
return $html;
}
/**
* Display start
* <code>
* Usage:
* Tag::start($element,$class);
* </code>
* @static
* @param String $element The html element, if any
* @param String $class The css class name
*/
function start($element='',$class='') {
$html = new Tag('',$element,$class);
$html->addHtml($html->getStart());
}
/**
* Display end
* <code>
* Usage:
* Tag::end($element);
* </code>
* @static
* @param String $element The html element, if any
*/
function end($element='') {
$html = new Tag('',$element);
$html->addHtml($html->getEnd());
}
/**
* Display html
* <code>
* Usage:
* Tag::display($text,$element,$class,$name);
* </code>
* @static
* @param String $text The text to show
* @param String $element The html element, if any
* @param String $class The css class name
* @param String $name The name/id attribute for a link identifier
*/
function display($text='',$element='',$class='',$name='') {
$html = new Tag($text,$element,$class,$name);
$html->addHtml();
}
}
?>