forked from samuelgilbertuk/absencetrackingsystem
/
CompanyRoleTable.php
322 lines (285 loc) · 12.3 KB
/
CompanyRoleTable.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
<?php
/* -----------------------------------------------------------------------------
* CONSTANTS
*
* These constants should be used when refering to the table and the fields
* within its records.
* ---------------------------------------------------------------------------*/
define("COMPANY_ROLE_TABLE", "companyRoleTable");
define("COMP_ROLE_ID", "companyRoleID");
define("COMP_ROLE_NAME", "roleName");
define("COMP_ROLE_MIN_STAFF", "minimumStaffingLevel");
/* ----------------------------------------------------------------------------
* Function CreateCompanyRoleTable
*
* 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 CreateCompanyRoleTable() {
$sql = "CREATE TABLE IF NOT EXISTS `mydb`.`companyRoleTable` (
`companyRoleID` INT NOT NULL AUTO_INCREMENT,
`roleName` VARCHAR(30) NOT NULL,
`minimumStaffingLevel` INT(1) NOT NULL,
PRIMARY KEY (`companyRoleID`));";
performSQL($sql);
}
/* ---------------------------------------------------------------------------
* Function CreateCompanyRole
*
* This function creates a new CompanyRole record in the
* table.
*
* $roleName (string) name of the role.
* $minStaffLevel (int) minimum number of staff that needs to be maintained.
*
* @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 CreateCompanyRole($roleName, $minStaffLevel) {
$role = NULL;
//-------------------------------------------------------------------------
// Validate Input parameters
//-------------------------------------------------------------------------
$inputIsValid = TRUE;
$statusMessage = "";
if ($roleName == NULL) {
$statusMessage .= "Invalid Company Role Name.";
error_log("Invalid NULL name passed to CreateCompanyRole.");
$inputIsValid = FALSE;
}
if (isNullOrEmptyString($roleName)) {
$statusMessage .= "Company Role Name must be specified.";
error_log("Invalid roleName passed to CreateCompanyRole.");
$inputIsValid = FALSE;
}
if (!is_numeric($minStaffLevel)) {
$statusMessage .= "Minimum Staff Level must be numeric.";
error_log("Invalid minStaffLevel parameter passed to CreateCompanyRole.");
$inputIsValid = FALSE;
}
//-------------------------------------------------------------------------
// Only attempt to insert a record in the database if the input parameters
// are ok.
//-------------------------------------------------------------------------
if ($inputIsValid) {
// Create an array with each field required in the record.
$role[COMP_ROLE_ID] = NULL;
$role[COMP_ROLE_NAME] = $roleName;
$role[COMP_ROLE_MIN_STAFF] = $minStaffLevel;
$success = sqlInsertCompanyRole($role);
if (!$success) {
$statusMessage .= "Failed to add company role to the database.".
"Please contact your system administrator.";
error_log("Failed to create company Role. " . print_r($role));
$role = NULL;
}
else
{
$statusMessage .= "Company Role created successfully.";
}
}
GenerateStatus($inputIsValid, $statusMessage);
return $role;
}
/* -----------------------------------------------------------------------------
* Function sqlInsertCompanyRole
*
* This function constructs the SQL statement required to insert a new record
* into the companyRole table.
*
* &$role (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 COMP_ROLE_ID entry in the
* array passed by the caller will be set to the ID of the record in the database.
* ---------------------------------------------------------------------------*/
function sqlInsertCompanyRole(&$role) {
$sql = "INSERT INTO companyroletable (roleName,minimumStaffingLevel) " .
"VALUES ('" . $role[COMP_ROLE_NAME] . "'," . $role[COMP_ROLE_MIN_STAFF] . ");";
$role[COMP_ROLE_ID] = performSQLInsert($sql);
return ($role[COMP_ROLE_ID] <> 0);
}
/* ----------------------------------------------------------------------------
* Function RetrieveCompanyRoleByID
*
* 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 RetrieveCompanyRoleByID($id) {
$filter[COMP_ROLE_ID] = $id;
$resultArray = performSQLSelect(COMPANY_ROLE_TABLE, $filter);
$result = NULL;
if (count($resultArray) == 1) { //Check to see if record was found.
$result = $resultArray[0];
}
return $result;
}
/* -----------------------------------------------------------------------------
* Function RetrieveCompanyRoles
*
* This function constructs the SQL statement required to query the CompanyRole
* 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 RetrieveCompanyRoles($filter = NULL) {
$inputIsValid = TRUE;
//--------------------------------------------------------------------------------
// Validate Input parameters
//--------------------------------------------------------------------------------
if ($filter <> NULL) {
foreach ($filter as $key => $value) {
if (strcmp($key, COMP_ROLE_ID) == 0) {
if (!is_numeric($value)) {
error_log("Invalid COMP_ROLE_ID of " . $value .
" passed to RetrieveCompanyRoles.");
$inputIsValid = FALSE;
}
} else if (strcmp($key, COMP_ROLE_NAME) == 0) {
if (isNullOrEmptyString($value)) {
error_log("Invalid COMP_ROLE_NAME of " . $value .
" passed to RetrieveCompanyRoles.");
$inputIsValid = FALSE;
}
} else if (strcmp($key, COMP_ROLE_MIN_STAFF) == 0) {
if (!is_numeric($value)) {
error_log("Invalid COMP_ROLE_MIN_STAFF of " . $value .
" passed to RetrieveCompanyRoles.");
$inputIsValid = FALSE;
}
} else {
error_log("Unknown Filter " . $key . " passed to RetrieveCompanyRoles.");
$inputIsValid = FALSE;
}
}
}
//--------------------------------------------------------------------------
// Only attempt to perform query in the database if the input parameters are
// ok.
//--------------------------------------------------------------------------
$result = NULL;
if ($inputIsValid) {
$result = performSQLSelect(COMPANY_ROLE_TABLE, $filter);
}
return $result;
}
/* -----------------------------------------------------------------------------
* Function UpdateCompanyRole
*
* This function constructs the SQL statement required to update a row in
* the CompanyRole 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 (COMP_ROLE_ID)
* and one or more other fields to be updated.
*
* @return (bool) TRUE if update succeeds. FALSE otherwise.
* ---------------------------------------------------------------------------*/
function UpdateCompanyRole($fields) {
//-------------------------------------------------------------------------
// Validate Input parameters
//-------------------------------------------------------------------------
$statusMessage="";
$inputIsValid = TRUE;
$validID = false;
$countOfFields = 0;
foreach ($fields as $key => $value) {
if ($key == COMP_ROLE_ID) {
$record = RetrieveCompanyRoleByID($value);
if ($record <> NULL) {
$validID = true;
$countOfFields++;
}
else
{
$statusMessage.= "Unable to locate Company Role in the database.";
error_log("No valid ID supplied in call to UpdateCompanyRole.");
$inputIsValid = FALSE;
}
} else if ($key == COMP_ROLE_NAME) {
$countOfFields++;
if (isNullOrEmptyString($value)) {
$statusMessage .= "You must enter a company role name.";
error_log("Invalid COMP_ROLE_NAME passed to UpdateCompanyRole.");
$inputIsValid = FALSE;
}
} else if ($key == COMP_ROLE_MIN_STAFF) {
$countOfFields++;
if (!is_numeric($value)) {
$statusMessage .= "You must enter a numeric value for minimum staff";
error_log("Invalid COMP_ROLE_MIN_STAFF passed to UpdateCompanyRole.");
$inputIsValid = FALSE;
}
} else {
$statusMessage .= "Invalid field.";
error_log("Invalid field passed to UpdateCompanyRole. $key=" . $key);
$inputIsValid = FALSE;
}
}
if ($countOfFields < 2) {
$statusMessage .= "You must alter at least one field before updating.";
error_log("Insufficent fields supplied in call to UpdateCompanyRole.");
$inputIsValid = FALSE;
}
//-------------------------------------------------------------------------
// Only attempt to update a record in the database if the input parameters
// are ok.
//-------------------------------------------------------------------------
$success = false;
if ($inputIsValid) {
$success = performSQLUpdate(COMPANY_ROLE_TABLE, COMP_ROLE_ID, $fields);
$statusMessage .= "Record has been updated successfully.";
}
GenerateStatus($inputIsValid, $statusMessage);
return $success;
}
/* ----------------------------------------------------------------------------
* Function DeleteCompanyRole
*
* This function constructs the SQL statement required to delete a row in
* the CompanyRole table.
*
* $ID(integer) ID of the record to be removed from the table. This should be set
* to the COMP_ROLE_ID value of the record you wish to delete.
*
* @return (int) count of rows deleted. 0 means delete was unsuccessful.
* -------------------------------------------------------------------------- */
function DeleteCompanyRole($ID) {
$statusMessage = "";
$result = 0;
$record = RetrieveCompanyRoleByID($ID);
if ($record <> NULL) {
$filter[EMP_COMPANY_ROLE] = $ID;
$employees = RetrieveEmployees($filter);
if ($employees <> NULL) {
foreach ($employees as $employee) {
DeleteEmployee($employee[EMP_ID]);
}
}
$sql = "DELETE FROM companyroletable WHERE companyRoleID=" . $ID . ";";
$result = performSQL($sql);
$statusMessage = "Role Deleted.</br>";
GenerateStatus(true, $statusMessage);
}
return $result;
}
?>