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

Source for file HTML.php

Documentation is available at HTML.php

  1. <?php
  2. /**
  3.  * Formagic
  4.  *
  5.  * LICENCE
  6.  *
  7.  * This source file is subject to the new BSD license that is bundled
  8.  * with this package in the file LICENSE.txt.
  9.  * It is also available through the world-wide-web at
  10.  * http://formagic.weasle.de/licence.txt
  11.  * If you did not receive a copy of the license and are unable to
  12.  * obtain it through the world-wide-web, please send an email
  13.  * to licence@weasle.de so we can send you a copy immediately.
  14.  *
  15.  * @category    Formagic
  16.  * @package     Renderer
  17.  * @author      Florian Sonnenburg
  18.  * @copyright   Copyright (c) 2007 Florian Sonnenburg
  19.  * @license     http://formagic.weasle.de/licence.txt     New BSD License
  20.  * @revision    $Revision: 14 $
  21.  */
  22.  
  23. /**
  24.  * Formagic Renderer HTML
  25.  *
  26.  * @category    Formagic
  27.  * @package     Renderer
  28.  * @author      Florian Sonnenburg
  29.  * @copyright   Copyright (c) 2007
  30.  * @version     $Id: HTML.php 14 2007-08-13 20:33:26Z meweasle $
  31.  ***/
  32.  
  33.     /**
  34.      * Form action, value of <form action=""> tag
  35.      * @var string 
  36.      ***/
  37.     private $_action;
  38.  
  39.     /**
  40.      * Overwrites default templates for item types or individual items
  41.      * @var array 
  42.      ***/
  43.     private $_templates array();
  44.  
  45.     /**
  46.      * Hidden inputs string
  47.      * @var string 
  48.      ***/
  49.     private $_hiddenString "";
  50.  
  51.     /**
  52.      * Error/notice/debug colors
  53.      * @var array 
  54.      ***/
  55.     public $messageColors = array(
  56.                 Formagic::ERROR     => '#F41414',
  57.                 Formagic::NOTICE    => '#DCB913',
  58.                 Formagic::DEBUG     => '#E2D726');
  59.  
  60.     /**
  61.      * HTML string for mandatory fields
  62.      * @var 
  63.      ***/
  64.     public $mandatoryMarker = "<span class=\"mandatory\">*</span>";
  65.  
  66.     /**
  67.      * Constructor
  68.      *
  69.      */
  70.     public function __construct()
  71.     {
  72.         // $this->_action = $_SERVER['PHP_SELF'];
  73.  
  74.         // set root template
  75.         $this->setRootTemplate('
  76. <!-- formagic HTML renderer start
  77. ============================= -->
  78. %MESSAGE%
  79. <form name="%NAME%" action="%ACTION%" id="%NAME%" method="%METHOD%">
  80. <!-- hidden inputs -->%HIDDENS%<!-- hidden end -->
  81.     %CONTENT%
  82. </form>
  83. <!-- formagic HTML renderer end
  84. =========================== -->
  85. ',
  86.             'main');
  87.  
  88.         // set container main template
  89.         $this->setTemplate('<table border="0" cellpadding="4" cellspacing="0"%ATTRIBUTES%>
  90.         <!-- form rows -->
  91. %ROWS%
  92.     </table>',
  93.             'main'null'container');
  94.  
  95.         // normal items template
  96.         $this->setTemplate('
  97.         <tr>
  98.             <td>%LABEL%</td>
  99.             <td>%ERROR%
  100.                 <!-- Input -->%INPUT%</td>
  101.         </tr>
  102. ',
  103.             'item'null'container');
  104.  
  105.         // wide template
  106.         $this->setTemplate('
  107.         <tr>
  108.             <td colspan="2">%ERROR%
  109.                 <!-- Input -->%INPUT%</td>
  110.         </tr>
  111. ',
  112.             'itemWide'null'container');
  113.  
  114.         // label template
  115.         $this->setTemplate('<!-- Label --><b><label for="%ID%"%ERRORCLASS%>%LABEL%%ERRORMARKER%</label></b>',
  116.             'label'null'container');
  117.  
  118.         // error template
  119.         $this->setTemplate('<!-- Error --><span style="color:#ff0000">%ERRORMSG%</span><br />',
  120.             'error'null'container');
  121.     }
  122.  
  123.     /**
  124.      * Formagic_Renderer_HTML::setCustomTemplate()
  125.      *
  126.      * @param string $template 
  127.      * @param string $item 
  128.      * @param string $itemType 
  129.      * @return void 
  130.      */
  131.     public function setTemplate($template$part='main'$item=null$itemType=null)
  132.     {
  133.         if ($item{
  134.             $this->_templates['__fm'][$item][$part$template;
  135.             return ;
  136.         }
  137.         if ($itemType{
  138.             $this->_templates[$itemType][$part$template;
  139.         }
  140.     }
  141.  
  142.     public function setRootTemplate($template)
  143.     {
  144.         $this->_templates['root'$template;
  145.     }
  146.  
  147.     /**
  148.      * Returns surrounding template
  149.      *
  150.      * @return string 
  151.      */
  152.     public function _getRootTemplate()
  153.     {
  154.         return $this->_templates['root'];
  155.     }
  156.  
  157.     /**
  158.      * Returns template string
  159.      *
  160.      * @param string $item 
  161.      * @param string $itemType 
  162.      * @return string 
  163.      */
  164.     private function _getTemplate($part='main'$item=null$itemType=null)
  165.     {
  166.         if ($item && isset($this->_templates['__fm'][$item][$part])) {
  167.             return $this->_templates['__fm'][$item][$part];
  168.         }
  169.         if ($itemType{
  170.             // don't check class hierarchy if not necessary
  171.             if (isset($this->_templates[$itemType][$part])) {
  172.                 return $this->_templates[$itemType][$part];
  173.             }
  174.  
  175.             $parents @class_parents('Formagic_Item_' ucfirst($itemType));
  176.             if (!$parents{
  177.                 throw new Formagic_Exception('Invalid item type: ' $itemType);
  178.             }
  179.  
  180.             foreach($parents as $parent{
  181.                 $itemTypeParent strToLower(str_replace('Formagic_Item_'''$parent));
  182.                 if (isset($this->_templates[$itemTypeParent][$part])) {
  183.                     $this->_templates[$itemType=$this->_templates[$itemTypeParent];
  184.                     return $this->_templates[$itemType][$part];
  185.                 }
  186.             }
  187.         }
  188.         return '';
  189.     }
  190.  
  191.     /**
  192.      * Returns form HTML string
  193.      *
  194.      * @param Formagic $fObj 
  195.      * @return string 
  196.      */
  197.     public function fetch(&$fObj)
  198.     {
  199.         $content  $this->_renderSubItemsRecursive($fObj->getItemHolder());
  200.         $rootTpl  $this->_getRootTemplate();
  201.         $messages $fObj->getMessages();
  202.         $msgStr  '';
  203.  
  204.         if (count($messages)) {
  205.             $msgStr '<ul>';
  206.             foreach($messages as $level => $texts{
  207.                 foreach($texts as $msg{
  208.                     $msgStr .= '<li style="color:' $this->messageColors[$level'">';
  209.                     $msgStr .= $msg '</li>';
  210.                 }
  211.             }
  212.             $msgStr .= '</ul>';
  213.         }
  214.         $str str_replace(array('%MESSAGE%''%NAME%''%ACTION%''%METHOD%''%HIDDENS%''%CONTENT%'),
  215.                            array($msgStr$fObj->name$fObj->getFormAction()$fObj->method$this->_hiddenString$content),
  216.                            $rootTpl);
  217.         return $str;
  218.     }
  219.  
  220.     /**
  221.      * Adds hidden fields to form HTML string
  222.      *
  223.      * @param string $name 
  224.      * @param string $value 
  225.      * @return boolean 
  226.      */
  227.     private function _saveHiddenItem($name$value)
  228.     {
  229.         $this->_hiddenString .= "<input type=\"hidden\" name=\"$name\" value=\"$value\" />\n";
  230.         return true;
  231.     }
  232.  
  233.     /**
  234.      * Recursively iterate through item objects
  235.      *
  236.      * @param object $item 
  237.      * @return string 
  238.      */
  239.     private function _renderSubItemsRecursive($container$start false)
  240.     {
  241.         $rows "";
  242.  
  243.         $items          $container->getItems();
  244.         $containerTpl   $this->_getTemplate('main'$container->name$container->type);
  245.         $errorTpl       $this->_getTemplate('error'$container->name$container->type);
  246.         $labelTpl       $this->_getTemplate('label'$container->name$container->type);
  247.         $itemTpl        $this->_getTemplate('item'$container->name$container->type);
  248.         $itemWideTpl    $this->_getTemplate('itemWide'$container->name$container->type);
  249.  
  250.         foreach($items as &$item{
  251.  
  252.             /**
  253.              * skip disabled inputs
  254.              **/
  255.             if ($item->isDisabled()) {
  256.                 continue;
  257.             }
  258.  
  259.             /**
  260.              * Run through containers recursively
  261.              **/
  262.             if ($item instanceOf Formagic_Item_Container{
  263.                 // render fake input string from container items
  264.                 $rows .= $this->_renderSubItemsRecursive($item);
  265.                 continue;
  266.             }
  267.  
  268.             /**
  269.              * Catch hiddens and continue to next input
  270.              **/
  271.             if ($item->isHidden()) {
  272.                 $this->_saveHiddenItem($item->name$item->getValue());
  273.                 continue;
  274.             }
  275.  
  276.  
  277.             /**
  278.              * Render "real" items
  279.              **/
  280.             $part $item->isWide
  281.                 // template spans both label and input column
  282.                 ? 'itemWide'
  283.                 // separate label and input columns
  284.                 : 'item';
  285.             $varName $part.'Tpl';
  286.             $rowTpl =$$varName;
  287.  
  288.             /**
  289.              * Error message
  290.              **/
  291.             $msg $item->getErrorMessage();
  292.             $error $msg
  293.                 // render error message
  294.                 ? preg_replace('/%ERRORMSG%/'$msg$errorTpl)
  295.                 // or discard it if no error message present
  296.                 : '';
  297.  
  298.             /**
  299.              * Label
  300.              **/
  301.             $itemLabel   $item->getLabel();
  302.  
  303.             // render label
  304.             if ($itemLabel{
  305.                 $errorMarker '';
  306.                 $errorClass  '';
  307.                 if ($item->hasRule('mandatory')) {
  308.                     $errorMarker $this->mandatoryMarker;
  309.                 }
  310.                 if ($msg{
  311.                     $errorClass ' class="inputError"';
  312.                 }
  313.                 $label str_replace(
  314.                         array('%LABEL%''%ERRORCLASS%''%ID%''%ERRORMARKER%'),
  315.                         array($itemLabel$errorClass$item->id$errorMarker),
  316.                         $labelTpl);
  317.  
  318.             // display nothing if no label present
  319.             else {
  320.                 $label '&nbsp;';
  321.             }
  322.  
  323.             /**
  324.              * render input HTML string
  325.              **/
  326.             $rows .= str_replace(
  327.                 array('%LABEL%''%ERROR%''%INPUT%'),
  328.                 array($label$error$item->getHtml()),
  329.                 $rowTpl);
  330.         }
  331.  
  332.         $res $container->hasHtml
  333.                 ? str_replace(
  334.                     array('%ROWS%''%ATTRIBUTES%'),
  335.                     array($rows$container->getAttributeStr())$containerTpl)
  336.                 : $rows;
  337.         return $res;
  338.     }
  339.  
  340.     /**
  341.      * Reformats $string so that it can be used as HTML ID
  342.      *
  343.      * @param string $string 
  344.      * @return string 
  345.      */
  346.     private function _htmlId($string)
  347.     {
  348.         return str_replace(array('[',']')array('_','')$string);
  349.     }
  350.  
  351. }

Documentation generated on Thu, 23 Aug 2007 00:29:40 +0200 by phpDocumentor 1.4.0