This repository has been archived by the owner on Nov 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubmitIDR.php
260 lines (251 loc) · 14.6 KB
/
submitIDR.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
<?php
include 'SQLFunctions.php';
include 'mailer.php';
session_start();
// if request is not a POST or POST is empty show the user Apache's default 404 by redirecting to a non-existent custom 404
if ($_SERVER['REQUEST_METHOD'] !== 'POST' || !count($_POST)) {
http_response_code('404');
header('Location: 404.php');
exit;
} else {
$post = filter_input_array(INPUT_POST, FILTER_SANITIZE_SPECIAL_CHARS);
$idrTable = 'IDR';
$equipTable = 'equipment';
$laborTable = 'labor';
$equipActLink = 'equipAct_link';
$laborActLink = 'laborAct_link';
$actTable = 'activity';
$link = f_sqlConnect();
$timestamp = date('Y-m-d H:i:s');
$editableUntil = new DateTime($timestamp);
// an IDR is editable until 1 AM on the day after its creation
$editableUntil->
setDate($editableUntil->format('Y'), $editableUntil-> format('m'), $editableUntil->format('j') + 1)->
setTime('00', '59', '59');
$userID = $_SESSION['userID'];
$username = $_SESSION['username'];
// check for existing submission
$idrForDate = $post['idrForDate'];
$locationID = $post['locationID'];
$check = "SELECT
idrID,
idrDate,
locationID
FROM IDR
WHERE idrForDate = '$idrForDate'
AND userID = $userID
AND locationID = $locationID";
$result = $link->query($check);
if ($result) {
while ($row = $result->fetch_array() > 0) {
http_response_code(409);
$code = http_response_code();
echo "duplicate record found: record # {$row[0]}, record date: {$row[1]}, locationID: {$row[2]}";
}
} else {
// if no dupe, handle POST data
// first, qry IDR column names, store them as keys of an array
$query = 'SHOW COLUMNS FROM '.$idrTable;
$result = $link->query($query);
while($row = $result->fetch_assoc()){
$key = $row['Field'];
// if POST data key name matches column name from IDR table, save value to idrData array
if ($post[$key]) {
$idrData[$key] = $link->escape_string($post[$key]);
}
// destroy in $post any key found in result
unset($post[$key]);
}
// append editableUntil to $idrData;
$idrData['editableUntil'] = $editableUntil->format($editableUntil::W3C);
if (!isset($idrData['userID'])) $idrData['userID'] = $userID;
$keys = implode(", ", array_keys($idrData));
$vals = implode("', '", array_values($idrData));
// currently this fcn, found in SQLFunctions, is broken
// don't repair it unless you're ready to deal with the bugs that may produce
// if (!f_tableExists($link, $idrTable, DB_NAME)) {
// shouldn't this be an error handler like the duplicate check above(?)
// echo 'table "'.$idrTable.'" could not be found';
// } else {
// create INSERT query
$query = "INSERT INTO $idrTable ($keys) VALUES ('$vals')";
// }
// this is the block that actually executes the INSERT query
if ($result = $link->query($query)) {
http_response_code(201);
$code = http_response_code();
$newIdrID = $link->insert_id;
// grab new ID and attach it to equip, labor, activity data
$laborData = [];
$equipData = [];
$actData = [];
// test for comment and insert if present
if ($comment = $link->escape_string($post['comment'])) {
$commentQry = "INSERT idrComments (userID, comment, idrID)
VALUES ('{$_POST['userID']}', '{$comment}', '{$newIdrID}')";
if ($result = $link->query($commentQry)) {
http_response_code(201);
$code = http_response_code();
}
else {
http_response_code(500);
$code = http_response_code();
echo "There was a problem adding your comment";
exit();
}
}
foreach ($post as $key => $val) {
// parse flattened POST data to nested arrays
// first, check for labor and equip keys
if (strpos($key, 'labor') !== false || strpos($key, 'equip') !== false) {
$num = intval(substr($key, strpos($key, '_') + 1));
// there shouldn't be any equipOrLabor keys submitted, but if one is found, rm it
if (strpos($key, 'equipOrLabor') !== false) unset($post[$key]);
elseif (strpos($key, 'labor') !== false) {
// if key includes 'Location', grab the 'Location...' part of the string
if (strpos($key, 'Location') !== false) {
$key = substr($key, strpos($key, 'Location'));
}
// assign 'labor' vals to 'labor' keys @ array[num]
// clean '_$num' out of any numbered $key
$laborKey = substr($key, 0, strpos($key, '_'));
$laborData[$num][$laborKey] = $link->escape_string($val);
// if idrID is not set, set it
if (!isset($laborData[$num]['idrID'])) {
$laborData[$num]['idrID'] = $newIdrID;
}
// unset $key from $post once it's parsed to array
unset($post, $key);
} else {
// if key includes 'Location', grab the 'Location...' part of the string
if (strpos($key, 'Location') !== false) {
$key = substr($key, strpos($key, 'Location'));
}
// assign 'equip' vals to 'equip' keys @ array[num]
// clean '_$num' out of any numbered $key
$equipKey = substr($key, 0, strpos($key, '_'));
$equipData[$num][$equipKey] = $link->escape_string($val);
$equipData[$num]['idrID'] = $newIdrID;
// unset $key from $post once it's parsed to array
// unset $key from $post once it's parsed to array
unset($post, $key);
}
}
// next, check for act keys
elseif (strpos($key, 'act') !== false || strpos($key, 'numResources') !== false) {
// assign 'act' vals to 'act' keys @ array[actNum]
// num following 1st '_' in $key is resource number (labor or equip)
$rsrcNum = intval(substr($key, strpos($key, '_') + 1, strpos($key, '_', strpos($key, '_') + 1) - (strpos($key, '_') + 1)));
// num following 2nd '_' in $key is activity number for that resource
$actNum = intval(substr($key, strpos($key, '_', strpos($key, '_') + 1) + 1));
$actKey = substr($key, 0, strpos($key, '_'));
$actData[$rsrcNum][$actNum][$actKey] = $val;
$actData[$rsrcNum][$actNum]['idrID'] = $newIdrID;
unset($post, $val);
// link laborID or equipID to activityID after INSERT and retrieval of insert_key
} else {
continue;
}
}
// build labor & equipment queries
if (count($laborData)) {
// foreach labor data, find associated activity data & parse it to array
foreach ($laborData as $index => $subarr) {
$keys = implode(", ", array_keys($subarr));
$vals = implode("', '", array_values($subarr));
$query = "INSERT INTO $laborTable ($keys) VALUES ('$vals')";
// once data is parsed & committed, rm it from data array
unset($laborData[$index]);
// after successful INSERT, unset $key, grab insert_key
if ($result = $link->query($query)) {
http_response_code(201);
$code = http_response_code();
// store db ID of new record
$newLaborID = $link->insert_id;
// build queries from activities that fall within same index as labor
if (count($actData[$index])) {
foreach ($actData[$index] as $key => $val) {
$actKeys = implode(", ", array_keys($val));
$actVals = implode("', '", array_values($val));
$actQry = "INSERT INTO $actTable ($actKeys) VALUES ('$actVals')";
if ($result = $link->query($actQry)) {
// each activity in the db will be ref'd by a linking table
$linkQry = "INSERT INTO $laborActLink (laborID, activityID) VALUES ('$newLaborID', '$link->insert_id')";
if ($result = $link->query($linkQry)) {
} else ;//printQryErr($linkQry, $link->error);
} else {
http_response_code(500);
error_log(__FILE__.': '.__LINE.': '.http_response_code().': '.$link->error, 0);
error_log(__FILE__.': '.__LINE.': '.http_response_code().': '.$link->error."\n", 3, '../error_log.log');
echo __FILE__.': '.__LINE__.': '.$link->error;
return;
}
}
} else continue;
} else {
http_response_code(500);
error_log(__FILE__.': '.__LINE.': '.http_response_code().': '.$link->error, 0);
error_log(__FILE__.': '.__LINE.': '.http_response_code().': '.$link->error."\n", 3, '../error_log.log');
echo __FILE__.': '.__LINE__.': '.$link->error;
return;
}
}
}
if (count($equipData)) {
// foreach equip data, find associated activity data & parse it to array
foreach ($equipData as $index => $subarr) {
$keys = implode(", ", array_keys($subarr));
$vals = implode("', '", array_values($subarr));
$query = "INSERT INTO $equipTable ($keys) VALUES ('$vals')";
// once data is parsed & committed, rm it from data array
unset($equipData[$index]);
// after successful INSERT, unset $key, grab insert_key
if ($result = $link->query($query)) {
http_response_code(201);
$code = http_response_code();
// store db ID of new record
$newEquipID = $link->insert_id;
// build queries from activities that fall within same index as equip
if (count($actData[$index])) {
foreach ($actData[$index] as $key => $val) {
$actKeys = implode(", ", array_keys($val));
$actVals = implode("', '", array_values($val));
$actQry = "INSERT INTO $actTable ($actKeys) VALUES ('$actVals')";
if ($result = $link->query($actQry)) {
// each activity in the db will be ref'd by a linking table
$linkQry = "INSERT INTO $equipActLink (equipID, activityID) VALUES ('$newEquipID', '$link->insert_id')";
if ($result = $link->query($linkQry)) {
} else ;//printQryErr($linkQry, $link->error);
} else {
http_response_code(500);
error_log(__FILE__.': '.__LINE.': '.http_response_code().': '.$link->error, 0);
error_log(__FILE__.': '.__LINE.': '.http_response_code().': '.$link->error."\n", 3, '../error_log.log');
echo __FILE__.': '.__LINE__.': '.$link->error;
return;
}
}
} else continue;
} else {
http_response_code(500);
error_log(__FILE__.': '.__LINE.': '.http_response_code().': '.$link->error, 0);
error_log(__FILE__.': '.__LINE.': '.http_response_code().': '.$link->error."\n", 3, '../error_log.log');
echo __FILE__.': '.__LINE__.': '.$link->error;
return;
}
}
}
http_response_code(201);
header("Location: /idr.php?idrID={$newIdrID}");
$code = http_response_code();
$reviewLink = "https://{$_SERVER['HTTP_HOST']}/idr.php?idrID={$newIdrID}";
echo "new record created: Inspector's Daily Report #{$newIdrID}\nhttps://{$reviewLink}\n{$username} {$timestamp}";
mailer($mailgunDistList,
'new Inspector Daily Report',
"{$reviewLink}\n{$username} {$timestamp}");
} else {
http_response_code(500);
$code = http_response_code();
}
}
}
?>