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
/
Copy pathdef.php
105 lines (93 loc) · 3.45 KB
/
def.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
<?php
require 'vendor/autoload.php';
require 'session.php';
if (!empty($_GET['bartDefID']) && !$_SESSION['bdPermit']) {
header("This is not for you", true, 403);
exit;
}
if (empty($_GET['bartDefID']) && empty($_GET['defID'])) {
header("We need a deficiency ID to show you anything", true, 400);
exit;
}
if (!empty($_GET)) $get = filter_input_array(INPUT_GET, FILTER_SANITIZE_NUMBER_INT);
// TODO: move these fields and queries into the Deficiency class
list(
$class,
$id,
$commentTable,
$commentTextField,
$attachmentsTable,
$pathField,
$templatePath
) = (!empty($get['defID'])
? [
'SVBX\Deficiency',
$get['defID'],
'cdlComments',
'cdlCommText',
'CDL_pics',
'pathToFile',
'def.html.twig'
]
: (!empty($get['bartDefID'])
? [
'SVBX\BARTDeficiency',
$get['bartDefID'],
'bartdlComments',
'bdCommText',
'bartdlAttachments',
'bdaFilepath',
'bartDef.html.twig'
]
: array_fill(0, 7, null)));
// TODO: handle case of no def ID
$context = [
'session' => $_SESSION,
'title' => "Deficiency no. $id",
'pageHeading' => "Deficiency No. $id",
];
try {
$def = new $class($id);
$context['data'] = $def->getReadable();
if (strcasecmp($context['data']['status'], "open") === 0) {
$color = "bg-red text-white";
} else {
$color = "bg-green text-white";
}
// query for comments associated with this Def
$link = new MySqliDB(DB_CREDENTIALS);
$link->join('users_enc u', "$commentTable.userID = u.userID");
$link->orderBy("$commentTable.date_created", 'DESC');
$link->where(($class === 'SVBX\Deficiency' ? 'defID' : 'bartdlID'), $id); // this is necessary because the name of the BART id field is different on the bartDef table and the comment table
$context['data']['comments'] = $link->get($commentTable, null, [ "$commentTextField as commentText", 'date_created', "CONCAT(firstname, ' ', lastname) as userFullName" ]);
// query for photos linked to this Def
// keep BART and Project photos | attachments separate for now
// to leave room for giving photos or attachments to either of those data types in the future
if (!empty($get['defID'])) {
$link->where('defID', $id);
$photos = $link->get($attachmentsTable, null, "$pathField as filepath");
$context['data']['photos'] = array_chunk($photos, 3);
} elseif (!empty($get['bartDefID'])) {
$link->where('bartdlID', $id);
$context['data']['attachments'] = $link->get($attachmentsTable, null, [ "$pathField as filepath", 'filename' ]);
}
// instantiate Twig
$loader = new Twig_Loader_Filesystem('./templates');
$twig = new Twig_Environment($loader, [ 'debug' => getenv('PHP_ENV') === 'dev' ]);
$twig->addExtension(new Twig_Extension_Debug());
// add custom Twig filters
$zerofill = new Twig_Filter('zerofill_*', function($num, $str) {
return $str ? str_pad($str, $num, '0', STR_PAD_LEFT) : $str;
});
$twig->addFilter($zerofill);
$twig->display($templatePath, $context);
} catch (Twig_Error $e) {
echo "Unable to render template";
error_log($e);
} catch (Exception $e) {
echo "Unable to retrieve record";
error_log($e);
} finally {
if (!empty($link) && is_a($link, 'MysqliDb')) $link->disconnect();
exit;
}