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

  • NCacheMacro
  • NCoreMacros
  • NFormMacros
  • NMacroSet
  • NUIMacros
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  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 NForms.
 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 NFormMacros extends NMacroSet
 27: {
 28: 
 29:     public static function install(NLatteCompiler $compiler)
 30:     {
 31:         $me = new self($compiler);
 32:         $me->addMacro('form',
 33:             'NFormMacros::renderFormBegin($form = $_form = (is_object(%node.word) ? %node.word : $_control[%node.word]), %node.array)',
 34:             'NFormMacros::renderFormEnd($_form)');
 35:         $me->addMacro('label', array($me, 'macroLabel'), '?></label><?php');
 36:         $me->addMacro('input', '$_input = (is_object(%node.word) ? %node.word : $_form[%node.word]); echo $_input->getControl()->addAttributes(%node.array)', NULL, array($me, 'macroAttrInput'));
 37:         $me->addMacro('formContainer', '$_formStack[] = $_form; $formContainer = $_form = (is_object(%node.word) ? %node.word : $_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(NMacroNode $node, NPhpWriter $writer)
 49:     {
 50:         $cmd = '$_input = is_object(%node.word) ? %node.word : $_form[%node.word]; if ($_label = $_input->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(NMacroNode $node, NPhpWriter $writer)
 65:     {
 66:         if ($node->htmlNode->attrs) {
 67:             $reset = array_fill_keys(array_keys($node->htmlNode->attrs), NULL);
 68:             return $writer->write('$_input = (is_object(%node.word) ? %node.word : $_form[%node.word]); echo $_input->getControl()->addAttributes(%var)->attributes()', $reset);
 69:         }
 70:         return $writer->write('$_input = (is_object(%node.word) ? %node.word : $_form[%node.word]); echo $_input->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(NForm $form, array $attrs)
 84:     {
 85:         $el = $form->getElementPrototype();
 86:         $el->action = $action = (string) $el->action;
 87:         $el = clone $el;
 88:         if (strcasecmp($form->getMethod(), 'get') === 0) {
 89:             list($el->action) = explode('?', $action, 2);
 90:             if (($i = strpos($action, '#')) !== FALSE) {
 91:                 $el->action .= substr($action, $i);
 92:             }
 93:         }
 94:         echo $el->addAttributes($attrs)->startTag();
 95:     }
 96: 
 97: 
 98: 
 99:     /**
100:      * Renders form end.
101:      * @return string
102:      */
103:     public static function renderFormEnd(NForm $form)
104:     {
105:         $s = '';
106:         if (strcasecmp($form->getMethod(), 'get') === 0) {
107:             $url = explode('?', $form->getElementPrototype()->action, 2);
108:             if (isset($url[1])) {
109:                 list($url[1]) = explode('#', $url[1], 2);
110:                 foreach (preg_split('#[;&]#', $url[1]) as $param) {
111:                     $parts = explode('=', $param, 2);
112:                     $name = urldecode($parts[0]);
113:                     if (!isset($form[$name])) {
114:                         $s .= NHtml::el('input', array('type' => 'hidden', 'name' => $name, 'value' => urldecode($parts[1])));
115:                     }
116:                 }
117:             }
118:         }
119: 
120:         foreach ($form->getComponents(TRUE, 'NHiddenField') as $control) {
121:             if (!$control->getOption('rendered')) {
122:                 $s .= $control->getControl();
123:             }
124:         }
125: 
126:         if (iterator_count($form->getComponents(TRUE, 'NTextInput')) < 2) {
127:             $s .= '<!--[if IE]><input type=IEbug disabled style="display:none"><![endif]-->';
128:         }
129: 
130:         echo ($s ? "<div>$s</div>\n" : '') . $form->getElementPrototype()->endTag() . "\n";
131:     }
132: 
133: }
134: 
Nette Framework 2.0.10 (for PHP 5.2, prefixed) API API documentation generated by ApiGen 2.8.0