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