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: class Horde_View_Helper_FormTag extends Horde_View_Helper_Base
25: {
26: public function formTag($urlForOptions = array(), $options = array())
27: {
28: $htmlOptions = $this->htmlOptionsForForm($urlForOptions, $options );
29: return $this->formTagHtml($htmlOptions);
30: }
31:
32: public function endFormTag()
33: {
34: return '</form>';
35: }
36:
37: public function selectTag($name, $optionTags = null, $options = array())
38: {
39: return $this->contentTag('select', $optionTags,
40: array_merge(array('name' => $name, 'id' => $name), $options));
41: }
42:
43: public function textFieldTag($name, $value = null, $options = array())
44: {
45: return $this->tag('input', array_merge(array('type' => 'text',
46: 'name' => $name,
47: 'id' => $name,
48: 'value' => $value),
49: $options));
50: }
51:
52: public function hiddenFieldTag($name, $value = null, $options = array())
53: {
54: return $this->textFieldTag($name, $value, array_merge($options, array('type' => 'hidden')));
55: }
56:
57: public function fileFieldTag($name, $options = array())
58: {
59: return $this->textFieldTag($name, null, array_merge($options, array('type' => 'file')));
60: }
61:
62: public function passwordFieldTag($name = 'password', $value = null, $options = array())
63: {
64: return $this->textFieldTag($name, $value, array_merge($options, array('type' => 'password')));
65: }
66:
67: public function textAreaTag($name, $content = null, $options = array())
68: {
69: if (isset($options['size'])) {
70: $size = $options['size'];
71: unset($options['size']);
72: if (strpos($size, 'x') !== false) {
73: list($options['cols'], $options['rows']) = explode('x', $size);
74: }
75: }
76:
77: return $this->contentTag('textarea', $content,
78: array_merge(array('name' => $name, 'id' => $name), $options));
79: }
80:
81: public function checkBoxTag($name, $value = '1', $checked = false, $options = array())
82: {
83: $htmlOptions = array_merge(array('type' => 'checkbox',
84: 'name' => $name,
85: 'id' => $name,
86: 'value' => $value,
87: 'checked' => $checked), $options);
88:
89: return $this->tag('input', $htmlOptions);
90: }
91:
92: public function radioButtonTag($name, $value, $checked = false, $options = array())
93: {
94: $prettyTagValue = preg_replace('/\s/', '_', $value);
95: $prettyTagValue = strtolower(preg_replace('/(?!-)\W/', '', $prettyTagValue));
96:
97: $htmlOptions = array_merge(array('type' => 'radio',
98: 'name' => $name,
99: 'id' => "{$name}_{$prettyTagValue}",
100: 'value' => $value,
101: 'checked' => $checked), $options);
102:
103: return $this->tag('input', $htmlOptions);
104: }
105:
106: public function submitTag($value = 'Save changes', $options = array())
107: {
108: if (isset($options['disableWith'])) {
109: $disableWith = $options['disableWith'];
110: unset($options['disableWith']);
111:
112: $options['onclick'] = implode(';', array(
113: "this.setAttribute('originalValue', this.value)",
114: "this.disabled=true",
115: "this.value='$disableWith'",
116: "{$options['onclick']}",
117: "result = (this.form.onsubmit ? (this.form.onsubmit() ? this.form.submit() : false) : this.form.submit())",
118: "if (result == false) { this.value = this.getAttribute('originalValue'); this.disabled = false }",
119: "return result"
120: ));
121: }
122:
123: return $this->tag('input', array_merge(array('type' => 'submit', 'name' => 'commit', 'value' => $value),
124: $options));
125: }
126:
127: public function imageSubmitTag($source, $options = array())
128: {
129:
130: return $this->tag('input', array_merge(array('type' => 'image',
131: 'src' => $this->imagePath($source)),
132: $options));
133: }
134:
135: private function ($htmlOptions)
136: {
137: $method = isset($htmlOptions['method']) ? strtolower($htmlOptions['method']) : '';
138: if ($method == 'get') {
139: $htmlOptions['method'] = 'get';
140: return array('', $htmlOptions);
141: } else if ($method == 'post' || $method == '') {
142: $htmlOptions['method'] = 'post';
143: return array('', $htmlOptions);
144: } else {
145: $htmlOptions['method'] = 'post';
146: $extraTags = $this->contentTag('div',
147: $this->tag('input', array('type' => 'hidden', 'name' => '_method',
148: 'value' => $method)), array('style' => 'margin:0;padding:0'));
149: return array($extraTags, $htmlOptions);
150: }
151:
152: }
153:
154: private function formTagHtml($htmlOptions)
155: {
156: list($extraTags, $htmlOptions) = $this->extraTagsForForm($htmlOptions);
157: return $this->tag('form', $htmlOptions, true) . $extraTags;
158: }
159:
160:
161: private function htmlOptionsForForm($urlForOptions, $options)
162: {
163: if (isset($options['multipart'])) {
164: unset($options['multipart']);
165: $options['enctype'] = 'multipart/form-data';
166: }
167:
168: $options['action'] = $this->urlFor($urlForOptions);
169:
170:
171:
172: return $options;
173: }
174:
175: }
176: