Moving Special:Userlevels and Special:Grouplevels to Special:Userrights and Special...
[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 and SpecialGroups.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;
17
18 /**
19 * @access private
20 * @param string $name Name of the fieldset.
21 * @param string $content HTML content to put in.
22 * @return string HTML fieldset
23 */
24 function fieldset( $name, $content ) {
25 return "<fieldset><legend>".wfMsg($this->mName.'-'.$name)."</legend>\n" .
26 $content . "\n</fieldset>\n";
27 }
28
29 /**
30 * @access private
31 * @param string $varname Name of the checkbox.
32 * @param boolean $checked Set true to check the box (default False).
33 */
34 function checkbox( $varname, $checked=false ) {
35 $checked = isset( $_POST[$varname] ) && $_POST[$varname] ;
36 return "<div><input type='checkbox' value=\"1\" id=\"{$varname}\" name=\"wpOp{$varname}\"" .
37 ( $checked ? ' checked="checked"' : '' ) .
38 " /><label for=\"{$varname}\">". wfMsg( $this->mName.'-'.$varname ) .
39 "</label></div>\n";
40 }
41
42 /**
43 * @access private
44 * @param string $varname Name of the textbox.
45 * @param string $value Optional value (default empty)
46 * @param integer $size Optional size of the textbox (default 20)
47 */
48 function textbox( $varname, $value='', $size=20 ) {
49 $value = isset( $_POST[$varname] ) ? $_POST[$varname] : $value;
50 return "<div><label>". wfMsg( $this->mName.'-'.$varname ) .
51 "<input type='text' name=\"{$varname}\" value=\"{$value}\" size=\"{$size}\" /></label></div>\n";
52 }
53
54 /**
55 * @access private
56 * @param string $varname Name of the radiobox.
57 * @param array $fields Various fields.
58 */
59 function radiobox( $varname, $fields ) {
60 foreach ( $fields as $value => $checked ) {
61 $s .= "<div><label><input type='radio' name=\"{$varname}\" value=\"{$value}\"" .
62 ( $checked ? ' checked="checked"' : '' ) . " />" . wfMsg( $this->mName.'-'.$varname.'-'.$value ) .
63 "</label></div>\n";
64 }
65 return $this->fieldset( $this->mName.'-'.$varname, $s );
66 }
67
68 /**
69 * @access private
70 * @param string $varname Name of the textareabox.
71 * @param string $value Optional value (default empty)
72 * @param integer $size Optional size of the textarea (default 20)
73 */
74 function textareabox ( $varname, $value='', $size=20 ) {
75 $value = isset( $_POST[$varname] ) ? $_POST[$varname] : $value;
76 return '<div><label>'.wfMsg( $this->mName.'-'.$varname ).
77 "<textarea name=\"{$varname}\" rows=\"5\" cols=\"{$size}\">$value</textarea></label></div>\n";
78 }
79
80 /**
81 * @access private
82 * @param string $varname Name of the arraybox.
83 * @param integer $size Optional size of the textarea (default 20)
84 */
85 function arraybox( $varname , $size=20 ) {
86 $s = '';
87 if ( isset( $_POST[$varname] ) && is_array( $_POST[$varname] ) ) {
88 foreach ( $_POST[$varname] as $index=>$element ) {
89 $s .= $element."\n";
90 }
91 }
92 return "<div><label>".wfMsg( $this->mName.'-'.$varname ).
93 "<textarea name=\"{$varname}\" rows=\"5\" cols=\"{$size}\">{$s}</textarea>\n";
94 }
95 } // end class
96
97
98 /** Build a select with all existent groups
99 * @param string $selectname Name of this element. Name of form is automaticly prefixed.
100 * @param array $selected Array of element selected when posted. Multiples will only show them.
101 * @param boolean $multiple A multiple elements select.
102 * @param integer $size Number of element to be shown ignored for non multiple (default 6).
103 * @param boolean $reverse If true, multiple select will hide selected elements (default false).
104 */
105 function HTMLSelectGroups($selectname, $selected=array(), $multiple=false, $size=6, $reverse=false) {
106 $dbr =& wfGetDB( DB_SLAVE );
107 $group = $dbr->tableName( 'group' );
108 $sql = "SELECT group_id, group_name FROM $group";
109 $res = $dbr->query($sql,'wfSpecialAdmin');
110
111 $out = wfMsg($selectname);
112 $out .= '<select name="'.$selectname;
113 if($multiple) { $out.='[]" multiple="multiple" size="'.$size; }
114 $out.= "\">\n";
115
116 while($g = $dbr->fetchObject( $res ) ) {
117 if($multiple) {
118 // for multiple will only show the things we want
119 if(in_array($g->group_id, $selected) xor $reverse) {
120 $out .= '<option value="'.$g->group_id.'">'.$g->group_name."</option>\n";
121 }
122 } else {
123 $out .= '<option ';
124 if(in_array($g->group_id, $selected)) { $out .= 'selected="selected" '; }
125 $out .= 'value="'.$g->group_id.'">'.$g->group_name."</option>\n";
126 }
127 }
128 $out .= "</select>\n";
129 return $out;
130 }
131
132 /** Build a select with all existent rights
133 * @param array $selected Names(?) of user rights that should be selected.
134 * @return string HTML select.
135 */
136 function HTMLSelectRights($selected='') {
137 global $wgAvailableRights;
138 $out = '<select name="editgroup-getrights[]" multiple="multiple">';
139 $groupRights = explode(',',$selected);
140
141 foreach($wgAvailableRights as $right) {
142
143 // check box when right exist
144 if(in_array($right, $groupRights)) { $selected = 'selected="selected" '; }
145 else { $selected = ''; }
146
147 $out .= '<option value="'.$right.'" '.$selected.'>'.$right."</option>\n";
148 }
149 $out .= "</select>\n";
150 return $out;
151 }
152 ?>