Fixed duplicate group problem, fixed isNewbie() function
[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() {
15 global $wgRequest;
16
17 $form = new GroupsForm($wgRequest);
18 $form->execute();
19 }
20
21 /**
22 * A class to manage group levels rights.
23 * @package MediaWiki
24 * @subpackage SpecialPage
25 */
26 class GroupsForm extends HTMLForm {
27 var $mPosted, $mRequest, $mSaveprefs, $mChangeAllowed;
28 var $mNewName, $mDescription, $mOldName, $mRights, $mId;
29 var $mAdd, $mEdit;
30
31 /** Escaped local url name*/
32 var $action, $location;
33
34 /** Constructor*/
35 function GroupsForm ( &$request ) {
36 global $wgUser;
37
38 $this->mPosted = $request->wasPosted();
39 $this->mRequest = $request;
40 $this->mName = 'groups';
41
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');
49
50
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;
59 }
60
61 $this->mChangeAllowed = $wgUser->isAllowed( 'grouprights' ) && !Group::getStaticGroups();
62 }
63
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.
67 */
68 function execute() {
69 global $wgOut;
70
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 );
82 }
83 } elseif ( $this->mAdd ) {
84 if ( $this->mPosted ) {
85 $wgOut->redirect( $this->location );
86 } else {
87 $this->switchForm();
88 $this->editGroupForm( );
89 }
90 } else {
91 $this->showAllGroups();
92 if ( $this->mChangeAllowed ) {
93 $this->switchForm();
94 }
95 }
96 }
97
98 /**
99 * Save a group
100 * @todo FIXME : Log is incorrect.
101 */
102 function saveGroup() {
103 global $wgOut;
104
105 $this->mNewName = trim($this->mNewName);
106
107 if ( $this->mNewName == '' ) {
108 $this->editGroupForm( $this->mGroupID, 'groups-noname' );
109 return false;
110 }
111
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;
119 }
120
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;
130 }
131 }
132
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) );
138 }
139
140 $g->save();
141
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() ) );
154 }
155 }
156
157 // Success, go back to all groups page
158 $titleObj = Title::makeTitle( NS_SPECIAL, 'Groups' );
159 $url = $titleObj->getLocalURL();
160
161 $wgOut->redirect( $url );
162 }
163
164 /**
165 * The entry form
166 * It allows a user to edit or eventually add a group
167 */
168 function switchForm() {
169 global $wgOut;
170
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').'" />'
177 ));
178 $wgOut->addHTML( "</form>\n" );
179 }
180
181 /**
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
185 */
186 function editGroupForm($groupID = 0, $error = '') {
187 global $wgOut;
188
189 if ( $error ) {
190 $errText = wfMsg( $error );
191 $wgOut->addHTML( "<p class='error'>$errText</p>" );
192 }
193
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';
202 }
203
204 $gName = $g->getName();
205 $gDescription = $g->getDescription();
206
207
208 $wgOut->addHTML( "<form name=\"editGroup\" action=\"$this->action\" method=\"post\">\n".
209 '<input type="hidden" name="editgroup-oldname" value="'.$gName."\" />\n" );
210
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').'" />'
219 ));
220
221 $wgOut->addHTML( "</form>\n" );
222 }
223
224 function showAllGroups() {
225 global $wgOut;
226 $groups =& Group::getAllGroups();
227
228 $groupsExisting = wfMsg( 'groups-existing' );
229 $groupsHeader = wfMsg( 'groups-tableheader' );
230
231 $s = "{| border=1
232 |+'''$groupsExisting'''
233 |-
234 !$groupsHeader
235 ";
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";
242 }
243 $s .= "|}\n";
244 $wgOut->addWikiText( $s );
245 }
246
247 function showRecord() {
248 global $wgOut;
249
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 );
261 }
262
263 } // end class GroupsForm
264 ?>