-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
271 lines (243 loc) · 6.75 KB
/
index.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
<?php
/**
* Index.php
* Main page
*/
//Register as entry point
define('DME', true);
//Connect to DB.
require_once('common.php');
//Clear all outstanding faults
//TODO: Move to separate file
if(isset($_POST['clear']))
{
if(isset($_POST['clearedby']) and $_POST['clearedby'] != "")
{
$query = $dbh -> prepare('UPDATE dme_messages SET status=2, clearedby=?, clearedtime=? WHERE status!=2 AND (time <=? AND clearedtime <=?)');
$query -> execute (array($_POST['clearedby'], time(), $_POST['clearbefore'], $_POST['clearbefore']));
header("Location: index.php?assignedto={$_POST['clearedby']}");
}
else
{
header("Location: index.php?error=Enter%20a%20name.");
}
}
//Assign all outstanding faults
//TODO: Move to separate page
if(isset($_POST['assign']))
{
if(isset($_POST['clearedby']) and $_POST['clearedby'] != "")
{
$query = $dbh -> prepare('UPDATE dme_messages SET status=1, clearedby=?, clearedtime=? WHERE status!=2 AND (time <=? AND clearedtime <=?)');
$query -> execute (array($_POST['clearedby'], time(), $_POST['clearbefore'], $_POST['clearbefore']));
header("Location: index.php?assignedto={$_POST['clearedby']}");
}
else
{
header("Location: index.php?error=Enter%20a%20name.");
}
}
//Get status for printers
//Complex query
$query = $dbh -> prepare(<<<sql
SELECT name, description, attendees, minstatus, faults
FROM ( /* Sub-Query for fault data */
SELECT
printer,
MIN(status) AS minstatus, /* Lower fault status = not processed */
GROUP_CONCAT(
DISTINCT clearedby
ORDER BY time ASC
) AS attendees,
GROUP_CONCAT( /* Gets a comma separated list of outstanding faults */
DISTINCT fault /* No repeats
ORDER BY time DESC /* Sort faults by report time */
SEPARATOR ", "
) AS faults
FROM dme_messages
WHERE status!=2 /* Show only outstanding faults*/
GROUP BY printer /* One row per printer with outstanding faults */
) AS statustable
RIGHT JOIN dme_printers ON statustable.printer=name /* Join creates a row even for printers with no outstanding faults, with minstatus=NULL */
sql
);
$query -> execute();
$status = $query -> fetchAll();
//Sanitise any error message
if(isset($_GET['error']))
{
$error = htmlentities($_GET['error']);
}
else
{
$error = "";
}
//Get the count of faults assigned to you
if(isset($_GET['assignedto']))
{
$showAssigned = true; //Show the assigned faults line.
$name = htmlentities($_GET['assignedto']);
$query = $dbh -> prepare('SELECT COUNT(*) FROM dme_messages WHERE clearedby=? AND status=1');
$query -> execute(array($_GET['assignedto']));
$numberAssigned = $query -> fetchColumn();
}
else
{
$name = "";
$showAssigned = false;
}
//Get the full fault log
$query = $dbh -> prepare('SELECT * FROM dme_messages ORDER BY status ASC, time DESC');
$query -> execute();
$log = $query -> fetchAll();
?>
<html>
<head>
<title>Fitz SCO Printer Monitoring</title>
<link rel="stylesheet" type="text/css" href="style.css" media="screen" />
<link rel="stylesheet" type="text/css" href="mobile.css" media="screen and (max-width: 480px)" />
<meta name="viewport" content="width=device-width; initial-scale=1.0">
<link rel="shortcut icon" href="images/favicon.png" />
<link href='http://fonts.googleapis.com/css?family=Droid+Sans' rel='stylesheet' type='text/css'>
<link href="http://fonts.googleapis.com/css?family=Quattrocento:regular" rel="stylesheet" type="text/css" >
</head>
<body>
<div id="container">
<div id="header">
<h1>
Fitz SCO Printer Monitoring
</h1>
</div>
<div id="content">
<h2>Status</h2>
<table id="status">
<?php
//Generate status table, one row per printer
foreach($status as $printer)
{
//Null status means no outstanding faults, see query above.
if(is_null($printer['minstatus']))
{
$faultDesc = "Online";
$image = "printer.png";
$class = ' class="online"';
$assignment = '';
}
else
{
$faultDesc = "Offline: " . $printer['faults'];
$class = ' class="fault"';
//Select icon based on the first (newest) fault in the list
$faultLength = strpos($printer['faults'], ',');
if($faultLength === false)
$faultLength = strlen($printer['faults']);
switch(substr($printer['faults'], 0, $faultLength))
{
case "Paper out":
$image = "printer_empty.png";
break;
case "Toner out":
$image = "printer_color.png";
break;
case "Down":
$image = "printer_delete.png";
break;
default:
$image = "printer_error.png";
break;
}
$attendees = explode(',', $printer['attendees']);
$assignment = (count($attendees > 0) && $attendees[0] != "")?"{$attendees[0]} is on their way.":"";
}
echo <<<html
<tr{$class}>
<th class="statusHeader">
{$printer['description']}
</th>
<td class="statusRow">
<img src="images/{$image}" class="statusIcon" />
{$faultDesc}
<div class="statusAssignment">{$assignment}</div>
</td>
</tr>
html;
}
?>
</table>
<h2>Clearing</h2>
<?php
if($error != "")
{
echo <<<html
<p id="error"><b>Error:</b> {$error}
html;
}
?>
<?php
if($showAssigned)
{
echo <<<html
<p id="assigned">{$numberAssigned} faults assigned to {$name}.</p>
html;
}
?>
<form method="post" id="clearing">
<input type="hidden" name="clearbefore" value="<?php echo time(); ?>" />
<table>
<tr>
<th>crsID:</th>
<td><input type="text" name="clearedby" value="<?php echo $name; ?>" /></td>
</tr>
</table>
<p>
<input type="submit" name="assign" value="Assign all Outstanding Faults to Me" />
<input type="submit" name="clear" value="Mark all Outstanding Faults as Clear" />
</p>
</form>
<h2>Log</h2>
<table id="log">
<thead>
<tr>
<th>Time</th>
<th>Printer</th>
<th>Error</th>
<th>Status</th>
</tr>
</thead>
<?php
//Index used for odd/even row striping
$i = 1;
//Generate log table
foreach($log as $entry)
{
?>
<tr class="<?php echo ($i % 2 == 0)?"even ":""; ?><?php echo ($entry['status'] == 2)?"clear":""; ?>">
<td><?php echo date('d/m/y H:i', $entry['time']); ?></td>
<td><?php echo str_replace('Copier_LIB_', '', $entry['printer']); ?></td>
<td><?php echo $entry['fault']; ?></td>
<td>
<?php
switch($entry['status'])
{
case 0:
echo "Outstanding";
break;
case 1:
echo "Assigned to {$entry['clearedby']} at " . date('H:i', $entry['clearedtime']);
break;
case 2:
echo "Cleared by {$entry['clearedby']} at " . date('H:i', $entry['clearedtime']);
break;
}
?>
</td>
</tr>
<?php
$i++;
}
?>
</table>
</div>
</div>
</body>
</html>