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