This repository was 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
Expand file tree
/
Copy pathdefs.php
More file actions
420 lines (392 loc) · 15 KB
/
defs.php
File metadata and controls
420 lines (392 loc) · 15 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
<?php
require_once 'vendor/autoload.php';
require_once 'session.php';
use SVBX\DbConnection;
use SVBX\DefCollection;
// check which view to show
$view = !empty(($_GET['view']))
? filter_var($_GET['view'], FILTER_SANITIZE_ENCODED)
: '';
unset($_GET['view']);
$orderBy = [];
// check for search params
// if no search params show all defs that are not 'deleted'
if(!empty($_GET)) {
// $get = array_filter($_GET); // filter to remove falsey values -- is this necessary?
// unset($get['view']);
$where = filter_var_array($_GET, FILTER_SANITIZE_SPECIAL_CHARS);
// retrieve 'sort_' vars from $_GET, removing them from $_GET along the way
$orderBy = $orderBy + array_reduce(array_keys($_GET), function($acc, $key) use (&$where) {
if (strpos($key, 'sort_') === 0 && array_search($where[$key], $acc) === false) {
$acc[$key] = $where[$key];
unset($where[$key]);
}
return $acc;
}, []);
// unset($get['sort_1'], $get['sort_2'], $get['sort_3']);
} else $where = null;
// set view-dependent variables
$bartTableHeadings = [
'id' => [ 'value' => 'ID', 'cellWd' => '1', 'collapse' => 'none def-table__col-id', 'href' => '/def.php?bartDefID=' ],
'status' => [ 'value' => 'Status', 'cellWd' => '2' ],
'date_created'=> [ 'value' => 'Date created', 'cellWd' => '3', 'collapse' => 'xs' ],
'descriptive_title_vta' => [ 'value' => 'Description', 'cellWd' => '', 'classList' => 'def-table__crop-content' ],
'resolution_vta' => [ 'value' => 'Resolution', 'cellWd' => '', 'collapse' => 'xs', 'classList' => 'def-table__crop-content' ],
'next_step'=> [ 'value' => 'Next step', 'cellWd' => '3', 'collapse' => 'xs' ],
'edit'=> [ 'value' => 'Edit', 'cellWd' => '1', 'collapse' => 'sm', 'href' => '/updateDef.php?class=bart&id=' ]
];
$projectTableHeadings = [
'id' => [ 'value' => 'ID', 'cellWd' => '1', 'collapse' => 'none def-table__col-id', 'href' => '/def.php?defID=' ],
'bartDefID' => [ 'value' => 'BART ID', 'filter' => 'zerofill_4', 'cellWd' => '1', 'collapse' => 'sm' ],
'location' => [ 'value' => 'Location', 'cellWd' => '2', 'collapse' => 'sm' ],
'severity' => [ 'value' => 'Severity', 'cellWd' => '1', 'collapse' => 'xs' ],
'status' => [ 'value' => 'Status', 'cellWd' => '2' ],
'systemAffected' => [ 'value' => 'System affected', 'cellWd' => '2', 'collapse' => 'sm', 'classList' => 'def-table__crop-content' ],
'groupToResolve' => [ 'value' => 'Group to resolve', 'cellWd' => '2', 'collapse' => '', 'classList' => 'def-table__crop-content' ],
'description' => [ 'value' => 'Description', 'cellWd' => '6', 'collapse' => 'xs', 'classList' => 'def-table__crop-content' ],
'specLoc' => [ 'value' => 'Specific location', 'cellWd' => '2', 'collapse' => 'md' ],
'requiredBy' => [ 'value' => 'Required prior to', 'cellWd' => '2', 'collapse' => '' ],
'dueDate' => [ 'value' => 'Due date', 'cellWd' => '2', 'collapse' => 'md' ],
'edit' => [ 'value' => 'Edit', 'cellWd' => '1', 'collapse' => 'sm', 'classList' => 'def-table__edit', 'href' => '/updateDef.php?id=' ]
];
$bartFields = [
'id',
's.statusName status',
'date_created',
'descriptive_title_vta',
'resolution_vta',
'n.nextStepName next_step'
];
$bartJoins = [
'status s' => 'b.status = s.statusID',
'bdNextStep n' => 'b.next_step = n.bdNextStepID'
];
$bartFilters = [
'status' => [
'table' => 'status s',
'fields' => ['statusID', 'statusName'],
'join' => [
'joinTable' => 'BARTDL b',
'joinOn' => 's.statusID = b.status',
'joinType' => 'INNER'
],
'groupBy' => 's.statusID',
'where' => [
'field' => 's.statusID',
'value' => '3',
'comparison' => '<>'
]
],
'next_step' => [
'table' => 'bdNextStep n',
'fields' => ['bdNextStepID', 'nextStepName'],
'join' => [
'joinTable' => 'BARTDL b',
'joinOn' => 'b.next_step = n.bdNextStepID',
'joinType' => 'INNER'
],
'groupBy' => 'n.bdNextStepID',
'where' => [
'field' => 'n.bdNextStepID',
'value' => '0',
'comparison' => '<>'
]
],
'bic' => [
'table' => 'bdParties p',
'fields' => ['partyID', 'partyName'],
'join' => [
'joinTable' => 'BARTDL b',
'joinOn' => 'p.partyID = b.bic',
'joinType' => 'INNER'
],
'groupBy' => 'p.partyID',
'where' => [
'field' => 'p.partyID',
'value' => '0',
'comparison' => '<>'
]
],
'safety_cert_vta' => [
'table' => 'yesNo y',
'fields' => ['yesNoID', 'yesNoName'],
'join' => [
'joinTable' => 'BARTDL b',
'joinOn' => 'y.yesNoID = b.safety_cert_vta',
'joinType' => 'INNER'
],
'groupBy' => 'y.yesNoID'
],
'resolution_disputed' => [
'table' => 'BARTDL',
'fields' => ['resolution_disputed', '(CASE WHEN resolution_disputed = 1 THEN "yes" ELSE "no" END) AS yesNoName'], // res_disp and structural use CASES to map 0 + 1 to 'no' + 'yes' b/c they don't line up nicely with our bool table, yesNo
'groupBy' => 'resolution_disputed'
],
'structural' => [
'table' => 'BARTDL',
'fields' => ['structural', '(CASE WHEN structural = 1 THEN "yes" ELSE "no" END) AS yesNoName'], // res_disp and structural use CASES to map 0 + 1 to 'no' + 'yes' b/c they don't line up nicely with our bool table, yesNo
'groupBy' => 'structural'
]
];
$projectFilters = [
"status" => [
'table' => 'status s',
'fields' => ['statusID', 'statusName'],
'join' => [
'joinTable' => 'CDL c',
'joinOn' => 'c.status = s.statusID',
'joinType' => 'INNER'
],
'groupBy' => 's.statusID',
'where' => [
'field' => 'statusID',
'value' => '3',
'comparison' => '<>'
]
],
"safetyCert" => [
'table' => 'yesNo y',
'fields' => ['yesNoID', 'yesNoName'],
'join' => [
'joinTable' => 'CDL c',
'joinOn' => 'c.safetyCert = y.yesNoID',
'joinType' => 'INNER'
],
'groupBy' => 'y.yesNoID'
],
"severity" => [
'table' => 'severity s',
'fields' => ['severityID', 'severityName'],
'join' => [
'joinTable' => 'CDL c',
'joinOn' => 's.severityID = c.severity',
'joinType' => 'INNER'
],
'groupBy' => 's.severityID'
],
"systemAffected" => [
'table' => 'system s',
'fields' => ['systemID', 'systemName'],
'join' => [
'joinTable' => 'CDL c',
'joinOn' => 's.systemID = c.systemAffected',
'joinType' => 'INNER'
],
'groupBy' => 's.systemID'
],
"groupToResolve" => [
'table' => 'system g',
'fields' => ['systemID', 'systemName'],
'join' => [
'joinTable' => 'CDL c',
'joinOn' => 'g.systemID = c.groupToResolve',
'joinType' => 'INNER'
],
'groupBy' => 'g.systemID'
],
"location" => [
'table' => 'location l',
'fields' => ['locationID', 'locationName'],
'join' => [
'joinTable' => 'CDL c',
'joinOn' => 'l.locationID = c.location',
'joinType' => 'INNER'
],
'groupBy' => 'l.locationID'
],
"identifiedBy" => [
'table' => 'CDL',
'fields' => 'identifiedBy',
'groupBy' => 'identifiedBy'
],
'requiredBy' => [
'table' => 'requiredBy r',
'fields' => 'reqByID, r.requiredBy',
'join' => [
'joinTable' => 'CDL c',
'joinOn' => 'r.reqByID = c.requiredBy',
'joinType' => 'INNER'
],
'groupBy' => 'reqByID'
],
'defType' => [
'table' => 'defType type',
'fields' => ['type.defTypeID', 'type.defTypeName'],
'join' => [
'joinTable' => 'CDL c',
'joinOn' => 'c.defType = type.defTypeID',
'joinType' => 'INNER'
],
'groupBy' => 'type.defTypeID'
]
];
$projectSort = [
'location' => 'Location',
'severity' => 'Severity',
'systemAffected' => 'System affected',
'groupToResolve' => 'Group to resolve',
'requiredBy' => 'Required prior to',
'dueDate' => 'Due date'
];
list($table, $tableAlias, $addPath, $tableHeadings, $fields, $joins, $filters, $sortOptions) = $view === 'BART'
? [ 'BARTDL b', 'b', 'newDef.php?class=bart', $bartTableHeadings, $bartFields, $bartJoins, $bartFilters, [] ]
: [ null, null, 'newDef.php', $projectTableHeadings, null, null, $projectFilters, $projectSort ];
if ($_SESSION['role'] <= 10) unset($tableHeadings['edit']);
$queryParams = [ 'fields' => $fields, 'joins' => $joins ];
// function to get filter options to display in <select> elements
function getFilterOptions($db, $queryParams) { // TODO: this logic should all live in Deficiency class
$options = [];
foreach ($queryParams as $fieldName => $params) {
$table = $params['table'];
$fields = $params['fields'];
if (!empty($params['join']))
$db->join($params['join']['joinTable'], $params['join']['joinOn'], $params['join']['joinType']);
if (!empty($params['where'])) {
$whereParams = $params['where'];
if (gettype($whereParams) === 'string')
// if where is string, use it as raw where query
$db->where($whereParams);
elseif (!empty($whereParams['comparison']))
$db->where($whereParams['field'], $whereParams['value'], $whereParams['comparison']);
else $db->where($whereParams['field'], $whereParams['value']);
}
if (!empty($params['groupBy'])) $db->groupBy($params['groupBy']);
if (!empty($params['orderBy'])) $db->orderBy($params['orderBy']);
if ($result = $db->get($table, null, $fields)) {
$options[$fieldName] = [];
foreach ($result as $row) {
$fieldNames = array_keys($row);
$value = $row[$fieldNames[0]];
if (count($fieldNames) > 1) $text = $row[$fieldNames[1]];
else $text = $value;
$options[$fieldName][$value] = $text;
}
} else {
$options[$fieldName] = "Unable to retrieve $fieldName list";
}
}
return $options;
}
function getBartStatusCount($db) { // TODO: this should instead return an object that can be consumed by $db
$table = 'BARTDL bart';
$fields = [
'COUNT(CASE WHEN s.statusName = "open" THEN 1 ELSE NULL END) AS statusOpen',
'COUNT(CASE WHEN s.statusName = "closed" THEN 1 ELSE NULL END) AS statusClosed'
];
$db->join('status stat', 'bart.status = stat.statusID', 'LEFT');
$db->groupBy('bart.status');
return $db->get($table, null, [
'statusName label',
'count(bart.status) count'
]);
}
// base context
$context = [
'session' => $_SESSION,
'title' => 'Deficiencies List',
'pageHeading' => 'Deficiencies',
'info' => 'Click Deficiency ID number to see full details',
'addPath' => $addPath,
// filter vars
'resetScript' => 'resetSearch',
'values' => $where,
'collapse' => empty($where),
'view' => $view,
'sortOptions' => $sortOptions,
'curSort' => $orderBy,
// table vars
'tableName' => $table,
'tableProps' => [
'classList' => 'def-table'
],
'dataDisplayName' => 'deficiency',
'tableHeadings' => $tableHeadings
];
try {
$db = new DbConnection(DB_CREDENTIALS);
if ($view === 'BART') {
// TODO: this should instead return an object that $db can use to fetch
$context['statusData'] = getBartStatusCount($db);
// build defs query
foreach ($queryParams['joins'] as $tableName => $on) {
$db->join($tableName, $on, 'LEFT');
}
// filter on user-selected query params
if (!empty($where)) {
foreach ($where as $param => $val) {
if ($param === 'description'
|| $param === 'defID'
|| $param === 'bartDefID'
|| $param === 'specLoc') $db->where($param, "%{$val}%", 'LIKE');
elseif ($param === 'systemAffected'
|| $param === 'groupToResolve'
&& is_array($val))
{
$arrayVals = [ array_shift($val) ];
foreach ($val as $extraVal) {
array_push($arrayVals, $extraVal);
}
$db->where("$tableAlias.$param", $arrayVals, 'IN');
}
else $db->where("$tableAlias.$param", $val);
}
}
$db->where('status', 3, '<>');
if (!empty($orderBy)) {
foreach ($orderBy as $field) {
$db->orderBy($field, 'ASC');
}
}
$db->orderBy('id', 'ASC');
// fetch table data and append it to $context for display by Twig template
$context['data'] = $db->get($table, null, $queryParams['fields']);
} else {
$db->where('status', 3, '<>'); // TODO: DefCollection should be able to process raw where conditions as strings
$context['data'] = $db->lazyGet(...DefCollection::getFetchableNum(
[
'id',
'bartDefID',
'locationName location',
'severityName severity',
'statusName status',
'systemName systemAffected',
'systemName groupToResolve',
'description',
'specLoc',
'requiredBy requiredBy', // because the name in the database is anti-pattern
'dueDate'
],
$where,
null,
$orderBy + [ 'id ASC' ]
));
}
// get filter select options, showing those that are currently filtered on
$context['selectOptions'] = getFilterOptions($db, $filters);
$context['count'] = $db->count;
// instantiate Twig
$twig = new Twig_Environment(new Twig_Loader_Filesystem('./templates'),
[
'debug' => getenv('PHP_ENV') === 'dev',
'autoescape' => false
]
);
if (getenv('PHP_ENV') === 'dev') $twig->addExtension(new Twig_Extension_Debug());
// add Twig filters
$filter_decode = new Twig_Filter('safe', function($str) {
return html_entity_decode($str);
});
$zerofill = new Twig_Filter('zerofill_*', function($num, $str) {
return $str ? str_pad($str, $num, '0', STR_PAD_LEFT) : $str;
});
$twig->addFilter($filter_decode);
$twig->addFilter($zerofill);
$twig->display('defs.html.twig', $context);
} catch (Twig_Error $e) {
error_log($e->getTemplateLine() . ' ' . $e->getRawMessage());
} catch (Exception | Error $e) {
error_log($e->getMessage());
} finally {
if (!empty($db) && is_a($db, 'MysqliDB')) $db->disconnect();
exit;
}