4 |
* This file contains various helper functions |
* This file contains various helper functions |
5 |
* for building forms and form elements. |
* for building forms and form elements. |
6 |
* |
* |
7 |
* $Id$ |
* $Id$ |
8 |
* |
* |
9 |
* @author Walter A. Boring IV <waboring@buildabetterweb.com> |
* @author Walter A. Boring IV <waboring@buildabetterweb.com> |
10 |
* @package phpHtmlLib |
* @package phpHtmlLib |
11 |
* |
* |
12 |
* |
* |
13 |
*/ |
*/ |
14 |
|
|
15 |
|
|
16 |
/** |
/** |
27 |
*/ |
*/ |
28 |
function html_form( $name, $action, $method="GET", $attributes=array()) { |
function html_form( $name, $action, $method="GET", $attributes=array()) { |
29 |
|
|
30 |
$attribs = array("name" => $name, "action" => $action, |
$attribs = array("name" => $name, "action" => $action, |
31 |
"method" => $method); |
"method" => $method); |
32 |
$attributes = array_merge( $attribs, $attributes); |
$attributes = array_merge( $attribs, $attributes); |
33 |
|
|
34 |
return new FORMtag( $attributes ); |
return new FORMtag( $attributes ); |
35 |
} |
} |
36 |
|
|
37 |
/** |
/** |
47 |
* @return string returns the raw form tag. |
* @return string returns the raw form tag. |
48 |
*/ |
*/ |
49 |
function form_open( $name, $action, $method="GET", $attributes=array(), $indent_level=0 ) { |
function form_open( $name, $action, $method="GET", $attributes=array(), $indent_level=0 ) { |
50 |
$form = html_form( $name, $action, $method, $attributes); |
$form = html_form( $name, $action, $method, $attributes); |
51 |
return $form->_render_tag( $indent_level ); |
return $form->_render_tag( $indent_level ); |
52 |
} |
} |
53 |
|
|
54 |
|
|
58 |
* @return string - the </form> tag. |
* @return string - the </form> tag. |
59 |
*/ |
*/ |
60 |
function form_close( $indent_level=0 ) { |
function form_close( $indent_level=0 ) { |
61 |
$form = new FORMtag; |
$form = new FORMtag; |
62 |
|
|
63 |
//just render the close tag. |
//just render the close tag. |
64 |
return $form->_render_close_tag( $indent_level ); |
return $form->_render_close_tag( $indent_level ); |
65 |
} |
} |
66 |
|
|
67 |
|
|
78 |
* @return mixed either returns an input object (default) or raw html. |
* @return mixed either returns an input object (default) or raw html. |
79 |
*/ |
*/ |
80 |
function form_text($name, $value=NULL, $size=NULL, $maxlength=NULL, |
function form_text($name, $value=NULL, $size=NULL, $maxlength=NULL, |
81 |
$attributes = array(), $render_flag=FALSE) { |
$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 |
if ( $size != NULL ) { |
$input = html_input("text", $name, $value, $attributes); |
91 |
$attributes["size"] = $size; |
|
92 |
} |
if ($render_flag) { |
93 |
if ( $maxlength != NULL ) { |
return $input->render(); |
94 |
$attributes["maxlength"] = $maxlength; |
} |
95 |
} |
else { |
96 |
|
//user wants the object |
97 |
$input = html_input("text", $name, $value, $attributes); |
return $input; |
98 |
|
} |
|
if ( $render_flag ) { |
|
|
return $input->render(); |
|
|
} else { |
|
|
//user wants the object |
|
|
return $input; |
|
|
} |
|
99 |
} |
} |
100 |
|
|
101 |
|
|
111 |
* @return mixed either returns an input object (default) or raw html. |
* @return mixed either returns an input object (default) or raw html. |
112 |
*/ |
*/ |
113 |
function form_password($name, $value=NULL, $size=NULL, $maxlength=NULL, |
function form_password($name, $value=NULL, $size=NULL, $maxlength=NULL, |
114 |
$attributes = array(), $render_flag=FALSE) { |
$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 |
if ( $size != NULL ) { |
$input = html_input( "password", $name, $value, $attributes ); |
124 |
$attributes["size"] = $size; |
|
125 |
} |
if ($render_flag) { |
126 |
if ( $maxlength != NULL ) { |
return $input->render(); |
127 |
$attributes["maxlength"] = $maxlength; |
} |
128 |
} |
else { |
129 |
|
//user wants the object |
130 |
$input = html_input( "password", $name, $value, $attributes ); |
return $input; |
131 |
|
} |
|
if ( $render_flag ) { |
|
|
return $input->render(); |
|
|
} else { |
|
|
//user wants the object |
|
|
return $input; |
|
|
} |
|
132 |
} |
} |
133 |
|
|
134 |
/** |
/** |
142 |
*/ |
*/ |
143 |
function form_button($name, $value=NULL, $attributes=array(), $render_flag=FALSE ) { |
function form_button($name, $value=NULL, $attributes=array(), $render_flag=FALSE ) { |
144 |
|
|
145 |
$input = html_input( "button", $name, $value, $attributes ); |
$input = html_input( "button", $name, $value, $attributes ); |
146 |
|
|
147 |
if ( $render_flag ) { |
if ($render_flag) { |
148 |
return $input->render(); |
return $input->render(); |
149 |
} else { |
} |
150 |
return $input; |
else { |
151 |
} |
return $input; |
152 |
|
} |
153 |
|
|
154 |
} |
} |
155 |
|
|
164 |
*/ |
*/ |
165 |
function form_submit($name, $value=NULL, $attributes=array(), $render_flag=FALSE ) { |
function form_submit($name, $value=NULL, $attributes=array(), $render_flag=FALSE ) { |
166 |
|
|
167 |
$input = html_input( "submit", $name, $value, $attributes ); |
$input = html_input( "submit", $name, $value, $attributes ); |
168 |
|
|
169 |
if ( $render_flag ) { |
if ($render_flag) { |
170 |
return $input->render(); |
return $input->render(); |
171 |
} else { |
} |
172 |
return $input; |
else { |
173 |
} |
return $input; |
174 |
|
} |
175 |
} |
} |
176 |
|
|
177 |
/** |
/** |
186 |
*/ |
*/ |
187 |
function form_image($name, $value=NULL, $src, $attributes=array(), $render_flag=FALSE ) { |
function form_image($name, $value=NULL, $src, $attributes=array(), $render_flag=FALSE ) { |
188 |
|
|
189 |
$attributes["src"] = $src; |
$attributes["src"] = $src; |
190 |
$input = html_input( "image", $name, $value, $attributes ); |
$input = html_input( "image", $name, $value, $attributes ); |
191 |
|
|
192 |
if ( $render_flag ) { |
if ($render_flag) { |
193 |
return $input->render(); |
return $input->render(); |
194 |
} else { |
} |
195 |
return $input; |
else { |
196 |
} |
return $input; |
197 |
|
} |
198 |
} |
} |
199 |
|
|
200 |
|
|
209 |
*/ |
*/ |
210 |
function form_radio($name, $value=NULL, $attributes=array(), $render_flag=FALSE ) { |
function form_radio($name, $value=NULL, $attributes=array(), $render_flag=FALSE ) { |
211 |
|
|
212 |
$input = html_input( "radio", $name, $value, $attributes ); |
$input = html_input( "radio", $name, $value, $attributes ); |
213 |
|
|
214 |
if ( $render_flag ) { |
if ($render_flag) { |
215 |
return $input->render(); |
return $input->render(); |
216 |
} else { |
} |
217 |
return $input; |
else { |
218 |
} |
return $input; |
219 |
|
} |
220 |
} |
} |
221 |
|
|
222 |
/** |
/** |
231 |
* @return object returns a container object |
* @return object returns a container object |
232 |
* @author Suren Markossian |
* @author Suren Markossian |
233 |
*/ |
*/ |
234 |
function form_active_radio($formname, $name, $list, $value, $line_break = TRUE, $attribs = FALSE) { |
function form_active_radio( $name, $list, $value, $line_break = TRUE, $attribs = FALSE) { |
235 |
|
|
236 |
$container = new ContainerWidget(); |
$container = new ContainerWidget(); |
237 |
$i=0; |
$i=0; |
238 |
while (list($text,$val) = each($list)) { |
while (list($text,$val) = each($list)) { |
239 |
if ($attribs[$val]) { |
if ($attribs[$val]) { |
240 |
$attr = $attribs[$val]; |
$attr = $attribs[$val]; |
241 |
} else { |
} |
242 |
$attr = array(); |
else { |
243 |
} |
$attr = array(); |
244 |
|
} |
245 |
|
|
246 |
if ($val == $value) $attr[] = "checked"; |
if ($val == $value) $attr[] = "checked"; |
|
|
|
247 |
$container->push(form_radio($name, $val, $attr)); |
$container->push(form_radio($name, $val, $attr)); |
248 |
|
|
249 |
if (count($list)>1) { |
if (count($list)>1) { |
250 |
if ($attribs[$val]) { |
if ($attribs[$val]) { |
251 |
$attr = $attribs[$val]; |
$attr = $attribs[$val]; |
252 |
} else { |
} |
253 |
$attr = array(); |
else { |
254 |
} |
$attr = array(); |
255 |
|
} |
256 |
|
|
257 |
$attr["class"] ="form_link"; |
$attr["class"] ="form_link"; |
258 |
$js = "javascript:if (!document.".$formname.".elements['".$name."'][".$i."]) "; |
$attr["href"] = "javascript:void(0)"; |
259 |
$js .= "document.".$formname.".elements['".$name."'].checked = true; "; |
$js = "javascript: function check(item){item.click();} check(".$name.".item($i));"; |
260 |
$js .= "else if (!document.".$formname.".elements['".$name."'][".$i."].checked) "; |
$attr["onclick"] = $js; |
|
$js .= "document.".$formname.".elements['".$name."'][".$i."].checked=true;"; |
|
|
$attr["href"] = "javascript:"; |
|
|
$attr["onclick"] = $js; |
|
261 |
|
|
262 |
$link = new Atag($attr, $text, FALSE); |
$link = new Atag($attr, $text, FALSE); |
263 |
} |
} |
282 |
*/ |
*/ |
283 |
function form_hidden($name, $value=NULL, $attributes=array(), $render_flag=FALSE ) { |
function form_hidden($name, $value=NULL, $attributes=array(), $render_flag=FALSE ) { |
284 |
|
|
285 |
$input = html_input( "hidden", $name, $value, $attributes ); |
$input = html_input( "hidden", $name, $value, $attributes ); |
286 |
|
|
287 |
if ( $render_flag ) { |
if ($render_flag) { |
288 |
return $input->render(); |
return $input->render(); |
289 |
} else { |
} |
290 |
return $input; |
else { |
291 |
} |
return $input; |
292 |
|
} |
293 |
} |
} |
294 |
|
|
295 |
/** |
/** |
303 |
*/ |
*/ |
304 |
function form_checkbox($name, $value=NULL, $attributes=array(), $render_flag=FALSE ) { |
function form_checkbox($name, $value=NULL, $attributes=array(), $render_flag=FALSE ) { |
305 |
|
|
306 |
$input = html_input( "checkbox", $name, $value, $attributes ); |
$input = html_input( "checkbox", $name, $value, $attributes ); |
307 |
|
|
308 |
if ( $render_flag ) { |
if ($render_flag) { |
309 |
return $input->render(); |
return $input->render(); |
310 |
} else { |
} |
311 |
return $input; |
else { |
312 |
} |
return $input; |
313 |
|
} |
314 |
} |
} |
315 |
|
|
316 |
/** |
/** |
317 |
* build/render an input tag of type CHECKBOX |
* build/render an input tag of type CHECKBOX |
318 |
* with onClick javaScript selection |
* with onClick javaScript selection |
319 |
* |
* |
|
* @param string $formname - the name of the form that |
|
|
* the checkbox lives |
|
320 |
* @param string $name - the name of the input tag. |
* @param string $name - the name of the input tag. |
321 |
* @param string $value - the value of the tag |
* @param string $value - the value of the tag |
322 |
* @param string $text - text label for the checkbox |
* @param string $text - text label for the checkbox |
323 |
|
* @param string class - CSS class of the text |
324 |
* @return object returns a container object |
* @return object returns a container object |
325 |
* @author Suren Markossian |
* @author Suren Markossian |
326 |
*/ |
*/ |
327 |
function form_active_checkbox($formname, $name, $text=NULL, $value=NULL, $onClick = NULL, $disabled=FALSE) { |
function form_active_checkbox($name, $text=NULL, $value=NULL, $class = NULL, $onClick = NULL, $disabled=FALSE) { |
328 |
|
|
329 |
$container = container(); |
$container = container(); |
330 |
|
|
331 |
if ($value) { |
if ($value) { |
332 |
$attrib = array("style"=>"vertical-align: middle;","checked"); |
$attrib = array("style"=>"vertical-align: middle;","checked"); |
333 |
} else { |
} |
334 |
$attrib = array("style"=>"vertical-align: middle;"); |
else { |
335 |
} |
$attrib = array("style"=>"vertical-align: middle;"); |
336 |
$check = form_checkbox($name, 1, $attrib); |
} |
337 |
|
$check = form_checkbox($name, 1, $attrib); |
338 |
|
|
339 |
if ($onClick) { |
if ($onClick) { |
340 |
$check->set_tag_attribute("onClick",$onClick); |
$check->set_tag_attribute("onClick",$onClick); |
341 |
} |
} |
342 |
if ($disabled) { |
if ($disabled) { |
343 |
$check->set_tag_attribute("disabled"); |
$check->set_tag_attribute("disabled"); |
344 |
} |
$container->add( $check, $text ); |
345 |
|
|
346 |
$container->push($check); |
} |
347 |
|
else { |
348 |
|
$container->add($check); |
349 |
|
|
350 |
$action = "javascript:document.".$formname.".elements['".$name."'].checked = !document.".$formname.".elements['".$name."'].checked;"; |
$js_action = NULL; |
351 |
if ($onClick) { |
if (!$disabled) { |
352 |
$action .= $onClick; |
$js_action = "javascript:" . $name . ".checked=!" . $name . ".checked;"; |
353 |
} |
} |
354 |
|
|
355 |
$attributes = array("href"=>"javascript:", |
if ($onClick) { |
356 |
"onMouseOver"=>"status='" . $text . "';return true;", |
$js_action .= $onClick; |
357 |
"onMouseOut"=>"status='';return true;", |
} |
358 |
"onClick"=>$action, |
|
359 |
"style" => "text-decoration:none;color: #000000;" |
$attributes = array("href"=>"javascript:void(0)", |
360 |
); |
"onClick"=>$js_action |
361 |
|
); |
362 |
|
|
363 |
|
if ($class != NULL) |
364 |
|
$attributes["class"] = $class; |
365 |
|
|
366 |
|
|
367 |
|
$link = new Atag($attributes, $text, FALSE); |
368 |
|
$container->add($link); |
369 |
|
} |
370 |
|
|
|
$link = new Atag($attributes, $text, FALSE); |
|
371 |
|
|
|
$container->push($link); |
|
372 |
|
|
373 |
return $container; |
return $container; |
374 |
} |
} |
385 |
*/ |
*/ |
386 |
function form_file($name, $value=NULL, $attributes=array(), $render_flag=FALSE ) { |
function form_file($name, $value=NULL, $attributes=array(), $render_flag=FALSE ) { |
387 |
|
|
388 |
$input = html_input( "file", $name, $value, $attributes ); |
$input = html_input( "file", $name, $value, $attributes ); |
389 |
|
|
390 |
if ( $render_flag ) { |
if ($render_flag) { |
391 |
return $input->render(); |
return $input->render(); |
392 |
} else { |
} |
393 |
return $input; |
else { |
394 |
} |
return $input; |
395 |
|
} |
396 |
} |
} |
397 |
|
|
398 |
|
|
412 |
*/ |
*/ |
413 |
function form_textarea($name, $value=NULL, $attributes=array(), $render_flag=FALSE) { |
function form_textarea($name, $value=NULL, $attributes=array(), $render_flag=FALSE) { |
414 |
|
|
415 |
$attrib = array("name" => $name ); |
$attrib = array("name" => $name ); |
416 |
$attributes = array_merge( $attrib, $attributes ); |
$attributes = array_merge( $attrib, $attributes ); |
417 |
|
|
418 |
$textarea = new TEXTAREAtag( $attributes ); |
$textarea = new TEXTAREAtag( $attributes ); |
419 |
|
|
420 |
$textarea->newline_after_opentag = FALSE; |
$textarea->newline_after_opentag = FALSE; |
421 |
$textarea->push( $value ); |
$textarea->push( $value ); |
422 |
|
|
423 |
return $textarea; |
return $textarea; |
424 |
} |
} |
425 |
|
|
426 |
/** |
/** |
435 |
* ie |
* ie |
436 |
* array( "test" => "foo") would give an option |
* array( "test" => "foo") would give an option |
437 |
* of <option value="foo"> test </option> |
* of <option value="foo"> test </option> |
438 |
|
* |
439 |
|
* NOTE: this also supports automatic building of |
440 |
|
* the optgroup. Just pass in an array of |
441 |
|
* array("foogroup" => array("name" => "value1", |
442 |
|
* "name2" => "value2"), |
443 |
|
* "bargroup" => array("blah" => "foo")); |
444 |
|
* |
445 |
|
* |
446 |
* @param mixed $selected - This can be either a string or an array. |
* @param mixed $selected - This can be either a string or an array. |
447 |
* If its a string then, it will be the selected |
* If its a string then, it will be the selected |
448 |
* option value |
* option value |
449 |
* <option value="foo" SELECTED>foo</option> |
* <option value="foo" SELECTED>foo</option> |
450 |
* If it is an array, then all of the option |
* If it is an array, then all of the option |
451 |
* values will be marked as SELECTED. This only |
* values will be marked as SELECTED. This only |
452 |
* makes sense to do if the multiple_flag is true. |
* makes sense to do if the multiple_flag is true. |
453 |
* @param boolean $multiple_flag - is this a multiple selection select box? |
* @param boolean $multiple_flag - is this a multiple selection select box? |
454 |
|
* @param array $attribs - additionnal attributes to the select tag |
455 |
* @return a SELECTtag object. |
* @return a SELECTtag object. |
456 |
*/ |
*/ |
457 |
function form_select($name, $options=array(), $selected="", $multiple_flag=FALSE) { |
function form_select($name, $options=array(), $selected="", $multiple_flag=FALSE, $attribs=false) { |
458 |
|
|
459 |
|
if (!$attribs) |
460 |
|
$attribs = array(); |
461 |
|
$attribs["name"] = $name; |
462 |
|
$select = new SELECTtag( $attribs ); |
463 |
|
if ($multiple_flag) { |
464 |
|
$select->set_tag_attribute("MULTIPLE"); |
465 |
|
} |
466 |
|
|
467 |
|
while (list($label, $value) = each($options)) { |
468 |
|
//see if they wanted an option group |
469 |
|
if (is_array($value)) { |
470 |
|
$option = html_optgroup($label); |
471 |
|
foreach( $value as $optname => $optvalue ) { |
472 |
|
$selected_value = form_select_is_selected($value, $selected); |
473 |
|
$option->add(html_option($optvalue, $optname, $selected_value)); |
474 |
|
} |
475 |
|
} else { |
476 |
|
$selected_value = form_select_is_selected($value, $selected); |
477 |
|
$option = html_option($value, $label, $selected_value); |
478 |
|
} |
479 |
|
|
480 |
|
$select->push( $option ); |
481 |
|
} |
482 |
|
return $select; |
483 |
|
} |
484 |
|
|
485 |
|
|
486 |
$select = new SELECTtag( array("name" => $name) ); |
/** |
487 |
if ( $multiple_flag ) { |
* This function is used by form_select |
488 |
$select->set_tag_attribute("MULTIPLE"); |
* to determin if a value is selected or not. |
489 |
} |
* |
490 |
|
* @param string value |
491 |
while ( list($label, $value) = each($options) ) { |
* @param string selected value |
492 |
$selected_value = ""; |
* @return voolean |
493 |
|
*/ |
494 |
if ( is_array($selected) ) { |
function form_select_is_selected($value, $selected) { |
495 |
//looks like this is a multiple select box |
$selected_value = false; |
496 |
//lets see if the value is in the array |
|
497 |
if ( in_array($value, $selected) ) { |
if (is_array($selected)) { |
498 |
$selected_value = "SELECTED"; |
//looks like this is a multiple select box |
499 |
} |
//lets see if the value is in the array |
500 |
} else { |
if (in_array($value, $selected)) { |
501 |
//must be a string |
$selected_value = TRUE; |
502 |
if ( $value == $selected ) { |
} |
503 |
$selected_value = "SELECTED"; |
} else { |
504 |
} |
//must be a string |
505 |
} |
if ($value == $selected) { |
506 |
|
$selected_value = TRUE; |
507 |
|
} |
508 |
$attributes = array( "value" => $value, $selected_value ); |
} |
509 |
$option = new OPTIONtag( $attributes ); |
return $selected_value; |
|
$option->push( $label ); |
|
|
$select->push( $option ); |
|
|
} |
|
|
return $select; |
|
510 |
} |
} |
511 |
|
|
512 |
?> |
?> |