home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / HTML / QuickForm / Renderer / ITDynamic.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  9.8 KB  |  301 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * A concrete renderer for HTML_QuickForm, using Integrated Templates.
  6.  * 
  7.  * PHP versions 4 and 5
  8.  *
  9.  * LICENSE: This source file is subject to version 3.01 of the PHP license
  10.  * that is available through the world-wide-web at the following URI:
  11.  * http://www.php.net/license/3_01.txt If you did not receive a copy of
  12.  * the PHP License and are unable to obtain it through the web, please
  13.  * send a note to license@php.net so we can mail you a copy immediately.
  14.  *
  15.  * @category    HTML
  16.  * @package     HTML_QuickForm
  17.  * @author      Alexey Borzov <avb@php.net>
  18.  * @copyright   2001-2007 The PHP Group
  19.  * @license     http://www.php.net/license/3_01.txt PHP License 3.01
  20.  * @version     CVS: $Id: ITDynamic.php,v 1.6 2007/05/29 18:34:36 avb Exp $
  21.  * @link        http://pear.php.net/package/HTML_QuickForm
  22.  */
  23.  
  24. /**
  25.  * An abstract base class for QuickForm renderers
  26.  */
  27. require_once 'HTML/QuickForm/Renderer.php';
  28.  
  29. /**
  30.  * A concrete renderer for HTML_QuickForm, using Integrated Templates.
  31.  * 
  32.  * This is a "dynamic" renderer, which means that concrete form look 
  33.  * is defined at runtime. This also means that you can define 
  34.  * <b>one</b> template file for <b>all</b> your forms. That template
  35.  * should contain a block for every element 'look' appearing in your 
  36.  * forms and also some special blocks (consult the examples). If a
  37.  * special block is not set for an element, the renderer falls back to
  38.  * a default one.
  39.  * 
  40.  * @category    HTML
  41.  * @package     HTML_QuickForm
  42.  * @author      Alexey Borzov <avb@php.net>
  43.  * @version     Release: 3.2.10
  44.  * @since       3.0
  45.  */
  46. class HTML_QuickForm_Renderer_ITDynamic extends HTML_QuickForm_Renderer
  47. {
  48.    /**#@+
  49.     * @access private
  50.     */
  51.    /**
  52.     * A template class (HTML_Template_ITX or HTML_Template_Sigma) instance
  53.     * @var HTML_Template_ITX|HTML_Template_Sigma
  54.     */
  55.     var $_tpl = null;
  56.  
  57.    /**
  58.     * The errors that were not shown near concrete fields go here
  59.     * @var array
  60.     */
  61.     var $_errors = array();
  62.  
  63.    /**
  64.     * Show the block with required note?
  65.     * @var bool
  66.     */
  67.     var $_showRequired = false;
  68.  
  69.    /**
  70.     * A separator for group elements
  71.     * @var mixed
  72.     */
  73.     var $_groupSeparator = null;
  74.  
  75.    /**
  76.     * The current element index inside a group
  77.     * @var integer
  78.     */
  79.     var $_groupElementIdx = 0;
  80.  
  81.    /**
  82.     * Blocks to use for different elements  
  83.     * @var array
  84.     */
  85.     var $_elementBlocks = array();
  86.  
  87.    /**
  88.     * Block to use for headers
  89.     * @var string
  90.     */
  91.     var $_headerBlock = null;
  92.    /**#@-*/
  93.  
  94.  
  95.    /**
  96.     * Constructor
  97.     *
  98.     * @param HTML_Template_ITX|HTML_Template_Sigma     Template object to use
  99.     */
  100.     function HTML_QuickForm_Renderer_ITDynamic(&$tpl)
  101.     {
  102.         $this->HTML_QuickForm_Renderer();
  103.         $this->_tpl =& $tpl;
  104.         $this->_tpl->setCurrentBlock('qf_main_loop');
  105.     }
  106.  
  107.  
  108.     function finishForm(&$form)
  109.     {
  110.         // display errors above form
  111.         if (!empty($this->_errors) && $this->_tpl->blockExists('qf_error_loop')) {
  112.             foreach ($this->_errors as $error) {
  113.                 $this->_tpl->setVariable('qf_error', $error);
  114.                 $this->_tpl->parse('qf_error_loop');
  115.             }
  116.         }
  117.         // show required note
  118.         if ($this->_showRequired) {
  119.             $this->_tpl->setVariable('qf_required_note', $form->getRequiredNote());
  120.         }
  121.         // assign form attributes
  122.         $this->_tpl->setVariable('qf_attributes', $form->getAttributes(true));
  123.         // assign javascript validation rules
  124.         $this->_tpl->setVariable('qf_javascript', $form->getValidationScript());
  125.     }
  126.       
  127.  
  128.     function renderHeader(&$header)
  129.     {
  130.         $blockName = $this->_matchBlock($header);
  131.         if ('qf_header' == $blockName && isset($this->_headerBlock)) {
  132.             $blockName = $this->_headerBlock;
  133.         }
  134.         $this->_tpl->setVariable('qf_header', $header->toHtml());
  135.         $this->_tpl->parse($blockName);
  136.         $this->_tpl->parse('qf_main_loop');
  137.     }
  138.  
  139.  
  140.     function renderElement(&$element, $required, $error)
  141.     {
  142.         $blockName = $this->_matchBlock($element);
  143.         // are we inside a group?
  144.         if ('qf_main_loop' != $this->_tpl->currentBlock) {
  145.             if (0 != $this->_groupElementIdx && $this->_tpl->placeholderExists('qf_separator', $blockName)) {
  146.                 if (is_array($this->_groupSeparator)) {
  147.                     $this->_tpl->setVariable('qf_separator', $this->_groupSeparator[($this->_groupElementIdx - 1) % count($this->_groupSeparator)]);
  148.                 } else {
  149.                     $this->_tpl->setVariable('qf_separator', (string)$this->_groupSeparator);
  150.                 }
  151.             }
  152.             $this->_groupElementIdx++;
  153.  
  154.         } elseif(!empty($error)) {
  155.             // show the error message or keep it for later use
  156.             if ($this->_tpl->blockExists($blockName . '_error')) {
  157.                 $this->_tpl->setVariable('qf_error', $error);
  158.             } else {
  159.                 $this->_errors[] = $error;
  160.             }
  161.         }
  162.         // show an '*' near the required element
  163.         if ($required) {
  164.             $this->_showRequired = true;
  165.             if ($this->_tpl->blockExists($blockName . '_required')) {
  166.                 $this->_tpl->touchBlock($blockName . '_required');
  167.             }
  168.         }
  169.         // Prepare multiple labels
  170.         $labels = $element->getLabel();
  171.         if (is_array($labels)) {
  172.             $mainLabel = array_shift($labels);
  173.         } else {
  174.             $mainLabel = $labels;
  175.         }
  176.         // render the element itself with its main label
  177.         $this->_tpl->setVariable('qf_element', $element->toHtml());
  178.         if ($this->_tpl->placeholderExists('qf_label', $blockName)) {
  179.             $this->_tpl->setVariable('qf_label', $mainLabel);
  180.         }
  181.         // render extra labels, if any
  182.         if (is_array($labels)) {
  183.             foreach($labels as $key => $label) {
  184.                 $key = is_int($key)? $key + 2: $key;
  185.                 if ($this->_tpl->blockExists($blockName . '_label_' . $key)) {
  186.                     $this->_tpl->setVariable('qf_label_' . $key, $label);
  187.                 }
  188.             }
  189.         }
  190.         $this->_tpl->parse($blockName);
  191.         $this->_tpl->parseCurrentBlock();
  192.     }
  193.    
  194.  
  195.     function renderHidden(&$element)
  196.     {
  197.         $this->_tpl->setVariable('qf_hidden', $element->toHtml());
  198.         $this->_tpl->parse('qf_hidden_loop');
  199.     }
  200.  
  201.  
  202.     function startGroup(&$group, $required, $error)
  203.     {
  204.         $blockName = $this->_matchBlock($group);
  205.         $this->_tpl->setCurrentBlock($blockName . '_loop');
  206.         $this->_groupElementIdx = 0;
  207.         $this->_groupSeparator  = is_null($group->_separator)? ' ': $group->_separator;
  208.         // show an '*' near the required element
  209.         if ($required) {
  210.             $this->_showRequired = true;
  211.             if ($this->_tpl->blockExists($blockName . '_required')) {
  212.                 $this->_tpl->touchBlock($blockName . '_required');
  213.             }
  214.         }
  215.         // show the error message or keep it for later use
  216.         if (!empty($error)) {
  217.             if ($this->_tpl->blockExists($blockName . '_error')) {
  218.                 $this->_tpl->setVariable('qf_error', $error);
  219.             } else {
  220.                 $this->_errors[] = $error;
  221.             }
  222.         }
  223.         $this->_tpl->setVariable('qf_group_label', $group->getLabel());
  224.     }
  225.  
  226.  
  227.     function finishGroup(&$group)
  228.     {
  229.         $this->_tpl->parse($this->_matchBlock($group));
  230.         $this->_tpl->setCurrentBlock('qf_main_loop');
  231.         $this->_tpl->parseCurrentBlock();
  232.     }
  233.  
  234.  
  235.    /**
  236.     * Returns the name of a block to use for element rendering
  237.     * 
  238.     * If a name was not explicitly set via setElementBlock(), it tries
  239.     * the names '{prefix}_{element type}' and '{prefix}_{element}', where
  240.     * prefix is either 'qf' or the name of the current group's block
  241.     * 
  242.     * @param HTML_QuickForm_element     form element being rendered
  243.     * @access private
  244.     * @return string    block name
  245.     */
  246.     function _matchBlock(&$element)
  247.     {
  248.         $name = $element->getName();
  249.         $type = $element->getType();
  250.         if (isset($this->_elementBlocks[$name]) && $this->_tpl->blockExists($this->_elementBlocks[$name])) {
  251.             if (('group' == $type) || ($this->_elementBlocks[$name] . '_loop' != $this->_tpl->currentBlock)) {
  252.                 return $this->_elementBlocks[$name];
  253.             }
  254.         }
  255.         if ('group' != $type && 'qf_main_loop' != $this->_tpl->currentBlock) {
  256.             $prefix = substr($this->_tpl->currentBlock, 0, -5); // omit '_loop' postfix
  257.         } else {
  258.             $prefix = 'qf';
  259.         }
  260.         if ($this->_tpl->blockExists($prefix . '_' . $type)) {
  261.             return $prefix . '_' . $type;
  262.         } elseif ($this->_tpl->blockExists($prefix . '_' . $name)) {
  263.             return $prefix . '_' . $name;
  264.         } else {
  265.             return $prefix . '_element';
  266.         }
  267.     }
  268.  
  269.  
  270.    /**
  271.     * Sets the block to use for element rendering
  272.     * 
  273.     * @param mixed      element name or array ('element name' => 'block name')
  274.     * @param string     block name if $elementName is not an array
  275.     * @access public
  276.     * @return void
  277.     */
  278.     function setElementBlock($elementName, $blockName = null)
  279.     {
  280.         if (is_array($elementName)) {
  281.             $this->_elementBlocks = array_merge($this->_elementBlocks, $elementName);
  282.         } else {
  283.             $this->_elementBlocks[$elementName] = $blockName;
  284.         }
  285.     }
  286.  
  287.  
  288.    /**
  289.     * Sets the name of a block to use for header rendering
  290.     *
  291.     * @param string     block name
  292.     * @access public
  293.     * @return void
  294.     */
  295.     function setHeaderBlock($blockName)
  296.     {
  297.         $this->_headerBlock = $blockName;
  298.     }
  299. }
  300. ?>
  301.