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

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