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

Classes

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