HTML_QuickForm
[ class tree: HTML_QuickForm ] [ index: HTML_QuickForm ] [ all elements ]

Source for file ITDynamic.php

Documentation is available at ITDynamic.php

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

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