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.34 2005/01/22 14:15:04 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 strtolower($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.                         // if value is nto set, it doesnt make any difference what you set ?
  244.                         if (!isset($this->attributes['value'])) {
  245.                             return;
  246.                         }
  247.                         //print_r($this); echo "SET TO "; serialize($value);
  248.                         if (substr($this->attributes['name'],-2== '[]'{
  249.                             if (is_array($value&& 
  250.                                 in_array((string) $this->attributes['value'],$value)
  251.                                 {
  252.                                 $this->attributes['checked'=  true;
  253.                             }
  254.                             return;
  255.                         }
  256.                         if ($this->attributes['value'== $value{
  257.                             $this->attributes['checked'=  true;
  258.                         }
  259.                         
  260.                         
  261.                         return;
  262.                     case 'radio':
  263.                         if (isset($this->attributes['checked'])) {
  264.                             unset($this->attributes['checked']);
  265.                         }
  266.                         
  267.                         if ($this->attributes['value'== $value{
  268.                             $this->attributes['checked'=  true;
  269.                         }
  270.                         return;
  271.                     
  272.                     default:
  273.                         // no other input accepts array as a value.
  274.                         if (is_array($value)) {
  275.                             return;
  276.                         }
  277.                     
  278.                         $this->attributes['value'$value;
  279.                         return;
  280.                 }
  281.                 
  282.             case 'select':
  283.                 
  284.                 if (!is_array($value)) {
  285.                     $value = array($value);
  286.                 }
  287.                 
  288.                 // its setting the default value..
  289.                 
  290.                 foreach($this->children as $i=>$child{
  291.                     
  292.                     if (is_string($child)) {
  293.                         continue;
  294.                     }
  295.                     if ($child->tag == 'optgroup'{
  296.                         foreach($this->children[$i]->children as $ii=>$child{
  297.                         
  298.                             // does the value exist and match..
  299.                             if (isset($child->attributes['value']
  300.                                 && in_array((string) $child->attributes['value']$value)) 
  301.                             {
  302.                                 $this->children[$i]->children[$ii]->attributes['selected'
  303.                                     isset($this->attributes['flexy:xhtml']'selected' : true;
  304.                                 continue;
  305.                             }
  306.                             if (isset($child->attributes['value']&& 
  307.                                 isset($this->children[$i]->children[$ii]->attributes['selected'])) 
  308.                             {
  309.                                 unset($this->children[$i]->children[$ii]->attributes['selected']);
  310.                                 continue;
  311.                             }
  312.                             // value doesnt exst..
  313.                           
  314.                             if (isset($this->children[$i]->children[$ii]->attributes['selected'])) {
  315.                                 unset($this->children[$i]->children[$ii]->attributes['selected']);
  316.                                 continue;
  317.                             }
  318.                         }
  319.                         continue;
  320.                     }
  321.                     
  322.                     // standard option value...
  323.                     //echo "testing {$child->attributes['value']} against ". print_r($value,true)."\n";
  324.                     // does the value exist and match..
  325.                     
  326.                     if (isset($child->attributes['value']
  327.                         && in_array((string) $child->attributes['value']$value)) 
  328.                     {
  329.                        // echo "MATCH!\n";
  330.                       
  331.                         $this->children[$i]->attributes['selected'
  332.                             isset($this->attributes['flexy:xhtml']'selected' : true;;
  333.                         continue;
  334.                     }
  335.                     if (isset($child->attributes['value']&& 
  336.                         isset($this->children[$i]->attributes['selected'])) 
  337.                     {
  338.                         //echo "clearing selected\n";
  339.                         unset($this->children[$i]->attributes['selected']);
  340.                         continue;
  341.                     }
  342.                     // value doesnt exst..
  343.                     
  344.                     if (isset($this->children[$i]->attributes['selected'])) {
  345.                         //echo "clearing selected\n";
  346.                         unset($this->children[$i]->attributes['selected']);
  347.                         continue;
  348.                     }
  349.                     
  350.                     
  351.                 }
  352.                 return;
  353.             case 'textarea':
  354.                 $this->children = array(htmlspecialchars($value));
  355.                 return;
  356.             case '':  // dummy objects.
  357.                 $this->value $value;
  358.                 return;
  359.                 
  360.             // XUL elements
  361.             case 'menulist':
  362.                 require_once 'HTML/Template/Flexy/Element/Xul.php';
  363.                 HTML_Template_Flexy_Element_Xul::setValue($this,$value);
  364.                 return ;
  365.                 
  366.             default:
  367.                 if (is_array($value)) {
  368.                     return;
  369.                 }
  370.                 $this->value $value;
  371.         }
  372.             
  373.         
  374.     
  375.     
  376.     }
  377.     /**
  378.      * Utility function equivilant to HTML_Select - loadArray **
  379.      * but using
  380.      * key=>value maps
  381.      * <option value="key">Value</option>
  382.      * Key=key (eg. both the same) maps to
  383.      * <option>key</option>
  384.      * and label = array(key=>value) maps to
  385.      * <optgroup label="label"> <option value="key">value</option></optgroup>
  386.      * 
  387.      * $element->setOptions(array('a'=>'xxx','b'=>'yyy'));
  388.      * or
  389.      * $element->setOptions(array('a','b','c','d'),true);
  390.      *
  391.      *
  392.      *.
  393.      * @param    HTML_Element   $from  override settings from another element.
  394.      * @param    HTML_Element   $noValue  ignore the key part of the array
  395.      * @access   public
  396.      */
  397.      
  398.     function setOptions($array,$noValue=false{
  399.         if (!is_array($array)) {
  400.             $this->children = array();
  401.             return;
  402.         }
  403.         
  404.         
  405.         $tag strtolower($this->tag);
  406.         $namespace '';
  407.         if (false !== strpos($this->tag':')) {
  408.             
  409.             $bits explode(':',$this->tag);
  410.             $namespace $bits[0':';
  411.             $tag strtolower($bits[1]);
  412.             
  413.         }
  414.         // if we have specified a xultag!!?
  415.         if (strlen($tag&& ($tag != 'select')) {
  416.                 require_once 'HTML/Template/Flexy/Element/Xul.php';
  417.                 return HTML_Template_Flexy_Element_Xul::setOptions($this,$array,$noValue);
  418.         }
  419.         
  420.         foreach($array as $k=>$v{
  421.             if (is_array($v)) {     // optgroup
  422.                 $child = new HTML_Template_Flexy_Element($namespace 'optgroup',array('label'=>$k));
  423.                 foreach($v as $kk=>$vv{
  424.                     $atts=array();
  425.                     if (($kk != $vv&& !$noValue{
  426.                         $atts = array('value'=>$kk);
  427.                     }
  428.                     $add = new HTML_Template_Flexy_Element($namespace 'option',$atts);
  429.                     $add->children = array(htmlspecialchars($vv));
  430.                     $child->children[$add;
  431.                 }
  432.                 $this->children[$child;
  433.                 continue;
  434.             
  435.             $atts=array();
  436.             if (($k !== $v&& !$noValue{
  437.                 $atts = array('value'=>$k);
  438.             else {
  439.                 $atts = array('value'=>$v);
  440.             }
  441.             $add = new HTML_Template_Flexy_Element($namespace 'option',$atts);
  442.             $add->children = array(htmlspecialchars($v));
  443.             $this->children[$add;
  444.         }
  445.        
  446.     }
  447.     /**
  448.      * Sets the HTML attributes
  449.      * @param    mixed   $attributes     Either a typical HTML attribute string or an associative array
  450.      * @access   public
  451.      */
  452.      
  453.     function setAttributes($attributes)
  454.     {
  455.         $attrs$this->parseAttributes($attributes);
  456.         if (!is_array($attrs)) {
  457.             return false;
  458.         }
  459.         foreach ($attrs as $key => $value{
  460.             $this->attributes[$key$value;
  461.         }
  462.     // end func updateAttributes
  463.  
  464.     /**
  465.      * Removes an attributes
  466.      * 
  467.      * @param     string    $attr   Attribute name
  468.      * @since     1.4
  469.      * @access    public
  470.      * @return    void 
  471.      * @throws
  472.      */
  473.     function removeAttributes($attrs)
  474.     {
  475.         if (is_string($attrs)) {
  476.             $attrs = array($attrs);
  477.         }
  478.         foreach ($attrs as $attr
  479.             if (isset($this->attributes[strtolower($attr)])) {
  480.                  $this->attributes[strtolower($attr)= false;
  481.             
  482.         }
  483.     //end func removeAttribute
  484.  
  485.       
  486.     /**
  487.      * Output HTML and children
  488.      *
  489.      * @access    public
  490.      * @param     object    $overlay = merge data from object.
  491.      * @return    string 
  492.      * @abstract
  493.      */
  494.     function toHtml($overlay=false)
  495.     {
  496.          
  497.         //echo "BEFORE<PRE>";print_R($this);
  498.         $ret $this;
  499.         if ($overlay !== false{
  500.             $ret = HTML_Template_Flexy::mergeElement($this,$overlay);
  501.         }
  502.         
  503.         if ($ret->override !== false{
  504.             return $ret->override;
  505.         }
  506.         $prefix $ret->prefix;
  507.         if (is_object($prefix)) {
  508.             $prefix $prefix->toHtml();
  509.         }
  510.         $suffix $ret->suffix;
  511.         if (is_object($suffix)) {
  512.             $suffix $suffix->toHtml();
  513.         }
  514.         //echo "AFTER<PRE>";print_R($ret);
  515.       
  516.         $tag $this->tag;
  517.         if (strpos($tag,':'!==  false{
  518.             $bits explode(':',$tag);
  519.             $tag $bits[1];
  520.         }
  521.         // tags that never should have closers  
  522.         $close = "</{$ret->tag}>";
  523.         if (in_array(strtoupper($tag),array("INPUT","IMG"))) {
  524.             $close '';
  525.         }
  526.         if (isset($this->attributes['/'])) {
  527.             $close '';
  528.         }
  529.  
  530.         $close .= $suffix ;
  531.        
  532.         return "{$prefix}<{$ret->tag}".$ret->attributesToHTML('>'.$ret->childrenToHTML(.$close;
  533.         
  534.          
  535.     // end func toHtml
  536.     
  537.     
  538.     /**
  539.      * Output Open Tag and any children and not Child tag (designed for use with <form + hidden elements>
  540.      *
  541.      * @access    public
  542.      * @param     object    $overlay = merge data from object.
  543.      * @return    string 
  544.      * @abstract
  545.      */
  546.     function toHtmlnoClose($overlay=false)
  547.     {
  548.         $ret $this;
  549.         if ($ret->override !== false{
  550.             return $ret->override;
  551.         }
  552.         if ($overlay !== false{
  553.             $ret = HTML_Template_Flexy::mergeElement($this,$overlay);
  554.         }
  555.         
  556.   
  557.         return "<{$ret->tag}".$ret->attributesToHTML('>' $ret->childrenToHTML();
  558.        
  559.          
  560.     // end func toHtml
  561.     
  562.     
  563.     /**
  564.      * Output HTML and children
  565.      *
  566.      * @access    public
  567.      * @return    string 
  568.      * @abstract
  569.      */
  570.     function childrenToHtml()
  571.     {
  572.         $ret '';
  573.         foreach($this->children as $child{
  574.             if (!is_object($child)) {
  575.                 $ret .= $child;
  576.                 continue;
  577.             }
  578.             
  579.             $ret .= $child->toHtml();
  580.         }
  581.         return $ret;
  582.     // end func toHtml
  583.     
  584.      
  585.     
  586.     
  587.     
  588. // end class HTML_Template_Flexy_Element

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