Completed reversion of removal of non-incremental link updates. Reasons previously...
[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, $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 global $wgOut;
124 $groups =& Group::getAllGroups();
125
126 $out = wfMsg($selectmsg);
127 $out .= '<select name="'.$selectname;
128 if($multiple) { $out.='[]" multiple="multiple" size="'.$size; }
129 $out.= "\">\n";
130
131 foreach ( $groups as $id => $g ) {
132 if($multiple) {
133 // for multiple will only show the things we want
134 if(in_array($id, $selected) xor $reverse) {
135 $out .= '<option value="'.$id.'">'.$wgOut->parse( $g->getExpandedName() )."</option>\n";
136 }
137 } else {
138 $out .= '<option ';
139 if(in_array($id, $selected)) { $out .= 'selected="selected" '; }
140 $out .= 'value="'.$id.'">'.$wgOut->parse( $g->getExpandedName() )."</option>\n";
141 }
142 }
143 $out .= "</select>\n";
144 return $out;
145 }
146
147 /** Build a select with all existent rights
148 * @param array $selected Names(?) of user rights that should be selected.
149 * @return string HTML select.
150 */
151 function HTMLSelectRights($selected='') {
152 global $wgAvailableRights;
153 $out = '<select name="editgroup-getrights[]" multiple="multiple">';
154 $groupRights = explode(',',$selected);
155
156 foreach($wgAvailableRights as $right) {
157
158 // check box when right exist
159 if(in_array($right, $groupRights)) { $selected = 'selected="selected" '; }
160 else { $selected = ''; }
161
162 $out .= '<option value="'.$right.'" '.$selected.'>'.$right."</option>\n";
163 }
164 $out .= "</select>\n";
165 return $out;
166 }
167 ?>