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.13 2003/12/20 02:12:01 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.         
  210.         
  211.         switch ($this->tag{
  212.             case 'input':
  213.                 switch (isset($this->attributes['type']strtolower($this->attributes['type']''{
  214.                     case 'checkbox':
  215.                         if (isset($this->attributes['checked'])) {
  216.                             unset($this->attributes['checked']);
  217.                         }
  218.                         //print_r($this); echo "SET TO "; serialize($value);
  219.                         if (substr($this->attributes['name'],-2== '[]'{
  220.                             if (is_array($value&& in_array($this->attributes['value'],$value))  {
  221.                                 $this->attributes['checked'= true;
  222.                             }
  223.                             
  224.                         else if ($this->attributes['value'== $value{
  225.                             $this->attributes['checked'= true;
  226.                         }
  227.                         
  228.                         
  229.                         return;
  230.                     case 'radio':
  231.                         if (isset($this->attributes['checked'])) {
  232.                             unset($this->attributes['checked']);
  233.                         }
  234.                         
  235.                         if ($this->attributes['value'== $value{
  236.                             $this->attributes['checked'= true;
  237.                         }
  238.                         return;
  239.                     
  240.                     default:
  241.                         // no other input accepts array as a value.
  242.                         if (is_array($value)) {
  243.                             return;
  244.                         }
  245.                     
  246.                         $this->attributes['value'$value;
  247.                         return;
  248.                 }
  249.                 
  250.             case 'select':
  251.                 
  252.                 if (!is_array($value)) {
  253.                     $value = array($value);
  254.                 }
  255.              
  256.                 // its setting the default value..
  257.                 foreach($this->children as $i=>$child{
  258.                     if ($child->tag == 'optgroup'{
  259.                         foreach($this->children[$i]->children as $ii=>$child{
  260.                         
  261.                             // does the value exist and match..
  262.                             if (isset($child->attributes['value']
  263.                                 && in_array($child->attributes['value']$value)) 
  264.                             {
  265.                                 $this->children[$i]->children[$ii]->attributes['selected'= true;
  266.                                 continue;
  267.                             }
  268.                             if (isset($child->attributes['value']&& 
  269.                                 isset($this->children[$i]->children[$ii]->attributes['selected'])) 
  270.                             {
  271.                                 unset($this->children[$i]->children[$ii]->attributes['selected']);
  272.                                 continue;
  273.                             }
  274.                             // value doesnt exst..
  275.                             if (in_array($child->children[0],$value)) {
  276.                                 $this->children[$i]->children[$ii]->attributes['selected'= true;
  277.                                 continue;
  278.                             }
  279.                             
  280.                             if (in_array($child->children[0],$value)) {
  281.                                 $this->children[$i]->children[$ii]->attributes['selected'= true;
  282.                                 continue;
  283.                             }
  284.                             if (isset($this->children[$i]->children[$ii]->attributes['selected'])) {
  285.                                 unset($this->children[$i]->children[$ii]->attributes['selected']);
  286.                                 continue;
  287.                             }
  288.                         }
  289.                         continue;
  290.                     }
  291.                     
  292.                     // standard option value...
  293.                     
  294.                      // does the value exist and match..
  295.                     if (isset($child->attributes['value']
  296.                         && in_array($child->attributes['value']$value)) 
  297.                     {
  298.                         $this->children[$i]->attributes['selected'= true;
  299.                         continue;
  300.                     }
  301.                     if (isset($child->attributes['value']&& 
  302.                         isset($this->children[$i]->attributes['selected'])) 
  303.                     {
  304.                         unset($this->children[$i]->attributes['selected']);
  305.                         continue;
  306.                     }
  307.                     // value doesnt exst..
  308.                     if (in_array($child->children[0],$value)) {
  309.                         $this->children[$i]->attributes['selected'= true;
  310.                         continue;
  311.                     }
  312.                     
  313.                     if (in_array($child->children[0],$value)) {
  314.                         $this->children[$i]->attributes['selected'= true;
  315.                         continue;
  316.                     }
  317.                     if (isset($this->children[$i]->attributes['selected'])) {
  318.                         unset($this->children[$i]->attributes['selected']);
  319.                         continue;
  320.                     }
  321.                 }
  322.                 return;
  323.             case 'textarea':
  324.                 $this->children = array($value);
  325.                 return;
  326.             case '':  // dummy objects.
  327.                 $this->value $value;
  328.                 return;
  329.             default:
  330.                 if (is_array($value)) {
  331.                     return;
  332.                 }
  333.                 $this->value $value;
  334.         }
  335.             
  336.         
  337.     
  338.     
  339.     }
  340.     /**
  341.      * Utility function equivilant to HTML_Select - loadArray **
  342.      * but using
  343.      * key=>value maps
  344.      * <option value="key">Value</option>
  345.      * Key=key (eg. both the same) maps to
  346.      * <option>key</option>
  347.      * and label = array(key=>value) maps to
  348.      * <optgroup label="label"> <option value="key">value</option></optgroup>
  349.      * 
  350.      * $element->setOptions(array('a'=>'xxx','b'=>'yyy'));
  351.      * or
  352.      * $element->setOptions(array('a','b','c','d'),true);
  353.      *
  354.      *
  355.      *.
  356.      * @param    HTML_Element   $from  override settings from another element.
  357.      * @param    HTML_Element   $noValue  ignore the key part of the array
  358.      * @access   public
  359.      */
  360.      
  361.     function setOptions($array,$noValue=false{
  362.         if (!is_array($array)) {
  363.             $this->children = array();
  364.             return;
  365.         }
  366.         foreach($array as $k=>$v{
  367.             if (is_array($v)) {     // optgroup
  368.                 $child = new HTML_Template_Flexy_Element('optgroup',array('label'=>$kk));
  369.                 foreach($v as $kk=>$vv{
  370.                     $atts=array();
  371.                     if (($kk != $vv&& !$noValue{
  372.                         $atts = array('value'=>$kk);
  373.                     }
  374.                     $add = new HTML_Template_Flexy_Element('option',$atts);
  375.                     $add->children = array($vv);
  376.                     $child->children[$add;
  377.                 }
  378.                 $this->children[$child;
  379.                 continue;
  380.             
  381.             $atts=array();
  382.             if (($k !== $v&& !$noValue{
  383.                 $atts = array('value'=>$k);
  384.             }
  385.             $add = new HTML_Template_Flexy_Element('option',$atts);
  386.             $add->children = array($v);
  387.             $this->children[$add;
  388.         }
  389.        
  390.     }
  391.     /**
  392.      * Sets the HTML attributes
  393.      * @param    mixed   $attributes     Either a typical HTML attribute string or an associative array
  394.      * @access   public
  395.      */
  396.      
  397.     function setAttributes($attributes)
  398.     {
  399.         $attrs$this->parseAttributes($attributes);
  400.         if (!is_array($attrs)) {
  401.             return false;
  402.         }
  403.         foreach ($attrs as $key => $value{
  404.             $this->attributes[$key$value;
  405.         }
  406.     // end func updateAttributes
  407.  
  408.     /**
  409.      * Removes an attributes
  410.      * 
  411.      * @param     string    $attr   Attribute name
  412.      * @since     1.4
  413.      * @access    public
  414.      * @return    void 
  415.      * @throws
  416.      */
  417.     function removeAttributes($attrs)
  418.     {
  419.         if (is_string($attrs)) {
  420.             $attrs = array($attrs);
  421.         }
  422.         foreach ($attrs as $attr
  423.             if (isset($this->attributes[strtolower($attr)])) {
  424.                  $this->attributes[strtolower($attr)= false;
  425.             
  426.         }
  427.     //end func removeAttribute
  428.  
  429.       
  430.     /**
  431.      * Output HTML and children
  432.      *
  433.      * @access    public
  434.      * @param     object    $overlay = merge data from object.
  435.      * @return    string 
  436.      * @abstract
  437.      */
  438.     function toHtml($overlay=false)
  439.     {
  440.         
  441.         if ($overlay !== false{
  442.             $this->mergeElement($overlay);
  443.         }
  444.         
  445.         if ($this->override !== false{
  446.             return $this->override;
  447.         }
  448.         $prefix $this->prefix;
  449.         if (is_object($prefix)) {
  450.             $prefix $prefix->toHtml();
  451.         }
  452.         $suffix $this->suffix;
  453.         if (is_object($suffix)) {
  454.             $suffix $suffix->toHtml();
  455.         }
  456.         //if ($this->children) {
  457.             return "{$prefix}<{$this->tag}".$this->attributesToHTML('>'.$this->childrenToHTML("</{$this->tag}>{$suffix}" ;
  458.         //} 
  459.         //return "{$prefix}<{$this->tag}".$this->attributesToHTML() . " />{$suffix}";
  460.         
  461.          
  462.     } // end func toHtml
  463.     
  464.     
  465.     /**
  466.      * Output Open Tag and any children and not Child tag (designed for use with <form + hidden elements>
  467.      *
  468.      * @access    public
  469.      * @param     object    $overlay = merge data from object.
  470.      * @return    string
  471.      * @abstract
  472.      */
  473.     function toHtmlnoClose($overlay=false)
  474.     {
  475.         
  476.         if ($overlay !== false) {
  477.             // FIXME!!!
  478.             $this->mergeElement($overlay);
  479.         }
  480.         
  481.   
  482.         return "<{$this->tag}".$this->attributesToHTML('>' $this->childrenToHTML();
  483.        
  484.          
  485.     } // end func toHtml
  486.     
  487.     
  488.     /**
  489.      * Output HTML and children
  490.      *
  491.      * @access    public
  492.      * @return    string
  493.      * @abstract
  494.      */
  495.     function childrenToHtml()
  496.     {
  497.         $ret = '';
  498.         foreach($this->children as $child{
  499.             if (!is_object($child)) {
  500.                 $ret .= $child;
  501.                 continue;
  502.             }
  503.             
  504.             $ret .= $child->toHtml();
  505.         }
  506.         return $ret;
  507.     } // end func toHtml
  508.     
  509.      
  510.     
  511.     
  512.     

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