Source for file Element.php
Documentation is available at Element.php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Author: Alan Knowles <alan@akbkhome.com> |
// | Based on HTML_Common by: Adam Daniel <adaniel1@eesus.jnj.com> |
// +----------------------------------------------------------------------+
// $Id: Element.php,v 1.20 2004/04/22 08:23:37 alan_k Exp $
* Lightweight HTML Element builder and render
* This differs from HTML_Common in the following ways:
* $element->attributes is Public
* $element->override if set to anything other than false, renders the value rather than
* $element->children is a recursvable child array which is rendered by toHTML
* $element->toHtml() is implemented
* $element->toHtmlNoClose() renders only the first tag and children (designed for <form
* No support for tab offsets, comments ...
* Full support for Select, and common Form elements using
* overlay support with SetFrom - base + inherited..
* attributes array values:
* key="value" // standard key="value" in output
* key = true // outputs just key.
* @author Adam Daniel <adaniel1@eesus.jnj.com>
class HTML_Template_Flexy_Element {
* Tag that this Element represents.
* Associative array of table attributes
* true == only display the key
var $attributes = array ();
* Sequence array of children
* children that are strings are assumed to be text
* if this is set to anything other than false, it will be output
* rather than the tags+children
* this is output by toHtml as a prefix to the tag (can be used for require tags)
* this is output by toHtml as a suffix to the tag (can be used for error messages)
* a value for delayed merging into live objects
* if you set this on an element, it is merged by setValue, at merge time.
* @param mixed $attributes Associative array of table tag attributes
* or HTML attributes name="value" pairs
function HTML_Template_Flexy_Element ($tag= '', $attributes=null )
$this->setAttributes ($attributes);
* Returns an HTML formatted attribute string
* @param array $attributes
function attributesToHTML ()
foreach ($this->attributes as $key => $value) {
// you shouldn't do this, but It shouldnt barf when you do..
// this is not xhtml compatible..
} // end func _getAttrString
* Static Method to get key/value array from attributes.
* Returns a valid atrributes array from either a string or array
* @param mixed $attributes Either a typical HTML attribute string or an associative array
function parseAttributes ($attributes)
foreach ($attributes as $key => $value) {
$preg = "/(([A-Za-z_:]|[^\\x00-\\x7F])([A-Za-z0-9_:.-]|[^\\x00-\\x7F])*)" .
"([ \\n\\t\\r]+)?(=([ \\n\\t\\r]+)?(\"[^\"]*\"|'[^']*'|[^ \\n\\t\\r]*))?/";
for ($counter=0; $counter< count($regs[1 ]); $counter++ ) {
$name = $regs[1 ][$counter];
$check = $regs[0 ][$counter];
$value = $regs[7 ][$counter];
if (substr($value, 0 , 1 ) == "\"" || substr($value, 0 , 1 ) == "'") {
$value = substr($value, 1 , -1 );
} // end func _parseAttributes
* Utility function to set values from common tag types.
* @param HTML_Element $from override settings from another element.
function setValue ($value) {
// store the value in all situations
if (strpos($tag,':') !== false ) {
switch (isset ($this->attributes['type']) ? strtolower($this->attributes['type']) : '') {
if (isset ($this->attributes['checked'])) {
unset ($this->attributes['checked']);
//print_r($this); echo "SET TO "; serialize($value);
if (substr($this->attributes['name'],-2 ) == '[]') {
$this->attributes['checked'] = true;
} else if ($this->attributes['value'] == $value) {
$this->attributes['checked'] = true;
if (isset ($this->attributes['checked'])) {
unset ($this->attributes['checked']);
if ($this->attributes['value'] == $value) {
$this->attributes['checked'] = true;
// no other input accepts array as a value.
$this->attributes['value'] = $value;
// its setting the default value..
foreach($this->children as $i=> $child) {
if ($child->tag == 'optgroup') {
foreach($this->children[$i]->children as $ii=> $child) {
// does the value exist and match..
if (isset ($child->attributes ['value'])
&& in_array($child->attributes ['value'], $value))
$this->children[$i]->children [$ii]->attributes ['selected'] = true;
if (isset ($child->attributes ['value']) &&
isset ($this->children[$i]->children [$ii]->attributes ['selected']))
unset ($this->children[$i]->children [$ii]->attributes ['selected']);
if (in_array($child->children [0 ],$value)) {
$this->children[$i]->children [$ii]->attributes ['selected'] = true;
if (in_array($child->children [0 ],$value)) {
$this->children[$i]->children [$ii]->attributes ['selected'] = true;
if (isset ($this->children[$i]->children [$ii]->attributes ['selected'])) {
unset ($this->children[$i]->children [$ii]->attributes ['selected']);
// standard option value...
// does the value exist and match..
if (isset ($child->attributes ['value'])
&& in_array($child->attributes ['value'], $value))
$this->children[$i]->attributes ['selected'] = true;
if (isset ($child->attributes ['value']) &&
isset ($this->children[$i]->attributes ['selected']))
unset ($this->children[$i]->attributes ['selected']);
if (in_array($child->children [0 ],$value)) {
$this->children[$i]->attributes ['selected'] = true;
if (in_array($child->children [0 ],$value)) {
$this->children[$i]->attributes ['selected'] = true;
if (isset ($this->children[$i]->attributes ['selected'])) {
unset ($this->children[$i]->attributes ['selected']);
case '': // dummy objects.
* Utility function equivilant to HTML_Select - loadArray **
* <option value="key">Value</option>
* Key=key (eg. both the same) maps to
* and label = array(key=>value) maps to
* <optgroup label="label"> <option value="key">value</option></optgroup>
* $element->setOptions(array('a'=>'xxx','b'=>'yyy'));
* $element->setOptions(array('a','b','c','d'),true);
* @param HTML_Element $from override settings from another element.
* @param HTML_Element $noValue ignore the key part of the array
function setOptions ($array,$noValue=false ) {
$this->children = array ();
foreach($array as $k=> $v) {
$child = new HTML_Template_Flexy_Element ('optgroup',array ('label'=> $kk));
foreach($v as $kk=> $vv) {
if (($kk != $vv) && !$noValue) {
$atts = array ('value'=> $kk);
$add = new HTML_Template_Flexy_Element ('option',$atts);
$child->children [] = $add;
$this->children[] = $child;
if (($k !== $v) && !$noValue) {
$atts = array ('value'=> $k);
$add = new HTML_Template_Flexy_Element ('option',$atts);
$this->children[] = $add;
* Sets the HTML attributes
* @param mixed $attributes Either a typical HTML attribute string or an associative array
function setAttributes ($attributes)
$attrs= $this->parseAttributes ($attributes);
foreach ($attrs as $key => $value) {
$this->attributes[$key] = $value;
} // end func updateAttributes
* @param string $attr Attribute name
function removeAttributes ($attrs)
foreach ($attrs as $attr) {
if (isset ($this->attributes[strtolower($attr)])) {
} //end func removeAttribute
* Output HTML and children
* @param object $overlay = merge data from object.
function toHtml ($overlay=false )
//echo "BEFORE<PRE>";print_R($this);
if ($overlay !== false ) {
$ret = HTML_Template_Flexy ::mergeElement ($this,$overlay);
if ($ret->override !== false ) {
$prefix = $prefix->toHtml ();
$suffix = $suffix->toHtml ();
//echo "AFTER<PRE>";print_R($ret);
// tags that never should have closers
if (strpos($tag,':') !== false ) {
$close = in_array(strtoupper($tag),array ("INPUT","IMG")) ? '' : " </{$ret->tag }>{$suffix}" ;
return "{ $prefix}<{ $ret->tag }". $ret->attributesToHTML () . '>'. $ret->childrenToHTML () . $close;
* Output Open Tag and any children and not Child tag (designed for use with <form + hidden elements>
* @param object $overlay = merge data from object.
function toHtmlnoClose ($overlay=false )
if ($overlay !== false ) {
$ret = HTML_Template_Flexy ::mergeElement ($this,$overlay);
return " <{$ret->tag }". $ret->attributesToHTML () . '>' . $ret->childrenToHTML ();
* Output HTML and children
function childrenToHtml ()
foreach($this->children as $child) {
$ret .= $child->toHtml ();
} // end class HTML_Common
Documentation generated on Mon, 11 Mar 2019 10:15:14 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|