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', '$_input = (is_object(%node.word) ? %node.word : $_form[%node.word]); echo $_input->getControl()->addAttributes(%node.array)', NULL, array($me, 'macroAttrName'));
37: $me->addMacro('formContainer', '$_formStack[] = $_form; $formContainer = $_form = (is_object(%node.word) ? %node.word : $_form[%node.word])', '$_form = array_pop($_formStack)');
38: $me->addMacro('name', NULL, NULL, array($me, 'macroAttrName'));
39: }
40:
41:
42:
43:
44:
45: 46: 47:
48: public function macroLabel(NMacroNode $node, NPhpWriter $writer)
49: {
50: $cmd = '$_input = is_object(%node.word) ? %node.word : $_form[%node.word]; if ($_label = $_input->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: public function macroAttrName(NMacroNode $node, NPhpWriter $writer)
64: {
65: if ($node->htmlNode->attrs) {
66: $reset = array_fill_keys(array_keys($node->htmlNode->attrs), NULL);
67: return $writer->write('$_input = (is_object(%node.word) ? %node.word : $_form[%node.word]); echo $_input->getControl()->addAttributes(%var)->attributes()', $reset);
68: }
69: return $writer->write('$_input = (is_object(%node.word) ? %node.word : $_form[%node.word]); echo $_input->getControl()->attributes()');
70: }
71:
72:
73:
74:
75:
76: 77: 78: 79:
80: public static function renderFormBegin(NForm $form, array $attrs)
81: {
82: $el = $form->getElementPrototype();
83: $el->action = $action = (string) $el->action;
84: $el = clone $el;
85: if (strcasecmp($form->getMethod(), 'get') === 0) {
86: list($el->action) = explode('?', $action, 2);
87: if (($i = strpos($action, '#')) !== FALSE) {
88: $el->action .= substr($action, $i);
89: }
90: }
91: echo $el->addAttributes($attrs)->startTag();
92: }
93:
94:
95: 96: 97: 98:
99: public static function renderFormEnd(NForm $form)
100: {
101: $s = '';
102: if (strcasecmp($form->getMethod(), 'get') === 0) {
103: $url = explode('?', $form->getElementPrototype()->action, 2);
104: if (isset($url[1])) {
105: list($url[1]) = explode('#', $url[1], 2);
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: