added docs to fast. we now have separate cvs module for that.
[squirrelmail.git] / functions / forms.php
CommitLineData
493b168d 1<?php
2/**
3 * forms.php
4 *
6c84ba1e 5 * Copyright (c) 2004-2005 The SquirrelMail Project Team
493b168d 6 * Licensed under the GNU GPL. For full terms see the file COPYING.
7 *
8 * Functions to build HTML forms in a safe and consistent manner.
9 * All name, value attributes are htmlentitied.
10 *
74f5d33f 11 * @version $Id$
493b168d 12 * @package squirrelmail
74f5d33f 13 * @subpackage forms
493b168d 14 */
15
16/**
17 * Helper function to create form fields, not to be called directly,
18 * only by other functions below.
19 */
9986098e 20function addInputField($type, $name = null, $value = null, $attributes = '', $id = null) {
10ff256e 21 return '<input type="'.$type.'"'.
745eb9e2 22 ($name !== null ? ' name="'.htmlspecialchars($name).'"' : '').
f8a1ed5a 23 ($id !== null ? ' id="'.htmlspecialchars($id).'"'
9986098e 24 : ($name !== null ? ' id="'.htmlspecialchars($name).'"' : '')).
745eb9e2 25 ($value !== null ? ' value="'.htmlspecialchars($value).'"' : '').
fad34a61 26 $attributes . " />\n";
493b168d 27}
28
10ff256e 29/**
30 * Password input field
31 */
6424bf75 32function addPwField($name , $value = null) {
33 return addInputField('password', $name , $value);
10ff256e 34}
35
493b168d 36
37/**
38 * Form checkbox
39 */
e0ac07ed 40function addCheckBox($name, $checked = false, $value = null, $xtra = '') {
493b168d 41 return addInputField('checkbox', $name, $value,
e0ac07ed 42 ($checked ? ' checked="checked"' : '') . ' ' . $xtra);
493b168d 43}
44
45/**
46 * Form radio box
47 */
d4b2cc0a 48function addRadioBox($name, $checked = false, $value = null) {
324ac3c5 49 return addInputField('radio', $name, $value,
9986098e 50 ($checked ? ' checked="checked"' : ''), $name . $value);
493b168d 51}
52
53/**
54 * A hidden form field.
55 */
56function addHidden($name, $value) {
57 return addInputField('hidden', $name, $value);
58}
59
60/**
61 * An input textbox.
62 */
63function addInput($name, $value = '', $size = 0, $maxlength = 0) {
64
65 $attr = '';
66 if ($size) {
67 $attr.= ' size="'.(int)$size.'"';
68 }
69 if ($maxlength) {
70 $attr.= ' maxlength="'.(int)$maxlength .'"';
71 }
72
73 return addInputField('text', $name, $value, $attr);
74}
75
76
77/**
78 * Function to create a selectlist from an array.
79 * Usage:
80 * name: html name attribute
f79fb61d 81 * values: array ( key => value ) -> <option value="key">value</option>
493b168d 82 * default: the key that will be selected
83 * usekeys: use the keys of the array as option value or not
84 */
85function addSelect($name, $values, $default = null, $usekeys = false)
86{
87 // only one element
88 if(count($values) == 1) {
89 $k = key($values); $v = array_pop($values);
90 return addHidden($name, ($usekeys ? $k:$v)).
745eb9e2 91 htmlspecialchars($v) . "\n";
493b168d 92 }
93
745eb9e2 94 $ret = '<select name="'.htmlspecialchars($name) . "\">\n";
493b168d 95 foreach ($values as $k => $v) {
96 if(!$usekeys) $k = $v;
97 $ret .= '<option value="' .
745eb9e2 98 htmlspecialchars( $k ) . '"' .
f79fb61d 99 (($default == $k) ? ' selected="selected"' : '') .
745eb9e2 100 '>' . htmlspecialchars($v) ."</option>\n";
493b168d 101 }
102 $ret .= "</select>\n";
103
104 return $ret;
105}
106
107/**
10ff256e 108 * Form submission button
109 * Note the switched value/name parameters!
110 */
111function addSubmit($value, $name = null) {
112 return addInputField('submit', $name, $value);
113}
114/**
115 * Form reset button, $value = caption
116 */
117function addReset($value) {
118 return addInputField('reset', null, $value);
119}
120
121/**
493b168d 122 * Textarea form element.
123 */
124function addTextArea($name, $text = '', $cols = 40, $rows = 10, $attr = '') {
745eb9e2 125 return '<textarea name="'.htmlspecialchars($name).'" '.
8a9f3f9d 126 'rows="'.(int)$rows .'" cols="'.(int)$cols.'" '.
127 $attr . '>'.htmlspecialchars($text) ."</textarea>\n";
493b168d 128}
129
130/**
131 * Make a <form> start-tag.
132 */
39bfea8f 133function addForm($action, $method = 'post', $name = '', $enctype = '', $charset = '')
493b168d 134{
135 if($name) {
136 $name = ' name="'.$name.'"';
137 }
138 if($enctype) {
139 $enctype = ' enctype="'.$enctype.'"';
140 }
141 if($charset) {
142 $charset = ' accept-charset="'.htmlspecialchars($charset).'"';
143 }
144
145 return '<form action="'. $action .'" method="'. $method .'"'.
9d6b3b72 146 $enctype . $name . $charset . ">\n";
493b168d 147}
148
f8a1ed5a 149?>