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.20 2004/04/22 08:23:37 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.         $this->tag strtolower($tag);
  124.         $this->setAttributes($attributes);
  125.     // end constructor
  126.  
  127.       
  128.     /**
  129.      * Returns an HTML formatted attribute string
  130.      * @param    array   $attributes 
  131.      * @return   string 
  132.      * @access   private
  133.      */
  134.     function attributesToHTML()
  135.     {
  136.         $strAttr '';
  137.         foreach ($this->attributes as $key => $value{
  138.         
  139.             // you shouldn't do this, but It shouldnt barf when you do..
  140.             if (is_array($value|| is_object($value)) {
  141.                 continue;
  142.             }
  143.             if ($value === false{
  144.                 continue;
  145.             }
  146.             if ($value === true{
  147.                 // this is not xhtml compatible..
  148.                 $strAttr .= ' ' $key;
  149.             else {
  150.                 $strAttr .= ' ' $key '="' htmlspecialchars($value'"';
  151.             }
  152.         }
  153.         return $strAttr;
  154.     // end func _getAttrString
  155.  
  156.     /**
  157.      * Static Method to get key/value array from attributes.
  158.      * Returns a valid atrributes array from either a string or array
  159.      * @param    mixed   $attributes     Either a typical HTML attribute string or an associative array
  160.      * @access   private
  161.      */
  162.     function parseAttributes($attributes)
  163.     {
  164.         if (is_array($attributes)) {
  165.             $ret = array();
  166.             foreach ($attributes as $key => $value{
  167.                 if (is_int($key)) {
  168.                     $ret[strtolower($value)= true;
  169.                 else {
  170.                     $ret[strtolower($key)]   $value;
  171.                 }
  172.             }
  173.             return $ret;
  174.  
  175.         elseif (is_string($attributes)) {
  176.             $preg "/(([A-Za-z_:]|[^\\x00-\\x7F])([A-Za-z0-9_:.-]|[^\\x00-\\x7F])*)" .
  177.                 "([ \\n\\t\\r]+)?(=([ \\n\\t\\r]+)?(\"[^\"]*\"|'[^']*'|[^ \\n\\t\\r]*))?/";
  178.             if (preg_match_all($preg$attributes$regs)) {
  179.                 for ($counter=0; $counter<count($regs[1])$counter++{
  180.                     $name  $regs[1][$counter];
  181.                     $check $regs[0][$counter];
  182.                     $value $regs[7][$counter];
  183.                     if (trim($name== trim($check)) {
  184.                         $arrAttr[strtolower(trim($name))strtolower(trim($name));
  185.                     else {
  186.                         if (substr($value01== "\"" || substr($value01== "'"{
  187.                             $value substr($value1-1);
  188.                         }
  189.                         $arrAttr[strtolower(trim($name))trim($value);
  190.                     }
  191.                 }
  192.                 return $arrAttr;
  193.             }
  194.         }
  195.     // end func _parseAttributes
  196.  
  197.      
  198.      
  199.        
  200.     /**
  201.      * Utility function to set values from common tag types.
  202.      * @param    HTML_Element   $from  override settings from another element.
  203.      * @access   public
  204.      */
  205.      
  206.     function setValue($value{
  207.         // store the value in all situations
  208.         $this->value $value;
  209.         $tag $this->tag;
  210.         if (strpos($tag,':'!==  false{
  211.             $bits explode(':',$tag);
  212.             $tag $bits[1];
  213.         }
  214.         switch ($tag{
  215.             case 'input':
  216.                 switch (isset($this->attributes['type']strtolower($this->attributes['type']''{
  217.                     case 'checkbox':
  218.                         if (isset($this->attributes['checked'])) {
  219.                             unset($this->attributes['checked']);
  220.                         }
  221.                         //print_r($this); echo "SET TO "; serialize($value);
  222.                         if (substr($this->attributes['name'],-2== '[]'{
  223.                             if (is_array($value&& in_array($this->attributes['value'],$value))  {
  224.                                 $this->attributes['checked'= true;
  225.                             }
  226.                             
  227.                         else if ($this->attributes['value'== $value{
  228.                             $this->attributes['checked'= true;
  229.                         }
  230.                         
  231.                         
  232.                         return;
  233.                     case 'radio':
  234.                         if (isset($this->attributes['checked'])) {
  235.                             unset($this->attributes['checked']);
  236.                         }
  237.                         
  238.                         if ($this->attributes['value'== $value{
  239.                             $this->attributes['checked'= true;
  240.                         }
  241.                         return;
  242.                     
  243.                     default:
  244.                         // no other input accepts array as a value.
  245.                         if (is_array($value)) {
  246.                             return;
  247.                         }
  248.                     
  249.                         $this->attributes['value'$value;
  250.                         return;
  251.                 }
  252.                 
  253.             case 'select':
  254.                 
  255.                 if (!is_array($value)) {
  256.                     $value = array($value);
  257.                 }
  258.              
  259.                 // its setting the default value..
  260.                 foreach($this->children as $i=>$child{
  261.                     if ($child->tag == 'optgroup'{
  262.                         foreach($this->children[$i]->children as $ii=>$child{
  263.                         
  264.                             // does the value exist and match..
  265.                             if (isset($child->attributes['value']
  266.                                 && in_array($child->attributes['value']$value)) 
  267.                             {
  268.                                 $this->children[$i]->children[$ii]->attributes['selected'= true;
  269.                                 continue;
  270.                             }
  271.                             if (isset($child->attributes['value']&& 
  272.                                 isset($this->children[$i]->children[$ii]->attributes['selected'])) 
  273.                             {
  274.                                 unset($this->children[$i]->children[$ii]->attributes['selected']);
  275.                                 continue;
  276.                             }
  277.                             // value doesnt exst..
  278.                             if (in_array($child->children[0],$value)) {
  279.                                 $this->children[$i]->children[$ii]->attributes['selected'= true;
  280.                                 continue;
  281.                             }
  282.                             
  283.                             if (in_array($child->children[0],$value)) {
  284.                                 $this->children[$i]->children[$ii]->attributes['selected'= true;
  285.                                 continue;
  286.                             }
  287.                             if (isset($this->children[$i]->children[$ii]->attributes['selected'])) {
  288.                                 unset($this->children[$i]->children[$ii]->attributes['selected']);
  289.                                 continue;
  290.                             }
  291.                         }
  292.                         continue;
  293.                     }
  294.                     
  295.                     // standard option value...
  296.                     
  297.                      // does the value exist and match..
  298.                     if (isset($child->attributes['value']
  299.                         && in_array($child->attributes['value']$value)) 
  300.                     {
  301.                         $this->children[$i]->attributes['selected'= true;
  302.                         continue;
  303.                     }
  304.                     if (isset($child->attributes['value']&& 
  305.                         isset($this->children[$i]->attributes['selected'])) 
  306.                     {
  307.                         unset($this->children[$i]->attributes['selected']);
  308.                         continue;
  309.                     }
  310.                     // value doesnt exst..
  311.                     if (in_array($child->children[0],$value)) {
  312.                         $this->children[$i]->attributes['selected'= true;
  313.                         continue;
  314.                     }
  315.                     
  316.                     if (in_array($child->children[0],$value)) {
  317.                         $this->children[$i]->attributes['selected'= true;
  318.                         continue;
  319.                     }
  320.                     if (isset($this->children[$i]->attributes['selected'])) {
  321.                         unset($this->children[$i]->attributes['selected']);
  322.                         continue;
  323.                     }
  324.                 }
  325.                 return;
  326.             case 'textarea':
  327.                 $this->children = array(htmlspecialchars($value));
  328.                 return;
  329.             case '':  // dummy objects.
  330.                 $this->value $value;
  331.                 return;
  332.             default:
  333.                 if (is_array($value)) {
  334.                     return;
  335.                 }
  336.                 $this->value $value;
  337.         }
  338.             
  339.         
  340.     
  341.     
  342.     }
  343.     /**
  344.      * Utility function equivilant to HTML_Select - loadArray **
  345.      * but using
  346.      * key=>value maps
  347.      * <option value="key">Value</option>
  348.      * Key=key (eg. both the same) maps to
  349.      * <option>key</option>
  350.      * and label = array(key=>value) maps to
  351.      * <optgroup label="label"> <option value="key">value</option></optgroup>
  352.      * 
  353.      * $element->setOptions(array('a'=>'xxx','b'=>'yyy'));
  354.      * or
  355.      * $element->setOptions(array('a','b','c','d'),true);
  356.      *
  357.      *
  358.      *.
  359.      * @param    HTML_Element   $from  override settings from another element.
  360.      * @param    HTML_Element   $noValue  ignore the key part of the array
  361.      * @access   public
  362.      */
  363.      
  364.     function setOptions($array,$noValue=false{
  365.         if (!is_array($array)) {
  366.             $this->children = array();
  367.             return;
  368.         }
  369.         foreach($array as $k=>$v{
  370.             if (is_array($v)) {     // optgroup
  371.                 $child = new HTML_Template_Flexy_Element('optgroup',array('label'=>$kk));
  372.                 foreach($v as $kk=>$vv{
  373.                     $atts=array();
  374.                     if (($kk != $vv&& !$noValue{
  375.                         $atts = array('value'=>$kk);
  376.                     }
  377.                     $add = new HTML_Template_Flexy_Element('option',$atts);
  378.                     $add->children = array(htmlspecialchars($vv));
  379.                     $child->children[$add;
  380.                 }
  381.                 $this->children[$child;
  382.                 continue;
  383.             
  384.             $atts=array();
  385.             if (($k !== $v&& !$noValue{
  386.                 $atts = array('value'=>$k);
  387.             }
  388.             $add = new HTML_Template_Flexy_Element('option',$atts);
  389.             $add->children = array(htmlspecialchars($v));
  390.             $this->children[$add;
  391.         }
  392.        
  393.     }
  394.     /**
  395.      * Sets the HTML attributes
  396.      * @param    mixed   $attributes     Either a typical HTML attribute string or an associative array
  397.      * @access   public
  398.      */
  399.      
  400.     function setAttributes($attributes)
  401.     {
  402.         $attrs$this->parseAttributes($attributes);
  403.         if (!is_array($attrs)) {
  404.             return false;
  405.         }
  406.         foreach ($attrs as $key => $value{
  407.             $this->attributes[$key$value;
  408.         }
  409.     // end func updateAttributes
  410.  
  411.     /**
  412.      * Removes an attributes
  413.      * 
  414.      * @param     string    $attr   Attribute name
  415.      * @since     1.4
  416.      * @access    public
  417.      * @return    void 
  418.      * @throws
  419.      */
  420.     function removeAttributes($attrs)
  421.     {
  422.         if (is_string($attrs)) {
  423.             $attrs = array($attrs);
  424.         }
  425.         foreach ($attrs as $attr
  426.             if (isset($this->attributes[strtolower($attr)])) {
  427.                  $this->attributes[strtolower($attr)= false;
  428.             
  429.         }
  430.     //end func removeAttribute
  431.  
  432.       
  433.     /**
  434.      * Output HTML and children
  435.      *
  436.      * @access    public
  437.      * @param     object    $overlay = merge data from object.
  438.      * @return    string 
  439.      * @abstract
  440.      */
  441.     function toHtml($overlay=false)
  442.     {
  443.          
  444.         //echo "BEFORE<PRE>";print_R($this);
  445.         $ret $this;
  446.         if ($overlay !== false{
  447.             $ret = HTML_Template_Flexy::mergeElement($this,$overlay);
  448.         }
  449.         
  450.         if ($ret->override !== false{
  451.             return $ret->override;
  452.         }
  453.         $prefix $ret->prefix;
  454.         if (is_object($prefix)) {
  455.             $prefix $prefix->toHtml();
  456.         }
  457.         $suffix $ret->suffix;
  458.         if (is_object($suffix)) {
  459.             $suffix $suffix->toHtml();
  460.         }
  461.         //echo "AFTER<PRE>";print_R($ret);
  462.         // tags that never should have closers  
  463.         $tag $this->tag;
  464.         if (strpos($tag,':'!==  false{
  465.             $bits explode(':',$tag);
  466.             $tag $bits[1];
  467.         }
  468.         $close in_array(strtoupper($tag),array("INPUT","IMG")) '' : "</{$ret->tag}>{$suffix}" ;
  469.         
  470.         return "{$prefix}<{$ret->tag}".$ret->attributesToHTML('>'.$ret->childrenToHTML(.$close;
  471.         
  472.          
  473.     // end func toHtml
  474.     
  475.     
  476.     /**
  477.      * Output Open Tag and any children and not Child tag (designed for use with <form + hidden elements>
  478.      *
  479.      * @access    public
  480.      * @param     object    $overlay = merge data from object.
  481.      * @return    string 
  482.      * @abstract
  483.      */
  484.     function toHtmlnoClose($overlay=false)
  485.     {
  486.         $ret $this;
  487.         if ($overlay !== false{
  488.             $ret = HTML_Template_Flexy::mergeElement($this,$overlay);
  489.         }
  490.         
  491.   
  492.         return "<{$ret->tag}".$ret->attributesToHTML('>' $ret->childrenToHTML();
  493.        
  494.          
  495.     // end func toHtml
  496.     
  497.     
  498.     /**
  499.      * Output HTML and children
  500.      *
  501.      * @access    public
  502.      * @return    string 
  503.      * @abstract
  504.      */
  505.     function childrenToHtml()
  506.     {
  507.         $ret '';
  508.         foreach($this->children as $child{
  509.             if (!is_object($child)) {
  510.                 $ret .= $child;
  511.                 continue;
  512.             }
  513.             
  514.             $ret .= $child->toHtml();
  515.         }
  516.         return $ret;
  517.     // end func toHtml
  518.     
  519.      
  520.     
  521.     
  522.     
  523. // 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.