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