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 299896 2010-05-28 06:03:39Z alan_k $
* 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.
* If an input element has a label element associated to it
* *and* the 'useElementLabels' option is true, then you can
* optionally set the text of this label. This permits
* to set custom strings for doing translations.
* @param mixed $attributes Associative array of table tag attributes
* or HTML attributes name="value" pairs
function HTML_Template_Flexy_Element ($tag= '', $attributes=null )
if (false !== strpos($tag, ':')) {
$this->setAttributes ($attributes);
* Returns an HTML formatted attribute string
* @param array $attributes
function attributesToHTML ()
$activeEngine = HTML_Template_Flexy ::$activeEngine;
$charset = empty ($activeEngine->options ['charset']) ?
$activeEngine->options ['charset'];
foreach ($this->attributes as $key => $value) {
// you shouldn't do this, but It shouldnt barf when you do..
if ($key == 'flexy:xhtml') {
// this is not xhtml compatible..
if (isset ($this->attributes['flexy:xhtml'])) {
$strAttr .= " {$key}=\"{$key}\"";
// dont replace & with &
if ($this->tag == 'textbox') { // XUL linefeed fix.
if (($this->tag == 'input') &&
$this->attributes['type'] == 'submit' &&
if (in_array($this->tag ,array ('input','textarea')) && $key == 'placeholder') {
$strAttr .= ' ' . $key . '="' . $value . '"';
} // 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']);
// if value is nto set, it doesnt make any difference what you set ?
if (!isset ($this->attributes['value'])) {
//print_r($this); echo "SET TO "; serialize($value);
if (isset ($this->attributes['name']) && (substr($this->attributes['name'],-2 ) == '[]')) {
in_array((string) $this->attributes['value'],$value)
$this->attributes['checked'] = true;
// removed - see bug 15279 - not sure if there is any knock on effects from this.
if ($this->attributes['value'] == $value) {
$this->attributes['checked'] = true;
if (isset ($this->attributes['checked'])) {
unset ($this->attributes['checked']);
// if we dont have values associated yet, store it..
if (!isset ($this->attributes['value'])) {
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((string) $child->attributes ['value'], $value))
$this->children[$i]->children [$ii]->attributes ['selected'] =
isset ($this->attributes['flexy:xhtml']) ? 'selected' : true;
if (isset ($child->attributes ['value']) &&
isset ($this->children[$i]->children [$ii]->attributes ['selected']))
unset ($this->children[$i]->children [$ii]->attributes ['selected']);
if (isset ($this->children[$i]->children [$ii]->attributes ['selected'])) {
unset ($this->children[$i]->children [$ii]->attributes ['selected']);
// standard option value...
//echo "testing {$child->attributes['value']} against ". print_r($value,true)."\n";
// does the value exist and match..
if (isset ($child->attributes ['value'])
&& in_array((string) $child->attributes ['value'], $value))
$this->children[$i]->attributes ['selected'] =
isset ($this->attributes['flexy:xhtml']) ? 'selected' : true;;
// no value attribute try and use the contents.
if (!isset ($child->attributes ['value'])
&& in_array((string) $child->children [0 ], $value))
$this->children[$i]->attributes ['selected'] =
isset ($this->attributes['flexy:xhtml']) ? 'selected' : true;
if (isset ($child->attributes ['value']) &&
isset ($this->children[$i]->attributes ['selected']))
//echo "clearing selected\n";
unset ($this->children[$i]->attributes ['selected']);
if (isset ($this->children[$i]->attributes ['selected'])) {
//echo "clearing selected\n";
unset ($this->children[$i]->attributes ['selected']);
$activeEngine = HTML_Template_Flexy ::$activeEngine;
$charset = empty ($activeEngine->options ['charset']) ?
$activeEngine->options ['charset'];
$this->children = array (htmlspecialchars ($value,ENT_COMPAT ,$charset));
case '': // dummy objects.
require_once 'HTML/Template/Flexy/Element/Xul.php';
HTML_Template_Flexy_Element_Xul ::setValue ($this,$value);
* 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 ();
$activeEngine = HTML_Template_Flexy ::$activeEngine;
$charset = empty ($activeEngine->options ['charset']) ?
$activeEngine->options ['charset'];
$tag = strtolower ($this->tag);
if (false !== strpos($this->tag, ':')) {
$namespace = $bits[0 ] . ':';
// if we have specified a xultag!!?
if (strlen($tag) && ($tag != 'select')) {
require_once 'HTML/Template/Flexy/Element/Xul.php';
return HTML_Template_Flexy_Element_Xul ::setOptions ($this,$array,$noValue);
foreach($array as $k=> $v) {
$child = new HTML_Template_Flexy_Element ($namespace . 'optgroup',array ('label'=> $k));
foreach($v as $kk=> $vv) {
if (($kk !== $vv) && !$noValue) {
$atts = array ('value'=> $kk);
$atts = array ('value'=> $vv);
$add = new HTML_Template_Flexy_Element ($namespace . 'option',$atts);
$child->children [] = $add;
$this->children[] = $child;
if (($k !== $v) && !$noValue) {
$atts = array ('value'=> $k);
$atts = array ('value'=> $v);
$add = new HTML_Template_Flexy_Element ($namespace . 'option',$atts);
$this->children[] = $add;
* Returns THIS select element's options as an associative array
* Validates that $this element is "select"
if (false !== strpos($this->tag, ':')) {
$namespace = $bits[0 ] . ':';
// this is not a select element
if (strlen($tag) && ($tag != 'select')) {
// creates an associative array that can be used by setOptions()
// null does work for no value ( a "Please Choose" option, for example)
foreach ($this->children as $child) {
$child->attributes ['value'] = isset ($child->attributes ['value']) ? $child->attributes ['value'] : '';
$children[$child->attributes ['value']] = $child->children [0 ];
* Removes all of this element's options
* Validates that $this element is "select"
function clearOptions ($children = array ())
if (false !== strpos($this->tag, ':')) {
$namespace = $bits[0 ] . ':';
// this is not a select element
if (strlen($tag) && ($tag != 'select')) {
// clear this select's options
$this->children = array (null );
$this->values = array (null );
// If called with an array of new options go ahead and set them
$this->setOptions ($children);
* 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);
if (strpos($tag,':') !== false ) {
// tags that never should have closers
$close = " </{$ret->tag }>";
if (isset ($ret->attributes ['flexy:xhtml'])) {
$this->attributes['/'] = true;
if (isset ($this->attributes['/'])) {
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 ($ret->override !== 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 ();
* merge this element with another
* originally was in main engine
* @param HTML_Template_Flexy_Element $new (with data to replace/merge)
* @return HTML_Template_Flexy_Element the combined/merged data.
// Clone objects is possible to avoid creating references between elements
$original = clone ($this);
// no new - return original
// If the properties of $original differ from those of $new and
// they are set on $new, set them to $new's. Otherwise leave them
if ($new->tag && ($new->tag != $original->tag )) {
$original->tag = $new->tag;
if ($new->override !== false ) {
$original->override = $new->override;
if (count($new->children )) {
//echo "<PRE> COPY CHILDREN"; print_r($from->children);
$original->children = $new->children;
foreach ($new->attributes as $key => $value) {
$original->attributes [$key] = $value;
// originals never have prefixes or suffixes..
$original->prefix = $new->prefix;
$original->suffix = $new->suffix;
if ($new->value !== null ) {
$original->setValue ($new->value );
if ($new->label !== null ) {
$original->label = $new->label;
} // end class HTML_Template_Flexy_Element
Documentation generated on Mon, 11 Mar 2019 15:59:56 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|