forked from samuelgilbertuk/absencetrackingsystem
/
databaseFunctions.php
496 lines (407 loc) · 18.6 KB
/
databaseFunctions.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
<?php
/* -----------------------------------------------------------------------------
* Includes
* -------------------------------------------------------------------------- */
include 'status.php';
include 'AdHocRequestTable.php';
include 'CompanyRoleTable.php';
include 'EmployeeTable.php';
include 'MainVacationRequestTable.php';
include 'AbsenceTypeTable.php';
include 'ApprovedAbsenceBookingTable.php';
include 'ApprovedAbsenceBookingDateTable.php';
include 'DateTable.php';
include 'PublicHolidayTable.php';
include 'MailFunctions.php';
include 'KeyAlgorithms.php';
/* -----------------------------------------------------------------------------
* Function IsValidDate
*
* This function checks the supplied date to determine whether or not
* the date is a valid date in the format YYYY-MM-DD
*
* $date (string) string date to check.
* @return (bool) TRUE if date supplied is a valid date, otherwise FALSE.
* -------------------------------------------------------------------------- */
function isValidDate($date) {
$d = DateTime::createFromFormat('Y-m-d', $date);
return $d && $d->format('Y-m-d') == $date;
}
/* -----------------------------------------------------------------------------
* Function IsNULLOrEmptyString
*
* This function performs a basic check to test whether the string supplied
* is NULL or empty.
*
* $inputString (string) string to check.
* @return (bool) TRUE if string is NULL or empty, otherwise FALSE.
* -------------------------------------------------------------------------- */
function IsNullOrEmptyString($inputString) {
return (!isset($inputString) || trim($inputString) == '');
}
/* -----------------------------------------------------------------------------
* Function printCalstackAndDie
*
* This function is used in the event of a fatal error in processing, where
* the program is no longer able to function. The code will print out the call
* stack to the screen which may be helpful in debugging.
* @return(none)
* -------------------------------------------------------------------------- */
function printCallstackAndDie() {
echo "Fatal Error. Please contact your system administrator.<br/>";
$callers = debug_backtrace();
echo "Dump Trace<br/>";
foreach ($callers as $caller) {
echo "Function: " . $caller['function'] . " Line: "
. $caller['line'] . "<br/>";
}
die();
}
/* -----------------------------------------------------------------------------
* Function connectToSQL
*
* This function performs the processing necessary to establish a connection
* with the SQL database.
*
* $server (string) server that the database is on
* $username (string) account username for the database.
* $password (string) account password for the database.
* @return (connection) id representing the database connection, or NULL if failed
* to establish a connection.
* -------------------------------------------------------------------------- */
function connectToSql($server, $username, $password) {
$connection = mysqli_connect($server, $username, $password);
if (!$connection) {
printCallstackAndDie();
}
return $connection;
}
/* -----------------------------------------------------------------------------
* Function performSQL
*
* This function takes an SQL string and executes this query agains the database.
*
* $sql (string) SQL statement to execute
* @return (array) array of rows containing the result, or NULL if the query failed.
* -------------------------------------------------------------------------- */
function performSQL($sql) {
$result = FALSE;
$conn = $GLOBALS["connection"];
$result = mysqli_query($conn, $sql);
if (!$result) {
error_log("PerformSQL failed. Sql = $sql");
}
return $result;
}
/* -----------------------------------------------------------------------------
* Function performSQLDelete
*
* This function takes an SQL string representing a delete from the database and
* executes this query agains the database.
*
* $sql (string) SQL statement to execute
* @return (int) count of rows deleted or 0 if no rows were deleted.
* -------------------------------------------------------------------------- */
function performSQLDelete($sql) {
$deletedRows = 0;
$conn = $GLOBALS["connection"];
$result = mysqli_query($conn, $sql);
if ($result) {
$deletedRows = mysqli_affected_rows($conn);
}
return $deletedRows;
}
/* -----------------------------------------------------------------------------
* Function performSQLInsert
*
* This function takes an SQL string representing an insert into the database and
* executes this query agains the database.
*
* $sql (string) SQL statement to execute
* @return (int)id of the record creased in the database or 0 if insert failed.
* -------------------------------------------------------------------------- */
function performSQLInsert($sql) {
$conn = $GLOBALS["connection"];
$result = mysqli_query($conn, $sql);
if (!$result) {
printCallstackAndDie();
}
return mysqli_insert_id($conn);
}
/* -----------------------------------------------------------------------------
* Function performSQLSelect
*
* This function forms an select query on the database
* and executes this query agains the database.
*
* $tableName (string) name of the database table that this query should be
* performed on
* $filter (array) set of key value pairs to use in the WHERE clause of the sql
* query. Note, this value can be NULL if you want to access
* all records in the table.
* @return (array) set of records matching the filter.
* -------------------------------------------------------------------------- */
function performSQLSelect($tableName, $filter) {
$conn = $GLOBALS["connection"];
$sql = "SELECT * FROM " . $tableName;
if ($filter <> NULL) {
$sql = $sql . " WHERE ";
foreach ($filter as $key => $value) {
$whereClause[] = $key . "='" . $value . "'";
}
$sql = $sql . implode(" AND ", $whereClause);
}
$result = mysqli_query($conn, $sql);
if (!$result) {
printCallstackAndDie();
}
$results = NULL;
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$results[] = $row;
}
return $results;
}
/* -----------------------------------------------------------------------------
* Function performSQLUpdate
*
* This function forms an update statement on the database
* and executes this query agains the database.
*
* $tableName (string) name of the database table that this update should be
* performed on
* $filter (array) set of key value pairs which contains the field names and
* values to be applied i this update.
* @return (bool) TRUE if the update was performed successfully, FALSE otherwise.
* -------------------------------------------------------------------------- */
function performSQLUpdate($tableName, $idFieldName, $fields) {
$conn = $GLOBALS["connection"];
$sql = "UPDATE " . $tableName . " SET ";
if ($fields <> NULL) {
foreach ($fields as $key => $value) {
if (!is_numeric($key) AND $key <> $idFieldName) {
if ($value <> NULL) {
$updateClause[] = $key . "='" . $value . "'";
} else {
$updateClause[] = $key . "=NULL";
}
}
}
$sql = $sql . implode(",", $updateClause);
}
$sql = $sql . " WHERE " . $idFieldName . "='" . $fields[$idFieldName] . "';";
$result = mysqli_query($conn, $sql);
if (!$result) {
echo mysqli_error($conn);
printCallstackAndDie();
}
return TRUE;
}
/* -----------------------------------------------------------------------------
* Function UseDB
*
* Very simple function that performs the SQL necessary to use the database.
* @return (none)
* -------------------------------------------------------------------------- */
function UseDB() {
$sql = "USE mydb;";
performSQL($sql);
}
/* -----------------------------------------------------------------------------
* Function DropDB
*
* Very simple function that performs the SQL necessary to drop the database.
* @return (none)
* -------------------------------------------------------------------------- */
function DropDB() {
$sql = "DROP DATABASE IF EXISTS `mydb`;";
performSQL($sql);
}
/* -----------------------------------------------------------------------------
* Function CreateDB
*
* Very simple function that performs the SQL necessary to create the database.
* @return (none)
* -------------------------------------------------------------------------- */
function CreateDB() {
$sql = "CREATE SCHEMA IF NOT EXISTS `mydb`" .
"DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;";
performSQL($sql);
}
/* -----------------------------------------------------------------------------
* Function CreateDefaultRecords
*
* This function is used to populate some of the tables in the database with
* basic date required for operation. This includes creating the admin account
* based on parametes supplied and creating the date entries in the
* date table.
*
* @return (none)
* -------------------------------------------------------------------------- */
function CreateDefaultRecords($adminName,$adminEmail,$adminPassword,
$adminDateJoined,$adminLeaveEntitlement) {
$filter[COMP_ROLE_NAME] = "Admin";
$records = RetrieveCompanyRoles($filter);
if (count($records) == 0)
{
$role = CreateCompanyRole("Admin", 0);
}
else
{
$role = $records[0];
}
$success = CreateEmployee($adminName, $adminEmail,
$adminPassword, $adminDateJoined,
$adminLeaveEntitlement, NULL,
$role[COMP_ROLE_ID], 1, 1);
if ($success)
{
date_default_timezone_set('UTC');
$date = '2015-01-01';
$end_date = '2055-12-31';
while (strtotime($date) <= strtotime($end_date)) {
CreateDate($date, NULL);
$date = date("Y-m-d", strtotime("+1 day", strtotime($date)));
}
}
return $success;
}
/* -----------------------------------------------------------------------------
* Function CreateNewDatabase
*
* This function creates the database and all tables within it.
*
* $destroyExistingDB(bool) ic TRUE, will destroy existing database
* $createWithTestData(bool) if TRUE, will populate new database with test data.
* @return (none)
* -------------------------------------------------------------------------- */
function CreateNewDatabase($destroyExistingDB = false, $createWithTestData = false) {
if ($destroyExistingDB) {
DropDB();
}
CreateDB();
UseDB();
CreateDateTable();
CreatePublicHolidayTable();
CreateAbsenceTypeTable();
CreateCompanyRoleTable();
CreateEmployeeTable();
CreateApprovedAbsenceBookingTable();
CreateApprovedAbsenceDateTable();
CreateAdHocAbsenceRequestTable();
CreateMainVacationRequestTable();
// CreateDefaultRecords("admin", "admin@admin.com","Zaq12wsx","2015-01-01", 19);
if ($createWithTestData) {
$annualLeave = CreateAbsenceType("Annual Leave", 1, 1);
$training = CreateAbsenceType("Training", 0, 1);
$sickness = CreateAbsenceType("Sickness", 0, 0);
$compasionate = CreateAbsenceType("Compasionate Leave", 0, 1);
$cashier = CreateCompanyRole("Cashier", 3);
$customerAdvisor = CreateCompanyRole("Customer Advisor", 2);
$manager = CreateCompanyRole("Manager", 1);
$filter[COMP_ROLE_NAME] = "Admin";
$results = RetrieveCompanyRoles($filter);
$admin = $results[0];
$steveBrookstein = CreateEmployee("Steve Brookstein",
"stevebrookstein@test.com",
"Zaq12wsx", "2005-01-01", 20, NULL,
$cashier[COMP_ROLE_ID], 0, 0);
$shayneWard = CreateEmployee("Shane Ward",
"shaneWard@test.com",
"Zaq12wsx", "2006-01-01", 20, NULL,
$cashier[COMP_ROLE_ID], 0, 0);
$leonJackson = CreateEmployee("Leon Jackson", "leonjackson@test.com",
"Zaq12wsx", "2008-01-01", 20, NULL,
$manager[COMP_ROLE_ID], 0, 0);
$alexandraBurke = CreateEmployee("Alexandra Burke", "alexburke@test.com",
"Zaq12wsx", "2009-01-01", 20, NULL,
$cashier[COMP_ROLE_ID], 0, 0);
$joeMcElderry = CreateEmployee("Joe McElderry", "JoeMcElderry@test.com",
"Zaq12wsx", "2010-01-01", 20, NULL,
$customerAdvisor[COMP_ROLE_ID], 0, 0);
$mattCardle = CreateEmployee("Matt Cardle", "mattCardle@test.com",
"Zaq12wsx", "2011-01-01", 20, NULL,
$customerAdvisor[COMP_ROLE_ID], 0, 0);
$jamesArthur = CreateEmployee("James Arthur", "jamesarthur@test.com",
"Zaq12wsx", "2012-01-01", 20, NULL,
$customerAdvisor[COMP_ROLE_ID], 0, 0);
$samBailey = CreateEmployee("Sam Bailey", "sambailey@test.com",
"Zaq12wsx", "2013-01-01", 20, NULL,
$customerAdvisor[COMP_ROLE_ID], 0, 0);
$benHaenow = CreateEmployee("Ben Haenow", "benHaenow@test.com",
"Zaq12wsx", "2014-01-01", 20, NULL,
$manager[COMP_ROLE_ID], 0, 1);
$dates = RetrieveDates();
if (count($dates) == 0) {
date_default_timezone_set('UTC');
// Start date
$date = '2015-01-01';
// End date
$end_date = '2055-12-31';
while (strtotime($date) <= strtotime($end_date)) {
CreateDate($date, NULL);
$date = date("Y-m-d", strtotime("+1 day", strtotime($date)));
}
}
$dateID = RetrieveDateIDByDate("2015-04-03");
$goodFriday = CreatePublicHoliday("Good Friday", $dateID);
$dateID = RetrieveDateIDByDate("2015-04-06");
$easterMonday = CreatePublicHoliday("Easter Monday", $dateID);
$dateID = RetrieveDateIDByDate("2015-05-04");
$earlyMay = CreatePublicHoliday("Early May Bank Holiday", $dateID);
$dateID = RetrieveDateIDByDate("2015-05-25");
$springHoliday = CreatePublicHoliday("Spring Bank Holiday", $dateID);
$dateID = RetrieveDateIDByDate("2015-08-31");
$summerHoliday = CreatePublicHoliday("Summer Bank Holiday", $dateID);
$dateID = RetrieveDateIDByDate("2015-12-25");
$christmasDay = CreatePublicHoliday("Christmas Day", $dateID);
$dateID = RetrieveDateIDByDate("2015-12-28");
$boxingDay = CreatePublicHoliday("Boxing Day (substitute day)", $dateID);
$request = CreateMainVactionRequest($steveBrookstein[EMP_ID],
"2015-01-10", "2015-01-15", "2015-02-10", "2015-02-15");
$request = CreateMainVactionRequest($shayneWard[EMP_ID],
"2015-01-10", "2015-01-15", "2015-02-10", "2015-02-15");
$request = CreateMainVactionRequest($leonJackson[EMP_ID],
"2015-01-10", "2015-01-15", "2015-02-10", "2015-02-15");
$request = CreateMainVactionRequest($alexandraBurke[EMP_ID],
"2015-01-10", "2015-01-15", "2015-02-10", "2015-02-15");
$request = CreateMainVactionRequest($joeMcElderry [EMP_ID],
"2015-01-10", "2015-01-15", "2015-02-10", "2015-02-15");
$request = CreateMainVactionRequest($jamesArthur [EMP_ID],
"2015-01-10", "2015-01-15", "2015-02-10", "2015-02-15");
$request = CreateMainVactionRequest($mattCardle[EMP_ID],
"2015-01-10", "2015-01-15", "2015-02-10", "2015-02-15");
$request = CreateMainVactionRequest($samBailey[EMP_ID],
"2015-01-10", "2015-01-15", "2015-02-10", "2015-02-15");
$request = CreateMainVactionRequest($benHaenow [EMP_ID],
"2015-01-10", "2015-01-15", "2015-02-10", "2015-02-15");
$request = CreateAdHocAbsenceRequest($steveBrookstein[EMP_ID],
"2015-03-10", "2015-03-15", $annualLeave[ABS_TYPE_ID]);
$request = CreateAdHocAbsenceRequest($shayneWard[EMP_ID],
"2015-03-10", "2015-03-15", $annualLeave[ABS_TYPE_ID]);
$request = CreateAdHocAbsenceRequest($leonJackson[EMP_ID],
"2015-03-10", "2015-03-15", $sickness[ABS_TYPE_ID]);
$request = CreateAdHocAbsenceRequest($alexandraBurke[EMP_ID],
"2015-03-10", "2015-03-15", $training[ABS_TYPE_ID]);
$request = CreateAdHocAbsenceRequest($joeMcElderry[EMP_ID],
"2015-03-10", "2015-03-15", $training[ABS_TYPE_ID]);
$request = CreateAdHocAbsenceRequest($mattCardle[EMP_ID],
"2015-03-10", "2015-03-15", $training[ABS_TYPE_ID]);
$request = CreateAdHocAbsenceRequest($jamesArthur[EMP_ID],
"2015-03-10", "2015-03-15", $training[ABS_TYPE_ID]);
$request = CreateAdHocAbsenceRequest($samBailey[EMP_ID],
"2015-03-10", "2015-03-15", $compasionate[ABS_TYPE_ID]);
$request = CreateAdHocAbsenceRequest($benHaenow[EMP_ID],
"2015-03-10", "2015-03-15", $compasionate[ABS_TYPE_ID]);
$leonaLewis = CreateEmployee("Leona Lewis", "leonalewis@test.com",
"Zaq12wsx", "2007-01-01", 20, NULL,
$admin[COMP_ROLE_ID], 1, 1);
$request = CreateAdHocAbsenceRequest($leonaLewis[EMP_ID],
"2015-03-10", "2015-03-15", $sickness[ABS_TYPE_ID]);
$request = CreateMainVactionRequest($leonaLewis[EMP_ID],
"2015-01-10", "2015-01-15", "2015-02-10", "2015-02-15");
$request = CreateApprovedAbsenceBooking($leonaLewis[EMP_ID],
"2015-04-01","2015-04-10",
$training[ABS_TYPE_ID]);
}
}
?>