forked from samuelgilbertuk/absencetrackingsystem
/
AdHocRequestTable.php
412 lines (368 loc) · 16.5 KB
/
AdHocRequestTable.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
<?php
/* ----------------------------------------------------------------------------
* CONSTANTS
*
* These constants should be used when refering to the table and the fields
* within its records.
* -------------------------------------------------------------------------- */
define("ADHOC_ABSENCE_REQUEST_TABLE", "adHocAbsenceRequestTable");
define("AD_HOC_REQ_ID", "adHocAbsenceRequestID");
define("AD_HOC_EMP_ID", "employeeID");
define("AD_HOC_START", "startDate");
define("AD_HOC_END", "endDate");
define("AD_HOC_ABSENCE_TYPE_ID", "absenceTypeID");
/* ---------------------------------------------------------------------------
* Function CreateAdHocAbsenceRequestTable
*
* This function creates the SQL statement needed to construct the table
* in the database.
*
* @return (bool) True if table is created successfully, false otherwise.
* ---------------------------------------------------------------------------*/
function CreateAdHocAbsenceRequestTable() {
$sql = "CREATE TABLE IF NOT EXISTS `mydb`.`adHocAbsenceRequestTable` (
`adHocAbsenceRequestID` INT NOT NULL AUTO_INCREMENT,
`employeeID` INT NOT NULL,
`startDate` DATE NOT NULL,
`endDate` DATE NOT NULL,
`absenceTypeID` INT NOT NULL,
PRIMARY KEY (`adHocAbsenceRequestID`),
INDEX `fk_adHocAbsenceRequest_absenceType1_idx` (`absenceTypeID` ASC),
INDEX `fk_adHocAbsenceRequest_Employee1_idx` (`employeeID` ASC),
CONSTRAINT `fk_adHocAbsenceRequest_absenceType1`
FOREIGN KEY (`absenceTypeID`)
REFERENCES `mydb`.`absenceTypeTable` (`absenceTypeID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_adHocAbsenceRequest_Employee1`
FOREIGN KEY (`employeeID`)
REFERENCES `mydb`.`EmployeeTable` (`employeeID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);";
performSQL($sql);
}
/* -----------------------------------------------------------------------------
* Function CreateAdHocAbsenceRequest
*
* This function creates a new AdHocAbsenceRequest record in the
* AdHocAbsenceRequestTable.
*
* $employeeID (int) ID of the employee record associated with this request.
* $startDate (string) Start date of the request. Should be in the form YYYY-MM-DD
* $endDate (string) End date of the request. Should be in the form YYYY-MM-DD
* $absenceTypeID (int) ID of the absenceType record assocaited with this request.
*
* @return (array) If successful, an array is returned where each key represents
* a field in the record. If unsuccessful, the return will be NULL.
* -------------------------------------------------------------------------- */
function CreateAdHocAbsenceRequest($employeeID, $startDate, $endDate, $absenceTypeID) {
$statusMessage = "";
$request = NULL;
//--------------------------------------------------------------------------------
// Validate Input parameters
//--------------------------------------------------------------------------------
$inputIsValid = TRUE;
$record = RetrieveEmployeeByID($employeeID);
if ($record == NULL) {
$statusMessage .= "Unable to locate the employees ID in the database.</br>";
error_log("employeeID passed to CreateAdHocAbsenceRequest does not " .
"exist in the database. ID=" . $employeeID);
$inputIsValid = FALSE;
}
if (!isValidDate($startDate)) {
$statusMessage .= "Start date given is not a valid date.</br>";
error_log("Invalid start date passed to CreateAdHocAbsenceRequest.".
"date=".$startDate);
$inputIsValid = FALSE;
}
if (!isValidDate($endDate)) {
$statusMessage .= "End date given is not a valid date.</br>";
error_log("Invalid end date passed to CreateAdHocAbsenceRequest. date=".
$endDate);
$inputIsValid = FALSE;
}
if (strtotime($endDate) < strtotime($startDate))
{
$statusMessage.="end Date is before start Date.</br>";
error_log("End Date is before Start Date.");
$inputIsValid = FALSE;
}
//ensure absenceType exists in the database.
$record = RetrieveAbsenceTypeByID($absenceTypeID);
if ($record == NULL) {
$statusMessage .= "Unable to locate the absence type in the database.</br>";
error_log("absenceType passed to CreateAdHocAbsenceRequest does not " .
"exist in the database. ID=" . $absenceTypeID);
$inputIsValid = FALSE;
}
//------------------------------------------------------------------------
// Only attempt to insert a record in the database if the input parameters
// are ok.
//------------------------------------------------------------------------
if ($inputIsValid) {
$request[AD_HOC_REQ_ID] = NULL;
$request[AD_HOC_EMP_ID] = $employeeID;
$request[AD_HOC_START] = $startDate;
$request[AD_HOC_END] = $endDate;
$request[AD_HOC_ABSENCE_TYPE_ID] = $absenceTypeID;
$success = sqlInsertAdHocAbsenceRequest($request);
if (!$success) {
$isValidInput = false;
$statusMessage .= "Unexpected issue when writing to the database.".
"Contact your system administrator.</br>";
error_log("Failed to create Ad Hoc Absence Request.");
$request = NULL;
}
else
{
$statusMessage.="Record successfully created.";
}
}
GenerateStatus($inputIsValid, $statusMessage);
return $request;
}
/* ----------------------------------------------------------------------------
* Function sqlInsertAdHocAbsenceRequest
*
* This function constructs the SQL statement required to insert a new record
* into the adHocAbsenceRequest table.
*
* &$adHocAbsenceRequest (array) Array containing all of the fields required for
* the record.
*
* @return (bool) TRUE if insert into database was successful, false otherwise.
*
* Note: If successful then the AD_HOC_REQ_ID entry in the array
* passed by the caller will be set to the ID of the record in the
* database.
* ---------------------------------------------------------------------------*/
function sqlInsertAdHocAbsenceRequest(&$adHocAbsenceRequest) {
$sql = "INSERT INTO adHocAbsenceRequestTable (employeeID,startDate,"
. "endDate,absenceTypeID) "
. "VALUES ('" . $adHocAbsenceRequest[AD_HOC_EMP_ID] .
"','" . $adHocAbsenceRequest[AD_HOC_START] . "','" .
$adHocAbsenceRequest[AD_HOC_END] .
"','" . $adHocAbsenceRequest[AD_HOC_ABSENCE_TYPE_ID] . "');";
$adHocAbsenceRequest[AD_HOC_REQ_ID] = performSQLInsert($sql);
return $adHocAbsenceRequest[AD_HOC_REQ_ID] <> 0;
}
/* -----------------------------------------------------------------------------
* Function RetrieveAdHocAbsenceRequestByID
*
* This function uses the ID supplied as a parameter to construct an SQL select
* statement and then performs this query, returning an array containing the key
* value pairs of the record (or NULL if no record is found matching the id).
*
* $id (int) id of the record to retrieve from the database..
*
* @return (array) array of key value pairs representing the fields in the
* record, or NULL if no record exists with the id supplied.
* ---------------------------------------------------------------------------*/
function RetrieveAdHocAbsenceRequestByID($id) {
$filter[AD_HOC_REQ_ID] = $id;
$resultArray = performSQLSelect(ADHOC_ABSENCE_REQUEST_TABLE, $filter);
$result = NULL;
if (count($resultArray) == 1) { //Check to see if record was found.
$result = $resultArray[0];
}
return $result;
}
/* -----------------------------------------------------------------------------
* Function RetrieveAdHocAbsenceRequests
*
* This function constructs the SQL statement required to query the
* AdHocAbsenceRequest table.
*
* $filter (array) Optional parameter. If supplied, then the array should
* contain a set of key value pairs, where the keys correspond
* to one (or more) fields in the record (see constants at top
* of file) and the values correspond to the values to filter
* against (IE: The WHERE clause).
*
* @return (array) If successful, an array of arrays, where each element
* corresponds to a row from the query. If a failure occurs,
* return will be NULL.
* -------------------------------------------------------------------------- */
function RetrieveAdHocAbsenceRequests($filter = NULL) {
$inputIsValid = TRUE;
//--------------------------------------------------------------------------
// Validate Input parameters
//--------------------------------------------------------------------------
if ($filter <> NULL) {
foreach ($filter as $key => $value) {
if (strcmp($key, AD_HOC_REQ_ID) == 0) {
if (!is_numeric($value)) {
error_log("Invalid AdHocRequestID of " . $value .
" passed to RetrieveAdHocAbsenceRequests.");
$inputIsValid = FALSE;
}
} else if (strcmp($key, AD_HOC_EMP_ID) == 0) {
if (!is_numeric($value)) {
error_log("InvalidAD_HOC_EMP_ID of " . $value .
" passed to RetrieveAdHocAbsenceRequests.");
$inputIsValid = FALSE;
}
} else if (strcmp($key, AD_HOC_START) == 0) {
if (!isValidDate($value)) {
error_log("Invalid AD_HOC_START of " . $value .
" passed to RetrieveAdHocAbsenceRequests.");
$inputIsValid = FALSE;
}
} else if (strcmp($key, AD_HOC_END) == 0) {
if (!isValidDate($value)) {
error_log("Invalid AD_HOC_END of " . $value .
" passed to RetrieveAdHocAbsenceRequests.");
$inputIsValid = FALSE;
}
} else if (strcmp($key, AD_HOC_ABSENCE_TYPE_ID) == 0) {
if (!is_numeric($value)) {
error_log("Invalid AD_HOC_ABSENCE_TYPE_ID of " . $value .
" passed to RetrieveAdHocAbsenceRequests.");
$inputIsValid = FALSE;
}
} else {
error_log("Unknown Filter " . $key . " passed to "
. "RetrieveAdHocAbsenceRequests.");
$inputIsValid = FALSE;
}
}
}
//-------------------------------------------------------------------------
// Only attempt to perform query in the database if the input parameters
// are ok.
//------------------------------------------------------------------------
$result = NULL;
if ($inputIsValid) {
$result = performSQLSelect(ADHOC_ABSENCE_REQUEST_TABLE, $filter);
}
return $result;
}
/* ----------------------------------------------------------------------------
* Function UpdateAdHocAbsenceRequest
*
* This function constructs the SQL statement required to update a row in
* the AdHocAbsenceRequest table.
*
* $fields (array) array of key value pairs, where keys correspond to fields
* in the record (see constants at start of this file). Note,
* this array MUST provide the id of the record (AD_HOC_REQ_ID)
* and one or more other fields to be updated.
*
* @return (bool) TRUE if update succeeds. FALSE otherwise.
* ---------------------------------------------------------------------------*/
function UpdateAdHocAbsenceRequest($fields) {
$statusMessage = "";
//-------------------------------------------------------------------------
// Validate Input parameters
//-------------------------------------------------------------------------
$inputIsValid = TRUE;
$validID = false;
$countOfFields = 0;
foreach ($fields as $key => $value) {
if ($key == AD_HOC_REQ_ID) {
$record = RetrieveAdHocAbsenceRequestByID($value);
if ($record <> NULL) {
$validID = true;
$countOfFields++;
}
} else if ($key == AD_HOC_EMP_ID) {
$countOfFields++;
$record = RetrieveEmployeeByID($value);
if ($record == NULL) {
$statusMessage .= "Employee specified can not be found in the "
. "database.</br>";
error_log("Invalid AD_HOC_EMP_ID passed to "
. "UpdateAdHocAbsenceRequest." .
" Value=" . $value);
$inputIsValid = FALSE;
}
} else if ($key == AD_HOC_START) {
$countOfFields++;
if (!isValidDate($value)) {
$statusMessage .= "Start date entered is not a valid date.</br>";
error_log("Invalid AD_HOC_START passed to UpdateAdHocAbsenceRequest." .
" Value=" . $value);
$inputIsValid = FALSE;
}
} else if ($key == AD_HOC_END) {
$countOfFields++;
if (!isValidDate($value)) {
$statusMessage .= "End date entered is not a valid date.</br>";
error_log("Invalid AD_HOC_END passed to UpdateAdHocAbsenceRequest." .
" Value=" . $value);
$inputIsValid = FALSE;
}
} else if ($key == AD_HOC_ABSENCE_TYPE_ID) {
$countOfFields++;
$record = RetrieveAbsenceTypeByID($value);
if ($record == NULL) {
$statusMessage .= "Absence Type selected can not be found in the "
. "database.</br>";
error_log("Invalid AD_HOC_ABSENCE_TYPE_ID passed to " .
"UpdateAdHocAbsenceRequest. Value=" . $value);
$inputIsValid = FALSE;
}
} else {
$statusMessage .= "Unknown field encountered.</br>";
error_log("Invalid field passed to UpdateAdHocAbsenceRequest."
." $key=" . $key);
$inputIsValid = FALSE;
}
}
$startDate = $fields[AD_HOC_START];
$endDate = $fields[AD_HOC_END];
if (strtotime($endDate) < strtotime($startDate))
{
$statusMessage.="end Date is before start Date.</br>";
error_log("End Date is before Start Date.");
$inputIsValid = FALSE;
}
if (!$validID) {
$statusMessage .= "No valid record ID found.</br>";
error_log("No valid ID supplied in call to UpdateAbsenceType.");
$inputIsValid = FALSE;
}
if ($countOfFields < 2) {
$statusMessage .= "Insufficent fields supplied in call to UpdateAbsenceType.</br>";
error_log("Insufficent fields supplied in call to UpdateAbsenceType.");
$inputIsValid = FALSE;
}
//-------------------------------------------------------------------------
// Only attempt to update a record in the database if the input parameters
// are ok.
//-------------------------------------------------------------------------
$success = false;
if ($inputIsValid) {
$success = performSQLUpdate(ADHOC_ABSENCE_REQUEST_TABLE,
AD_HOC_REQ_ID, $fields);
if ($success)
{
$statusMessage .= "Record successfully updated.</br>";
}
else
{
$statusMessage .= "Unexpected error encountered when updating database.".
"Contact your system administrator.</br>";
$inputIsValid = false;
}
}
GenerateStatus($inputIsValid, $statusMessage);
return $success;
}
/* ----------------------------------------------------------------------------
* Function DeleteAdHocAbsenceRequest
*
* This function constructs the SQL statement required to delete a row in
* the AdHocAbsenceRequest table.
*
* $ID(integer) ID of the record to be removed from the table. This should be set to
* the AD_HOC_REQ_ID value of the record you wish to delete.
*
* @return (int) count of rows deleted. 0 means delete was unsuccessful.
* -------------------------------------------------------------------------- */
function DeleteAdHocAbsenceRequest($ID) {
$sql = "DELETE FROM adHocAbsenceRequestTable WHERE adHocAbsenceRequestID=" . $ID . ";";
GenerateStatus(true,"Record successfully deleted");
return performSQL($sql);
}
?>