]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/HTMLForm.php
MediaWiki 1.5.8 (initial commit)
[autoinstalls/mediawiki.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, $mRequest;
17
18         function HTMLForm( &$request ) {
19                 $this->mRequest = $request;
20         }
21
22         /**
23          * @access private
24          * @param string $name Name of the fieldset.
25          * @param string $content 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          * @access private
35          * @param string $varname Name of the checkbox.
36          * @param boolean $checked 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          * @access private
50          * @param string $varname Name of the textbox.
51          * @param string $value Optional value (default empty)
52          * @param integer $size 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          * @access private
65          * @param string $varname Name of the radiobox.
66          * @param array $fields 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( $this->mName.'-'.$varname, $s );
75         }
76         
77         /** 
78          * @access private
79          * @param string $varname Name of the textareabox.
80          * @param string $value Optional value (default empty)
81          * @param integer $size 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          * @access private
94          * @param string $varname Name of the arraybox.
95          * @param integer $size 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 $index=>$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
113 // functions used by SpecialUserrights.php and SpecialGroups.php
114
115 /** Build a select with all defined groups
116  * @param string $selectname Name of this element. Name of form is automaticly prefixed.
117  * @param array $selected Array of element selected when posted. Only multiples will show them.
118  * @param boolean $multiple A multiple elements select.
119  * @param integer $size Number of elements to be shown ignored for non-multiple (default 6).
120  * @param boolean $reverse If true, multiple select will hide selected elements (default false).
121 */
122 function HTMLSelectGroups($selectname, $selectmsg, $selected=array(), $multiple=false, $size=6, $reverse=false) {
123         $groups = User::getAllGroups();
124         $out = htmlspecialchars( wfMsg( $selectmsg ) );
125         
126         if( $multiple ) {
127                 $attribs = array(
128                         'name'    => $selectname . '[]',
129                         'multiple'=> 'multiple',
130                         'size'    => $size );
131         } else {
132                 $attribs = array( 'name' => $selectname );
133         }
134         $out .= wfElement( 'select', $attribs, null );
135         
136         foreach( $groups as $group ) {
137                 $attribs = array( 'value' => $group );
138                 if( $multiple ) {
139                         // for multiple will only show the things we want
140                         if( !in_array( $group, $selected ) xor $reverse ) {
141                                 continue;
142                         }
143                 } else {
144                         if( in_array( $group, $selected ) ) {
145                                 $attribs['selected'] = 'selected';
146                         }
147                 }
148                 $out .= wfElement( 'option', $attribs, User::getGroupName( $group ) ) . "\n";
149         }
150
151         $out .= "</select>\n";
152         return $out;
153 }
154
155 /** Build a select with all existent rights
156  * @param array $selected Names(?) of user rights that should be selected.
157  * @return string HTML select.
158  */
159 function HTMLSelectRights($selected='') {
160         global $wgAvailableRights;
161         $out = '<select name="editgroup-getrights[]" multiple="multiple">';
162         $groupRights = explode(',',$selected);
163         
164         foreach($wgAvailableRights as $right) {
165         
166                 // check box when right exist
167                 if(in_array($right, $groupRights)) { $selected = 'selected="selected" '; }
168                 else { $selected = ''; }
169                                         
170                 $out .= '<option value="'.$right.'" '.$selected.'>'.$right."</option>\n";
171         }
172         $out .= "</select>\n";
173         return $out;
174 }
175 ?>