1 |
jonen |
1.1 |
<?php |
2 |
|
|
|
3 |
|
|
/** |
4 |
|
|
* This file contains various helper functions |
5 |
|
|
* for building forms and form elements. |
6 |
|
|
* |
7 |
jonen |
1.3 |
* $Id: form_utils.inc,v 1.13 2003/07/18 17:59:21 hemna Exp $ |
8 |
jonen |
1.1 |
* |
9 |
|
|
* @author Walter A. Boring IV <waboring@buildabetterweb.com> |
10 |
|
|
* @package phpHtmlLib |
11 |
|
|
* |
12 |
jonen |
1.2 |
* |
13 |
|
|
*/ |
14 |
jonen |
1.1 |
|
15 |
|
|
|
16 |
|
|
/** |
17 |
|
|
* render an form open tag only. |
18 |
|
|
* This is usefull for forms that are inside a table. |
19 |
|
|
* you would render the form tag first, then the table |
20 |
|
|
* with the form fields. |
21 |
|
|
* @param string $name - name attribute of the form tag. |
22 |
|
|
* @param string $action - the form action. |
23 |
|
|
* @param string $method - form method |
24 |
|
|
* @param string $attributes - any extra name='value' attributes for |
25 |
|
|
* the form tag. |
26 |
|
|
* @return string returns the raw form tag. |
27 |
|
|
*/ |
28 |
|
|
function html_form( $name, $action, $method="GET", $attributes=array()) { |
29 |
|
|
|
30 |
jonen |
1.2 |
$attribs = array("name" => $name, "action" => $action, |
31 |
|
|
"method" => $method); |
32 |
|
|
$attributes = array_merge( $attribs, $attributes); |
33 |
jonen |
1.1 |
|
34 |
jonen |
1.2 |
return new FORMtag( $attributes ); |
35 |
jonen |
1.1 |
} |
36 |
|
|
|
37 |
|
|
/** |
38 |
|
|
* render an form open tag only. |
39 |
|
|
* This is usefull for forms that are inside a table. |
40 |
|
|
* you would render the form tag first, then the table |
41 |
|
|
* with the form fields. |
42 |
|
|
* @param string $name - name attribute of the form tag. |
43 |
|
|
* @param string $action - the form action. |
44 |
|
|
* @param string $method - form method |
45 |
|
|
* @param string $attributes - any extra name='value' attributes for |
46 |
|
|
* the form tag. |
47 |
|
|
* @return string returns the raw form tag. |
48 |
|
|
*/ |
49 |
|
|
function form_open( $name, $action, $method="GET", $attributes=array(), $indent_level=0 ) { |
50 |
jonen |
1.2 |
$form = html_form( $name, $action, $method, $attributes); |
51 |
|
|
return $form->_render_tag( $indent_level ); |
52 |
jonen |
1.1 |
} |
53 |
|
|
|
54 |
|
|
|
55 |
|
|
/** |
56 |
|
|
* render a form close tag |
57 |
|
|
* |
58 |
|
|
* @return string - the </form> tag. |
59 |
|
|
*/ |
60 |
|
|
function form_close( $indent_level=0 ) { |
61 |
jonen |
1.2 |
$form = new FORMtag; |
62 |
jonen |
1.1 |
|
63 |
jonen |
1.2 |
//just render the close tag. |
64 |
|
|
return $form->_render_close_tag( $indent_level ); |
65 |
jonen |
1.1 |
} |
66 |
|
|
|
67 |
|
|
|
68 |
|
|
|
69 |
|
|
/** |
70 |
|
|
* build/render an input tag of type text |
71 |
|
|
* |
72 |
|
|
* @param string $name - the name of the input tag. |
73 |
|
|
* @param string $value - the value of the tag |
74 |
|
|
* @param int $size - the size in characters of the text tag |
75 |
|
|
* @param int $maxlength - the maximum @ of characters for the field |
76 |
|
|
* @param array $attributes - any extra name='value' pair attributes |
77 |
|
|
* @param boolean $render_flag - render an object (FALSE) or raw html (TRUE); |
78 |
|
|
* @return mixed either returns an input object (default) or raw html. |
79 |
|
|
*/ |
80 |
|
|
function form_text($name, $value=NULL, $size=NULL, $maxlength=NULL, |
81 |
jonen |
1.2 |
$attributes = array(), $render_flag=FALSE) { |
82 |
jonen |
1.1 |
|
83 |
jonen |
1.2 |
if ($size != NULL) { |
84 |
|
|
$attributes["size"] = $size; |
85 |
|
|
} |
86 |
|
|
if ($maxlength != NULL) { |
87 |
|
|
$attributes["maxlength"] = $maxlength; |
88 |
|
|
} |
89 |
|
|
|
90 |
|
|
$input = html_input("text", $name, $value, $attributes); |
91 |
|
|
|
92 |
|
|
if ($render_flag) { |
93 |
|
|
return $input->render(); |
94 |
|
|
} |
95 |
|
|
else { |
96 |
|
|
//user wants the object |
97 |
|
|
return $input; |
98 |
|
|
} |
99 |
jonen |
1.1 |
} |
100 |
|
|
|
101 |
|
|
|
102 |
|
|
/** |
103 |
|
|
* build/render an input tag of type password |
104 |
|
|
* |
105 |
|
|
* @param string $name - the name of the input tag. |
106 |
|
|
* @param string $value - the value of the tag |
107 |
|
|
* @param int $size - the size in characters of the text tag |
108 |
|
|
* @param int $maxlength - the maximum @ of characters for the field |
109 |
|
|
* @param array $attributes - any extra name='value' pair attributes |
110 |
|
|
* @param boolean $render_flag - render an object (FALSE) or raw html (TRUE); |
111 |
|
|
* @return mixed either returns an input object (default) or raw html. |
112 |
|
|
*/ |
113 |
|
|
function form_password($name, $value=NULL, $size=NULL, $maxlength=NULL, |
114 |
jonen |
1.2 |
$attributes = array(), $render_flag=FALSE) { |
115 |
|
|
|
116 |
|
|
if ($size != NULL) { |
117 |
|
|
$attributes["size"] = $size; |
118 |
|
|
} |
119 |
|
|
if ($maxlength != NULL) { |
120 |
|
|
$attributes["maxlength"] = $maxlength; |
121 |
|
|
} |
122 |
|
|
|
123 |
|
|
$input = html_input( "password", $name, $value, $attributes ); |
124 |
jonen |
1.1 |
|
125 |
jonen |
1.2 |
if ($render_flag) { |
126 |
|
|
return $input->render(); |
127 |
|
|
} |
128 |
|
|
else { |
129 |
|
|
//user wants the object |
130 |
|
|
return $input; |
131 |
|
|
} |
132 |
jonen |
1.1 |
} |
133 |
|
|
|
134 |
|
|
/** |
135 |
|
|
* build/render an input tag of type button |
136 |
|
|
* |
137 |
|
|
* @param string $name - the name of the input tag. |
138 |
|
|
* @param string $value - the value of the tag |
139 |
|
|
* @param array $attributes - any extra name='value' pair attributes |
140 |
|
|
* @param boolean $render_flag - render an object (FALSE) or raw html (TRUE); |
141 |
|
|
* @return mixed either returns an input object (default) or raw html. |
142 |
|
|
*/ |
143 |
|
|
function form_button($name, $value=NULL, $attributes=array(), $render_flag=FALSE ) { |
144 |
|
|
|
145 |
jonen |
1.2 |
$input = html_input( "button", $name, $value, $attributes ); |
146 |
jonen |
1.1 |
|
147 |
jonen |
1.2 |
if ($render_flag) { |
148 |
|
|
return $input->render(); |
149 |
|
|
} |
150 |
|
|
else { |
151 |
|
|
return $input; |
152 |
|
|
} |
153 |
jonen |
1.1 |
|
154 |
|
|
} |
155 |
|
|
|
156 |
|
|
/** |
157 |
|
|
* build/render an input tag of type submit |
158 |
|
|
* |
159 |
|
|
* @param string $name - the name of the input tag. |
160 |
|
|
* @param string $value - the value of the tag |
161 |
|
|
* @param array $attributes - any extra name='value' pair attributes |
162 |
|
|
* @param boolean $render_flag - render an object (FALSE) or raw html (TRUE); |
163 |
|
|
* @return mixed either returns an input object (default) or raw html. |
164 |
|
|
*/ |
165 |
|
|
function form_submit($name, $value=NULL, $attributes=array(), $render_flag=FALSE ) { |
166 |
|
|
|
167 |
jonen |
1.2 |
$input = html_input( "submit", $name, $value, $attributes ); |
168 |
jonen |
1.1 |
|
169 |
jonen |
1.2 |
if ($render_flag) { |
170 |
|
|
return $input->render(); |
171 |
|
|
} |
172 |
|
|
else { |
173 |
|
|
return $input; |
174 |
|
|
} |
175 |
jonen |
1.1 |
} |
176 |
|
|
|
177 |
|
|
/** |
178 |
|
|
* build/render an input tag of type image |
179 |
|
|
* |
180 |
|
|
* @param string $name - the name of the input tag. |
181 |
|
|
* @param string $value - the value of the tag |
182 |
|
|
* @param string $src - the image src |
183 |
|
|
* @param array $attributes - any extra name='value' pair attributes |
184 |
|
|
* @param boolean $render_flag - render an object (FALSE) or raw html (TRUE); |
185 |
|
|
* @return mixed either returns an input object (default) or raw html. |
186 |
|
|
*/ |
187 |
|
|
function form_image($name, $value=NULL, $src, $attributes=array(), $render_flag=FALSE ) { |
188 |
|
|
|
189 |
jonen |
1.2 |
$attributes["src"] = $src; |
190 |
|
|
$input = html_input( "image", $name, $value, $attributes ); |
191 |
jonen |
1.1 |
|
192 |
jonen |
1.2 |
if ($render_flag) { |
193 |
|
|
return $input->render(); |
194 |
|
|
} |
195 |
|
|
else { |
196 |
|
|
return $input; |
197 |
|
|
} |
198 |
jonen |
1.1 |
} |
199 |
|
|
|
200 |
|
|
|
201 |
|
|
/** |
202 |
|
|
* build/render an input tag of type radio |
203 |
|
|
* |
204 |
|
|
* @param string $name - the name of the input tag. |
205 |
|
|
* @param string $value - the value of the tag |
206 |
|
|
* @param array $attributes - any extra name='value' pair attributes |
207 |
|
|
* @param boolean $render_flag - render an object (FALSE) or raw html (TRUE); |
208 |
|
|
* @return mixed either returns an input object (default) or raw html. |
209 |
|
|
*/ |
210 |
|
|
function form_radio($name, $value=NULL, $attributes=array(), $render_flag=FALSE ) { |
211 |
|
|
|
212 |
jonen |
1.2 |
$input = html_input( "radio", $name, $value, $attributes ); |
213 |
jonen |
1.1 |
|
214 |
jonen |
1.2 |
if ($render_flag) { |
215 |
|
|
return $input->render(); |
216 |
|
|
} |
217 |
|
|
else { |
218 |
|
|
return $input; |
219 |
|
|
} |
220 |
jonen |
1.1 |
} |
221 |
|
|
|
222 |
|
|
/** |
223 |
|
|
* build/render an set of radio buttons |
224 |
|
|
* with onClick javaScript selection |
225 |
|
|
* |
226 |
|
|
* @param string $name - the name of the input tag. |
227 |
|
|
* @param array $list - an array of text label = value pairs |
228 |
|
|
* @param string $value - the default value (the one that will be checked) |
229 |
|
|
* @param bool $line_break - specify whether to put a line break after each radio button |
230 |
|
|
* @param array $attribs - specify addionnal attribs for each value |
231 |
|
|
* @return object returns a container object |
232 |
|
|
* @author Suren Markossian |
233 |
|
|
*/ |
234 |
jonen |
1.2 |
function form_active_radio( $name, $list, $value, $line_break = TRUE, $attribs = FALSE) { |
235 |
jonen |
1.1 |
|
236 |
|
|
$container = new ContainerWidget(); |
237 |
|
|
$i=0; |
238 |
|
|
while (list($text,$val) = each($list)) { |
239 |
|
|
if ($attribs[$val]) { |
240 |
jonen |
1.2 |
$attr = $attribs[$val]; |
241 |
|
|
} |
242 |
|
|
else { |
243 |
|
|
$attr = array(); |
244 |
|
|
} |
245 |
jonen |
1.1 |
|
246 |
|
|
if ($val == $value) $attr[] = "checked"; |
247 |
|
|
$container->push(form_radio($name, $val, $attr)); |
248 |
|
|
|
249 |
|
|
if (count($list)>1) { |
250 |
|
|
if ($attribs[$val]) { |
251 |
jonen |
1.2 |
$attr = $attribs[$val]; |
252 |
|
|
} |
253 |
|
|
else { |
254 |
|
|
$attr = array(); |
255 |
|
|
} |
256 |
jonen |
1.1 |
|
257 |
|
|
$attr["class"] ="form_link"; |
258 |
jonen |
1.3 |
$attr["href"] = "javascript:void(0)"; |
259 |
jonen |
1.2 |
$js = "javascript: function check(item){item.click();} check(".$name.".item($i));"; |
260 |
|
|
$attr["onclick"] = $js; |
261 |
jonen |
1.1 |
|
262 |
|
|
$link = new Atag($attr, $text, FALSE); |
263 |
|
|
} |
264 |
|
|
else $link = $text; |
265 |
|
|
|
266 |
|
|
$container->push($link); |
267 |
|
|
if ($line_break) $container->push(html_br()); |
268 |
|
|
$i++; |
269 |
|
|
} |
270 |
|
|
return $container; |
271 |
|
|
} |
272 |
|
|
|
273 |
|
|
|
274 |
|
|
/** |
275 |
|
|
* build/render an input tag of type hidden |
276 |
|
|
* |
277 |
|
|
* @param string $name - the name of the input tag. |
278 |
|
|
* @param string $value - the value of the tag |
279 |
|
|
* @param array $attributes - any extra name='value' pair attributes |
280 |
|
|
* @param boolean $render_flag - render an object (FALSE) or raw html (TRUE); |
281 |
|
|
* @return mixed either returns an input object (default) or raw html. |
282 |
|
|
*/ |
283 |
|
|
function form_hidden($name, $value=NULL, $attributes=array(), $render_flag=FALSE ) { |
284 |
|
|
|
285 |
jonen |
1.2 |
$input = html_input( "hidden", $name, $value, $attributes ); |
286 |
jonen |
1.1 |
|
287 |
jonen |
1.2 |
if ($render_flag) { |
288 |
|
|
return $input->render(); |
289 |
|
|
} |
290 |
|
|
else { |
291 |
|
|
return $input; |
292 |
|
|
} |
293 |
jonen |
1.1 |
} |
294 |
|
|
|
295 |
|
|
/** |
296 |
|
|
* build/render an input tag of type CHECKBOX |
297 |
|
|
* |
298 |
|
|
* @param string $name - the name of the input tag. |
299 |
|
|
* @param string $value - the value of the tag |
300 |
|
|
* @param array $attributes - any extra name='value' pair attributes |
301 |
|
|
* @param boolean $render_flag - render an object (FALSE) or raw html (TRUE); |
302 |
|
|
* @return mixed either returns an input object (default) or raw html. |
303 |
|
|
*/ |
304 |
|
|
function form_checkbox($name, $value=NULL, $attributes=array(), $render_flag=FALSE ) { |
305 |
|
|
|
306 |
jonen |
1.2 |
$input = html_input( "checkbox", $name, $value, $attributes ); |
307 |
jonen |
1.1 |
|
308 |
jonen |
1.2 |
if ($render_flag) { |
309 |
|
|
return $input->render(); |
310 |
|
|
} |
311 |
|
|
else { |
312 |
|
|
return $input; |
313 |
|
|
} |
314 |
jonen |
1.1 |
} |
315 |
|
|
|
316 |
|
|
/** |
317 |
|
|
* build/render an input tag of type CHECKBOX |
318 |
|
|
* with onClick javaScript selection |
319 |
|
|
* |
320 |
|
|
* @param string $name - the name of the input tag. |
321 |
|
|
* @param string $value - the value of the tag |
322 |
|
|
* @param string $text - text label for the checkbox |
323 |
jonen |
1.2 |
* @param string class - CSS class of the text |
324 |
jonen |
1.1 |
* @return object returns a container object |
325 |
|
|
* @author Suren Markossian |
326 |
|
|
*/ |
327 |
jonen |
1.2 |
function form_active_checkbox($name, $text=NULL, $value=NULL, $class = NULL, $onClick = NULL, $disabled=FALSE) { |
328 |
jonen |
1.1 |
|
329 |
|
|
$container = container(); |
330 |
|
|
|
331 |
|
|
if ($value) { |
332 |
jonen |
1.2 |
$attrib = array("style"=>"vertical-align: middle;","checked"); |
333 |
|
|
} |
334 |
|
|
else { |
335 |
|
|
$attrib = array("style"=>"vertical-align: middle;"); |
336 |
|
|
} |
337 |
|
|
$check = form_checkbox($name, 1, $attrib); |
338 |
jonen |
1.1 |
|
339 |
|
|
if ($onClick) { |
340 |
jonen |
1.2 |
$check->set_tag_attribute("onClick",$onClick); |
341 |
|
|
} |
342 |
jonen |
1.1 |
if ($disabled) { |
343 |
jonen |
1.2 |
$check->set_tag_attribute("disabled"); |
344 |
|
|
$container->add( $check, $text ); |
345 |
|
|
|
346 |
|
|
} |
347 |
|
|
else { |
348 |
|
|
$container->add($check); |
349 |
|
|
|
350 |
|
|
$js_action = NULL; |
351 |
|
|
if (!$disabled) { |
352 |
|
|
$js_action = "javascript:" . $name . ".checked=!" . $name . ".checked;"; |
353 |
|
|
} |
354 |
|
|
|
355 |
|
|
if ($onClick) { |
356 |
|
|
$js_action .= $onClick; |
357 |
|
|
} |
358 |
|
|
|
359 |
jonen |
1.3 |
$attributes = array("href"=>"javascript:void(0)", |
360 |
jonen |
1.2 |
"onClick"=>$js_action |
361 |
|
|
); |
362 |
jonen |
1.1 |
|
363 |
jonen |
1.2 |
if ($class != NULL) |
364 |
|
|
$attributes["class"] = $class; |
365 |
jonen |
1.1 |
|
366 |
|
|
|
367 |
jonen |
1.2 |
$link = new Atag($attributes, $text, FALSE); |
368 |
|
|
$container->add($link); |
369 |
|
|
} |
370 |
jonen |
1.1 |
|
371 |
|
|
|
372 |
|
|
|
373 |
|
|
return $container; |
374 |
|
|
} |
375 |
|
|
|
376 |
|
|
|
377 |
|
|
/** |
378 |
|
|
* build/render an html tag of file |
379 |
|
|
* |
380 |
|
|
* @param string $name - the name of the input tag. |
381 |
|
|
* @param string $value - the value of the tag |
382 |
|
|
* @param array $attributes - any extra name='value' pair attributes |
383 |
|
|
* @param boolean $render_flag - render an object (FALSE) or raw html (TRUE); |
384 |
|
|
* @return mixed either returns an input object (default) or raw html. |
385 |
|
|
*/ |
386 |
|
|
function form_file($name, $value=NULL, $attributes=array(), $render_flag=FALSE ) { |
387 |
|
|
|
388 |
jonen |
1.2 |
$input = html_input( "file", $name, $value, $attributes ); |
389 |
jonen |
1.1 |
|
390 |
jonen |
1.2 |
if ($render_flag) { |
391 |
|
|
return $input->render(); |
392 |
|
|
} |
393 |
|
|
else { |
394 |
|
|
return $input; |
395 |
|
|
} |
396 |
jonen |
1.1 |
} |
397 |
|
|
|
398 |
|
|
|
399 |
|
|
//odd html tags here. |
400 |
|
|
//tags that are normally only used inside a form, |
401 |
|
|
//so they live in the form_utils.php |
402 |
|
|
|
403 |
|
|
/** |
404 |
|
|
* build a textarea tag with name and attributes. |
405 |
|
|
* |
406 |
|
|
* @author Walt A. Boring |
407 |
|
|
* @param string $name - the name of the textarea tag. |
408 |
|
|
* @param string $value - data to display in the textarea. |
409 |
|
|
* @param array $attributes - any extra name='value' pair attributes |
410 |
|
|
* @param boolean $render_flag - render an object (FALSE) or raw html (TRUE); |
411 |
|
|
* @return TEXTAREAtag object. |
412 |
|
|
*/ |
413 |
|
|
function form_textarea($name, $value=NULL, $attributes=array(), $render_flag=FALSE) { |
414 |
|
|
|
415 |
jonen |
1.2 |
$attrib = array("name" => $name ); |
416 |
|
|
$attributes = array_merge( $attrib, $attributes ); |
417 |
jonen |
1.1 |
|
418 |
jonen |
1.2 |
$textarea = new TEXTAREAtag( $attributes ); |
419 |
jonen |
1.1 |
|
420 |
jonen |
1.2 |
$textarea->newline_after_opentag = FALSE; |
421 |
|
|
$textarea->push( $value ); |
422 |
jonen |
1.1 |
|
423 |
jonen |
1.2 |
return $textarea; |
424 |
jonen |
1.1 |
} |
425 |
|
|
|
426 |
|
|
/** |
427 |
|
|
* Build a select tag with all of its option tags |
428 |
|
|
* |
429 |
|
|
* @author Walt A. Boring IV |
430 |
|
|
* @param string $name - name of the select. |
431 |
|
|
* @param array $options - an array of name value pairs for |
432 |
|
|
* the options. the format is |
433 |
|
|
* array( "LABEL" => VALUE ); |
434 |
|
|
* each <option value="VALUE"> LABEL </option> |
435 |
|
|
* ie |
436 |
|
|
* array( "test" => "foo") would give an option |
437 |
|
|
* of <option value="foo"> test </option> |
438 |
|
|
* @param mixed $selected - This can be either a string or an array. |
439 |
|
|
* If its a string then, it will be the selected |
440 |
jonen |
1.2 |
* option value |
441 |
jonen |
1.1 |
* <option value="foo" SELECTED>foo</option> |
442 |
|
|
* If it is an array, then all of the option |
443 |
|
|
* values will be marked as SELECTED. This only |
444 |
|
|
* makes sense to do if the multiple_flag is true. |
445 |
|
|
* @param boolean $multiple_flag - is this a multiple selection select box? |
446 |
jonen |
1.2 |
* @param array $attribs - additionnal attributes to the select tag |
447 |
jonen |
1.1 |
* @return a SELECTtag object. |
448 |
|
|
*/ |
449 |
jonen |
1.2 |
function form_select($name, $options=array(), $selected="", $multiple_flag=FALSE, $attribs=false) { |
450 |
|
|
|
451 |
|
|
if (!$attribs) |
452 |
|
|
$attribs = array(); |
453 |
|
|
$attribs["name"] = $name; |
454 |
|
|
$select = new SELECTtag( $attribs ); |
455 |
|
|
if ($multiple_flag) { |
456 |
|
|
$select->set_tag_attribute("MULTIPLE"); |
457 |
|
|
} |
458 |
jonen |
1.1 |
|
459 |
jonen |
1.2 |
while (list($label, $value) = each($options)) { |
460 |
|
|
$selected_value = ""; |
461 |
|
|
|
462 |
|
|
if (is_array($selected)) { |
463 |
|
|
//looks like this is a multiple select box |
464 |
|
|
//lets see if the value is in the array |
465 |
|
|
if (in_array($value, $selected)) { |
466 |
|
|
$selected_value = "SELECTED"; |
467 |
|
|
} |
468 |
|
|
} |
469 |
|
|
else { |
470 |
|
|
//must be a string |
471 |
|
|
if ($value == $selected) { |
472 |
|
|
$selected_value = "SELECTED"; |
473 |
|
|
} |
474 |
|
|
} |
475 |
|
|
|
476 |
|
|
|
477 |
|
|
$attributes = array( "value" => $value, $selected_value ); |
478 |
|
|
$option = new OPTIONtag( $attributes ); |
479 |
|
|
$option->push( $label ); |
480 |
|
|
$select->push( $option ); |
481 |
|
|
} |
482 |
|
|
return $select; |
483 |
jonen |
1.1 |
} |
484 |
|
|
|
485 |
|
|
?> |