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