home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / ITDynamic.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  9.6 KB  |  286 lines

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