Packages

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
      • Adapters
      • Extensions
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Diagnostics
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
      • PhpGenerator
  • NetteModule
  • None
  • PHP

Classes

  • CacheMacro
  • CoreMacros
  • FormMacros
  • MacroSet
  • UIMacros
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://nette.org)
  5:  *
  6:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  7:  *
  8:  * For the full copyright and license information, please view
  9:  * the file license.txt that was distributed with this source code.
 10:  * @package Nette\Latte\Macros
 11:  */
 12: 
 13: 
 14: 
 15: /**
 16:  * Macros for Forms.
 17:  *
 18:  * - {form name} ... {/form}
 19:  * - {input name}
 20:  * - {label name /} or {label name}... {/label}
 21:  * - {formContainer name} ... {/formContainer}
 22:  *
 23:  * @author     David Grudl
 24:  * @package Nette\Latte\Macros
 25:  */
 26: class FormMacros extends MacroSet
 27: {
 28: 
 29:     public static function install(LatteCompiler $compiler)
 30:     {
 31:         $me = new self($compiler);
 32:         $me->addMacro('form',
 33:             'FormMacros::renderFormBegin($form = $_form = (is_object(%node.word) ? %node.word : $_control[%node.word]), %node.array)',
 34:             'FormMacros::renderFormEnd($_form)');
 35:         $me->addMacro('label', array($me, 'macroLabel'), '?></label><?php');
 36:         $me->addMacro('input', 'echo $_form[%node.word]->getControl()->addAttributes(%node.array)', NULL, array($me, 'macroAttrInput'));
 37:         $me->addMacro('formContainer', '$_formStack[] = $_form; $formContainer = $_form = $_form[%node.word]', '$_form = array_pop($_formStack)');
 38:     }
 39: 
 40: 
 41: 
 42:     /********************* macros ****************d*g**/
 43: 
 44: 
 45:     /**
 46:      * {label ...} and optionally {/label}
 47:      */
 48:     public function macroLabel(MacroNode $node, PhpWriter $writer)
 49:     {
 50:         $cmd = 'if ($_label = $_form[%node.word]->getLabel()) echo $_label->addAttributes(%node.array)';
 51:         if ($node->isEmpty = (substr($node->args, -1) === '/')) {
 52:             $node->setArgs(substr($node->args, 0, -1));
 53:             return $writer->write($cmd);
 54:         } else {
 55:             return $writer->write($cmd . '->startTag()');
 56:         }
 57:     }
 58: 
 59: 
 60: 
 61:     /**
 62:      * n:input
 63:      */
 64:     public function macroAttrInput(MacroNode $node, PhpWriter $writer)
 65:     {
 66:         if ($node->htmlNode->attrs) {
 67:             $reset = array_fill_keys(array_keys($node->htmlNode->attrs), NULL);
 68:             return $writer->write('echo $_form[%node.word]->getControl()->addAttributes(%var)->attributes()', $reset);
 69:         }
 70:         return $writer->write('echo $_form[%node.word]->getControl()->attributes()');
 71:     }
 72: 
 73: 
 74: 
 75:     /********************* run-time writers ****************d*g**/
 76: 
 77: 
 78: 
 79:     /**
 80:      * Renders form begin.
 81:      * @return void
 82:      */
 83:     public static function renderFormBegin(Form $form, array $attrs)
 84:     {
 85:         $el = $form->getElementPrototype();
 86:         $el->action = (string) $el->action;
 87:         $el = clone $el;
 88:         if (strcasecmp($form->getMethod(), 'get') === 0) {
 89:             list($el->action) = explode('?', $el->action, 2);
 90:         }
 91:         echo $el->addAttributes($attrs)->startTag();
 92:     }
 93: 
 94: 
 95: 
 96:     /**
 97:      * Renders form end.
 98:      * @return string
 99:      */
100:     public static function renderFormEnd(Form $form)
101:     {
102:         $s = '';
103:         if (strcasecmp($form->getMethod(), 'get') === 0) {
104:             $url = explode('?', $form->getElementPrototype()->action, 2);
105:             if (isset($url[1])) {
106:                 foreach (preg_split('#[;&]#', $url[1]) as $param) {
107:                     $parts = explode('=', $param, 2);
108:                     $name = urldecode($parts[0]);
109:                     if (!isset($form[$name])) {
110:                         $s .= Html::el('input', array('type' => 'hidden', 'name' => $name, 'value' => urldecode($parts[1])));
111:                     }
112:                 }
113:             }
114:         }
115: 
116:         foreach ($form->getComponents(TRUE, 'HiddenField') as $control) {
117:             if (!$control->getOption('rendered')) {
118:                 $s .= $control->getControl();
119:             }
120:         }
121: 
122:         if (iterator_count($form->getComponents(TRUE, 'TextInput')) < 2) {
123:             $s .= '<!--[if IE]><input type=IEbug disabled style="display:none"><![endif]-->';
124:         }
125: 
126:         echo ($s ? "<div>$s</div>\n" : '') . $form->getElementPrototype()->endTag() . "\n";
127:     }
128: 
129: }
130: 
Nette Framework 2.0.3 (for PHP 5.2, un-prefixed) API API documentation generated by ApiGen 2.7.0