-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateNewUser.php
More file actions
673 lines (593 loc) · 23.2 KB
/
createNewUser.php
File metadata and controls
673 lines (593 loc) · 23.2 KB
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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
<?php
include "home.php";
$role = $_SESSION['hrrolesession'];
if (strpos($role, "hradmin") === false) {
session_destroy();
exit();
}
?>
<script>
$(document).ready(function() {
$("#myInput").on("keyup", function() {
var value = $(this).val().toLowerCase();
$("#myTable tr").filter(function() {
$(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
});
});
});
</script>
<script>
$(function() {
$("#startDate").datepicker({
appendText: "(yy-mm-dd)",
changeMonth: true,
changeYear: true,
yearRange: "c-0:c+1",
dateFormat: "yy-mm-dd"
});
$("#endDate").datepicker({
appendText: "(yy-mm-dd)",
changeMonth: true,
changeYear: true,
yearRange: "c-0:c+1",
dateFormat: "yy-mm-dd"
});
$(".dateElement").datepicker({
appendText: "(yy-mm-dd)",
changeMonth: true,
changeYear: true,
yearRange: "c-65:c+0",
dateFormat: "yy-mm-dd"
});
});
$("#indiv").click(function() {
$("#impo").toggle();
});
</script>
<script>
$(document).ready(function() {
//$("#impo").hide();
$("#indiv").click(function() {
$("#impo").show();
$("#all").prop("checked", false);
});
$("#all").click(function() {
$("#indiv").prop("checked", false);
$('#impo option:first').prop('selected', true);
$("#impo").hide();
});
$("#days").click(function() {
for (var c = 0, e = 0, d = new Date($("#startDate").val()), a = (new Date($("#endDate").val()) - d) / 864E5; 0 <= a; a--) {
var b = new Date(d);
b.setDate(b.getDate() + a);
1 == Math.ceil(b.getDay() % 6 / 6) ? c++ : e++
}
$("#days").val(c);
$("#days").attr('readonly', 'readonly');
});
});
</script>
<script>
function pass_validation() {
var firstpassword = document.createUserForm.password.value;
var secondpassword = document.createUserForm.passwordConf.value;
if (firstpassword == secondpassword) {
return true;
} else {
alert("password one and two must be same!");
return false;
}
}
</script>
<style type="text/css">
body,
td {
font-size: 10pt;
}
table#tablelist {
background-color: black;
border: 1px black solid;
border-collapse: collapse;
}
th {
border: 1px outset silver;
background-color: grey;
color: white;
}
tr.nostriped {
background-color: white;
margin: 1px;
}
tr.striped {
background-color: whitesmoke;
}
td {
padding: 1px 8px;
}
.tableFixHead {
overflow-y: auto;
height: 100px;
}
.tableFixHead thead th {
position: sticky;
top: 0;
}
</style>
<script type="text/javascript">
$(function() {
$("#tablelist tr:nth-child(even)").addClass("striped");
$("#tablelist tr:nth-child(odd)").addClass("nostriped");
});
$(document).ready(function() {
$('#dtBasicExample').DataTable();
$('.dataTables_length').addClass('bs-select');
});
console.log("____________________")
</script>
<h1>Create new employee<h2>
<br />
<form name="createUserForm" action="createNewUser.php" method="post" onsubmit="return pass_validation()" enctype="multipart/form-data">
<fieldset>
<legend>1.User/Employee Details</legend>
<table>
<tr>
<td>First Name</td>
<td><input type="text" name="firstName"></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" name="lastName"></td>
</tr>
<tr>
<td>Phone Number</td>
<td><input type='text' name='phonenumber'></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td>Position</td>
<td><select name="position">
<option value='Program staff'>Program staff</option>
<option value='Coordinator'>Coordinator</option>
<option value='Manager'>Manager</option>
<option value='Director'>Director</option>
<option value='Senior director'>Senior director</option>
<option value='Chief'>Chief</option>
<option value='Executive Director'>Executive Director</option>
</select></td>
</tr>
<tr>
<td>Detailed Position</td>
<td> <input type="text" name="detaildposition"> </td>
</tr>
<tr>
<td>Site Location</td>
<td><select name="site">
<option value="KIGALI">KIGALI</option>
<option value="ZL">Zanmi Lasante</option>
<option value="BURERA">BURERA</option>
<option value="KAYONZA">KAYONZA</option>
<option value="KIREHE">KIREHE</option>
<option value="CROSS_SITE">CROSS SITE</option>
</select></td>
</tr>
<tr>
<td>Head of Department</td>
<td><select name="headofdepartment">
<option value="" selected="">-- Select Select Head of Department</option> <?php
include("connect.php");
$date = date("Y-m-d H:i:s");
$users = mysqli_query($conn, "select * from users where role like '%head_of_department%' and userid!=1 order by firstname");
if (mysqli_num_rows($users)) {
while ($row = mysqli_fetch_array($users)) {
$userid = $row['userid'];
$firstname = $row['firstname'];
$lastname = $row['lastname'];
print("<option value='$userid'>$firstname $lastname</option>");
}
}
?>
</select></td>
</tr>
<tr>
<td>Office</td>
<td><select name="department">
<?php
include("connect.php");
$dpt = mysqli_query($conn, "select name from hr_dpt_leaders");
if (mysqli_num_rows($dpt)) {
while ($row = mysqli_fetch_array($dpt)) {
$name = $row['name'];
print("<option value='$name'>$name</option>");
}
}
?>
</select>
</td>
</tr>
<tr>
<td>Program</td>
<td><select name='program'>
<?php
include("connect.php");
$program = mysqli_query($conn, "select name from program_leaders order by name");
if (mysqli_num_rows($program)) {
while ($row = mysqli_fetch_array($program)) {
$name = $row['name'];
print("<option value='$name'>$name</option>");
}
}
?>
</select></td>
</tr>
<tr>
<td>Type of contract</td>
<td><select name="typeofcotract">
<option value="Fixed contract">Fixed contract</option>
<option value="Open-ended contract">Open-ended contract</option>
</select></td>
</tr>
<tr>
<td>National ID/Passport</td>
<td><input type="text" name="nationalID"></td>
</tr>
<tr>
<td>Date of Birth</td>
<td><input type="text" name="dateofbirth" class="dateElement"></td>
</tr>
<tr>
<td>Date Employment</td>
<td><input type="text" name="dateofemployment" class="dateElement"></td>
</tr>
<tr>
<td>Gender</td>
<td><select name="gender">
<option value=""></option>
<option value="M">Male</option>
<option value="F">Female</option>
<option value="Other">Other</option>
</select></td>
</tr>
<tr>
<td>Employee Photo</td>
<td><input type="file" name="filetoupload" id="imageupload" onchange="PreviewImage();">
<img id="uploadPreview" style="width: 60px; height: 70px" />
<script type="text/javascript">
function PreviewImage() {
//alert("Innnnnnnnnnnnnnnnnn");
var oFReader = new FileReader();
oFReader.readAsDataURL(document.getElementById("imageupload").files[0]);
oFReader.onload = function(oFREvent) {
document.getElementById("uploadPreview").src = oFREvent.target.result;
};
};
</script>
</td>
</tr>
</table>
</fieldset>
<br />
<fieldset>
<legend>2.Account</legend>
<table>
<tr>
<td>UserName </td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td>Re-enter Password</td>
<td><input type="password" name="passwordConf"></td>
</tr>
<tr>
<td>Privilege</td>
<td><select name="privileges[]" multiple="multiple" size="12">
<option value="employee">Employee</option>
<option value="supervisor">Supervisor</option>
<option value="head_of_department">Head of Office</option>
<option value="hr_pa_officer">HR PA Officer</option>
<option value='hr_leave_officer'>HR Leave Officer</option>
<option value='hradmin'>HR Admin</option>
<option value="hr_archiving">Archiving</option>
<option value="hrsadmin">System Admin</option>
<option value="head_of_division">head_of_division</option>
<option value='edit_user'>Edit User</option>
<option value='executive_director'>Executive Director</option>
<option value='logistics'>Logistics</option>
<option value='driver'>Driver</option>
<option value='run_report'>Run report</option>
<option value='site_assistant'>Site assistant</option>
<option value='district_operation_manager'>DOM</option>
<option value='finance_manage'>Finance manager</option>
<option value='finance_payment'>Finance payment</option>
<option value='finance_payment_approval'>Finance payment approval</option>
<option value='mentorship'>Mentorship</option>
</select></td>
</tr>
<tr>
<td>Your Supervisor</td>
<td><select name="supervisor">
<option value="" selected="">-- Select Supervisor</option> <?php
include("connect.php");
$date = date("Y-m-d H:i:s");
$users = mysqli_query($conn, "select * from users where role like '%supervisor%' order by firstname");
if (mysqli_num_rows($users)) {
while ($row = mysqli_fetch_array($users)) {
$userid = $row['userid'];
$firstname = $row['firstname'];
$lastname = $row['lastname'];
print("<option value='$userid'>$firstname $lastname</option>");
}
}
?>
</select></td>
</tr>
</table>
<br>
Authorized: <select name="authorized">
<option value="YES">YES</option>
<option value="NO">NO</option>
</select>
<br><br>
Card UID: <input type="text" name="cuid">
<br>
</fieldset>
<div style="text-align: center;"><input type="submit" name="save" value="Create User" size="20"></div>
</form>
<br><br>
<?php
include "connect.php";
if (isset($_POST['save'])) {
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$position = $_POST['position'];
$email = $_POST['email'];
$site = $_POST['site'];
$username = $_POST['username'];
$password = $_POST['password'];
$passwordConf = $_POST['passwordConf'];
$privileges = $_POST['privileges'];
$authorized = $_POST['authorized'];
$supervisor = $_POST['supervisor'];
$department = $_POST['department'];
$program = $_POST['program'];
$phonenumber = $_POST['phonenumber'];
$detaildposition = $_POST['detaildposition'];
$headofdepartment = $_POST['headofdepartment'];
$typeofcotract = $_POST['typeofcotract'];
$nationalID = $_POST['nationalID'];
$dateofbirth = $_POST['dateofbirth'];
$dateofemployment = $_POST['dateofemployment'];
$gender = $_POST['gender'];
$cuid = $_POST['cuid'];
//$passenc=md5($password);
$passenc = hash('sha256', $password);
$privilegesString = "";
$i = 0;
foreach ($privileges as $privilege) {
if ($i != 0) {
$privilegesString .= "," . $privilege;
} else {
$privilegesString = $privilege;
$i++;
}
}
$file_name = "";
if (isset($_FILES['filetoupload'])) {
$errors = array();
$file_name = $_FILES['filetoupload']['name'];
$file_size = $_FILES['filetoupload']['size'];
$file_tmp = $_FILES['filetoupload']['tmp_name'];
$file_type = $_FILES['filetoupload']['type'];
//$file_ext=strtolower(end(explode('.',$_FILES['filetoupload']['name'])));
$tmp = explode('.', $file_name);
$file_ext = strtolower(end($tmp));
$extensions = array("jpeg", "jpg", "png", "pdf", "xls", "xlsx", "doc", "docx");
/* if(in_array($file_ext,$extensions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
*/
// if($file_size > 2097152){
if ($file_size > 524288000) {
$errors[] = 'File size must be small or equal to 500 MB';
}
//$bidNo = str_replace("/", "|", $bidNo);
$file_name = $firstName . "_" . $lastName . "." . $file_ext;
if (empty($errors) == true) {
//echo realpath($file_name);
move_uploaded_file($file_tmp, "employeephoto/" . $file_name);
//echo "Success";
} else {
print_r("" + $errors);
}
}
//print("Privileges: $privileges");
$insertUsequery = mysqli_query($conn, "INSERT INTO users (firstname,lastname,username,password,role,site,position,email,authorized,supervisor,department,program,phonenumber,active,detaildposition,headofdepartment,typeofcotract,nationalID,dateofbirth,dateofemployment,gender,card_UID,photofilename) VALUES ('$firstName', '$lastName','$username', '$passenc','$privilegesString','$site','$position','$email','$authorized','$supervisor','$department','$program','$phonenumber','YES','$detaildposition','$headofdepartment','$typeofcotract','$nationalID','$dateofbirth','$dateofemployment','$gender','$cuid','$file_name')");
$employeeLeaveTypes = mysqli_query($conn, "select name from hr_employee_leave_type order by name");
if (mysqli_num_rows($employeeLeaveTypes)) {
while ($row = mysqli_fetch_array($employeeLeaveTypes)) {
$name = $row['name'];
$considerUserInLeave = mysqli_query($conn, "INSERT INTO hr_employee_leave_days (employee_id, number_of_days,leave_type) SELECT MAX(userid),0,'$name' FROM users");
}
}
echo mysqli_error($conn);
print("
<script>
alert('New User is Saved');
</script>
");
}
?>
<?php
include "connect.php";
if (isset($_POST['delete'])) {
$user_id = $_POST['userid'];
$userId = $_SESSION['useridsession'];
//print("Start: ".$emploId);
$deleteUser = mysqli_query($conn, "delete from users where userid='$user_id'");
//$deleteLeaveDaysOfEmployeequery=mysqli_query($conn,"delete from employee_leave_days where employee_id='$emploId'");
//print("End: ".$emploId);
print("
<script>
alert('User is deleted');
</script>
");
}
?>
<?php
include "connect.php";
if (isset($_POST['authorizeDeny'])) {
$user_id = $_POST['userid'];
$authorized = $_POST['authorized'];
$userId = $_SESSION['useridsession'];
if ($authorized == "YES") {
$updateUser = mysqli_query($conn, "update users set authorized='NO' where userid='$user_id'");
} else {
$updateUser = mysqli_query($conn, "update users set authorized='YES' where userid='$user_id'");
}
//print("Start: ".$emploId);
//$deleteLeaveDaysOfEmployeequery=mysqli_query($conn,"delete from employee_leave_days where employee_id='$emploId'");
//print("End: ".$emploId);
print("
<script>
alert('Access is changed');
</script>
");
}
?>
<?php
include "connect.php";
if (isset($_POST['becomeuser'])) {
$current_user = $_SESSION['hruseridsession'];
$user_id = $_POST['userid'];
$loginDate = date("Y-m-d H:i:s:a");
$reslog = mysqli_query($conn, "SELECT * FROM users WHERE userid='$user_id'");
if ($num = mysqli_num_rows($reslog)) {
$ip = "";
if (isset($_SERVER['REMOTE_HOST'])) { //check
$ip = $_SERVER['REMOTE_HOST'];
} else {
$ip = $_SERVER['REMOTE_ADDR']; //else assign the real IP
}
// $result = json_decode(file_get_contents('http://ip-api.io/json/'.$ip));
//To display how data are mapped
//var_dump($result);
$other_info = "IP:" . $ip;
// , Country:".$result->country_name.", City:".$result->city.", Organisation:".$result->organisation;
while ($row = mysqli_fetch_array($reslog)) {
//$_SESSION['timeout']=time();
$_SESSION['hruseridsession'] = $row['userid'];
$_SESSION['hrusernamesession'] = $row['username'];
$_SESSION['hrpasswordsession'] = $row['password'];
$_SESSION['firstnamesession'] = $row['firstname'];
$_SESSION['lastnamesession'] = $row['lastname'];
$_SESSION['hrrolesession'] = $row['role'];
$_SESSION['sitesession'] = $row['site'];
$_SESSION['authorizedsession'] = $row['authorized'];
$_SESSION['department'] = $row['department'];
$_SESSION['position'] = $row['position'];
$_SESSION['supervisor'] = $row['supervisor'];
$userId = $row['userid'];
$_SESSION['headofdepartment'] = $row['headofdepartment'];
//$loginDate=date("Y-m-d H:i:s:a");
$log = mysqli_query($conn, "insert into user_login(user_id,date_and_time,other_info) values ('$userId','$loginDate','$other_info')");
}
}
$become = mysqli_query($conn, "insert into user_become(user,userbecome,userbecomedate) values ('$current_user','$user_id','$loginDate')");
$firstname = $_SESSION['firstnamesession'];
print("
<script>
alert('Now you become $firstname .');
</script>
");
}
?>
<center>
<div style='height: 400px;width: 95%;overflow: scroll;' class="tableFixHead">
<?php
include("connect.php");
$userList = mysqli_query($conn, "SELECT * FROM users order by lastname");
if (mysqli_num_rows($userList)) {
print("<div align='right'><span>Search: <input id='myInput' type='text' placeholder='Search..'></span></div>
<table width='100%' id='tablelist'><thead><tr>
<th>No</th>
<th>Photo</th>
<th>Last Name</th>
<th>First Name</th>
<th>User Name</th>
<th>Privilege</th>
<th>Site</th>
<th>Position</th>
<th>Email</th>
<th>Card UID</th>
<th>Authorized</th>
<th>Edit/Delete</th></tr></thead><tbody id='myTable'>");
$no = 0;
while ($row = mysqli_fetch_array($userList)) {
$no = $no + 1;
$userid = $row['userid'];
$firstname = $row['firstname'];
$lastname = $row['lastname'];
$username = $row['username'];
$userrole = $row['role'];
$site = $row['site'];
$position = $row['position'];
$email = $row['email'];
$authorized = $row['authorized'];
$photofilename = $row['photofilename'];
$card_UID = $row['card_UID'];
print("<tr><td>$no</td>
<td><img src='employeephoto/$photofilename' width='60' height='70' alt='' /></td>
<td>$lastname</td>
<td>$firstname</td>
<td>$username</td>
<td>$userrole</td>
<td>$site</td>
<td>$position</td>
<td>$email</td>
<td>$card_UID</td>
<td>$authorized</td>
<td>");
$role = $_SESSION['hrrolesession'];
$userroleArray = explode(',', $userrole);
$allowBocome = 0;
foreach ($userroleArray as $value) {
if (strpos($role, "$value") !== false) {
$allowBocome = 1;
} else {
$allowBocome = 0;
break;
}
}
//if(strpos($role,"hrsadmin") !== false){
if ($allowBocome == 1 || strpos($role, "hrsadmin") !== false) {
print("
<form action='createNewUser.php' method='post'>
<input type='hidden' name='userid' value='$userid'>
<input type='hidden' name='authorized' value='$authorized'>
<input type='submit' name='authorizeDeny' value='Authorize/Deny'>
<!-- <input type='submit' name='delete' value='Delete'> -->
<input type='submit' name='becomeuser' value='Become this User'>
</form>");
}
if (($username != $_SESSION['hrusernamesession'] && $allowBocome == 1) || strpos($role, "edit_user") !== false) {
print("
<form action='editUser.php' method='post'>
<input type='hidden' name='userid' value='$userid'>
<input type='submit' name='edit' value='Edit'>
</form>
</td></tr>");
}
}
print("</tbody></table>");
} else {
print("<br> No employee registered");
}
?>
</div>
</center>