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, 'macroAttrName'));
 37:         $me->addMacro('formContainer', '$_formStack[] = $_form; $formContainer = $_form = (is_object(%node.word) ? %node.word : $_form[%node.word])', '$_form = array_pop($_formStack)');
 38:         $me->addMacro('name', NULL, NULL, array($me, 'macroAttrName'));
 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:      * <input n:name> or alias n:input
 62:      */
 63:     public function macroAttrName(NMacroNode $node, NPhpWriter $writer)
 64:     {
 65:         if ($node->htmlNode->attrs) {
 66:             $reset = array_fill_keys(array_keys($node->htmlNode->attrs), NULL);
 67:             return $writer->write('$_input = (is_object(%node.word) ? %node.word : $_form[%node.word]); echo $_input->getControl()->addAttributes(%var)->attributes()', $reset);
 68:         }
 69:         return $writer->write('$_input = (is_object(%node.word) ? %node.word : $_form[%node.word]); echo $_input->getControl()->attributes()');
 70:     }
 71: 
 72: 
 73:     /********************* run-time writers ****************d*g**/
 74: 
 75: 
 76:     /**
 77:      * Renders form begin.
 78:      * @return void
 79:      */
 80:     public static function renderFormBegin(NForm $form, array $attrs)
 81:     {
 82:         $el = $form->getElementPrototype();
 83:         $el->action = $action = (string) $el->action;
 84:         $el = clone $el;
 85:         if (strcasecmp($form->getMethod(), 'get') === 0) {
 86:             list($el->action) = explode('?', $action, 2);
 87:             if (($i = strpos($action, '#')) !== FALSE) {
 88:                 $el->action .= substr($action, $i);
 89:             }
 90:         }
 91:         echo $el->addAttributes($attrs)->startTag();
 92:     }
 93: 
 94: 
 95:     /**
 96:      * Renders form end.
 97:      * @return string
 98:      */
 99:     public static function renderFormEnd(NForm $form)
100:     {
101:         $s = '';
102:         if (strcasecmp($form->getMethod(), 'get') === 0) {
103:             $url = explode('?', $form->getElementPrototype()->action, 2);
104:             if (isset($url[1])) {
105:                 list($url[1]) = explode('#', $url[1], 2);
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 .= NHtml::el('input', array('type' => 'hidden', 'name' => $name, 'value' => urldecode($parts[1])));
111:                     }
112:                 }
113:             }
114:         }
115: 
116:         foreach ($form->getComponents(TRUE, 'NHiddenField') as $control) {
117:             if (!$control->getOption('rendered')) {
118:                 $s .= $control->getControl();
119:             }
120:         }
121: 
122:         if (iterator_count($form->getComponents(TRUE, 'NTextInput')) < 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.13 (for PHP 5.2, prefixed) API API documentation generated by ApiGen 2.8.0