1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Nette\Latte\Macros;
13:
14: use Nette,
15: Nette\Latte,
16: Nette\Latte\MacroNode,
17: Nette\Latte\CompileException,
18: Nette\Utils\Strings;
19:
20:
21:
22: 23: 24: 25: 26: 27: 28: 29: 30: 31:
32: class FormMacros extends MacroSet
33: {
34:
35: public static function install(Latte\Compiler $compiler)
36: {
37: $me = new static($compiler);
38: $me->addMacro('form',
39: 'Nette\Latte\Macros\FormMacros::renderFormBegin($form = $_form = $_control[%node.word], %node.array)',
40: 'Nette\Latte\Macros\FormMacros::renderFormEnd($_form)');
41: $me->addMacro('label', array($me, 'macroLabel'), '?></label><?php');
42: $me->addMacro('input', 'echo $_form[%node.word]->getControl()->addAttributes(%node.array)', NULL, array($me, 'macroAttrInput'));
43: $me->addMacro('formContainer', '$_formStack[] = $_form; $formContainer = $_form = $_form[%node.word]', '$_form = array_pop($_formStack)');
44: }
45:
46:
47:
48:
49:
50:
51: 52: 53:
54: public function macroLabel(MacroNode $node, $writer)
55: {
56: $cmd = 'if ($_label = $_form[%node.word]->getLabel()) echo $_label->addAttributes(%node.array)';
57: if ($node->isEmpty = (substr($node->args, -1) === '/')) {
58: $node->setArgs(substr($node->args, 0, -1));
59: return $writer->write($cmd);
60: } else {
61: return $writer->write($cmd . '->startTag()');
62: }
63: }
64:
65:
66:
67: 68: 69:
70: public function macroAttrInput(MacroNode $node, $writer)
71: {
72: if ($node->htmlNode->attrs) {
73: $reset = array_fill_keys(array_keys($node->htmlNode->attrs), NULL);
74: return $writer->write('echo $_form[%node.word]->getControl()->addAttributes(%var)->attributes()', $reset);
75: }
76: return $writer->write('echo $_form[%node.word]->getControl()->attributes()');
77: }
78:
79:
80:
81:
82:
83:
84:
85: 86: 87: 88:
89: public static function renderFormBegin($form, $attrs)
90: {
91: $el = $form->getElementPrototype();
92: $el->action = (string) $el->action;
93: $el = clone $el;
94: if (strcasecmp($form->getMethod(), 'get') === 0) {
95: list($el->action) = explode('?', $el->action, 2);
96: }
97: echo $el->addAttributes($attrs)->startTag();
98: }
99:
100:
101:
102: 103: 104: 105:
106: public static function renderFormEnd($form)
107: {
108: $s = '';
109: if (strcasecmp($form->getMethod(), 'get') === 0) {
110: $url = explode('?', $form->getElementPrototype()->action, 2);
111: if (isset($url[1])) {
112: foreach (preg_split('#[;&]#', $url[1]) as $param) {
113: $parts = explode('=', $param, 2);
114: $name = urldecode($parts[0]);
115: if (!isset($form[$name])) {
116: $s .= Nette\Utils\Html::el('input', array('type' => 'hidden', 'name' => $name, 'value' => urldecode($parts[1])));
117: }
118: }
119: }
120: }
121:
122: foreach ($form->getComponents(TRUE, 'Nette\Forms\Controls\HiddenField') as $control) {
123: if (!$control->getOption('rendered')) {
124: $s .= $control->getControl();
125: }
126: }
127:
128: if (iterator_count($form->getComponents(TRUE, 'Nette\Forms\Controls\TextInput')) < 2) {
129: $s .= '<!--[if IE]><input type=IEbug disabled style="display:none"><![endif]-->';
130: }
131:
132: echo ($s ? "<div>$s</div>\n" : '') . $form->getElementPrototype()->endTag() . "\n";
133: }
134:
135: }
136: