/[cvs]/nfo/php/libs/com.newsblob.phphtmllib/tag_utils/form_utils.inc
ViewVC logotype

Contents of /nfo/php/libs/com.newsblob.phphtmllib/tag_utils/form_utils.inc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (show annotations)
Thu May 6 16:27:36 2004 UTC (20 years, 2 months ago) by jonen
Branch: MAIN
CVS Tags: HEAD
Changes since 1.3: +45 -18 lines
 updated all to v2.4.1 - Apr 01, 2004

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.15 2003/09/16 21:50:55 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 }
95 else {
96 //user wants the object
97 return $input;
98 }
99 }
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 $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
125 if ($render_flag) {
126 return $input->render();
127 }
128 else {
129 //user wants the object
130 return $input;
131 }
132 }
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 $input = html_input( "button", $name, $value, $attributes );
146
147 if ($render_flag) {
148 return $input->render();
149 }
150 else {
151 return $input;
152 }
153
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 $input = html_input( "submit", $name, $value, $attributes );
168
169 if ($render_flag) {
170 return $input->render();
171 }
172 else {
173 return $input;
174 }
175 }
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 $attributes["src"] = $src;
190 $input = html_input( "image", $name, $value, $attributes );
191
192 if ($render_flag) {
193 return $input->render();
194 }
195 else {
196 return $input;
197 }
198 }
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 $input = html_input( "radio", $name, $value, $attributes );
213
214 if ($render_flag) {
215 return $input->render();
216 }
217 else {
218 return $input;
219 }
220 }
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 function form_active_radio( $name, $list, $value, $line_break = TRUE, $attribs = FALSE) {
235
236 $container = new ContainerWidget();
237 $i=0;
238 while (list($text,$val) = each($list)) {
239 if ($attribs[$val]) {
240 $attr = $attribs[$val];
241 }
242 else {
243 $attr = array();
244 }
245
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 $attr = $attribs[$val];
252 }
253 else {
254 $attr = array();
255 }
256
257 $attr["class"] ="form_link";
258 $attr["href"] = "javascript:void(0)";
259 $js = "javascript: function check(item){item.click();} check(".$name.".item($i));";
260 $attr["onclick"] = $js;
261
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 $input = html_input( "hidden", $name, $value, $attributes );
286
287 if ($render_flag) {
288 return $input->render();
289 }
290 else {
291 return $input;
292 }
293 }
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 $input = html_input( "checkbox", $name, $value, $attributes );
307
308 if ($render_flag) {
309 return $input->render();
310 }
311 else {
312 return $input;
313 }
314 }
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 * @param string class - CSS class of the text
324 * @return object returns a container object
325 * @author Suren Markossian
326 */
327 function form_active_checkbox($name, $text=NULL, $value=NULL, $class = NULL, $onClick = NULL, $disabled=FALSE) {
328
329 $container = container();
330
331 if ($value) {
332 $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
339 if ($onClick) {
340 $check->set_tag_attribute("onClick",$onClick);
341 }
342 if ($disabled) {
343 $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 $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
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 $input = html_input( "file", $name, $value, $attributes );
389
390 if ($render_flag) {
391 return $input->render();
392 }
393 else {
394 return $input;
395 }
396 }
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 $attrib = array("name" => $name );
416 $attributes = array_merge( $attrib, $attributes );
417
418 $textarea = new TEXTAREAtag( $attributes );
419
420 $textarea->newline_after_opentag = FALSE;
421 $textarea->push( $value );
422
423 return $textarea;
424 }
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 *
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.
447 * If its a string then, it will be the selected
448 * option value
449 * <option value="foo" SELECTED>foo</option>
450 * If it is an array, then all of the option
451 * values will be marked as SELECTED. This only
452 * makes sense to do if the multiple_flag is true.
453 * @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.
456 */
457 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 /**
487 * This function is used by form_select
488 * to determin if a value is selected or not.
489 *
490 * @param string value
491 * @param string selected value
492 * @return voolean
493 */
494 function form_select_is_selected($value, $selected) {
495 $selected_value = false;
496
497 if (is_array($selected)) {
498 //looks like this is a multiple select box
499 //lets see if the value is in the array
500 if (in_array($value, $selected)) {
501 $selected_value = TRUE;
502 }
503 } else {
504 //must be a string
505 if ($value == $selected) {
506 $selected_value = TRUE;
507 }
508 }
509 return $selected_value;
510 }
511
512 ?>

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed