Major changes to user groups:
[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 // functions used by SpecialUserrights.php and SpecialGroups.php
99
100 /** Build a select with all defined groups
101 * @param string $selectname Name of this element. Name of form is automaticly prefixed.
102 * @param array $selected Array of element selected when posted. Multiples will only show them.
103 * @param boolean $multiple A multiple elements select.
104 * @param integer $size Number of element to be shown ignored for non multiple (default 6).
105 * @param boolean $reverse If true, multiple select will hide selected elements (default false).
106 */
107 function HTMLSelectGroups($selectname, $selectmsg, $selected=array(), $multiple=false, $size=6, $reverse=false) {
108 $groups =& Group::getAllGroups();
109
110 $out = wfMsg($selectmsg);
111 $out .= '<select name="'.$selectname;
112 if($multiple) { $out.='[]" multiple="multiple" size="'.$size; }
113 $out.= "\">\n";
114
115 foreach ( $groups as $id => $g ) {
116 if($multiple) {
117 // for multiple will only show the things we want
118 if(in_array($id, $selected) xor $reverse) {
119 $out .= '<option value="'.$id.'">'.$g->getExpandedName()."</option>\n";
120 }
121 } else {
122 $out .= '<option ';
123 if(in_array($id, $selected)) { $out .= 'selected="selected" '; }
124 $out .= 'value="'.$id.'">'.$g->getExpandedName()."</option>\n";
125 }
126 }
127 $out .= "</select>\n";
128 return $out;
129 }
130
131 /** Build a select with all existent rights
132 * @param array $selected Names(?) of user rights that should be selected.
133 * @return string HTML select.
134 */
135 function HTMLSelectRights($selected='') {
136 global $wgAvailableRights;
137 $out = '<select name="editgroup-getrights[]" multiple="multiple">';
138 $groupRights = explode(',',$selected);
139
140 foreach($wgAvailableRights as $right) {
141
142 // check box when right exist
143 if(in_array($right, $groupRights)) { $selected = 'selected="selected" '; }
144 else { $selected = ''; }
145
146 $out .= '<option value="'.$right.'" '.$selected.'>'.$right."</option>\n";
147 }
148 $out .= "</select>\n";
149 return $out;
150 }
151 ?>