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.52 2009/03/02 07:58:38 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.                             // removed - see bug 15279 - not sure if there is any knock on effects from this.
  261.                             ///return;
  262.                         }
  263.                         if ($this->attributes['value'== $value{
  264.                             $this->attributes['checked'=  true;
  265.                         }
  266.                         
  267.                         
  268.                         return;
  269.                     case 'radio':
  270.                         if (isset($this->attributes['checked'])) {
  271.                             unset($this->attributes['checked']);
  272.                         }
  273.                         // if we dont have values associated yet, store it..
  274.                         if (!isset($this->attributes['value'])) {
  275.                             $this->value $value;
  276.                             return;
  277.                         }
  278.                         if ($this->attributes['value'== $value{
  279.                             $this->attributes['checked'=  true;
  280.                         }
  281.                         return;
  282.                     
  283.                     default:
  284.                         // no other input accepts array as a value.
  285.                         if (is_array($value)) {
  286.                             return;
  287.                         }
  288.                     
  289.                         $this->attributes['value'$value;
  290.                         return;
  291.                 }
  292.                 
  293.             case 'select':
  294.                 
  295.                 if (!is_array($value)) {
  296.                     $value = array($value);
  297.                 }
  298.                 
  299.                 // its setting the default value..
  300.                 
  301.                 foreach($this->children as $i=>$child{
  302.                     
  303.                     if (is_string($child)) {
  304.                         continue;
  305.                     }
  306.                     if ($child->tag == 'optgroup'{
  307.                         foreach($this->children[$i]->children as $ii=>$child{
  308.                         
  309.                             // does the value exist and match..
  310.                             if (isset($child->attributes['value']
  311.                                 && in_array((string) $child->attributes['value']$value)) 
  312.                             {
  313.                                 $this->children[$i]->children[$ii]->attributes['selected'
  314.                                     isset($this->attributes['flexy:xhtml']'selected' : true;
  315.                                 continue;
  316.                             }
  317.                             if (isset($child->attributes['value']&& 
  318.                                 isset($this->children[$i]->children[$ii]->attributes['selected'])) 
  319.                             {
  320.                                 unset($this->children[$i]->children[$ii]->attributes['selected']);
  321.                                 continue;
  322.                             }
  323.                             // value doesnt exst..
  324.                           
  325.                             if (isset($this->children[$i]->children[$ii]->attributes['selected'])) {
  326.                                 unset($this->children[$i]->children[$ii]->attributes['selected']);
  327.                                 continue;
  328.                             }
  329.                         }
  330.                         continue;
  331.                     }
  332.                     
  333.                     // standard option value...
  334.                     //echo "testing {$child->attributes['value']} against ". print_r($value,true)."\n";
  335.                     // does the value exist and match..
  336.                      
  337.                     if (isset($child->attributes['value']
  338.                         && in_array((string) $child->attributes['value']$value)) 
  339.                     {
  340.                         
  341.                       
  342.                         $this->children[$i]->attributes['selected'
  343.                             isset($this->attributes['flexy:xhtml']'selected' : true;;
  344.                         continue;
  345.                     }
  346.                     // no value attribute try and use the contents.
  347.                     if (!isset($child->attributes['value'])
  348.                         && is_string($child->children[0])
  349.                         && in_array((string) $child->children[0]$value))
  350.                     {
  351.                         
  352.                         $this->children[$i]->attributes['selected'=
  353.                             isset($this->attributes['flexy:xhtml']'selected' : true;
  354.                         continue;
  355.                     }
  356.                      
  357.                     if (isset($child->attributes['value']&& 
  358.                         isset($this->children[$i]->attributes['selected'])) 
  359.                     {
  360.                         //echo "clearing selected\n";
  361.                         unset($this->children[$i]->attributes['selected']);
  362.                         continue;
  363.                     }
  364.                     // value doesnt exst..
  365.                     
  366.                     if (isset($this->children[$i]->attributes['selected'])) {
  367.                         //echo "clearing selected\n";
  368.                         unset($this->children[$i]->attributes['selected']);
  369.                         continue;
  370.                     }
  371.                     
  372.                     
  373.                 }
  374.                 return;
  375.             case 'textarea':
  376.             case 'label':
  377.                 $charset = empty($GLOBALS['HTML_Template_Flexy']['options']['charset']'ISO-8859-1' $GLOBALS['HTML_Template_Flexy']['options']['charset'];
  378.                 $this->children = array(htmlspecialchars($value,ENT_COMPAT,$charset));
  379.                 return;
  380.             case '':  // dummy objects.
  381.                 $this->value $value;
  382.                 return;
  383.                 
  384.             // XUL elements
  385.             case 'menulist':
  386.             case 'textbox':
  387.             case 'checkbox':
  388.                 require_once 'HTML/Template/Flexy/Element/Xul.php';
  389.                 HTML_Template_Flexy_Element_Xul::setValue($this,$value);
  390.                 return ;
  391.                 
  392.             default:
  393.                 if (is_array($value)) {
  394.                     return;
  395.                 }
  396.                 $this->value $value;
  397.         }
  398.             
  399.         
  400.     
  401.     
  402.     }
  403.     /**
  404.      * Utility function equivilant to HTML_Select - loadArray **
  405.      * but using
  406.      * key=>value maps
  407.      * <option value="key">Value</option>
  408.      * Key=key (eg. both the same) maps to
  409.      * <option>key</option>
  410.      * and label = array(key=>value) maps to
  411.      * <optgroup label="label"> <option value="key">value</option></optgroup>
  412.      * 
  413.      * $element->setOptions(array('a'=>'xxx','b'=>'yyy'));
  414.      * or
  415.      * $element->setOptions(array('a','b','c','d'),true);
  416.      *
  417.      *
  418.      *.
  419.      * @param    HTML_Element   $from  override settings from another element.
  420.      * @param    HTML_Element   $noValue  ignore the key part of the array
  421.      * @access   public
  422.      */
  423.      
  424.     function setOptions($array,$noValue=false
  425.     {
  426.         if (!is_array($array)) {
  427.             $this->children = array();
  428.             return;
  429.         }
  430.         
  431.         $charset = empty($GLOBALS['HTML_Template_Flexy']['options']['charset']'ISO-8859-1' $GLOBALS['HTML_Template_Flexy']['options']['charset'];
  432.         
  433.         $tag strtolower($this->tag);
  434.         $namespace '';
  435.         if (false !== strpos($this->tag':')) {
  436.             
  437.             $bits explode(':',$this->tag);
  438.             $namespace $bits[0':';
  439.             $tag strtolower($bits[1]);
  440.             
  441.         }
  442.         // if we have specified a xultag!!?
  443.         if (strlen($tag&& ($tag != 'select')) {
  444.                 require_once 'HTML/Template/Flexy/Element/Xul.php';
  445.                 return HTML_Template_Flexy_Element_Xul::setOptions($this,$array,$noValue);
  446.         }
  447.         
  448.         foreach($array as $k=>$v{
  449.             if (is_array($v)) {     // optgroup
  450.                 $child = new HTML_Template_Flexy_Element($namespace 'optgroup',array('label'=>$k));
  451.                 foreach($v as $kk=>$vv{
  452.                     $atts=array();
  453.                     if (($kk !== $vv&& !$noValue{
  454.                         $atts = array('value'=>$kk);
  455.                     else {
  456.                         $atts = array('value'=>$vv);
  457.                     }
  458.                     $add = new HTML_Template_Flexy_Element($namespace 'option',$atts);
  459.                     $add->children = array(htmlspecialchars($vv,ENT_COMPAT,$charset));
  460.                     $child->children[$add;
  461.                 }
  462.                 $this->children[$child;
  463.                 continue;
  464.             
  465.             $atts=array();
  466.             if (($k !== $v&& !$noValue{
  467.                 $atts = array('value'=>$k);
  468.             else {
  469.                 $atts = array('value'=>$v);
  470.             }
  471.             $add = new HTML_Template_Flexy_Element($namespace 'option',$atts);
  472.             $add->children = array(htmlspecialchars($v,ENT_COMPAT,$charset));
  473.             $this->children[$add;
  474.         }
  475.        
  476.     }
  477.     
  478.     
  479.     
  480.     /**
  481.      *  Returns THIS select element's options as an associative array
  482.      *  Validates that $this element is "select"
  483.      * @return array $options
  484.      * @access public
  485.     */
  486.     function getOptions()
  487.     {
  488.  
  489.         $tag strtolower($this->tag);
  490.         $namespace '';
  491.         if (false !== strpos($this->tag':')) {
  492.             $bits explode(':',$this->tag);
  493.             $namespace $bits[0':';
  494.             $tag strtolower($bits[1]);
  495.         }
  496.  
  497.         // this is not a select element
  498.         if (strlen($tag&& ($tag != 'select'))  {
  499.             return false;
  500.         }
  501.  
  502.         // creates an associative array that can be used by setOptions()
  503.         // null does work for no value ( a "Please Choose" option, for example)
  504.         foreach ($this->children as $child{
  505.             if (is_object($child)) {
  506.                 $child->attributes['value'= isset($child->attributes['value']$child->attributes['value''';
  507.                 $children[$child->attributes['value']] $child->children[0];
  508.             }
  509.         }
  510.         return $children;
  511.     }
  512.  
  513.      /**
  514.      *  Removes all of this element's options
  515.      *  Validates that $this element is "select"
  516.      * @return bool result
  517.      * @access public
  518.     */
  519.     function clearOptions($children = array())
  520.     {
  521.         $tag strtolower($this->tag);
  522.         $namespace '';
  523.         if (false !== strpos($this->tag':')) {
  524.             $bits explode(':',$this->tag);
  525.             $namespace $bits[0':';
  526.             $tag strtolower($bits[1]);
  527.         }
  528.  
  529.         // this is not a select element
  530.         if (strlen($tag&& ($tag != 'select')) {
  531.             return false;
  532.         }
  533.  
  534.         // clear this select's options
  535.         $this->children = array(null);
  536.         $this->values = array(null);
  537.  
  538.         // If called with an array of new options go ahead and set them
  539.         $this->setOptions($children);
  540.  
  541.         return true;
  542.     }
  543.     
  544.     /**
  545.      * Sets the HTML attributes
  546.      * @param    mixed   $attributes     Either a typical HTML attribute string or an associative array
  547.      * @access   public
  548.      */
  549.      
  550.     function setAttributes($attributes)
  551.     {
  552.         $attrs$this->parseAttributes($attributes);
  553.         if (!is_array($attrs)) {
  554.             return false;
  555.         }
  556.         foreach ($attrs as $key => $value{
  557.             $this->attributes[$key$value;
  558.         }
  559.     // end func updateAttributes
  560.  
  561.     /**
  562.      * Removes an attributes
  563.      * 
  564.      * @param     string    $attr   Attribute name
  565.      * @since     1.4
  566.      * @access    public
  567.      * @return    void 
  568.      * @throws
  569.      */
  570.     function removeAttributes($attrs)
  571.     {
  572.         if (is_string($attrs)) {
  573.             $attrs = array($attrs);
  574.         }
  575.         foreach ($attrs as $attr
  576.             if (isset($this->attributes[strtolower($attr)])) {
  577.                  $this->attributes[strtolower($attr)= false;
  578.             
  579.         }
  580.     //end func removeAttribute
  581.  
  582.       
  583.     /**
  584.      * Output HTML and children
  585.      *
  586.      * @access    public
  587.      * @param     object    $overlay = merge data from object.
  588.      * @return    string 
  589.      * @abstract
  590.      */
  591.     function toHtml($overlay=false)
  592.     {
  593.          
  594.         //echo "BEFORE<PRE>";print_R($this);
  595.         $ret $this;
  596.         if ($overlay !== false{
  597.             $ret = HTML_Template_Flexy::mergeElement($this,$overlay);
  598.         }
  599.         
  600.         if ($ret->override !== false{
  601.             return $ret->override;
  602.         }
  603.         $prefix $ret->prefix;
  604.         if (is_object($prefix)) {
  605.             $prefix $prefix->toHtml();
  606.         }
  607.         $suffix $ret->suffix;
  608.         if (is_object($suffix)) {
  609.             $suffix $suffix->toHtml();
  610.         }
  611.         //echo "AFTER<PRE>";print_R($ret);
  612.       
  613.         $tag $this->tag;
  614.         if (strpos($tag,':'!==  false{
  615.             $bits explode(':',$tag);
  616.             $tag $bits[1];
  617.         }
  618.         // tags that never should have closers  
  619.         $close = "</{$ret->tag}>";
  620.          
  621.         if (in_array(strtoupper($tag),array("INPUT","IMG""LINK""META""HR""BR"))) {
  622.             $close '';
  623.             if (isset($ret->attributes['flexy:xhtml'])) {
  624.                 $this->attributes['/'= true;
  625.             }
  626.  
  627.         }
  628.         if (isset($this->attributes['/'])) {
  629.             $close '';
  630.         }
  631.  
  632.         $close .= $suffix ;
  633.        
  634.         return "{$prefix}<{$ret->tag}" . $ret->attributesToHTML('>' .  $ret->childrenToHTML($close;
  635.         
  636.          
  637.     // end func toHtml
  638.     
  639.     
  640.     /**
  641.      * Output Open Tag and any children and not Child tag (designed for use with <form + hidden elements>
  642.      *
  643.      * @access    public
  644.      * @param     object    $overlay = merge data from object.
  645.      * @return    string 
  646.      * @abstract
  647.      */
  648.     function toHtmlnoClose($overlay=false)
  649.     {
  650.         $ret $this;
  651.         if ($ret->override !== false{
  652.             return $ret->override;
  653.         }
  654.         if ($overlay !== false{
  655.             $ret = HTML_Template_Flexy::mergeElement($this,$overlay);
  656.         }
  657.         
  658.   
  659.         return "<{$ret->tag}".$ret->attributesToHTML('>' $ret->childrenToHTML();
  660.        
  661.          
  662.     // end func toHtml
  663.     
  664.     
  665.     /**
  666.      * Output HTML and children
  667.      *
  668.      * @access    public
  669.      * @return    string 
  670.      * @abstract
  671.      */
  672.     function childrenToHtml()
  673.     {
  674.         $ret '';
  675.         foreach($this->children as $child{
  676.             if (!is_object($child)) {
  677.                 $ret .= $child;
  678.                 continue;
  679.             }
  680.             
  681.             $ret .= $child->toHtml();
  682.         }
  683.         return $ret;
  684.     // end func toHtml
  685.     
  686.      
  687.     
  688.     
  689.     
  690. // end class HTML_Template_Flexy_Element

Documentation generated on Mon, 11 Mar 2019 15:30:28 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.