Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationLatte
      • ApplicationTracy
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsLatte
      • Framework
      • HttpTracy
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • none
  • Tracy

Classes

  • FormMacros
  • Overview
  • Namespace
  • 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 (https://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette\Bridges\FormsLatte;
  9: 
 10: use Nette;
 11: use Latte;
 12: use Latte\MacroNode;
 13: use Latte\PhpWriter;
 14: use Latte\CompileException;
 15: use Latte\Macros\MacroSet;
 16: use Nette\Forms\Form;
 17: 
 18: 
 19: /**
 20:  * Macros for Nette\Forms.
 21:  *
 22:  * - {form name} ... {/form}
 23:  * - {input name}
 24:  * - {label name /} or {label name}... {/label}
 25:  * - {inputError name}
 26:  * - {formContainer name} ... {/formContainer}
 27:  *
 28:  * @author     David Grudl
 29:  */
 30: class FormMacros extends MacroSet
 31: {
 32: 
 33:     public static function install(Latte\Compiler $compiler)
 34:     {
 35:         $me = new static($compiler);
 36:         $me->addMacro('form', array($me, 'macroForm'), 'Nette\Bridges\FormsLatte\FormMacros::renderFormEnd($_form)');
 37:         $me->addMacro('formContainer', array($me, 'macroFormContainer'), '$formContainer = $_form = array_pop($_formStack)');
 38:         $me->addMacro('label', array($me, 'macroLabel'), array($me, 'macroLabelEnd'));
 39:         $me->addMacro('input', array($me, 'macroInput'), NULL, array($me, 'macroInputAttr'));
 40:         $me->addMacro('name', array($me, 'macroName'), array($me, 'macroNameEnd'), array($me, 'macroNameAttr'));
 41:         $me->addMacro('inputError', array($me, 'macroInputError'));
 42:     }
 43: 
 44: 
 45:     /********************* macros ****************d*g**/
 46: 
 47: 
 48:     /**
 49:      * {form ...}
 50:      */
 51:     public function macroForm(MacroNode $node, PhpWriter $writer)
 52:     {
 53:         if ($node->prefix) {
 54:             throw new CompileException('Did you mean <form n:name=...> ?');
 55:         }
 56:         $name = $node->tokenizer->fetchWord();
 57:         if ($name === FALSE) {
 58:             throw new CompileException("Missing form name in {{$node->name}}.");
 59:         }
 60:         $node->tokenizer->reset();
 61:         $node->replaced = TRUE;
 62:         return $writer->write(
 63:             'Nette\Bridges\FormsLatte\FormMacros::renderFormBegin($form = $_form = '
 64:             . ($name[0] === '$' ? 'is_object(%node.word) ? %node.word : ' : '')
 65:             . '$_control[%node.word], %node.array)'
 66:         );
 67:     }
 68: 
 69: 
 70:     /**
 71:      * {formContainer ...}
 72:      */
 73:     public function macroFormContainer(MacroNode $node, PhpWriter $writer)
 74:     {
 75:         $name = $node->tokenizer->fetchWord();
 76:         if ($name === FALSE) {
 77:             throw new CompileException("Missing name in {{$node->name}}.");
 78:         }
 79:         $node->tokenizer->reset();
 80:         return $writer->write(
 81:             '$_formStack[] = $_form; $formContainer = $_form = ' . ($name[0] === '$' ? 'is_object(%node.word) ? %node.word : ' : '') . '$_form[%node.word]'
 82:         );
 83:     }
 84: 
 85: 
 86:     /**
 87:      * {label ...}
 88:      */
 89:     public function macroLabel(MacroNode $node, PhpWriter $writer)
 90:     {
 91:         $words = $node->tokenizer->fetchWords();
 92:         if (!$words) {
 93:             throw new CompileException("Missing name in {{$node->name}}.");
 94:         }
 95:         $name = array_shift($words);
 96:         return $writer->write(
 97:             ($name[0] === '$' ? '$_input = is_object(%0.word) ? %0.word : $_form[%0.word]; if ($_label = $_input' : 'if ($_label = $_form[%0.word]')
 98:                 . '->%1.raw) echo $_label'
 99:                 . ($node->tokenizer->isNext() ? '->addAttributes(%node.array)' : ''),
100:             $name,
101:             $words ? ('getLabelPart(' . implode(', ', array_map(array($writer, 'formatWord'), $words)) . ')') : 'getLabel()'
102:         );
103:     }
104: 
105: 
106:     /**
107:      * {/label}
108:      */
109:     public function macroLabelEnd(MacroNode $node, PhpWriter $writer)
110:     {
111:         if ($node->content != NULL) {
112:             $node->openingCode = rtrim($node->openingCode, '?> ') . '->startTag() ?>';
113:             return $writer->write('if ($_label) echo $_label->endTag()');
114:         }
115:     }
116: 
117: 
118:     /**
119:      * {input ...}
120:      */
121:     public function macroInput(MacroNode $node, PhpWriter $writer)
122:     {
123:         $words = $node->tokenizer->fetchWords();
124:         if (!$words) {
125:             throw new CompileException("Missing name in {{$node->name}}.");
126:         }
127:         $name = array_shift($words);
128:         return $writer->write(
129:             ($name[0] === '$' ? '$_input = is_object(%0.word) ? %0.word : $_form[%0.word]; echo $_input' : 'echo $_form[%0.word]')
130:                 . '->%1.raw'
131:                 . ($node->tokenizer->isNext() ? '->addAttributes(%node.array)' : ''),
132:             $name,
133:             $words ? 'getControlPart(' . implode(', ', array_map(array($writer, 'formatWord'), $words)) . ')' : 'getControl()'
134:         );
135:     }
136: 
137: 
138:     /**
139:      * deprecated n:input
140:      */
141:     public function macroInputAttr(MacroNode $node, PhpWriter $writer)
142:     {
143:         throw new CompileException('Use n:name instead of n:input.');
144:     }
145: 
146: 
147:     /**
148:      * <form n:name>, <input n:name>, <select n:name>, <textarea n:name> and <label n:name>
149:      */
150:     public function macroNameAttr(MacroNode $node, PhpWriter $writer)
151:     {
152:         $words = $node->tokenizer->fetchWords();
153:         if (!$words) {
154:             throw new CompileException("Missing name in n:{$node->name}.");
155:         }
156:         $name = array_shift($words);
157:         $tagName = strtolower($node->htmlNode->name);
158:         $node->isEmpty = !in_array($tagName, array('form', 'select', 'textarea'), TRUE);
159:         $node->replaced = TRUE;
160: 
161:         if ($tagName === 'form') {
162:             return $writer->write(
163:                 'Nette\Bridges\FormsLatte\FormMacros::renderFormBegin($form = $_form = '
164:                     . ($name[0] === '$' ? 'is_object(%0.word) ? %0.word : ' : '')
165:                     . '$_control[%0.word], %1.var, FALSE)',
166:                 $name,
167:                 array_fill_keys(array_keys($node->htmlNode->attrs), NULL)
168:             );
169:         } else {
170:             $method = $tagName === 'label' ? 'getLabel' : 'getControl';
171:             return $writer->write(
172:                 '$_input = ' . ($name[0] === '$' ? 'is_object(%0.word) ? %0.word : ' : '')
173:                     . '$_form[%0.word]; echo $_input->%1.raw'
174:                     . ($node->htmlNode->attrs ? '->addAttributes(%2.var)' : '') . '->attributes()',
175:                 $name,
176:                 $words
177:                     ? $method . 'Part(' . implode(', ', array_map(array($writer, 'formatWord'), $words)) . ')'
178:                     : "{method_exists(\$_input, '{$method}Part')?'{$method}Part':'{$method}'}()",
179:                 array_fill_keys(array_keys($node->htmlNode->attrs), NULL)
180:             );
181:         }
182:     }
183: 
184: 
185:     public function macroName(MacroNode $node, PhpWriter $writer)
186:     {
187:         if (!$node->prefix) {
188:             throw new CompileException("Unknown macro {{$node->name}}, use n:{$node->name} attribute.");
189:         } elseif ($node->prefix !== MacroNode::PREFIX_NONE) {
190:             throw new CompileException("Unknown attribute n:{$node->prefix}-{$node->name}, use n:{$node->name} attribute.");
191:         }
192:     }
193: 
194: 
195:     public function macroNameEnd(MacroNode $node, PhpWriter $writer)
196:     {
197:         preg_match('#^(.*? n:\w+>)(.*)(<[^?].*)\z#s', $node->content, $parts);
198:         $node->replaced = TRUE;
199:         if (strtolower($node->htmlNode->name) === 'form') {
200:             $node->content = $parts[1] . $parts[2] . '<?php Nette\Bridges\FormsLatte\FormMacros::renderFormEnd($_form, FALSE) ?>' . $parts[3];
201:         } else { // select, textarea
202:             $node->content = $parts[1] . '<?php echo $_input->getControl()->getHtml() ?>' . $parts[3];
203:         }
204:     }
205: 
206: 
207:     /**
208:      * {inputError ...}
209:      */
210:     public function macroInputError(MacroNode $node, PhpWriter $writer)
211:     {
212:         $name = $node->tokenizer->fetchWord();
213:         if (!$name) {
214:             return $writer->write('echo %escape($_input->getError())');
215:         } elseif ($name[0] === '$') {
216:             return $writer->write('$_input = is_object(%0.word) ? %0.word : $_form[%0.word]; echo %escape($_input->getError())', $name);
217:         } else {
218:             return $writer->write('echo %escape($_form[%0.word]->getError())', $name);
219:         }
220:     }
221: 
222: 
223:     /********************* run-time writers ****************d*g**/
224: 
225: 
226:     /**
227:      * Renders form begin.
228:      * @return void
229:      */
230:     public static function renderFormBegin(Form $form, array $attrs, $withTags = TRUE)
231:     {
232:         foreach ($form->getControls() as $control) {
233:             $control->setOption('rendered', FALSE);
234:         }
235:         $el = $form->getElementPrototype();
236:         $el->action = (string) $el->action;
237:         $el = clone $el;
238:         if (strcasecmp($form->getMethod(), 'get') === 0) {
239:             $el->action = preg_replace('~\?[^#]*~', '', $el->action, 1);
240:         }
241:         $el->addAttributes($attrs);
242:         echo $withTags ? $el->startTag() : $el->attributes();
243:     }
244: 
245: 
246:     /**
247:      * Renders form end.
248:      * @return string
249:      */
250:     public static function renderFormEnd(Form $form, $withTags = TRUE)
251:     {
252:         $s = '';
253:         if (strcasecmp($form->getMethod(), 'get') === 0) {
254:             foreach (preg_split('#[;&]#', parse_url($form->getElementPrototype()->action, PHP_URL_QUERY), NULL, PREG_SPLIT_NO_EMPTY) as $param) {
255:                 $parts = explode('=', $param, 2);
256:                 $name = urldecode($parts[0]);
257:                 if (!isset($form[$name])) {
258:                     $s .= Nette\Utils\Html::el('input', array('type' => 'hidden', 'name' => $name, 'value' => urldecode($parts[1])));
259:                 }
260:             }
261:         }
262: 
263:         foreach ($form->getComponents(TRUE, 'Nette\Forms\Controls\HiddenField') as $control) {
264:             if (!$control->getOption('rendered')) {
265:                 $s .= $control->getControl();
266:             }
267:         }
268: 
269:         if (iterator_count($form->getComponents(TRUE, 'Nette\Forms\Controls\TextInput')) < 2) {
270:             $s .= '<!--[if IE]><input type=IEbug disabled style="display:none"><![endif]-->';
271:         }
272: 
273:         echo ($s ? "<div>$s</div>\n" : '') . ($withTags ? $form->getElementPrototype()->endTag() . "\n" : '');
274:     }
275: 
276: }
277: 
Nette 2.2 API documentation generated by ApiGen 2.8.0