/ -> groups.php
1 <?php
2
3 // groups.php - List, modify, delete and add groups
4 // groups.php - author: Nico Stuurman <nicost@soureforge.net>
5 // TABLES: groups
6 /***************************************************************************
7 * This script displays a table with groups in phplabware. It can only be *
8 * called by the sysadmin *
9 * Copyright (c) 2001 by Nico Stuurman *
10 * ------------------------------------------------------------------------ *
11 * This program is free software; you can redistribute it and/or modify it *
12 * under the terms of the GNU General Public License as published by the *
13 * Free Software Foundation; either version 2 of the License, or (at your *
14 * option) any later version. *
15 \**************************************************************************/
16
17 // main include thingies
18 require("include.php");
19
20 allowonly($SUPER, $USER["permissions"]);
21
22 // register variables
23 $PHP_SELF = $HTTP_SERVER_VARS ["PHP_SELF"];
24 $get_vars = "mod,groupid,groupname,";
25 globalize_vars ($get_vars,$HTTP_GET_VARS);
26 $post_vars = "add,groupid,groupname,submit,";
27 globalize_vars ($post_vars, $HTTP_POST_VARS);
28
29 // main global vars
30 $title = "Admin Groups";
31
32 ////
33 // !Adds group to database.
34 // An error string is returned when problems occur
35 function add_new_group ($db,$groupname) {
36
37 // check if a groupname is entered
38 if ($groupname) {
39 $query = "SELECT * FROM groups WHERE name='$groupname'";
40 $r = $db->Execute($query);
41 // test if a result is found
42 if (!$r->EOF) {
43 return "groupname already exists; please try again";
44 }
45 else {
46 $id=$db->GenID("groups_id_seq");
47 if ($db->Execute("INSERT INTO groups(id,name) VALUES('$id','$groupname') ") ) {
48 echo "Group <b>$groupname</b> added,";
49 }
50 else
51 echo "Group was not added<br>";
52 }
53 }
54 else
55 return "Please enter a groupname!";
56 }
57
58 ////
59 // !Change that groupname.
60 // On error, returns an error string
61 function modify_group ($db, $groupid, $new_name) {
62 // Only change the name when there is a new one provided
63 if (!$new_name)
64 return "Please enter a groupname!";
65
66 // check whether a group with the new name already exists
67 $r = $db->Execute ("SELECT id FROM groups WHERE
68 (name='$new_name' AND NOT id='$groupid');");
69 if (!$r->EOF)
70 return "Groupname <b>$new_name</b> already exists, please select another name";
71 // now change the name
72 else {
73 // get the old name
74 $old_name = get_cell($db,"groups","name","id",$groupid);
75 $query = "UPDATE groups SET name='$new_name' WHERE id='$groupid';";
76 if ($db->Execute($query))
77 echo "New groupname is <b>$new_name</b><br><br>";
78 }
79 }
80
81
82 ////
83 // !Delete given group
84 // The group is only deleted when it has no users
85 function delete_group ($db, $groupid) {
86
87 // check if a groupname is entered
88 if (!$groupid)
89 return false;
90
91 $query = "SELECT name FROM groups WHERE id='$groupid'";
92 $r = $db->Execute($query);
93
94 // just in case, test if a result has been found
95 if ($r->EOF)
96 echo "Group does not exists; Nothing to delete.";
97 else {
98 // get groupname from database result
99 $groupname = $r->fields["name"];
100
101 // test if group has no more users
102 $users_query = "SELECT id FROM users WHERE groupid='$groupid'";
103 $r2 = $db->Execute($users_query);
104 if (!$r2->EOF) {
105 echo "Group <b>$groupname</b> can not be removed since it still has users!!<br>";
106 echo "First delete all users, then remove the group.<br>";
107 }
108 else {
109 // remove given group entry from database
110 $db_remove_query = "DELETE FROM groups WHERE id=$groupid";
111 if ($db->Execute($db_remove_query))
112 echo "Group <b>$groupname</b> has been deleted.";
113 else
114 echo "Failed to remove group <b>$groupname</b>.";
115 }
116 }
117 }
118
119
120 ////
121 // !Displays form to modify or add a group
122 // if groupid is given the groupname will be modified, otherwise add a new group
123 function group_form ($groupid, $groupname) {
124 global $PHP_SELF;
125 ?>
126 <form method='post' action='<?php echo $PHP_SELF ?>?<?=SID?>'>
127 <?php
128 if ($groupid)
129 echo "<input type='hidden' name='groupid' value='$groupid'>\n";
130 echo "<table align='center'>\n";
131 echo "<tr><td>New Group Name:</td>\n";
132 echo "<td><input type='text' name='groupname' value='$groupname'></td></tr>\n";
133 echo "<tr><td colspan=2 align='center'>";
134 if ($groupid)
135 echo "<input type='submit' name='submit' value='Modify Group'>";
136 else
137 echo "<input type='submit' name='submit' value='Add Group'>";
138 echo "</td></tr>\n";
139 echo "</table>\n";
140 echo "</form>\n";
141
142 }
143 /****************************************************************/
144
145 printheader("Groups of PhpLabware");
146 navbar($USER["permissions"]);
147
148 // when the 'Add a new Group' button has been chosen:
149 if ($add)
150 group_form ("","");
151
152 // when modify has been pressed:
153 elseif ($mod == "true")
154 group_form ($groupid, $groupname);
155
156 else {
157 // print header of table
158 echo "<table border=\"1\" align=center >\n";
159 echo "<caption>\n";
160 // first handle addition of a new group
161 if ($submit == "Add Group") {
162 if ($test = add_new_group ($db, $groupname) ) {
163 echo "</caption>\n</table>\n";
164 echo "<table align='center'><caption>$test</caption></table>";
165 group_form ("",$groupname);
166 printfooter ();
167 exit;
168 }
169 }
170 // then look whether groupname should be modified
171 elseif ($submit =="Modify Group") {
172 if ($test = modify_group ($db,$groupid, $groupname)) {
173 echo "</caption>\n</table>\n";
174 echo "<table align='center'><caption>$test</caption></table>";
175 group_form ($groupid, $groupname);
176 printfooter ();
177 exit;
178 }
179 }
180 //determine wether or not the remove-command is given and act on it
181 elseif ($HTTP_POST_VARS) {
182 while((list($key, $val) = each($HTTP_POST_VARS))) {
183 if (substr($key, 0, 3) == "del") {
184 $delarray = explode("_", $key);
185 delete_group($db, $delarray[1]);
186 }
187 if (substr($key, 0, 3) == "mod") {
188 $modarray = explode("_", $key);
189 echo "</caption>\n</table>\n";
190 group_form($modarray[1],get_cell($db,"groups","name","id",$modarray[1]) );
191 printfooter();
192 exit();
193 }
194 }
195 }
196
197 echo "</caption>\n";
198 // print form needed for 'delete' buttons
199 ?>
200 <form name='form' method='post' action='<?php echo $PHP_SELF?>?<?=SID?>'>
201 <?php
202 echo "<tr>\n";
203 echo "<th>Group</th>";
204 echo "<th>Admins</th>";
205 echo "<th>Users</th>";
206 echo "<th colspan=\"2\">Action</th>\n";
207 echo "</tr>\n";
208
209 // retrieve all groups and their info from database
210 $query = "SELECT * FROM groups ORDER BY name";
211 $r=$db->Execute($query);
212 // print all group admins per group in table cells
213 while (!($r->EOF)) {
214
215 // get results of each row
216 $groupid = $r->fields["id"];
217 $groupname = $r->fields["name"];
218 $adminid = $r->fields["adminid"];
219
220 // print start of row of selected group
221 echo "<tr>\n";
222 echo "<td>$groupname</td>\n";
223
224 // get names of admins belonging to selected group
225 $query2="SELECT firstname,lastname,id,login FROM users WHERE groupid='$groupid'
226 AND (permissions >= $ADMIN)";
227 $r2=$db->Execute($query2);
228 // if number of rows greater than zero then print found results
229 if (!$r2->EOF) {
230 echo "<td>";
231 while (!$r2->EOF) {
232 $username = $r2->fields["firstname"]." ".$r2->fields["lastname"];
233 if ($username==" ")
234 $username=$r2->fields["login"];
235 echo "<b>".$username."</b><br>";
236 $r2->MoveNext();
237 }
238 echo "</td>\n";
239 }
240 else
241 echo "<td> </td>\n";
242
243 // get names of users belonging to selected group
244 $query2="SELECT firstname,lastname,id,login FROM users WHERE groupid='$groupid'
245 AND (permissions < $ADMIN)";
246 $r2=$db->Execute($query2);
247 // if number of rows greater than zero then print found results
248 if (!$r2->EOF) {
249 echo "<td>";
250 while (!$r2->EOF) {
251 $username = $r2->fields["firstname"]." ".$r2->fields["lastname"];
252 if ($username==" ")
253 $username=$r2->fields["login"];
254 echo "<b>".$username."</b><br>";
255 $r2->MoveNext();
256 }
257 echo "</td>\n";
258 }
259 else
260 echo "<td> </td>\n";
261
262
263 // print last columns with links to adjust group
264 $modstring = "<input type=\"submit\" name=\"mod_" . $groupid . "\" value=\"Modify\">";
265 echo "<td align='center'>$modstring</td>\n";
266 $delstring = "<input type=\"submit\" name=\"del_" . $groupid . "\" value=\"Remove\" ";
267 $delstring .= "Onclick=\"if(confirm('Are you sure the group $groupname ";
268 $delstring .= "should be removed?')){return true;}return false;\">";
269 echo "<td align='center'>$delstring</td>\n";
270 echo "</tr>\n";
271
272 $r->MoveNext();
273 }
274
275 // print footer of table
276 echo "<tr><td colspan=5 align='center'>";
277 echo "<input type=\"submit\" name=\"add\" value=\"Add Group\">";
278 echo "</td></tr>";
279 echo "</table>\n";
280 echo "</form>\n";
281
282 }
283
284 printfooter();
285
286 ?>