Commit | Line | Data |
---|---|---|
bc13d5d6 H |
1 | <?php |
2 | // | |
3 | // +----------------------------------------------------------------------+ | |
4 | // | PHP version 4.0 | | |
5 | // +----------------------------------------------------------------------+ | |
6 | // | Copyright (c) 1997, 1998, 1999, 2000 The PHP Group | | |
7 | // +----------------------------------------------------------------------+ | |
8 | // | This source file is subject to version 2.0 of the PHP license, | | |
9 | // | that is bundled with this package in the file LICENSE, and is | | |
10 | // | available at through the world-wide-web at | | |
11 | // | http://www.php.net/license/2_02.txt. | | |
12 | // | If you did not receive a copy of the PHP license and are unable to | | |
13 | // | obtain it through the world-wide-web, please send a note to | | |
14 | // | license@php.net so we can mail you a copy immediately. | | |
15 | // +----------------------------------------------------------------------+ | |
16 | // | Authors: Stig Bakken <ssb@fast.no> | | |
17 | // | Urs Gehrig <urs@circle.ch> | | |
18 | // +----------------------------------------------------------------------+ | |
19 | // | |
20 | // $Id: form.inc,v 1.1.1.1 2003/04/18 11:10:05 kybu Exp $ | |
21 | // | |
22 | // HTML form utility functions. | |
23 | // | |
24 | ||
25 | if (!defined('HTML_FORM_TEXT_SIZE')) { | |
26 | define('HTML_FORM_TEXT_SIZE', 20); | |
27 | } | |
28 | ||
29 | class HTML_Form { | |
30 | // {{{ properties | |
31 | ||
32 | /** ACTION attribute of <FORM> tag */ | |
33 | var $action; | |
34 | ||
35 | /** METHOD attribute of <FORM> tag */ | |
36 | var $method; | |
37 | ||
38 | /** NAME attribute of <FORM> tag */ | |
39 | var $name; | |
40 | ||
41 | /** an array of entries for this form */ | |
42 | var $fields; | |
43 | ||
44 | /** DB_storage object, if tied to one */ | |
45 | var $storageObject; | |
46 | var $onSubmit; | |
47 | /** Encryption type */ | |
48 | var $enctype; | |
49 | // }}} | |
50 | ||
51 | // {{{ constructor | |
52 | ||
53 | function HTML_Form($action, $method = 'GET', $name = '', $enctype = '',$onsubmit=false) { | |
54 | $this->enctype = $enctype; | |
55 | $this->action = $action; | |
56 | $this->method = $method; | |
57 | $this->name = $name; | |
58 | $this->fields = array(); | |
59 | $this->onSubmit = $onsubmit; | |
60 | } | |
61 | ||
62 | // }}} | |
63 | ||
64 | // {{{ addText() | |
65 | ||
66 | function addText($name, $title, $default, $size = HTML_FORM_TEXT_SIZE) { | |
67 | $this->fields[] = array("text", $name, $title, $default, $size); | |
68 | } | |
69 | ||
70 | // }}} | |
71 | // {{{ addPassword() | |
72 | ||
73 | function addFile($name,$title,$accept="image/*,text/plain") { | |
74 | $this->fields[]= array("file",$name,$title,$accept); | |
75 | } | |
76 | ||
77 | function addPassword($name, $title, $default, $size = HTML_FORM_PASSWD_SIZE) { | |
78 | $this->fields[] = array("password", $name, $title, $default, $size); | |
79 | } | |
80 | ||
81 | // }}} | |
82 | // {{{ addCheckbox() | |
83 | ||
84 | function addCheckbox($name, $title, $default) { | |
85 | $this->fields[] = array("checkbox", $name, $title, $default); | |
86 | } | |
87 | ||
88 | // }}} | |
89 | // {{{ addTextarea() | |
90 | ||
91 | function addTextarea($name, $title, $default, | |
92 | $width = HTML_FORM_TEXTAREA_WT, | |
93 | $height = HTML_FORM_TEXTAREA_HT) { | |
94 | $this->fields[] = array("textarea", $name, $title, &$default, $width, $height); | |
95 | } | |
96 | ||
97 | // }}} | |
98 | // {{{ addSubmit | |
99 | ||
100 | function addSubmit($name = "submit", $title = "Submit Changes") { | |
101 | $this->fields[] = array("submit", $name, $title); | |
102 | } | |
103 | ||
104 | // }}} | |
105 | // {{{ addReset() | |
106 | ||
107 | function addReset($title = "Discard Changes") { | |
108 | $this->fields[] = array("reset", $title); | |
109 | } | |
110 | ||
111 | // }}} | |
112 | // {{{ addSelect() | |
113 | ||
114 | function addSelect($name, $title, $entries, $default = '', $size = 1, | |
115 | $blank = '', $multiple = false) { | |
116 | $this->fields[] = array("select", $name, $title, &$entries, $default, $size, | |
117 | $blank, $multiple); | |
118 | } | |
119 | ||
120 | // }}} | |
121 | // {{{ addRadio() | |
122 | ||
123 | function addRadio($name, $title, $value, $default) { | |
124 | $this->fields[] = array("radio", $name, $title, $value, $default); | |
125 | } | |
126 | ||
127 | // }}} | |
128 | // {{{ addImage() | |
129 | ||
130 | function addImage($name, $src) { | |
131 | $this->fields[] = array("image", $name, $src); | |
132 | } | |
133 | ||
134 | // }}} | |
135 | // {{{ addHidden() | |
136 | ||
137 | function addHidden($name, $value) { | |
138 | $this->fields[] = array("hidden", $name, $value); | |
139 | } | |
140 | ||
141 | // }}} | |
142 | ||
143 | // {{{ start() | |
144 | ||
145 | function start() { | |
146 | print "<FORM ACTION=\"" . basename ($this->action) . "\" METHOD=\"$this->method\""; | |
147 | if ($this->name) { | |
148 | print " NAME=\"$this->name\""; | |
149 | } | |
150 | if ($this->enctype) { | |
151 | print " ENCTYPE=\"$this->enctype\""; | |
152 | } | |
153 | ||
154 | if($this->onSubmit) { | |
155 | print " onSubmit=\"return validate(this)\""; | |
156 | } | |
157 | print ">"; | |
158 | } | |
159 | ||
160 | // }}} | |
161 | // {{{ end() | |
162 | ||
163 | function end() { | |
164 | $fields = array(); | |
165 | reset($this->fields); | |
166 | while (list($i, $data) = each($this->fields)) { | |
167 | if ($data[0] == 'reset') { | |
168 | continue; | |
169 | } | |
170 | $fields[$data[1]] = true; | |
171 | } | |
172 | $this->displayHidden("_fields", implode(":", array_keys($fields))); | |
173 | print "</FORM>"; | |
174 | } | |
175 | ||
176 | // }}} | |
177 | ||
178 | // {{{ displayText() | |
179 | function displayFile($name, $accept) { | |
180 | print "<INPUT type=file NAME=\"$name\" accept=\"$accept\">"; | |
181 | } | |
182 | ||
183 | function displayFileRow($name, $title, $accept) { | |
184 | print " <TR>\n"; | |
185 | print " <TH ALIGN=\"right\">$title</TH>"; | |
186 | print " <TD>"; | |
187 | $this->displayFile($name, $accept); | |
188 | print "</TD>\n"; | |
189 | print " </TR>\n"; | |
190 | } | |
191 | ||
192 | function displayText($name, $default = '', $size = HTML_FORM_TEXT_SIZE) { | |
193 | print "<INPUT NAME=\"$name\" VALUE=\"$default\" SIZE=\"$size\">"; | |
194 | } | |
195 | ||
196 | // }}} | |
197 | // {{{ displayTextRow() | |
198 | ||
199 | function displayTextRow($name, $title, $default = '', $size = HTML_FORM_TEXT_SIZE) { | |
200 | print " <TR>\n"; | |
201 | print " <TH ALIGN=\"right\">$title</TH>"; | |
202 | print " <TD>"; | |
203 | $this->displayText($name, $default, $size); | |
204 | print "</TD>\n"; | |
205 | print " </TR>\n"; | |
206 | } | |
207 | ||
208 | // }}} | |
209 | // {{{ displayPassword() | |
210 | ||
211 | function displayPassword($name, $default = '', $size = HTML_FORM_PASSWD_SIZE) { | |
212 | print "<INPUT NAME=\"$name\" TYPE=\"password\" VALUE=\"$default\" SIZE=\"$size\">"; | |
213 | } | |
214 | ||
215 | // }}} | |
216 | // {{{ displayPasswordRow() | |
217 | ||
218 | function displayPasswordRow($name, $title, $default = '', $size = HTML_FORM_PASSWD_SIZE) { | |
219 | print "<TR>\n"; | |
220 | print " <TH ALIGN=\"right\">$title</TH>\n"; | |
221 | print " <TD>"; | |
222 | $this->displayPassword($name, $default, $size); | |
223 | print " repeat: "; | |
224 | $this->displayPassword($name."2", $default, $size); | |
225 | print "</TD>\n"; | |
226 | print "</TR>\n"; | |
227 | } | |
228 | ||
229 | // }}} | |
230 | // {{{ displayCheckbox() | |
231 | ||
232 | function displayCheckbox($name, $default = false) { | |
233 | print "<INPUT TYPE=\"checkbox\" NAME=\"$name\""; | |
234 | if ($default && $default != 'off') { | |
235 | print " CHECKED"; | |
236 | } | |
237 | print ">"; | |
238 | } | |
239 | ||
240 | // }}} | |
241 | // {{{ displayCheckboxRow() | |
242 | ||
243 | function displayCheckboxRow($name, $title, $default = false) { | |
244 | print " <TR>\n"; | |
245 | print " <TH ALIGN=\"left\">$title</TH>"; | |
246 | print " <TD>"; | |
247 | $this->displayCheckbox($name, $default); | |
248 | print "</TD>\n"; | |
249 | print " </TR>\n"; | |
250 | } | |
251 | ||
252 | // }}} | |
253 | // {{{ displayTextarea() | |
254 | ||
255 | function displayTextarea($name, $default = '', $width = 40, $height = 5) { | |
256 | print "<TEXTAREA NAME=\"$name\" COLS=\"$width\" ROWS=\"$height\">"; | |
257 | print $default; | |
258 | print "</TEXTAREA>"; | |
259 | } | |
260 | ||
261 | // }}} | |
262 | // {{{ displayTextareaRow() | |
263 | ||
264 | function displayTextareaRow($name, $title, $default = '', $width = 40, $height = 5) { | |
265 | print " <TR>\n"; | |
266 | print " <TH ALIGN=\"right\">$title</TH>\n"; | |
267 | print " <TD>"; | |
268 | $this->displayTextarea($name, &$default, $width, $height); | |
269 | print "</TD>\n"; | |
270 | print " </TR>\n"; | |
271 | } | |
272 | ||
273 | // }}} | |
274 | // {{{ displaySubmit() | |
275 | ||
276 | function displaySubmit($title = 'Submit Changes', $name = "submit") { | |
277 | print $this->displaySubmit($title, $name); | |
278 | } | |
279 | ||
280 | // }}} | |
281 | // {{{ displaySubmitRow() | |
282 | ||
283 | function displaySubmitRow($name = "submit", $title = 'Submit Changes') { | |
284 | print $this->returnSubmitRow($name, $title); | |
285 | } | |
286 | ||
287 | // }}} | |
288 | // {{{ displayReset() | |
289 | ||
290 | function displayReset($title = 'Clear contents') { | |
291 | print $this->returnReset($title); | |
292 | } | |
293 | ||
294 | // }}} | |
295 | // {{{ displayResetRow() | |
296 | /* | |
297 | function displayReset($title = 'Clear contents') { | |
298 | print $this->returnReset($title); | |
299 | } | |
300 | */ | |
301 | // }}} | |
302 | // {{{ displaySelect() | |
303 | ||
304 | function displaySelect($name, $entries, $default = '', $size = 1, | |
305 | $blank = '', $multiple = false) { | |
306 | print $this->returnSelect($name, $entries, $default, $size, $blank, | |
307 | $multiple); | |
308 | } | |
309 | ||
310 | // }}} | |
311 | // {{{ displaySelectRow() | |
312 | ||
313 | function displaySelectRow($name, $title, &$entries, $default = '', | |
314 | $size = 1, $blank = '', $multiple = false) | |
315 | { | |
316 | print $this->returnSelectRow($name, $title, $entries, $default, $size, | |
317 | $blank, $multiple); | |
318 | } | |
319 | ||
320 | // }}} | |
321 | // {{{ displayHidden() | |
322 | ||
323 | function displayHidden($name, $value) { | |
324 | print $this->returnHidden($name, $value); | |
325 | } | |
326 | ||
327 | // }}} | |
328 | ||
329 | // XXX missing: displayRadio displayRadioRow | |
330 | ||
331 | // {{{ returnText() | |
332 | ||
333 | function returnText($name, $default = '', $size = HTML_FORM_TEXT_SIZE) { | |
334 | return "<INPUT NAME=\"$name\" VALUE=\"$default\" SIZE=\"$size\">"; | |
335 | } | |
336 | ||
337 | // }}} | |
338 | // {{{ returnTextRow() | |
339 | ||
340 | function returnTextRow($name, $title, $default = '', $size = HTML_FORM_TEXT_SIZE) { | |
341 | $str .= " <TR>\n"; | |
342 | $str .= " <TH ALIGN=\"right\">$title</TH>"; | |
343 | $str .= " <TD>"; | |
344 | $str .= $this->returnText($name, $default, $size); | |
345 | $str .= "</TD>\n"; | |
346 | $str .= " </TR>\n"; | |
347 | ||
348 | return $str; | |
349 | } | |
350 | ||
351 | // }}} | |
352 | // {{{ returnPassword() | |
353 | ||
354 | function returnPassword($name, $default = '', $size = HTML_FORM_PASSWD_SIZE) { | |
355 | return "<INPUT NAME=\"$name\" TYPE=\"password\" VALUE=\"$default\" SIZE=\"$size\">"; | |
356 | } | |
357 | ||
358 | // }}} | |
359 | // {{{ returnPasswordRow() | |
360 | ||
361 | function returnPasswordRow($name, $title, $default = '', $size = HTML_FORM_PASSWD_SIZE) { | |
362 | $str .= "<TR>\n"; | |
363 | $str .= " <TH ALIGN=\"right\">$title</TH>\n"; | |
364 | $str .= " <TD>"; | |
365 | $str .= $this->returnPassword($name, $default, $size); | |
366 | $str .= " repeat: "; | |
367 | $str .= $this->returnPassword($name."2", $default, $size); | |
368 | $str .= "</TD>\n"; | |
369 | $str .= "</TR>\n"; | |
370 | ||
371 | return $str; | |
372 | } | |
373 | ||
374 | // }}} | |
375 | // {{{ returnCheckbox() | |
376 | ||
377 | function returnCheckbox($name, $default = false) { | |
378 | $str .= "<INPUT TYPE=\"checkbox\" NAME=\"$name\""; | |
379 | if ($default && $default != 'off') { | |
380 | $str .= " CHECKED"; | |
381 | } | |
382 | $str .= ">"; | |
383 | ||
384 | return $str; | |
385 | } | |
386 | ||
387 | // }}} | |
388 | // {{{ returnCheckboxRow() | |
389 | ||
390 | function returnCheckboxRow($name, $title, $default = false) { | |
391 | $str .= " <TR>\n"; | |
392 | $str .= " <TH ALIGN=\"right\">$title</TH>\n"; | |
393 | $str .= " <TD>"; | |
394 | $str .= $this->returnCheckbox($name, $default); | |
395 | $str .= "</TD>\n"; | |
396 | $str .= " </TR>\n"; | |
397 | ||
398 | return $str; | |
399 | } | |
400 | ||
401 | // }}} | |
402 | // {{{ returnTextarea() | |
403 | ||
404 | function returnTextarea($name, $default = '', $width = 40, $height = 5) { | |
405 | $str .= "<TEXTAREA NAME=\"$name\" COLS=\"$width\" ROWS=\"$height\">"; | |
406 | $str .= $default; | |
407 | $str .= "</TEXTAREA>"; | |
408 | ||
409 | return $str; | |
410 | } | |
411 | ||
412 | // }}} | |
413 | // {{{ returnTextareaRow() | |
414 | ||
415 | function returnTextareaRow($name, $title, $default = '', $width = 40, $height = 5) { | |
416 | $str .= " <TR>\n"; | |
417 | $str .= " <TH ALIGN=\"right\">$title</TH>\n"; | |
418 | $str .= " <TD>"; | |
419 | $str .= $this->returnTextarea($name, &$default, $width, $height); | |
420 | $str .= "</TD>\n"; | |
421 | $str .= " </TR>\n"; | |
422 | ||
423 | return $str; | |
424 | } | |
425 | ||
426 | // }}} | |
427 | // {{{ returnSubmit() | |
428 | ||
429 | function returnSubmit($title = 'Submit Changes', $name = "submit") { | |
430 | return "<INPUT NAME=\"$name\" TYPE=\"submit\" VALUE=\"$title\">"; | |
431 | } | |
432 | ||
433 | // }}} | |
434 | // {{{ returnSubmitRow() | |
435 | ||
436 | function returnSubmitRow($name = "submit", $title = 'Submit Changes') { | |
437 | $str .= " <TR>\n"; | |
438 | $str .= " <TD> </TD>\n"; | |
439 | $str .= " <TD>"; | |
440 | $str .= $this->returnSubmit($title, $name); | |
441 | $str .= "</TD>\n"; | |
442 | $str .= " </TR>\n"; | |
443 | ||
444 | return $str; | |
445 | } | |
446 | ||
447 | // }}} | |
448 | // {{{ returnReset() | |
449 | ||
450 | function returnReset($title = 'Clear contents') { | |
451 | return "<INPUT TYPE=\"reset\" VALUE=\"$title\">"; | |
452 | } | |
453 | ||
454 | // }}} | |
455 | // {{{ returnResetRow() | |
456 | ||
457 | function returnResetRow($title = 'Clear contents') { | |
458 | $str .= " <TR>\n"; | |
459 | $str .= " <TD> </TD>\n"; | |
460 | $str .= " <TD>"; | |
461 | $str .= $this->returnReset($title); | |
462 | $str .= "</TD>\n"; | |
463 | $str .= " </TR>\n"; | |
464 | ||
465 | return $str; | |
466 | } | |
467 | ||
468 | // }}} | |
469 | // {{{ returnSelect() | |
470 | ||
471 | function returnSelect($name, $entries, $default = '', $size = 1, | |
472 | $blank = '', $multiple = false) { | |
473 | $str .= " <SELECT NAME=\"$name\""; | |
474 | if ($size) { | |
475 | $str .= " SIZE=\"$size\""; | |
476 | } | |
477 | if ($multiple) { | |
478 | $str .= " MULTIPLE"; | |
479 | } | |
480 | $str .= ">\n"; | |
481 | if ($blank) { | |
482 | $str .= " <OPTION VALUE=\"\">$blank\n"; | |
483 | } | |
484 | while (list($val, $text) = each($entries)) { | |
485 | $str .= ' <OPTION '; | |
486 | if ($default && $default == $val) { | |
487 | $str .= 'SELECTED '; | |
488 | } | |
489 | $str .= "VALUE=\"$val\">$text\n"; | |
490 | } | |
491 | $str .= " </SELECT>\n"; | |
492 | ||
493 | return $str; | |
494 | } | |
495 | ||
496 | // }}} | |
497 | // {{{ returnSelectRow() | |
498 | ||
499 | function returnSelectRow($name, $title, &$entries, $default = '', $size = 1, | |
500 | $blank = '', $multiple = false) | |
501 | { | |
502 | $str .= " <TR>\n"; | |
503 | $str .= " <TH ALIGN=\"right\">$title:</TH>\n"; | |
504 | $str .= " <TD>\n"; | |
505 | $str .= $this->returnSelect($name, &$entries, $default, $size, $blank, $multiple); | |
506 | $str .= " </TD>\n"; | |
507 | $str .= " </TR>\n"; | |
508 | ||
509 | return $str; | |
510 | } | |
511 | ||
512 | // }}} | |
513 | // {{{ returnHidden() | |
514 | ||
515 | function returnHidden($name, $value) { | |
516 | return "<INPUT TYPE=\"hidden\" NAME=\"$name\" VALUE=\"$value\">"; | |
517 | } | |
518 | ||
519 | // }}} | |
520 | ||
521 | // {{{ returnStart() | |
522 | ||
523 | function returnStart() { | |
524 | $ret = "<FORM ACTION=\"" . basename ($this->action) . "\" METHOD=\"$this->method\""; | |
525 | if ($this->name) { | |
526 | $ret .= " NAME=\"$this->name\""; | |
527 | } | |
528 | $ret .= ">"; | |
529 | return $ret; | |
530 | } | |
531 | ||
532 | // }}} | |
533 | // {{{ returnEnd() | |
534 | ||
535 | function returnEnd() { | |
536 | $fields = array(); | |
537 | reset($this->fields); | |
538 | while (list($i, $data) = each($this->fields)) { | |
539 | if ($data[0] == 'reset') { | |
540 | continue; | |
541 | } | |
542 | $fields[$data[1]] = true; | |
543 | } | |
544 | $ret = $this->returnHidden("_fields", implode(":", array_keys($fields))); | |
545 | $ret .= "</FORM>"; | |
546 | return $ret; | |
547 | } | |
548 | ||
549 | // }}} | |
550 | ||
551 | // {{{ display() | |
552 | ||
553 | function display() { | |
554 | $this->start(); | |
555 | print "<TABLE>\n"; | |
556 | reset($this->fields); | |
557 | $hidden = array(); | |
558 | $call_cache = array(); | |
559 | while (list($i, $data) = each($this->fields)) { | |
560 | switch ($data[0]) { | |
561 | case "hidden": | |
562 | $hidden[] = $i; | |
563 | continue 2; | |
564 | case "reset": | |
565 | $params = 1; | |
566 | break; | |
567 | case "submit": | |
568 | case "image": | |
569 | $params = 2; | |
570 | break; | |
571 | case "checkbox": | |
572 | $params = 3; | |
573 | break; | |
574 | case "text": | |
575 | case "password": | |
576 | case "radio": | |
577 | $params = 4; | |
578 | break; | |
579 | case "textarea": | |
580 | $params = 5; | |
581 | break; | |
582 | case "select": | |
583 | $params = 7; | |
584 | break; | |
585 | case "file": | |
586 | $params=8; | |
587 | break; | |
588 | default: | |
589 | // unknown field type | |
590 | continue 2; | |
591 | } | |
592 | $str = $call_cache[$params]; | |
593 | if (!$str) { | |
594 | $str = '$this->display'.ucfirst($data[0])."Row("; | |
595 | for ($i = 1; $i <= $params; $i++) { | |
596 | $str .= '$data['.$i.']'; | |
597 | if ($i < $params) $str .= ', '; | |
598 | } | |
599 | $str .= ');'; | |
600 | $call_cache[$params] = $str; | |
601 | } | |
602 | eval($str); | |
603 | } | |
604 | print "</TABLE>\n"; | |
605 | for ($i = 0; $i < sizeof($hidden); $i++) { | |
606 | $this->displayHidden($this->fields[$hidden[$i]][1], | |
607 | $this->fields[$hidden[$i]][2]); | |
608 | } | |
609 | $this->end(); | |
610 | } | |
611 | ||
612 | // }}} | |
613 | ||
614 | ||
615 | ||
616 | ||
617 | function showForm($login='',$email='',$web='',$info='',$skinarray) {; | |
618 | $this->addText("login","Prihlasovacie meno:",$login); | |
619 | $this->addPassword("password1","Heslo:","",10); | |
620 | $this->addPassword("password2","Heslo pre overenie:","",10); | |
621 | $this->addText("email:","Emailova adresa",$email); | |
622 | $this->addText("web","Stranka:",$web); | |
623 | $this->addSelect("skin","Skin:",$skinarray); | |
624 | $this->addFile("image","Obrazok:"); | |
625 | $this->addTextarea("info","Dodatocne informacie",$info); | |
626 | $this->addSubmit("submit","Zaregistruj sa!",""); | |
627 | $this->addHidden("sent","ok"); | |
628 | $this->display(); | |
629 | ||
630 | } | |
631 | ||
632 | } | |
633 | ?> |