Use Doxygen @addtogroup instead of phpdoc @package && @subpackage
[lhc/web/wiklou.git] / includes / HTMLForm.php
1 <?php
2 /**
3 * This file contain a class to easily build HTML forms as well as custom
4 * functions used by SpecialUserrights.php
5 */
6
7 /**
8 * Class to build various forms
9 *
10 * @author jeluf, hashar
11 */
12 class HTMLForm {
13 /** name of our form. Used as prefix for labels */
14 var $mName, $mRequest;
15
16 function HTMLForm( &$request ) {
17 $this->mRequest = $request;
18 }
19
20 /**
21 * @private
22 * @param $name String: name of the fieldset.
23 * @param $content String: HTML content to put in.
24 * @return string HTML fieldset
25 */
26 function fieldset( $name, $content ) {
27 return "<fieldset><legend>".wfMsg($this->mName.'-'.$name)."</legend>\n" .
28 $content . "\n</fieldset>\n";
29 }
30
31 /**
32 * @private
33 * @param $varname String: name of the checkbox.
34 * @param $checked Boolean: set true to check the box (default False).
35 */
36 function checkbox( $varname, $checked=false ) {
37 if ( $this->mRequest->wasPosted() && !is_null( $this->mRequest->getVal( $varname ) ) ) {
38 $checked = $this->mRequest->getCheck( $varname );
39 }
40 return "<div><input type='checkbox' value=\"1\" id=\"{$varname}\" name=\"wpOp{$varname}\"" .
41 ( $checked ? ' checked="checked"' : '' ) .
42 " /><label for=\"{$varname}\">". wfMsg( $this->mName.'-'.$varname ) .
43 "</label></div>\n";
44 }
45
46 /**
47 * @private
48 * @param $varname String: name of the textbox.
49 * @param $value String: optional value (default empty)
50 * @param $size Integer: optional size of the textbox (default 20)
51 */
52 function textbox( $varname, $value='', $size=20 ) {
53 if ( $this->mRequest->wasPosted() ) {
54 $value = $this->mRequest->getText( $varname, $value );
55 }
56 $value = htmlspecialchars( $value );
57 return "<div><label>". wfMsg( $this->mName.'-'.$varname ) .
58 "<input type='text' name=\"{$varname}\" value=\"{$value}\" size=\"{$size}\" /></label></div>\n";
59 }
60
61 /**
62 * @private
63 * @param $varname String: name of the radiobox.
64 * @param $fields Array: Various fields.
65 */
66 function radiobox( $varname, $fields ) {
67 foreach ( $fields as $value => $checked ) {
68 $s .= "<div><label><input type='radio' name=\"{$varname}\" value=\"{$value}\"" .
69 ( $checked ? ' checked="checked"' : '' ) . " />" . wfMsg( $this->mName.'-'.$varname.'-'.$value ) .
70 "</label></div>\n";
71 }
72 return $this->fieldset( $varname, $s );
73 }
74
75 /**
76 * @private
77 * @param $varname String: name of the textareabox.
78 * @param $value String: optional value (default empty)
79 * @param $size Integer: optional size of the textarea (default 20)
80 */
81 function textareabox ( $varname, $value='', $size=20 ) {
82 if ( $this->mRequest->wasPosted() ) {
83 $value = $this->mRequest->getText( $varname, $value );
84 }
85 $value = htmlspecialchars( $value );
86 return '<div><label>'.wfMsg( $this->mName.'-'.$varname ).
87 "<textarea name=\"{$varname}\" rows=\"5\" cols=\"{$size}\">$value</textarea></label></div>\n";
88 }
89
90 /**
91 * @private
92 * @param $varname String: name of the arraybox.
93 * @param $size Integer: Optional size of the textarea (default 20)
94 */
95 function arraybox( $varname , $size=20 ) {
96 $s = '';
97 if ( $this->mRequest->wasPosted() ) {
98 $arr = $this->mRequest->getArray( $varname );
99 if ( is_array( $arr ) ) {
100 foreach ( $_POST[$varname] as $element ) {
101 $s .= htmlspecialchars( $element )."\n";
102 }
103 }
104 }
105 return "<div><label>".wfMsg( $this->mName.'-'.$varname ).
106 "<textarea name=\"{$varname}\" rows=\"5\" cols=\"{$size}\">{$s}</textarea>\n";
107 }
108 } // end class
109
110 /** Build a select with all defined groups
111 *
112 * used by SpecialUserrights.php
113 * @todo move it to there, and don't forget to copy it for SpecialMakesysop.php
114 *
115 * @param $selectname String: name of this element. Name of form is automaticly prefixed.
116 * @param $selectmsg String: FIXME
117 * @param $selected Array: array of element selected when posted. Only multiples will show them.
118 * @param $multiple Boolean: A multiple elements select.
119 * @param $size Integer: number of elements to be shown ignored for non-multiple (default 6).
120 * @param $reverse Boolean: if true, multiple select will hide selected elements (default false).
121 * @todo Document $selectmsg
122 */
123 function HTMLSelectGroups($selectname, $selectmsg, $selected=array(), $multiple=false, $size=6, $reverse=false) {
124 $groups = User::getAllGroups();
125 $out = htmlspecialchars( wfMsg( $selectmsg ) );
126
127 if( $multiple ) {
128 $attribs = array(
129 'name' => $selectname . '[]',
130 'multiple'=> 'multiple',
131 'size' => $size );
132 } else {
133 $attribs = array( 'name' => $selectname );
134 }
135 $out .= wfElement( 'select', $attribs, null );
136
137 foreach( $groups as $group ) {
138 $attribs = array( 'value' => $group );
139 if( $multiple ) {
140 // for multiple will only show the things we want
141 if( !in_array( $group, $selected ) xor $reverse ) {
142 continue;
143 }
144 } else {
145 if( in_array( $group, $selected ) ) {
146 $attribs['selected'] = 'selected';
147 }
148 }
149 $out .= wfElement( 'option', $attribs, User::getGroupName( $group ) ) . "\n";
150 }
151
152 $out .= "</select>\n";
153 return $out;
154 }
155
156 ?>