* Don't show useless empty preview on new page creation
[mediawiki.git] / includes / HTMLForm.php
blob12147d551025bdd2cd47d00a67b77a70a1a426d6
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 */
8 /**
9 * Class to build various forms
11 * @package MediaWiki
12 * @author jeluf, hashar
14 class HTMLForm {
15 /** name of our form. Used as prefix for labels */
16 var $mName, $mRequest;
18 function HTMLForm( &$request ) {
19 $this->mRequest = $request;
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
28 function fieldset( $name, $content ) {
29 return "<fieldset><legend>".wfMsg($this->mName.'-'.$name)."</legend>\n" .
30 $content . "\n</fieldset>\n";
33 /**
34 * @access private
35 * @param string $varname Name of the checkbox.
36 * @param boolean $checked Set true to check the box (default False).
38 function checkbox( $varname, $checked=false ) {
39 if ( $this->mRequest->wasPosted() && !is_null( $this->mRequest->getVal( $varname ) ) ) {
40 $checked = $this->mRequest->getCheck( $varname );
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";
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)
54 function textbox( $varname, $value='', $size=20 ) {
55 if ( $this->mRequest->wasPosted() ) {
56 $value = $this->mRequest->getText( $varname, $value );
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";
63 /**
64 * @access private
65 * @param string $varname Name of the radiobox.
66 * @param array $fields Various fields.
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";
74 return $this->fieldset( $this->mName.'-'.$varname, $s );
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)
83 function textareabox ( $varname, $value='', $size=20 ) {
84 if ( $this->mRequest->wasPosted() ) {
85 $value = $this->mRequest->getText( $varname, $value );
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";
92 /**
93 * @access private
94 * @param string $varname Name of the arraybox.
95 * @param integer $size Optional size of the textarea (default 20)
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";
107 return "<div><label>".wfMsg( $this->mName.'-'.$varname ).
108 "<textarea name=\"{$varname}\" rows=\"5\" cols=\"{$size}\">{$s}</textarea>\n";
110 } // end class
113 // functions used by SpecialUserrights.php and SpecialGroups.php
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).
122 function HTMLSelectGroups($selectname, $selectmsg, $selected=array(), $multiple=false, $size=6, $reverse=false) {
123 $groups = User::getAllGroups();
124 $out = htmlspecialchars( wfMsg( $selectmsg ) );
126 if( $multiple ) {
127 $attribs = array(
128 'name' => $selectname . '[]',
129 'multiple'=> 'multiple',
130 'size' => $size );
131 } else {
132 $attribs = array( 'name' => $selectname );
134 $out .= wfElement( 'select', $attribs, null );
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;
143 } else {
144 if( in_array( $group, $selected ) ) {
145 $attribs['selected'] = 'selected';
148 $out .= wfElement( 'option', $attribs, User::getGroupName( $group ) ) . "\n";
151 $out .= "</select>\n";
152 return $out;
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.
159 function HTMLSelectRights($selected='') {
160 global $wgAvailableRights;
161 $out = '<select name="editgroup-getrights[]" multiple="multiple">';
162 $groupRights = explode(',',$selected);
164 foreach($wgAvailableRights as $right) {
166 // check box when right exist
167 if(in_array($right, $groupRights)) { $selected = 'selected="selected" '; }
168 else { $selected = ''; }
170 $out .= '<option value="'.$right.'" '.$selected.'>'.$right."</option>\n";
172 $out .= "</select>\n";
173 return $out;