forked from opendocman/opendocman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
department.php
562 lines (503 loc) · 22.2 KB
/
department.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
<?php
use Aura\Html\Escaper as e;
/*
department.php - Administer Departments
Copyright (C) 2002-2011 Stephen Lawrence Jr.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
// check for valid session
session_start();
// includes
include('odm-load.php');
if (!isset($_SESSION['uid'])) {
redirect_visitor();
}
$last_message = (isset($_REQUEST['last_message']) ? $_REQUEST['last_message'] : '');
// Make sure user is admin
$user_obj = new User($_SESSION['uid'], $pdo);
//If the user is not an admin and he/she is trying to access other account that
// is not his, error out.
if (!$user_obj->isAdmin() == true) {
header('Location:error.php?ec=4');
exit;
}
/*
Add A New Department
*/
if (isset($_GET['submit']) && $_GET['submit']=='add') {
draw_header(msg('area_add_new_department'), $last_message);
?>
<form id="addDepartmentForm" action="department.php" method="POST" enctype="multipart/form-data">
<table border="0" cellspacing="5" cellpadding="5">
<tr>
<td>
<b><?php echo msg('department')?></b>
</td>
<td colspan="3">
<input name="department" type="text" class="required" minlength="2">
<?php
// Call the plugin API
callPluginMethod('onDepartmentAddForm');
?>
</td>
<td align="center">
<input type="hidden" name="submit" value="Add Department">
<div class="buttons">
<button class="positive" type="submit" name="submit" value="Add Department"><?php echo msg('button_add_department')?></button>
</div>
</td>
<td align="center">
<div class="buttons">
<button class="negative cancel" type="submit" name="submit" value="Cancel"><?php echo msg('button_cancel')?></button>
</div>
</td>
</tr>
</table>
</form>
<script>
$(document).ready(function(){
$('#addDepartmentForm').validate();
});
</script>
<?php
draw_footer();
} elseif (isset($_POST['submit']) && 'Add Department' == $_POST['submit']) {
//Add Departments
//
// Make sure they are an admin
if (!$user_obj->isAdmin()) {
header('Location:error.php?ec=4');
exit;
}
$department = (isset($_POST['department']) ? $_POST['department'] : '');
if ($department == '') {
$last_message=msg('departmentpage_department_name_required');
header('Location: admin.php?last_message=' . urlencode($last_message));
exit;
}
//Check to see if this department is already in DB
$query = "SELECT name FROM {$GLOBALS['CONFIG']['db_prefix']}department where name = :department";
$stmt = $pdo->prepare($query);
$stmt->execute(array(':department' => $department));
$result = $stmt->fetchAll();
if ($stmt->rowCount() != 0) {
header('Location: error.php?ec=3&message=' . urlencode($department) . ' already exist in the database');
exit;
}
$query = "INSERT INTO {$GLOBALS['CONFIG']['db_prefix']}department (name) VALUES (:department)";
$stmt = $pdo->prepare($query);
$stmt->execute(array(':department' => $department));
// back to main page
$last_message = urlencode(msg('message_department_successfully_added'));
/////////Give New Department data's default rights///////////
////Get all default rights////
$query = "SELECT id, default_rights FROM {$GLOBALS['CONFIG']['db_prefix']}data";
$stmt = $pdo->prepare($query);
$result = $stmt->fetchAll();
$num_rows = $stmt->rowCount();
$data_array = array();
$index = 0;
foreach ($result as $row) {
$data_array[$index][0] = $row[0];
$data_array[$index][1] = $row[1];
$index++;
}
//////Get the new department's id////////////
$query = "SELECT id FROM {$GLOBALS['CONFIG']['db_prefix']}department WHERE name = :department";
$stmt = $pdo->prepare($query);
$stmt->execute(array(':department' => $department));
$result = $stmt->fetchAll();
$num_rows = $stmt->rowCount();
if ($num_rows != 1) {
header('Location: error.php?ec=14&message=' . urlencode('unable to identify ' . $department));
exit;
}
$newly_added_dept_id = $result[0]['id'];
////Set default rights into department//////
$num_rows = sizeof($data_array);
for ($index = 0; $index < $num_rows; $index++) {
$query = "
INSERT INTO {$GLOBALS['CONFIG']['db_prefix']}dept_perms
(
fid,
dept_id,
rights
) values(
:index0,
:newly_added_dept_id,
:index1
)";
$stmt = $pdo->prepare($query);
$stmt->execute(array(
':index0' => $data_array[$index][0],
':newly_added_dept_id' => $newly_added_dept_id,
':index1' => $data_array[$index][1]
));
}
// Call the plugin API
callPluginMethod('onDepartmentAddSave', $result['id']);
header('Location: admin.php?last_message=' . urlencode($last_message));
} elseif (isset($_POST['submit']) && $_POST['submit'] == 'Show Department') {
// query to show item
draw_header(msg('area_department_information'), $last_message);
//select name
$query = "SELECT name,id FROM {$GLOBALS['CONFIG']['db_prefix']}department where id = :item";
$stmt = $pdo->prepare($query);
;
$stmt->execute(array(':item' => $_POST['item']));
$result = $stmt->fetch();
echo '<table name="main" cellspacing="15" border="0">';
echo '<th>ID</th><th>' . msg('department') . '</th>';
echo '<tr><td>' . e::h($result['id']) . '</td>';
echo '<td>' . e::h($result['name']) . '</td></tr>';
?>
<tr>
<td align="center" colspan="2"><b><?php echo msg('label_users_in_department')?></b></td>
</tr>
<?php
// Display all users assigned to this department
$query = "
SELECT
dept.id,
u.first_name,
u.last_name
FROM
{$GLOBALS['CONFIG']['db_prefix']}department dept,
{$GLOBALS['CONFIG']['db_prefix']}user u
WHERE
dept.id = :item
AND
u.department = :item
";
$stmt = $pdo->prepare($query);
$stmt->execute(array(':item' => $_POST['item']));
$result = $stmt->fetchAll();
foreach ($result as $row) {
echo '<tr><td colspan="2">' . e::h($row['first_name']) . ' ' . e::h($row['last_name']) . '</td></tr>';
}
?>
<form action="admin.php" method="POST" enctype="multipart/form-data">
<tr>
<td colspan="4" align="center"><div class="buttons"><button class="regular" type="Submit" name="" value="Back"><?php echo msg('button_back')?></button></div></td>
</tr>
</table>
</form>
<?php
draw_footer();
} elseif (isset($_REQUEST['submit']) && $_REQUEST['submit'] == 'showpick') {
draw_header(msg('area_choose_department'), $last_message);
$showpick='';
?>
<table border="0" cellspacing="5" cellpadding="5">
<form action="department.php" method="POST" enctype="multipart/form-data">
<tr>
<input type="hidden" name="state" value="<?php echo(e::h($_GET['state']+1));
?>">
<td><b><?php echo msg('department')?></b></td>
<td colspan=3><select name="item">
<?php
$query = "SELECT id, name FROM {$GLOBALS['CONFIG']['db_prefix']}department ORDER BY name";
$stmt = $pdo->prepare($query);
$stmt->execute();
$result = $stmt->fetchAll();
foreach ($result as $row) {
echo '<option value="' . e::h($row['id']) . '">' . e::h($row['name']) . '</option>';
}
?>
</select></td>
<td colspan="" align="center">
<div class="buttons">
<button class="positive" type="submit" name="submit" value="Show Department"><?php echo msg('button_view_department')?></button>
</div>
</td>
<td>
<div class="buttons">
<button class="negative" type="Submit" name="submit" value="Cancel"><?php echo msg('button_cancel')?></button>
</div>
</td>
</td>
</tr>
</table>
</form>
<?php
draw_footer();
} elseif (isset($_REQUEST['submit']) && $_REQUEST['submit'] == 'delete') {
draw_header(msg('department') . ': ' . msg('label_delete'), $last_message);
$delete='';
// Pull up a list of departments excluding the one being deleted
$reassign_list_query = "SELECT id, name FROM {$GLOBALS['CONFIG']['db_prefix']}department WHERE id != :item ORDER BY name";
$stmt = $pdo->prepare($reassign_list_query);
$stmt->execute(array(':item' => $_REQUEST['item']));
$reassign_list_query_result = $stmt->fetchAll();
// If the above statement returns less than 1 row they will need to create another category to re-assign to so display error
if ($stmt->rowCount() < 1) {
echo msg('message_need_one_department');
exit;
}
// query to show item
echo ' <form action="department.php" method="POST" enctype="multipart/form-data">';
echo '<table border=0>';
$query = "SELECT id, name FROM {$GLOBALS['CONFIG']['db_prefix']}department where id = :item";
$stmt = $pdo->prepare($query);
$stmt->execute(array(':item' => $_REQUEST['item']));
$result = $stmt->fetchAll();
foreach ($result as $row) {
echo '<tr><td>' .msg('label_id'). ' # :</td><td>' . e::h($row['id']) . '</td></tr>';
echo '<tr><td>'.msg('label_name').' :</td><td>' . e::h($row['name']) . '</td></tr>';
?>
<input type="hidden" name="id" value="<?php echo (int) $_REQUEST['item'];
?>">
<tr>
<td>
<?php echo msg('label_reassign_to');
?>:
</td>
<td>
<select name="assigned_id">
<?php
foreach ($reassign_list_query_result as $row) {
echo '<option value="' . e::h($row['id']) . '">' . e::h($row['name']) . '</option>';
}
?>
</select>
</td>
</tr>
<tr>
<td valign="top"><?php echo msg('message_are_you_sure_remove')?></td>
<td align="center">
<div class="buttons">
<button class="positive" type="submit" name="deletedepartment" value="Yes"><?php echo msg('button_yes')?></button>
</div>
<div class="buttons">
<button class="negative" type="submit" name="submit" value="Cancel"><?php echo msg('button_cancel')?></button>
</div>
</td>
</tr>
</table>
</form>
<?php
}
draw_footer();
} elseif (isset($_REQUEST['submit']) && $_REQUEST['submit'] == 'deletepick') {
draw_header(msg('department') . ': ' . msg('label_delete'), $last_message);
?>
<table border="0" cellspacing="5" cellpadding="5">
<form action="department.php" method="POST" enctype="multipart/form-data">
<input type="hidden" name="state" value="<?php echo(e::h($_REQUEST['state']+1));
?>">
<tr>
<td><b><?php echo msg('department')?></b></td>
<td colspan=3><select name="item">
<?php
$query = "SELECT id, name FROM {$GLOBALS['CONFIG']['db_prefix']}department ORDER BY name";
$stmt = $pdo->prepare($query);
$stmt->execute();
$result = $stmt->fetchAll();
foreach ($result as $row) {
$str = '<option value="' . e::h($row['id']) . '"';
$str .= '>' . e::h($row['name']) . '</option>';
echo $str;
}
$deletepick='';
?>
</select></td>
<td></td>
<td align="center">
<div class="buttons">
<button class="positive" type="submit" name="submit" value="delete"><?php echo msg('button_delete')?></button>
</div>
</td>
<td>
<div class="buttons">
<button class="negative cancel" type="submit" name="submit" value="Cancel"><?php echo msg('button_cancel')?></button>
</div>
</td>
</tr>
</form>
</table>
<?php
draw_footer();
} elseif (isset($_REQUEST['deletedepartment'])) {
// Make sure they are an admin
if (!$user_obj->isAdmin()) {
header('Location: error.php?ec=4');
exit;
}
// Set all old dept_id's to the new re-assigned dept_id or remove the old dept_id
// Update entries in data table
$query = "UPDATE {$GLOBALS['CONFIG']['db_prefix']}data SET department = :assigned_id WHERE department = :id";
$stmt = $pdo->prepare($query);
$stmt->execute(array(
':assigned_id' => $_REQUEST['assigned_id'],
':id' => $_REQUEST['id']
));
// Update entries in user
$query = "UPDATE {$GLOBALS['CONFIG']['db_prefix']}user SET department = :assigned_id WHERE department = :id";
$stmt = $pdo->prepare($query);
$stmt->execute(array(
':assigned_id' => $_REQUEST['assigned_id'],
':id' => $_REQUEST['id']
));
// Update entries in dept perms
$query = "UPDATE {$GLOBALS['CONFIG']['db_prefix']}dept_perms SET dept_id = :assigned_id WHERE dept_id = :id";
$stmt = $pdo->prepare($query);
$stmt->execute(array(
':assigned_id' => $_REQUEST['assigned_id'],
':id' => $_REQUEST['id']
));
// Update entries in dept_reviewer
$query = "UPDATE {$GLOBALS['CONFIG']['db_prefix']}dept_reviewer SET dept_id = :assigned_id WHERE dept_id = :id";
$stmt = $pdo->prepare($query);
$stmt->execute(array(
':assigned_id' => $_REQUEST['assigned_id'],
':id' => $_REQUEST['id']
));
// Delete from department
$query = "DELETE FROM {$GLOBALS['CONFIG']['db_prefix']}department where id = :id";
$stmt = $pdo->prepare($query);
$stmt->execute(array(':id' => $_REQUEST['id']));
// back to main page
$last_message = msg('message_all_actions_successfull') . ' id:' . (int) $_REQUEST['id'];
header('Location: admin.php?last_message=' . urlencode($last_message));
} elseif (isset($_REQUEST['submit']) && $_REQUEST['submit'] == 'modify') {
$dept_obj = new Department($_REQUEST['item'], $pdo);
draw_header(msg('area_update_department') .': ' . $dept_obj->getName(), $last_message);
?>
<form action="department.php" id="modifyDeptForm" method="POST" enctype="multipart/form-data">
<table border="0" cellspacing="5" cellpadding="5">
<tr>
<?php
$query = "SELECT id, name FROM {$GLOBALS['CONFIG']['db_prefix']}department where id = :item";
$stmt = $pdo->prepare($query);
$stmt->execute(array(':item' => $_REQUEST['item']));
$result = $stmt->fetchAll();
foreach ($result as $row) {
?>
<td>
<b><?php echo msg('department')?></b>
</td>
<td colspan="3">
<input type="textbox" name="name" value="<?php echo e::h($row['name']);
?>" class="required" maxlength="40">
<input type="hidden" name="id" value="<?php echo e::h($row['id']);
?>">
<?php
// Call the plugin API
callPluginMethod('onDepartmentEditForm', $row['id']);
?>
</td>
<?php
}
?>
</tr>
<tr>
<td align="center">
<div class="buttons">
<button class="positive" type="Submit" name="submit" value="Update Department"><?php echo msg('button_save')?></button>
</div>
</td>
<td align="center">
<div class="buttons">
<button class="negative cancel" type="Submit" name="submit" value="Cancel"><?php echo msg('button_cancel')?></button>
</div>
</td>
</tr>
</table>
</form>
<script>
$(document).ready(function(){
$('#modifyDeptForm').validate();
});
</script>
<?php
draw_footer();
} elseif (isset($_REQUEST['submit']) && $_REQUEST['submit'] == 'updatepick') {
draw_header(msg('area_choose_department'), $last_message);
?>
<form action="department.php" method="GET" enctype="multipart/form-data">
<INPUT type="hidden" name="state" value="<?php echo(e::h($_REQUEST['state']+1));
?>">
<table border="0" cellspacing="5" cellpadding="5">
<tr>
<td><b><?php echo msg('label_department_to_modify')?>:</b></td>
<td colspan="3"><select name="item">
<?php
// query to get a list of departments
$query = "SELECT id, name FROM {$GLOBALS['CONFIG']['db_prefix']}department ORDER BY name";
$stmt = $pdo->prepare($query);
$stmt->execute();
$result = $stmt->fetchAll();
foreach ($result as $row) {
echo '<option value="' . e::h($row['id']) . '">' . e::h($row['name']) . '</option>';
}
?>
</td>
<td>
<div class="buttons">
<button class="positive" type="submit" name="submit" value="modify"><?php echo msg('button_modify_department')?></button>
</div>
</td>
<td >
<div class="buttons">
<button class="negative" type="Submit" name="submit" value="Cancel"><?php echo msg('button_cancel')?></button>
</div>
</td>
</tr>
</table>
</form>
<?php
draw_footer();
} elseif (isset($_POST['submit']) && 'Update Department' == $_POST['submit']) {
// UPDATE Department
//
//
// Make sure they are an admin
if (!$user_obj->isAdmin()) {
header('Location: error.php?ec=4');
exit;
}
$name = (isset($_POST['name']) ? $_POST['name'] : '');
if ($name == '') {
$last_message=msg('departmentpage_department_name_required');
header('Location: admin.php?last_message=' . urlencode($last_message));
exit;
}
//Check to see if this department is already in DB
$query = "SELECT name FROM {$GLOBALS['CONFIG']['db_prefix']}department WHERE name = :name AND id != :id ";
$stmt = $pdo->prepare($query);
$stmt->execute(array(
':id' => $_POST['id'],
':name' => $_POST['name']
));
$result = $stmt->fetchAll();
if ($stmt->rowCount() != 0) {
header('Location: error.php?ec=3&last_message=' . urlencode($_POST['name'] . ' already exist in the database'));
exit;
}
$query = "UPDATE {$GLOBALS['CONFIG']['db_prefix']}department SET name = :name WHERE id = :id";
$stmt = $pdo->prepare($query);
;
$stmt->execute(array(
':id' => $_POST['id'],
':name' => $_POST['name']
));
// back to main page
$last_message = urlencode(msg('message_department_successfully_updated') . ' - ' . htmlentities($name) . '- id=' . (int) $_POST['id']);
// Call the plugin API
callPluginMethod('onDepartmentModifySave', $_REQUEST);
header('Location: admin.php?last_message=' . urlencode($last_message));
} elseif (isset($_REQUEST['submit']) and $_REQUEST['submit'] == 'Cancel') {
header('Location: admin.php?last_message=' . urlencode(msg('message_action_cancelled')));
} else {
header('Location: admin.php?last_message="' . urlencode(msg('message_nothing_to_do')));
}