HTML_Template_Flexy
[ class tree: HTML_Template_Flexy ] [ index: HTML_Template_Flexy ] [ all elements ]

Source for file Element.php

Documentation is available at Element.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group             |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Alan Knowles <alan@akbkhome.com>                             |
  17. // | Based on HTML_Common by: Adam Daniel <adaniel1@eesus.jnj.com>        |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: Element.php,v 1.46 2005/12/20 01:45:06 alan_k Exp $
  21.  
  22. /**
  23.  * Lightweight HTML Element builder and render
  24.  *
  25.  * This differs from HTML_Common in the following ways:
  26.  *
  27.  * $element->attributes is Public
  28.  * $element->override if set to anything other than false, renders the value rather than
  29.  *   the defined element
  30.  *
  31.  * $element->children is a recursvable child array which is rendered by toHTML
  32.  * $element->toHtml() is implemented
  33.  * $element->toHtmlNoClose() renders  only the first tag and children (designed for <form
  34.  * No support for tab offsets, comments ...
  35.  *
  36.  * Full support for Select, and common Form elements using
  37.  * setValue()
  38.  * setOptions()
  39.  * 
  40.  * overlay support with SetFrom - base + inherited..
  41.  *
  42.  * attributes array values:
  43.  *  key="value" // standard key="value" in output
  44.  *  key = true // outputs just key.
  45.  *
  46.  * children can be
  47.  *  another HTML_Element
  48.  *  or string (raw text)
  49.  *
  50.  *
  51.  * @author      Adam Daniel <adaniel1@eesus.jnj.com>
  52.  * @version     1.6
  53.  * @since       PHP 4.0.3pl1
  54.  * @abstract
  55.  */
  56. class HTML_Template_Flexy_Element {
  57.  
  58.     
  59.  
  60.     /**
  61.      * Tag that this Element represents.
  62.      * @var  array 
  63.      * @access   public
  64.      */
  65.     var $tag =  '';
  66.     /**
  67.      * Associative array of table attributes
  68.      * Note Special values:
  69.      *   true == only display the key
  70.      *   false == remove
  71.      *
  72.      * @var  array 
  73.      * @access   public
  74.      */
  75.     var $attributes = array();
  76.  
  77.     /**
  78.      * Sequence array of children
  79.      * children that are strings are assumed to be text
  80.      * @var  array 
  81.      * @access   public
  82.      */
  83.     var $children = array();
  84.     
  85.     /**
  86.      * override the tag.
  87.      * if this is set to anything other than false, it will be output
  88.      * rather than the tags+children
  89.      * @var  array 
  90.      * @access   public
  91.      */
  92.     var $override = false;
  93.     /**
  94.      * prefix the tag.
  95.      * this is output by toHtml as a prefix to the tag (can be used for require tags)
  96.      * @var  array 
  97.      * @access   private
  98.      */
  99.     var $prefix '';
  100.     /**
  101.      * suffix the tag.
  102.      * this is output by toHtml as a suffix to the tag (can be used for error messages)
  103.      * @var  array 
  104.      * @access   private
  105.      */
  106.     var $suffix '';
  107.     
  108.     /**
  109.      * a value for delayed merging into live objects
  110.      * if you set this on an element, it is merged by setValue, at merge time.
  111.      * @var  array 
  112.      * @access   public
  113.      */
  114.     var $value = null;
  115.     /**
  116.      * Class constructor
  117.      * @param    mixed   $attributes     Associative array of table tag attributes
  118.      *                                    or HTML attributes name="value" pairs
  119.      * @access   public
  120.      */
  121.     function HTML_Template_Flexy_Element($tag=''$attributes=null)
  122.     {
  123.         
  124.         $this->tag strtolower($tag);
  125.         if (false !== strpos($tag':')) {
  126.             $bits explode(':',$this->tag);
  127.             $this->tag $bits[0':'.strtolower($bits[1]);
  128.         }
  129.         
  130.         $this->setAttributes($attributes);
  131.     // end constructor
  132.  
  133.       
  134.     /**
  135.      * Returns an HTML formatted attribute string
  136.      * @param    array   $attributes 
  137.      * @return   string 
  138.      * @access   private
  139.      */
  140.     function attributesToHTML()
  141.     {
  142.         $strAttr '';
  143.         $xhtmlclose '';
  144.         $charset = empty($GLOBALS['HTML_Template_Flexy']['options']['charset']'ISO-8859-1' $GLOBALS['HTML_Template_Flexy']['options']['charset'];
  145.         foreach ($this->attributes as $key => $value{
  146.         
  147.             // you shouldn't do this, but It shouldnt barf when you do..
  148.             if (is_array($value|| is_object($value)) {
  149.                 continue;
  150.             }
  151.             
  152.             if ($key == 'flexy:xhtml'{
  153.                 continue;
  154.             }
  155.             if ($value === false{
  156.                 continue;
  157.             }
  158.             if ($value === true{
  159.                 // this is not xhtml compatible..
  160.                 if ($key == '/'{
  161.                     $xhtmlclose ' /';
  162.                     continue;
  163.                 }
  164.                 if (isset($this->attributes['flexy:xhtml'])) {
  165.                     $strAttr .= " {$key}=\"{$key}\"";
  166.                 else {
  167.                     $strAttr .= ' ' $key;
  168.                 }
  169.             else {
  170.                 // dont replace & with &amp;
  171.                 if ($this->tag == 'textbox'{  // XUL linefeed fix.
  172.                     $value str_replace("\n"'&#13;'htmlspecialchars($value,ENT_COMPAT,$charset));
  173.                 else {
  174.                     $value str_replace('&amp;nbsp;','&nbsp;',htmlspecialchars($value,ENT_COMPAT,$charset));
  175.                 }
  176.                 $strAttr .= ' ' $key '="' $value  '"';
  177.             }
  178.             
  179.         }
  180.         $strAttr .= $xhtmlclose;
  181.         return $strAttr;
  182.     // end func _getAttrString
  183.  
  184.     /**
  185.      * Static Method to get key/value array from attributes.
  186.      * Returns a valid atrributes array from either a string or array
  187.      * @param    mixed   $attributes     Either a typical HTML attribute string or an associative array
  188.      * @access   private
  189.      */
  190.     function parseAttributes($attributes)
  191.     {
  192.         if (is_array($attributes)) {
  193.             $ret = array();
  194.             foreach ($attributes as $key => $value{
  195.                 if (is_int($key)) {
  196.                     $ret[strtolower($value)= true;
  197.                 else {
  198.                     $ret[strtolower($key)]   $value;
  199.                 }
  200.             }
  201.             return $ret;
  202.  
  203.         elseif (is_string($attributes)) {
  204.             $preg "/(([A-Za-z_:]|[^\\x00-\\x7F])([A-Za-z0-9_:.-]|[^\\x00-\\x7F])*)" .
  205.                 "([ \\n\\t\\r]+)?(=([ \\n\\t\\r]+)?(\"[^\"]*\"|'[^']*'|[^ \\n\\t\\r]*))?/";
  206.             if (preg_match_all($preg$attributes$regs)) {
  207.                 for ($counter=0; $counter<count($regs[1])$counter++{
  208.                     $name  $regs[1][$counter];
  209.                     $check $regs[0][$counter];
  210.                     $value $regs[7][$counter];
  211.                     if (trim($name== trim($check)) {
  212.                         $arrAttr[strtolower(trim($name))strtolower(trim($name));
  213.                     else {
  214.                         if (substr($value01== "\"" || substr($value01== "'"{
  215.                             $value substr($value1-1);
  216.                         }
  217.                         $arrAttr[strtolower(trim($name))trim($value);
  218.                     }
  219.                 }
  220.                 return $arrAttr;
  221.             }
  222.         }
  223.     // end func _parseAttributes
  224.  
  225.      
  226.      
  227.        
  228.     /**
  229.      * Utility function to set values from common tag types.
  230.      * @param    HTML_Element   $from  override settings from another element.
  231.      * @access   public
  232.      */
  233.      
  234.     function setValue($value{
  235.         // store the value in all situations
  236.         $this->value $value;
  237.         $tag strtolower($this->tag);
  238.         if (strpos($tag,':'!==  false{
  239.             $bits explode(':',$tag);
  240.             $tag $bits[1];
  241.         }
  242.         switch ($tag{
  243.             case 'input':
  244.                 switch (isset($this->attributes['type']strtolower($this->attributes['type']''{
  245.                     case 'checkbox':
  246.                         if (isset($this->attributes['checked'])) {
  247.                             unset($this->attributes['checked']);
  248.                         }
  249.                         // if value is nto set, it doesnt make any difference what you set ?
  250.                         if (!isset($this->attributes['value'])) {
  251.                             return;
  252.                         }
  253.                         //print_r($this); echo "SET TO "; serialize($value);
  254.                         if (isset($this->attributes['name']&& (substr($this->attributes['name'],-2== '[]')) {
  255.                             if (is_array($value&& 
  256.                                 in_array((string) $this->attributes['value'],$value)
  257.                                 {
  258.                                 $this->attributes['checked'=  true;
  259.                             }
  260.                             return;
  261.                         }
  262.                         if ($this->attributes['value'== $value{
  263.                             $this->attributes['checked'=  true;
  264.                         }
  265.                         
  266.                         
  267.                         return;
  268.                     case 'radio':
  269.                         if (isset($this->attributes['checked'])) {
  270.                             unset($this->attributes['checked']);
  271.                         }
  272.                         // if we dont have values associated yet, store it..
  273.                         if (!isset($this->attributes['value'])) {
  274.                             $this->value $value;
  275.                             return;
  276.                         }
  277.                         if ($this->attributes['value'== $value{
  278.                             $this->attributes['checked'=  true;
  279.                         }
  280.                         return;
  281.                     
  282.                     default:
  283.                         // no other input accepts array as a value.
  284.                         if (is_array($value)) {
  285.                             return;
  286.                         }
  287.                     
  288.                         $this->attributes['value'$value;
  289.                         return;
  290.                 }
  291.                 
  292.             case 'select':
  293.                 
  294.                 if (!is_array($value)) {
  295.                     $value = array($value);
  296.                 }
  297.                 
  298.                 // its setting the default value..
  299.                 
  300.                 foreach($this->children as $i=>$child{
  301.                     
  302.                     if (is_string($child)) {
  303.                         continue;
  304.                     }
  305.                     if ($child->tag == 'optgroup'{
  306.                         foreach($this->children[$i]->children as $ii=>$child{
  307.                         
  308.                             // does the value exist and match..
  309.                             if (isset($child->attributes['value']
  310.                                 && in_array((string) $child->attributes['value']$value)) 
  311.                             {
  312.                                 $this->children[$i]->children[$ii]->attributes['selected'
  313.                                     isset($this->attributes['flexy:xhtml']'selected' : true;
  314.                                 continue;
  315.                             }
  316.                             if (isset($child->attributes['value']&& 
  317.                                 isset($this->children[$i]->children[$ii]->attributes['selected'])) 
  318.                             {
  319.                                 unset($this->children[$i]->children[$ii]->attributes['selected']);
  320.                                 continue;
  321.                             }
  322.                             // value doesnt exst..
  323.                           
  324.                             if (isset($this->children[$i]->children[$ii]->attributes['selected'])) {
  325.                                 unset($this->children[$i]->children[$ii]->attributes['selected']);
  326.                                 continue;
  327.                             }
  328.                         }
  329.                         continue;
  330.                     }
  331.                     
  332.                     // standard option value...
  333.                     //echo "testing {$child->attributes['value']} against ". print_r($value,true)."\n";
  334.                     // does the value exist and match..
  335.                      
  336.                     if (isset($child->attributes['value']
  337.                         && in_array((string) $child->attributes['value']$value)) 
  338.                     {
  339.                         
  340.                       
  341.                         $this->children[$i]->attributes['selected'
  342.                             isset($this->attributes['flexy:xhtml']'selected' : true;;
  343.                         continue;
  344.                     }
  345.                     // no value attribute try and use the contents.
  346.                     if (!isset($child->attributes['value'])
  347.                         && is_string($child->children[0])
  348.                         && in_array((string) $child->children[0]$value))
  349.                     {
  350.                         
  351.                         $this->children[$i]->attributes['selected'=
  352.                             isset($this->attributes['flexy:xhtml']'selected' : true;
  353.                         continue;
  354.                     }
  355.                      
  356.                     if (isset($child->attributes['value']&& 
  357.                         isset($this->children[$i]->attributes['selected'])) 
  358.                     {
  359.                         //echo "clearing selected\n";
  360.                         unset($this->children[$i]->attributes['selected']);
  361.                         continue;
  362.                     }
  363.                     // value doesnt exst..
  364.                     
  365.                     if (isset($this->children[$i]->attributes['selected'])) {
  366.                         //echo "clearing selected\n";
  367.                         unset($this->children[$i]->attributes['selected']);
  368.                         continue;
  369.                     }
  370.                     
  371.                     
  372.                 }
  373.                 return;
  374.             case 'textarea':
  375.                 $charset = empty($GLOBALS['HTML_Template_Flexy']['options']['charset']'ISO-8859-1' $GLOBALS['HTML_Template_Flexy']['options']['charset'];
  376.                 $this->children = array(htmlspecialchars($value,ENT_COMPAT,$charset));
  377.                 return;
  378.             case '':  // dummy objects.
  379.                 $this->value $value;
  380.                 return;
  381.                 
  382.             // XUL elements
  383.             case 'menulist':
  384.             case 'textbox':
  385.             case 'checkbox':
  386.                 require_once 'HTML/Template/Flexy/Element/Xul.php';
  387.                 HTML_Template_Flexy_Element_Xul::setValue($this,$value);
  388.                 return ;
  389.                 
  390.             default:
  391.                 if (is_array($value)) {
  392.                     return;
  393.                 }
  394.                 $this->value $value;
  395.         }
  396.             
  397.         
  398.     
  399.     
  400.     }
  401.     /**
  402.      * Utility function equivilant to HTML_Select - loadArray **
  403.      * but using
  404.      * key=>value maps
  405.      * <option value="key">Value</option>
  406.      * Key=key (eg. both the same) maps to
  407.      * <option>key</option>
  408.      * and label = array(key=>value) maps to
  409.      * <optgroup label="label"> <option value="key">value</option></optgroup>
  410.      * 
  411.      * $element->setOptions(array('a'=>'xxx','b'=>'yyy'));
  412.      * or
  413.      * $element->setOptions(array('a','b','c','d'),true);
  414.      *
  415.      *
  416.      *.
  417.      * @param    HTML_Element   $from  override settings from another element.
  418.      * @param    HTML_Element   $noValue  ignore the key part of the array
  419.      * @access   public
  420.      */
  421.      
  422.     function setOptions($array,$noValue=false{
  423.         if (!is_array($array)) {
  424.             $this->children = array();
  425.             return;
  426.         }
  427.         
  428.         $charset = empty($GLOBALS['HTML_Template_Flexy']['options']['charset']'ISO-8859-1' $GLOBALS['HTML_Template_Flexy']['options']['charset'];
  429.         
  430.         $tag strtolower($this->tag);
  431.         $namespace '';
  432.         if (false !== strpos($this->tag':')) {
  433.             
  434.             $bits explode(':',$this->tag);
  435.             $namespace $bits[0':';
  436.             $tag strtolower($bits[1]);
  437.             
  438.         }
  439.         // if we have specified a xultag!!?
  440.         if (strlen($tag&& ($tag != 'select')) {
  441.                 require_once 'HTML/Template/Flexy/Element/Xul.php';
  442.                 return HTML_Template_Flexy_Element_Xul::setOptions($this,$array,$noValue);
  443.         }
  444.         
  445.         foreach($array as $k=>$v{
  446.             if (is_array($v)) {     // optgroup
  447.                 $child = new HTML_Template_Flexy_Element($namespace 'optgroup',array('label'=>$k));
  448.                 foreach($v as $kk=>$vv{
  449.                     $atts=array();
  450.                     if (($kk !== $vv&& !$noValue{
  451.                         $atts = array('value'=>$kk);
  452.                     else {
  453.                         $atts = array('value'=>$vv);
  454.                     }
  455.                     $add = new HTML_Template_Flexy_Element($namespace 'option',$atts);
  456.                     $add->children = array(htmlspecialchars($vv,ENT_COMPAT,$charset));
  457.                     $child->children[$add;
  458.                 }
  459.                 $this->children[$child;
  460.                 continue;
  461.             
  462.             $atts=array();
  463.             if (($k !== $v&& !$noValue{
  464.                 $atts = array('value'=>$k);
  465.             else {
  466.                 $atts = array('value'=>$v);
  467.             }
  468.             $add = new HTML_Template_Flexy_Element($namespace 'option',$atts);
  469.             $add->children = array(htmlspecialchars($v,ENT_COMPAT,$charset));
  470.             $this->children[$add;
  471.         }
  472.        
  473.     }
  474.     /**
  475.      * Sets the HTML attributes
  476.      * @param    mixed   $attributes     Either a typical HTML attribute string or an associative array
  477.      * @access   public
  478.      */
  479.      
  480.     function setAttributes($attributes)
  481.     {
  482.         $attrs$this->parseAttributes($attributes);
  483.         if (!is_array($attrs)) {
  484.             return false;
  485.         }
  486.         foreach ($attrs as $key => $value{
  487.             $this->attributes[$key$value;
  488.         }
  489.     // end func updateAttributes
  490.  
  491.     /**
  492.      * Removes an attributes
  493.      * 
  494.      * @param     string    $attr   Attribute name
  495.      * @since     1.4
  496.      * @access    public
  497.      * @return    void 
  498.      * @throws
  499.      */
  500.     function removeAttributes($attrs)
  501.     {
  502.         if (is_string($attrs)) {
  503.             $attrs = array($attrs);
  504.         }
  505.         foreach ($attrs as $attr
  506.             if (isset($this->attributes[strtolower($attr)])) {
  507.                  $this->attributes[strtolower($attr)= false;
  508.             
  509.         }
  510.     //end func removeAttribute
  511.  
  512.       
  513.     /**
  514.      * Output HTML and children
  515.      *
  516.      * @access    public
  517.      * @param     object    $overlay = merge data from object.
  518.      * @return    string 
  519.      * @abstract
  520.      */
  521.     function toHtml($overlay=false)
  522.     {
  523.          
  524.         //echo "BEFORE<PRE>";print_R($this);
  525.         $ret $this;
  526.         if ($overlay !== false{
  527.             $ret = HTML_Template_Flexy::mergeElement($this,$overlay);
  528.         }
  529.         
  530.         if ($ret->override !== false{
  531.             return $ret->override;
  532.         }
  533.         $prefix $ret->prefix;
  534.         if (is_object($prefix)) {
  535.             $prefix $prefix->toHtml();
  536.         }
  537.         $suffix $ret->suffix;
  538.         if (is_object($suffix)) {
  539.             $suffix $suffix->toHtml();
  540.         }
  541.         //echo "AFTER<PRE>";print_R($ret);
  542.       
  543.         $tag $this->tag;
  544.         if (strpos($tag,':'!==  false{
  545.             $bits explode(':',$tag);
  546.             $tag $bits[1];
  547.         }
  548.         // tags that never should have closers  
  549.         $close = "</{$ret->tag}>";
  550.         if (in_array(strtoupper($tag),array("INPUT","IMG"))) {
  551.             $close '';
  552.         }
  553.         if (isset($this->attributes['/'])) {
  554.             $close '';
  555.         }
  556.  
  557.         $close .= $suffix ;
  558.        
  559.         return "{$prefix}<{$ret->tag}".$ret->attributesToHTML('>'.$ret->childrenToHTML(.$close;
  560.         
  561.          
  562.     // end func toHtml
  563.     
  564.     
  565.     /**
  566.      * Output Open Tag and any children and not Child tag (designed for use with <form + hidden elements>
  567.      *
  568.      * @access    public
  569.      * @param     object    $overlay = merge data from object.
  570.      * @return    string 
  571.      * @abstract
  572.      */
  573.     function toHtmlnoClose($overlay=false)
  574.     {
  575.         $ret $this;
  576.         if ($ret->override !== false{
  577.             return $ret->override;
  578.         }
  579.         if ($overlay !== false{
  580.             $ret = HTML_Template_Flexy::mergeElement($this,$overlay);
  581.         }
  582.         
  583.   
  584.         return "<{$ret->tag}".$ret->attributesToHTML('>' $ret->childrenToHTML();
  585.        
  586.          
  587.     // end func toHtml
  588.     
  589.     
  590.     /**
  591.      * Output HTML and children
  592.      *
  593.      * @access    public
  594.      * @return    string 
  595.      * @abstract
  596.      */
  597.     function childrenToHtml()
  598.     {
  599.         $ret '';
  600.         foreach($this->children as $child{
  601.             if (!is_object($child)) {
  602.                 $ret .= $child;
  603.                 continue;
  604.             }
  605.             
  606.             $ret .= $child->toHtml();
  607.         }
  608.         return $ret;
  609.     // end func toHtml
  610.     
  611.      
  612.     
  613.     
  614.     
  615. // end class HTML_Template_Flexy_Element

Documentation generated on Mon, 11 Mar 2019 14:34:23 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.