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

Documentation generated on Mon, 11 Mar 2019 13:52:01 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.