Remove includes/class_api_error.php and all of the places we require() it
[bugdar.git] / admin / user.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar
5 || # Copyright ©2002-2007 Blue Static
6 || #
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version 2 of the License.
10 || #
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 || # more details.
15 || #
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
21
22 require_once('./global.php');
23 require_once('./includes/api_user.php');
24 require_once('./includes/class_sort.php');
25
26 APIError(array(new API_Error_Handler($admin), 'admin_error'));
27
28 NavLinks::usersPages();
29 $navigator->set_focus('tab', 'users', null);
30
31 if (!can_perform('canadminusers'))
32 {
33 admin_login();
34 }
35
36 // ###################################################################
37 /**
38 * Generate an inline checkbox
39 *
40 * @access public
41 *
42 * @param string Name
43 * @param bool Checked?
44 *
45 * @return string HTML checkbxo
46 */
47 function draw_checkbox($name, $checked)
48 {
49 return "<input type=\"checkbox\" class=\"button\" name=\"{$name}\" value=\"1\"" . ($checked == true ? ' checked="checked"' : '') . " />";
50 }
51
52 // ###################################################################
53
54 if (empty($_REQUEST['do']))
55 {
56 $_REQUEST['do'] = 'modify';
57 }
58
59 // ###################################################################
60
61 if ($_REQUEST['do'] == 'kill')
62 {
63 $user = new UserAPI();
64 $user->set('userid', $input->in['userid']);
65 $user->set_condition();
66 $user->delete();
67
68 $admin->redirect('user.php');
69 }
70
71 // ###################################################################
72
73 if ($_REQUEST['do'] == 'delete')
74 {
75 $admin->page_confirm(T('Are you sure you want to delete this user?'), 'user.php', 'kill', array('userid' => $input->inputClean('userid', TYPE_UINT)));
76 }
77
78 // ###################################################################
79
80 if ($_POST['do'] == 'insert')
81 {
82 $user = new UserAPI();
83 $user->set('displayname', $input->in['displayname']);
84 $user->set('email', $input->in['email']);
85 $user->set('showemail', $input->in['showemail']);
86 $user->set('showcolors', $input->in['showcolors']);
87 $user->set('usergroupid', $input->in['usergroupid']);
88 $user->set('groupids', $input->in['groupids']);
89 $user->set('languageid', $input->in['languageid']);
90 $user->set('timezone', $input->in['timezone']);
91 $user->set('usedst', $input->in['usedst']);
92 $user->set('password', $input->in['password']);
93 $user->set('hidestatuses', $input->in['hidestatuses']);
94 $user->set('defaultsortkey', $input->in['defaultsortkey']);
95 $user->set('defaultsortas', $input->in['defaultsortas']);
96 $user->insert();
97
98 // post_insert will set the email options... so we now have to dump them
99 $db->query("DELETE FROM " . TABLE_PREFIX . "useremail WHERE userid = " . $user->insertid);
100
101 $input->inputClean('emailopts', TYPE_INT);
102 if (is_array($input->in['emailopts']))
103 {
104 foreach ($input->in['emailopts'] AS $relation => $bitarr)
105 {
106 $bitmask = 0;
107 foreach ($bitarr AS $option => $yes)
108 {
109 $bitmask += $option * $yes;
110 }
111 $db->query("INSERT INTO " . TABLE_PREFIX . "useremail (userid, relation, mask) VALUES (" . $user->insertid . ", $relation, $bitmask)");
112 }
113 }
114
115 $admin->redirect('user.php?do=edit&userid=' . $user->insertid);
116 }
117
118 // ###################################################################
119
120 if ($_POST['do'] == 'update')
121 {
122 $user = new UserAPI();
123 $user->set('userid', $input->in['userid']);
124 $user->set_condition();
125 $user->set('displayname', $input->in['displayname']);
126 $user->set('email', $input->in['email']);
127 $user->set('showemail', $input->in['showemail']);
128 $user->set('showcolors', $input->in['showcolors']);
129 $user->set('usergroupid', $input->in['usergroupid']);
130 $user->set('groupids', $input->in['groupids']);
131 $user->set('languageid', $input->in['languageid']);
132 $user->set('timezone', $input->in['timezone']);
133 $user->set('usedst', $input->in['usedst']);
134 $user->set('password', $input->in['password']);
135 $user->set('hidestatuses', $input->in['hidestatuses']);
136 $user->set('defaultsortkey', $input->in['defaultsortkey']);
137 $user->set('defaultsortas', $input->in['defaultsortas']);
138 $user->update();
139
140 $db->query("DELETE FROM " . TABLE_PREFIX . "useremail WHERE userid = " . $user->values['userid']);
141 $input->inputClean('emailopts', TYPE_INT);
142 if (is_array($input->in['emailopts']))
143 {
144 foreach ($input->in['emailopts'] AS $relation => $bitarr)
145 {
146 $bitmask = 0;
147 foreach ($bitarr AS $option => $yes)
148 {
149 $bitmask += $option * $yes;
150 }
151 $db->query("INSERT INTO " . TABLE_PREFIX . "useremail (userid, relation, mask) VALUES (" . $user->values['userid'] . ", $relation, $bitmask)");
152 }
153 }
154
155 $admin->redirect('user.php?do=edit&userid=' . $user->record['userid']);
156 }
157
158 // ###################################################################
159
160 if ($_REQUEST['do'] == 'edit' OR $_REQUEST['do'] == 'add')
161 {
162 $add = ($_REQUEST['do'] == 'add');
163 $edit = (!$add);
164
165 if ($edit)
166 {
167 NavLinks::usersEdit($input->in['userid']);
168 $navigator->set_focus('link', 'users-pages-users', 'users-pages');
169
170 $user = new UserAPI();
171 $user->set('userid', $input->in['userid']);
172 $user->set_condition();
173 $user->fetch();
174 }
175 else
176 {
177 NavLinks::usersAdd();
178 $navigator->set_focus('link', 'users-add', 'users');
179 }
180
181 $admin->page_start(($add ? T('Add User') : T('Edit User')));
182
183 $admin->form_start('user.php', ($add ? 'insert' : 'update'));
184
185 if ($edit)
186 {
187 $admin->form_hidden_field('userid', $user->record['userid']);
188 }
189
190 $admin->table_start();
191
192 $admin->table_head(($add ? T('Add User') : sprintf(T('Edit User (userid: %1$s)'), $user->record['userid'])));
193
194 $admin->row_input(T('Display Name'), 'displayname', $user->record['displayname']);
195 $admin->row_input(T('Email'), 'email', $user->record['email']);
196 $admin->row_input(($add ? T('Password') : T('Password (Leave blank for no change)')), 'password');
197
198 foreach (bugdar::$datastore['usergroup'] AS $group)
199 {
200 $admin->list_item($group['title'], $group['usergroupid'], ($user->record['usergroupid'] == $group['usergroupid']));
201 }
202 $admin->row_list(T('Primary Usergroup'), 'usergroupid');
203
204 $ids = explode(',', $user->record['groupids']);
205 foreach (bugdar::$datastore['usergroup'] AS $id => $group)
206 {
207 if ($id == $user->record['usergroupid'])
208 {
209 continue;
210 }
211 $admin->list_item($group['title'], $group['usergroupid'], in_array($id, $ids));
212 }
213 $admin->row_checkbox(T('Secondary Usergroups'), 'groupids');
214
215 $admin->row_yesno(T('Show Email Publicly'), 'showemail', $user->record['showemail']);
216 $admin->row_yesno(T('Show Status Colors on Bug Listings'), 'showcolors', $user->record['showcolors']);
217
218 foreach (bugdar::$datastore['language'] AS $language)
219 {
220 $admin->list_item($language['title'], $language['languageid'], ($user->record['languageid'] == $language['languageid']));
221 }
222 $admin->row_list(T('Language'), 'languageid');
223
224 foreach ($datef->fetch_timezone_list() AS $value => $string)
225 {
226 $admin->list_item($string, $value, ($user->record['timezone'] == $value));
227 }
228 $admin->row_list(T('Timezone'), 'timezone');
229
230 $admin->row_yesno(T('Observe Daylight Savings Time (DST)'), 'usedst', $user->record['usedst']);
231 $admin->row_text(T('Hidden Statuses on Bug Listing'), construct_option_select('hidestatuses', bugdar::$datastore['status'], $user->record['hidestatuses'], 'statusid', 'status', 0, true));
232 $admin->row_text(T('Default Sort Order Column'), construct_option_select('defaultsortkey', ListSorter::fetch_by_text(false), $user->record['defaultsortkey']));
233 $admin->row_text(T('Default Sort Order Direction'), construct_option_select('defaultsortas', ListSorter::fetch_as_text(false), $user->record['defaultsortas']));
234
235 $admin->table_end();
236
237 // -------------------------------------------------------------------
238 $admin->table_start(false);
239 $admin->table_head(T('Email Options'), 6);
240
241 if ($user->record['userid'])
242 {
243 $options = $db->query("SELECT * FROM " . TABLE_PREFIX . "useremail WHERE userid = " . $user->record['userid']);
244 foreach ($options as $opt)
245 {
246 foreach ($bugsys->emailoptions['notifications'] AS $name => $notif)
247 {
248 foreach ($bugsys->emailoptions['relations'] AS $name => $relation)
249 {
250 if ($opt['mask'] & $notif AND $opt['relation'] == $relation)
251 {
252 $checked["$relation"]["$notif"] = HTML_CHECKED;
253 }
254 }
255 }
256 }
257 }
258
259 $admin->table_column_head(array('', T('Reporter'), T('Assignee'), T('Favorite'), T('Voter'), T('Commenter')));
260
261 // -------------------------------------------------------------------
262
263 $admin->row_text(T('New bug is added'), '<div style="text-align: center">' . draw_checkbox('emailopts[0][2048]', $checked[0][2048]) . '</div>', 'top', 6);
264
265 $admin->row_text(T('I am made the assignee'), '<div style="text-align: center">' . draw_checkbox('emailopts[0][32]', $checked[0][32]) . '</div>', 'top', 6);
266
267 $admin->row_multi_item(array(
268 T('Status or resolution changes') => 'l',
269 draw_checkbox('emailopts[1][64]', $checked[1][64]) => 'c',
270 draw_checkbox('emailopts[2][64]', $checked[2][64]) => 'c',
271 draw_checkbox('emailopts[4][64]', $checked[4][64]) => 'c',
272 draw_checkbox('emailopts[8][64]', $checked[8][64]) => 'c',
273 draw_checkbox('emailopts[16][64]', $checked[16][64]) => 'c',
274 ));
275
276 $admin->row_multi_item(array(
277 T("'Duplicates' field is changed") => 'l',
278 draw_checkbox('emailopts[1][128]', $checked[1][128]) => 'c',
279 draw_checkbox('emailopts[2][128]', $checked[2][128]) => 'c',
280 draw_checkbox('emailopts[4][128]', $checked[4][128]) => 'c',
281 draw_checkbox('emailopts[8][128]', $checked[8][128]) => 'c',
282 draw_checkbox('emailopts[16][128]', $checked[16][128]) => 'c',
283 ));
284
285 $admin->row_multi_item(array(
286 T('A new comment is added') => 'l',
287 draw_checkbox('emailopts[1][256]', $checked[1][256]) => 'c',
288 draw_checkbox('emailopts[2][256]', $checked[2][256]) => 'c',
289 draw_checkbox('emailopts[4][256]', $checked[4][256]) => 'c',
290 draw_checkbox('emailopts[8][256]', $checked[8][256]) => 'c',
291 draw_checkbox('emailopts[16][256]', $checked[16][256]) => 'c',
292 ));
293
294 $admin->row_multi_item(array(
295 T('A new attachment is added') => 'l',
296 draw_checkbox('emailopts[1][512]', $checked[1][512]) => 'c',
297 draw_checkbox('emailopts[2][512]', $checked[2][512]) => 'c',
298 draw_checkbox('emailopts[4][512]', $checked[4][512]) => 'c',
299 draw_checkbox('emailopts[8][512]', $checked[8][512]) => 'c',
300 draw_checkbox('emailopts[16][512]', $checked[16][512]) => 'c',
301 ));
302
303 $admin->row_multi_item(array(
304 T('Any other field changes') => 'l',
305 draw_checkbox('emailopts[1][1024]', $checked[1][1024]) => 'c',
306 draw_checkbox('emailopts[2][1024]', $checked[2][1024]) => 'c',
307 draw_checkbox('emailopts[4][1024]', $checked[4][1024]) => 'c',
308 draw_checkbox('emailopts[8][1024]', $checked[8][1024]) => 'c',
309 draw_checkbox('emailopts[16][1024]', $checked[16][1024]) => 'c',
310 ));
311
312 // -------------------------------------------------------------------
313 $admin->row_submit(null, ':save:', ':reset:', 6);
314
315 $admin->table_end();
316 $admin->form_end();
317
318 $admin->page_end();
319 }
320
321 // ###################################################################
322
323 if ($_REQUEST['do'] == 'search')
324 {
325 $fail = false;
326
327 NavLinks::usersAdd();
328 $navigator->set_focus('link', 'users-pages-users', 'users-pages');
329
330 if (is_numeric($input->in['userdata']))
331 {
332 $input->inputClean('userdata', TYPE_UINT);
333 if ($db->queryFirst("SELECT * FROM " . TABLE_PREFIX . "user WHERE userid = " . $input->in['userdata']))
334 {
335 header('Location: user.php?do=edit&userid=' . $input->in['userdata']);
336 }
337 else
338 {
339 $fail = true;
340 }
341 }
342 else
343 {
344 $input->in['userdata'] = str_replace('%', '\%', $input->in['userdata']);
345 $results = $db->query("SELECT * FROM " . TABLE_PREFIX . "user WHERE email LIKE '%" . $bugsys->input_escape('userdata') . "%' OR displayname LIKE '%" . $bugsys->input_escape('userdata') . "%'");
346
347 if ($db->num_rows($results) < 1)
348 {
349 $fail = true;
350 }
351 else
352 {
353 $admin->page_start(T('Search Results'));
354
355 $admin->table_start();
356 $admin->table_head(T('Search Results'), 4);
357 $admin->table_column_head(array(T('Display Name'), T('Email'), T('User ID'), T('Actions')));
358
359 foreach ($results as $row)
360 {
361 $admin->row_multi_item(array(
362 $row['displayname'] => 'l',
363 $row['email'] => 'c',
364 $row['userid'] => 'c',
365 '<a href="user.php?do=edit&amp;userid=' . $row['userid'] . '">[' . T('Edit') . ']</a>' => 'c'
366 ));
367 }
368
369 $admin->table_end();
370
371 $admin->page_end();
372 }
373 }
374
375 if ($fail)
376 {
377 $admin->error(T('Sorry, we could not find any users that matched your criteria.'));
378 }
379 }
380
381 // ###################################################################
382
383 if ($_REQUEST['do'] == 'showall')
384 {
385 NavLinks::usersAdd();
386 $navigator->set_focus('link', 'users-showall', 'users');
387
388 LoadPaginationFramework();
389 $pagination->setBitProcessor('AdminPageNavigatorBitCallback');
390 $pagination->setNavigatorProcessor('AdminPageNavigatorCallback');
391
392 $admin->page_start(T('Show All Users'));
393 $admin->table_start();
394 $admin->table_head(T('Show All Users'), 4);
395 $admin->table_column_head(array(T('Display Name'), T('Email'), T('User ID'), T('Actions')));
396
397 $count = $db->queryFirst("SELECT COUNT(*) AS count FROM " . TABLE_PREFIX . "user");
398 $pagination->setTotal($count['count']);
399 $pagination->splitPages();
400
401 $users = $db->query("SELECT * FROM " . TABLE_PREFIX . "user ORDER BY userid ASC LIMIT " . $pagination->fetchLimit($pagination->getPage() - 1) . ", " . $pagination->getPerPage());
402 foreach ($users as $user)
403 {
404 $admin->row_multi_item(array(
405 $user['displayname'] => 'l',
406 $user['email'] => 'c',
407 $user['userid'] => 'c',
408 '<a href="user.php?do=edit&amp;userid=' . $user['userid'] . '">[' . T('Edit') . ']</a>' => 'c'
409 ));
410 }
411
412 $admin->table_end();
413
414 $admin->page_code($pagination->constructPageNav('user.php?do=showall'));
415
416 $admin->page_end();
417 }
418
419 // ###################################################################
420
421 if ($_REQUEST['do'] == 'modify')
422 {
423 NavLinks::usersAdd();
424 $navigator->set_focus('link', 'users-pages-users', 'users-pages');
425
426 $admin->page_start(T('User Search'));
427
428 $admin->form_start('user.php', 'search');
429 $admin->table_start(true, '45%');
430
431 $admin->table_head(T('User Search'));
432 $admin->row_input(T('Name/Email/ID'), 'userdata');
433
434 $admin->row_submit('', ':save:', '');
435
436 $admin->table_end();
437 $admin->form_end();
438
439 $admin->page_end();
440 }
441
442 /*=====================================================================*\
443 || ###################################################################
444 || # $HeadURL$
445 || # $Id$
446 || ###################################################################
447 \*=====================================================================*/
448 ?>