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