Packages

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

Classes

  • NCacheMacro
  • NCoreMacros
  • NFormMacros
  • NMacroSet
  • NUIMacros
  • 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 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 = $_control[%node.word], %node.array)',
 34:             'NFormMacros::renderFormEnd($_form)');
 35:         $me->addMacro('label', array($me, 'macroLabel'), '?></label><?php');
 36:         $me->addMacro('@input', array($me, 'macroAttrInput'));
 37:         $me->addMacro('input', 'echo $_form[%node.word]->getControl()->addAttributes(%node.array)');
 38:         $me->addMacro('formContainer', '$_formStack[] = $_form; $formContainer = $_form = $_form[%node.word]', '$_form = array_pop($_formStack)');
 39:     }
 40: 
 41: 
 42: 
 43:     /********************* macros ****************d*g**/
 44: 
 45: 
 46:     /**
 47:      * {label ...} and optionally {/label}
 48:      */
 49:     public function macroLabel(NMacroNode $node, $writer)
 50:     {
 51:         $cmd = 'if ($_label = $_form[%node.word]->getLabel()) echo $_label->addAttributes(%node.array)';
 52:         if ($node->isEmpty = (substr($node->args, -1) === '/')) {
 53:             $node->setArgs(substr($node->args, 0, -1));
 54:             return $writer->write($cmd);
 55:         } else {
 56:             return $writer->write($cmd . '->startTag()');
 57:         }
 58:     }
 59: 
 60: 
 61: 
 62:     /**
 63:      * n:input
 64:      */
 65:     public function macroAttrInput(NMacroNode $node, $writer)
 66:     {
 67:         if ($node->htmlNode->attrs) {
 68:             $reset = array_fill_keys(array_keys($node->htmlNode->attrs), NULL);
 69:             return $writer->write('echo $_form[%node.word]->getControl()->addAttributes(%var)->attributes()', $reset);
 70:         }
 71:         return $writer->write('echo $_form[%node.word]->getControl()->attributes()');
 72:     }
 73: 
 74: 
 75: 
 76:     /********************* run-time writers ****************d*g**/
 77: 
 78: 
 79: 
 80:     /**
 81:      * Renders form begin.
 82:      * @return void
 83:      */
 84:     public static function renderFormBegin($form, $attrs)
 85:     {
 86:         $el = $form->getElementPrototype();
 87:         $el->action = (string) $el->action;
 88:         $el = clone $el;
 89:         if (strcasecmp($form->getMethod(), 'get') === 0) {
 90:             list($el->action) = explode('?', $el->action, 2);
 91:         }
 92:         echo $el->addAttributes($attrs)->startTag();
 93:     }
 94: 
 95: 
 96: 
 97:     /**
 98:      * Renders form end.
 99:      * @return string
100:      */
101:     public static function renderFormEnd($form)
102:     {
103:         $s = '';
104:         if (strcasecmp($form->getMethod(), 'get') === 0) {
105:             $url = explode('?', $form->getElementPrototype()->action, 2);
106:             if (isset($url[1])) {
107:                 foreach (preg_split('#[;&]#', $url[1]) as $param) {
108:                     $parts = explode('=', $param, 2);
109:                     $name = urldecode($parts[0]);
110:                     if (!isset($form[$name])) {
111:                         $s .= NHtml::el('input', array('type' => 'hidden', 'name' => $name, 'value' => urldecode($parts[1])));
112:                     }
113:                 }
114:             }
115:         }
116: 
117:         foreach ($form->getComponents(TRUE, 'NHiddenField') as $control) {
118:             if (!$control->getOption('rendered')) {
119:                 $s .= $control->getControl();
120:             }
121:         }
122: 
123:         if (iterator_count($form->getComponents(TRUE, 'NTextInput')) < 2) {
124:             $s .= '<!--[if IE]><input type=IEbug disabled style="display:none"><![endif]-->';
125:         }
126: 
127:         echo ($s ? "<div>$s</div>\n" : '') . $form->getElementPrototype()->endTag() . "\n";
128:     }
129: 
130: }
131: 
Nette Framework 2.0beta2 (for PHP 5.2) API API documentation generated by ApiGen 2.3.0