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