1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 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', 'echo $_form[%node.word]->getControl()->addAttributes(%node.array)', NULL, array($me, 'macroAttrInput'));
37: $me->addMacro('formContainer', '$_formStack[] = $_form; $formContainer = $_form = $_form[%node.word]', '$_form = array_pop($_formStack)');
38: }
39:
40:
41:
42:
43:
44:
45: 46: 47:
48: public function macroLabel(NMacroNode $node, NPhpWriter $writer)
49: {
50: $cmd = 'if ($_label = $_form[%node.word]->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: 62: 63:
64: public function macroAttrInput(NMacroNode $node, NPhpWriter $writer)
65: {
66: if ($node->htmlNode->attrs) {
67: $reset = array_fill_keys(array_keys($node->htmlNode->attrs), NULL);
68: return $writer->write('echo $_form[%node.word]->getControl()->addAttributes(%var)->attributes()', $reset);
69: }
70: return $writer->write('echo $_form[%node.word]->getControl()->attributes()');
71: }
72:
73:
74:
75:
76:
77:
78:
79: 80: 81: 82:
83: public static function renderFormBegin(NForm $form, array $attrs)
84: {
85: $el = $form->getElementPrototype();
86: $el->action = (string) $el->action;
87: $el = clone $el;
88: if (strcasecmp($form->getMethod(), 'get') === 0) {
89: list($el->action) = explode('?', $el->action, 2);
90: }
91: echo $el->addAttributes($attrs)->startTag();
92: }
93:
94:
95:
96: 97: 98: 99:
100: public static function renderFormEnd(NForm $form)
101: {
102: $s = '';
103: if (strcasecmp($form->getMethod(), 'get') === 0) {
104: $url = explode('?', $form->getElementPrototype()->action, 2);
105: if (isset($url[1])) {
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: