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