Major changes to user groups:
[lhc/web/wiklou.git] / includes / SpecialGroups.php
1 <?php
2 /**
3 * Provide an administration interface
4 * DO NOT USE: INSECURE.
5 * @package MediaWiki
6 * @subpackage SpecialPage
7 */
8
9 /** */
10 require_once('HTMLForm.php');
11 require_once('Group.php');
12
13 /** Entry point */
14 function wfSpecialGroups($par=null) {
15 global $wgRequest;
16 # Debug statement
17 // print_r($_POST);
18 $form = new GroupsForm($wgRequest);
19 $form->execute();
20 }
21
22 /**
23 * A class to manage group levels rights.
24 * @package MediaWiki
25 * @subpackage SpecialPage
26 */
27 class GroupsForm extends HTMLForm {
28 var $mPosted, $mRequest, $mSaveprefs, $mChangeAllowed;
29 var $mNewName, $mDescription, $mOldName, $mRights, $mId;
30 var $mAdd, $mEdit;
31
32 /** Escaped local url name*/
33 var $action, $location;
34
35 /** Constructor*/
36 function GroupsForm ( &$request ) {
37 global $wgUser;
38
39 $this->mPosted = $request->wasPosted();
40 $this->mRequest = $request;
41 $this->mName = 'groups';
42
43 $this->mNewName = trim( $request->getText('editgroup-name') );
44 $this->mOldName = trim( $request->getText('editgroup-oldname' ) );
45 $this->mDescription = trim( $request->getText( 'editgroup-description' ) );
46 $this->mRights = $request->getArray( 'editgroup-getrights' );
47 $this->mId = $this->mRequest->getInt('id');
48 $this->mEdit = $request->getCheck('edit');
49 $this->mAdd = $request->getCheck('add');
50
51
52 $titleObj = Title::makeTitle( NS_SPECIAL, 'Groups' );
53 $this->action = $titleObj->escapeLocalURL();
54 if ( $this->mAdd ) {
55 $this->location = $titleObj->getLocalURL( "add=1&id={$this->mId}" );
56 } elseif ( $this->mEdit ) {
57 $this->location = $titleObj->getLocalURL( "edit=1&id={$this->mId}" );
58 } else {
59 $this->location = $this->action;
60 }
61
62 $this->mChangeAllowed = $wgUser->isAllowed( 'grouprights' ) && !Group::getStaticGroups();
63 }
64
65 /**
66 * Manage forms to be shown according to posted datas.
67 * Depending on the submit button used : Call a form or a saving function.
68 */
69 function execute() {
70 global $wgOut;
71
72 if ( $this->mRequest->getBool( 'showrecord' ) ) {
73 $this->showRecord();
74 } elseif ( $this->mPosted && $this->mChangeAllowed && $this->mRequest->getCheck('savegroup') ) {
75 // save settings
76 $this->saveGroup();
77 } elseif ( $this->mEdit ) {
78 if ( $this->mPosted ) {
79 $wgOut->redirect( $this->location );
80 } else {
81 $this->switchForm();
82 $this->editGroupForm( $this->mId );
83 }
84 } elseif ( $this->mAdd ) {
85 if ( $this->mPosted ) {
86 $wgOut->redirect( $this->location );
87 } else {
88 $this->switchForm();
89 $this->editGroupForm( );
90 }
91 } else {
92 $this->showAllGroups();
93 if ( $this->mChangeAllowed ) {
94 $this->switchForm();
95 }
96 }
97 }
98
99 /**
100 * Save a group
101 * @todo FIXME : Log is incorrect.
102 */
103 function saveGroup() {
104 global $wgOut;
105
106 $this->mNewName = trim($this->mNewName);
107
108 if ( $this->mNewName == '' ) {
109 $this->editGroupForm( $this->mGroupID, 'groups-noname' );
110 return false;
111 }
112
113 if($this->mOldName == '') {
114 // Check if the group already exists
115 $add = true;
116 $g = Group::newFromName( $this->mNewName );
117 if ( $g ) {
118 $this->editGroupForm( 0, 'groups-already-exists' );
119 return;
120 }
121
122 // Create a new group
123 $g = new group();
124 $g->addToDatabase();
125 } else {
126 $add = false;
127 $g = Group::newFromName($this->mOldName);
128 if ( !$g ) {
129 $this->editGroupForm( 0, 'groups-noname' );
130 return;
131 }
132 }
133
134 // save stuff
135 $g->setName($this->mNewName);
136 $g->setDescription($this->mDescription);
137 if( is_array( $this->mRights ) ) {
138 $g->setRights( implode(',',$this->mRights) );
139 }
140
141 $g->save();
142
143 // Make the log entry
144 $log = new LogPage( 'rights' );
145 $dummyTitle = Title::makeTitle( 0, '' );
146 if ( $add ) {
147 $log->addEntry( 'addgroup', $dummyTitle, '', array( $g->getNameForContent() ) );
148 } else {
149 if ( $this->mOldName != $this->mNewName ) {
150 // Abbreviated action name, must be less than 10 bytes
151 $log->addEntry( 'rngroup', $dummyTitle, '', array( Group::getMessageForContent( $this->mOldName ),
152 $g->getNameForContent() ) );
153 } else {
154 $log->addEntry( 'chgroup', $dummyTitle, '', array( $g->getNameForContent() ) );
155 }
156 }
157
158 // Success, go back to all groups page
159 $titleObj = Title::makeTitle( NS_SPECIAL, 'Groups' );
160 $url = $titleObj->getLocalURL();
161
162 $wgOut->redirect( $url );
163 }
164
165 /**
166 * The entry form
167 * It allows a user to edit or eventually add a group
168 */
169 function switchForm() {
170 global $wgOut;
171
172 // group selection
173 $wgOut->addHTML( "<form name=\"ulgroup\" action=\"$this->action\" method=\"post\">\n" );
174 $wgOut->addHTML( $this->fieldset( 'lookup-group',
175 HTMLSelectGroups('id', $this->mName.'-group-edit', array(0 => $this->mRequest->getVal('id')) ) .
176 ' <input type="submit" name="edit" value="'.wfMsg('editgroup').'" />' .
177 '<br /><input type="submit" name="add" value="'.wfMsg('addgroup').'" />'
178 ));
179 $wgOut->addHTML( "</form>\n" );
180 }
181
182 /**
183 * Edit a group properties and rights.
184 * @param string $groupname Name of a group to be edited.
185 * @param string $error message name of the error to display
186 */
187 function editGroupForm($groupID = 0, $error = '') {
188 global $wgOut;
189
190 if ( $error ) {
191 $errText = wfMsg( $error );
192 $wgOut->addHTML( "<p class='error'>$errText</p>" );
193 }
194
195 if($this->mRequest->getVal('edit')) {
196 // fetch data if we edit a group
197 $g = Group::newFromID($groupID);
198 $fieldname = 'editgroup';
199 } else {
200 // default datas when we add a group
201 $g = new group();
202 $fieldname = 'addgroup';
203 }
204
205 $gName = $g->getName();
206 $gDescription = $g->getDescription();
207
208
209 $wgOut->addHTML( "<form name=\"editGroup\" action=\"$this->action\" method=\"post\">\n".
210 '<input type="hidden" name="editgroup-oldname" value="'.$gName."\" />\n" );
211
212 $wgOut->addHTML( $this->fieldset( $fieldname,
213 '<p>' . wfMsg( 'groups-editgroup-preamble' ) . "</p>\n" .
214 $this->textbox( 'editgroup-name', $gName ) .
215 $this->textareabox( 'editgroup-description', $gDescription ) .
216 '<br /><table border="0" align="center"><tr><td>'.
217 HTMLSelectRights($g->getRights()).
218 '</td></tr></table>'."\n".
219 '<input type="submit" name="savegroup" value="'.wfMsg('savegroup').'" />'
220 ));
221
222 $wgOut->addHTML( "</form>\n" );
223 }
224
225 function showAllGroups() {
226 global $wgOut;
227 $groups =& Group::getAllGroups();
228
229 $groupsExisting = wfMsg( 'groups-existing' );
230 $groupsHeader = wfMsg( 'groups-tableheader' );
231
232 $s = "{| border=1
233 |+'''$groupsExisting'''
234 |-
235 !$groupsHeader
236 ";
237 foreach ( $groups as $group ) {
238 $s .= "|-\n| " . $group->getId() . ' || ' .
239 $group->getExpandedName() . ' || ' .
240 $group->getExpandedDescription() . ' || '.
241 // Insert spaces to make it wrap
242 str_replace( ',', ', ', $group->getRights() ) . "\n";
243 }
244 $s .= "|}\n";
245 $wgOut->addWikiText( $s );
246 }
247
248 function showRecord() {
249 global $wgOut;
250
251 $groups =& Group::getAllGroups();
252 $rec = serialize( $groups );
253 // Escape it for PHP
254 $rec = str_replace( array( '\\', "'" ), array( '\\\\', "\\'" ), $rec );
255 // Escape it for HTML
256 $rec = htmlspecialchars( $rec );
257 $s = "<p>Copy the following into LocalSettings.php:</p>\n" .
258 "<textarea readonly rows=20>\n" .
259 "\$wgStaticGroups = '$rec';\n" .
260 "</textarea>";
261 $wgOut->addHTML( $s );
262 }
263
264 } // end class GroupsForm
265 ?>