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.21 2004/04/23 07:59:11 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 (is_string($child)) {
  262.                         continue;
  263.                     }
  264.                     if ($child->tag == 'optgroup'{
  265.                         foreach($this->children[$i]->children as $ii=>$child{
  266.                         
  267.                             // does the value exist and match..
  268.                             if (isset($child->attributes['value']
  269.                                 && in_array($child->attributes['value']$value)) 
  270.                             {
  271.                                 $this->children[$i]->children[$ii]->attributes['selected'= true;
  272.                                 continue;
  273.                             }
  274.                             if (isset($child->attributes['value']&& 
  275.                                 isset($this->children[$i]->children[$ii]->attributes['selected'])) 
  276.                             {
  277.                                 unset($this->children[$i]->children[$ii]->attributes['selected']);
  278.                                 continue;
  279.                             }
  280.                             // value doesnt exst..
  281.                             if (in_array($child->children[0],$value)) {
  282.                                 $this->children[$i]->children[$ii]->attributes['selected'= true;
  283.                                 continue;
  284.                             }
  285.                             
  286.                             if (in_array($child->children[0],$value)) {
  287.                                 $this->children[$i]->children[$ii]->attributes['selected'= true;
  288.                                 continue;
  289.                             }
  290.                             if (isset($this->children[$i]->children[$ii]->attributes['selected'])) {
  291.                                 unset($this->children[$i]->children[$ii]->attributes['selected']);
  292.                                 continue;
  293.                             }
  294.                         }
  295.                         continue;
  296.                     }
  297.                     
  298.                     // standard option value...
  299.                     
  300.                      // does the value exist and match..
  301.                     if (isset($child->attributes['value']
  302.                         && in_array($child->attributes['value']$value)) 
  303.                     {
  304.                         $this->children[$i]->attributes['selected'= true;
  305.                         continue;
  306.                     }
  307.                     if (isset($child->attributes['value']&& 
  308.                         isset($this->children[$i]->attributes['selected'])) 
  309.                     {
  310.                         unset($this->children[$i]->attributes['selected']);
  311.                         continue;
  312.                     }
  313.                     // value doesnt exst..
  314.                     if (in_array($child->children[0],$value)) {
  315.                         $this->children[$i]->attributes['selected'= true;
  316.                         continue;
  317.                     }
  318.                     
  319.                     if (in_array($child->children[0],$value)) {
  320.                         $this->children[$i]->attributes['selected'= true;
  321.                         continue;
  322.                     }
  323.                     if (isset($this->children[$i]->attributes['selected'])) {
  324.                         unset($this->children[$i]->attributes['selected']);
  325.                         continue;
  326.                     }
  327.                 }
  328.                 return;
  329.             case 'textarea':
  330.                 $this->children = array(htmlspecialchars($value));
  331.                 return;
  332.             case '':  // dummy objects.
  333.                 $this->value $value;
  334.                 return;
  335.             default:
  336.                 if (is_array($value)) {
  337.                     return;
  338.                 }
  339.                 $this->value $value;
  340.         }
  341.             
  342.         
  343.     
  344.     
  345.     }
  346.     /**
  347.      * Utility function equivilant to HTML_Select - loadArray **
  348.      * but using
  349.      * key=>value maps
  350.      * <option value="key">Value</option>
  351.      * Key=key (eg. both the same) maps to
  352.      * <option>key</option>
  353.      * and label = array(key=>value) maps to
  354.      * <optgroup label="label"> <option value="key">value</option></optgroup>
  355.      * 
  356.      * $element->setOptions(array('a'=>'xxx','b'=>'yyy'));
  357.      * or
  358.      * $element->setOptions(array('a','b','c','d'),true);
  359.      *
  360.      *
  361.      *.
  362.      * @param    HTML_Element   $from  override settings from another element.
  363.      * @param    HTML_Element   $noValue  ignore the key part of the array
  364.      * @access   public
  365.      */
  366.      
  367.     function setOptions($array,$noValue=false{
  368.         if (!is_array($array)) {
  369.             $this->children = array();
  370.             return;
  371.         }
  372.         foreach($array as $k=>$v{
  373.             if (is_array($v)) {     // optgroup
  374.                 $child = new HTML_Template_Flexy_Element('optgroup',array('label'=>$kk));
  375.                 foreach($v as $kk=>$vv{
  376.                     $atts=array();
  377.                     if (($kk != $vv&& !$noValue{
  378.                         $atts = array('value'=>$kk);
  379.                     }
  380.                     $add = new HTML_Template_Flexy_Element('option',$atts);
  381.                     $add->children = array(htmlspecialchars($vv));
  382.                     $child->children[$add;
  383.                 }
  384.                 $this->children[$child;
  385.                 continue;
  386.             
  387.             $atts=array();
  388.             if (($k !== $v&& !$noValue{
  389.                 $atts = array('value'=>$k);
  390.             }
  391.             $add = new HTML_Template_Flexy_Element('option',$atts);
  392.             $add->children = array(htmlspecialchars($v));
  393.             $this->children[$add;
  394.         }
  395.        
  396.     }
  397.     /**
  398.      * Sets the HTML attributes
  399.      * @param    mixed   $attributes     Either a typical HTML attribute string or an associative array
  400.      * @access   public
  401.      */
  402.      
  403.     function setAttributes($attributes)
  404.     {
  405.         $attrs$this->parseAttributes($attributes);
  406.         if (!is_array($attrs)) {
  407.             return false;
  408.         }
  409.         foreach ($attrs as $key => $value{
  410.             $this->attributes[$key$value;
  411.         }
  412.     // end func updateAttributes
  413.  
  414.     /**
  415.      * Removes an attributes
  416.      * 
  417.      * @param     string    $attr   Attribute name
  418.      * @since     1.4
  419.      * @access    public
  420.      * @return    void 
  421.      * @throws
  422.      */
  423.     function removeAttributes($attrs)
  424.     {
  425.         if (is_string($attrs)) {
  426.             $attrs = array($attrs);
  427.         }
  428.         foreach ($attrs as $attr
  429.             if (isset($this->attributes[strtolower($attr)])) {
  430.                  $this->attributes[strtolower($attr)= false;
  431.             
  432.         }
  433.     //end func removeAttribute
  434.  
  435.       
  436.     /**
  437.      * Output HTML and children
  438.      *
  439.      * @access    public
  440.      * @param     object    $overlay = merge data from object.
  441.      * @return    string 
  442.      * @abstract
  443.      */
  444.     function toHtml($overlay=false)
  445.     {
  446.          
  447.         //echo "BEFORE<PRE>";print_R($this);
  448.         $ret $this;
  449.         if ($overlay !== false{
  450.             $ret = HTML_Template_Flexy::mergeElement($this,$overlay);
  451.         }
  452.         
  453.         if ($ret->override !== false{
  454.             return $ret->override;
  455.         }
  456.         $prefix $ret->prefix;
  457.         if (is_object($prefix)) {
  458.             $prefix $prefix->toHtml();
  459.         }
  460.         $suffix $ret->suffix;
  461.         if (is_object($suffix)) {
  462.             $suffix $suffix->toHtml();
  463.         }
  464.         //echo "AFTER<PRE>";print_R($ret);
  465.         // tags that never should have closers  
  466.         $tag $this->tag;
  467.         if (strpos($tag,':'!==  false{
  468.             $bits explode(':',$tag);
  469.             $tag $bits[1];
  470.         }
  471.         $close in_array(strtoupper($tag),array("INPUT","IMG")) '' : "</{$ret->tag}>{$suffix}" ;
  472.         
  473.         return "{$prefix}<{$ret->tag}".$ret->attributesToHTML('>'.$ret->childrenToHTML(.$close;
  474.         
  475.          
  476.     // end func toHtml
  477.     
  478.     
  479.     /**
  480.      * Output Open Tag and any children and not Child tag (designed for use with <form + hidden elements>
  481.      *
  482.      * @access    public
  483.      * @param     object    $overlay = merge data from object.
  484.      * @return    string 
  485.      * @abstract
  486.      */
  487.     function toHtmlnoClose($overlay=false)
  488.     {
  489.         $ret $this;
  490.         if ($overlay !== false{
  491.             $ret = HTML_Template_Flexy::mergeElement($this,$overlay);
  492.         }
  493.         
  494.   
  495.         return "<{$ret->tag}".$ret->attributesToHTML('>' $ret->childrenToHTML();
  496.        
  497.          
  498.     // end func toHtml
  499.     
  500.     
  501.     /**
  502.      * Output HTML and children
  503.      *
  504.      * @access    public
  505.      * @return    string 
  506.      * @abstract
  507.      */
  508.     function childrenToHtml()
  509.     {
  510.         $ret '';
  511.         foreach($this->children as $child{
  512.             if (!is_object($child)) {
  513.                 $ret .= $child;
  514.                 continue;
  515.             }
  516.             
  517.             $ret .= $child->toHtml();
  518.         }
  519.         return $ret;
  520.     // end func toHtml
  521.     
  522.      
  523.     
  524.     
  525.     
  526. // end class HTML_Common

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