-
Notifications
You must be signed in to change notification settings - Fork 3
/
edit_labs.php
440 lines (364 loc) · 11.2 KB
/
edit_labs.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
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
<?php
/*
* edit_labs.php
*
* A place to edit/update the lab table
*
*/
include_once 'checkinstance.php';
if ( !isset($_SESSION['userlevel']) ||
( ($_SESSION['userlevel'] != 0) &&
($_SESSION['userlevel'] != 4) &&
($_SESSION['userlevel'] != 5) ) ) // Super user, admin and super admin only
{
header('Location: index.php');
exit();
}
include 'config.php';
include 'db.php';
include 'lib/utility.php';
// ini_set('display_errors', 'On');
// Are we being directed here from a push button?
if (isset($_POST['prior']))
{
do_prior($link);
exit();
}
else if (isset($_POST['next']))
{
do_next($link);
exit();
}
else if (isset($_POST['delete']))
{
do_delete($link);
exit();
}
else if (isset($_POST['update']))
{
do_update($link);
exit();
}
else if (isset($_POST['create']))
{
do_create($link);
exit();
}
else if (isset($_POST['create']))
{
do_create($link);
exit();
}
// Start displaying page
$page_title = 'Edit Labs';
$js = 'js/edit_labs.js';
include 'header.php';
?>
<!-- Begin page content -->
<div id='content'>
<h1 class="title">Edit Labs</h1>
<!-- Place page content here -->
<?php if ( isset($_SESSION['message']) )
{
echo "<p class='message'>{$_SESSION['message']}</p>\n";
unset($_SESSION['message']);
} ?>
<?php
// Edit or display a record
if ( isset($_POST['edit']) )
edit_record($link);
else if ( isset($_POST['new']) )
do_new($link);
else
display_record($link);
?>
</div>
<?php
include 'footer.php';
exit();
// Function to redirect to prior record
function do_prior($link)
{
$labID = $_POST['labID'];
$query = "SELECT labID FROM lab " .
"ORDER BY name ";
$result = mysqli_query($link, $query)
or die("Query failed : $query<br />\n" . mysqli_error($link));
// Find prior record
$current = null;
list($current) = mysqli_fetch_array($result);
while ($current != null && $labID != $current)
{
$prior = $current;
list($current) = mysqli_fetch_array($result);
}
$redirect = ($prior == null) ? "" : "?ID=$prior";
header("Location: $_SERVER[PHP_SELF]$redirect");
exit();
}
// Function to redirect to next record
function do_next($link)
{
$labID = $_POST['labID'];
$query = "SELECT labID FROM lab " .
"ORDER BY name ";
$result = mysqli_query($link, $query)
or die("Query failed : $query<br />\n" . mysqli_error($link));
// Find next record
$next = null;
while ($labID != $next)
list($next) = mysqli_fetch_array($result);
list($next) = mysqli_fetch_array($result);
$redirect = ($next == null) ? "?ID=$labID" : "?ID=$next";
header("Location: $_SERVER[PHP_SELF]$redirect");
exit();
}
// Function to delete the current record
function do_delete($link)
{
$labID = $_POST['labID'];
// Tests to see if the current lab can be deleted
$found = false;
$query = "SELECT COUNT(*) FROM instrument " .
"WHERE labID = $labID ";
$result = mysqli_query($link, $query)
or die("Query failed : $query<br />\n" . mysqli_error($link));
list( $count ) = mysqli_fetch_array( $result );
if ( $count > 0 ) $found = true;
$query = "SELECT COUNT(*) FROM experiment " .
"WHERE labID = $labID ";
$result = mysqli_query($link, $query)
or die("Query failed : $query<br />\n" . mysqli_error($link));
list( $count ) = mysqli_fetch_array( $result );
if ( $count > 0 ) $found = true;
$query = "SELECT COUNT(*) FROM rotor " .
"WHERE labID = $labID ";
$result = mysqli_query($link, $query)
or die("Query failed : $query<br />\n" . mysqli_error($link));
list( $count ) = mysqli_fetch_array( $result );
if ( $count > 0 ) $found = true;
if ( ! $found )
{
$query = "DELETE FROM lab " .
"WHERE labID = $labID ";
mysqli_query($link, $query)
or die("Query failed : $query<br />\n" . mysqli_error($link));
}
else
$_SESSION['message'] = "This lab cannot be deleted while instruments, " .
"rotors, or labs are associated with it.\n";
header("Location: $_SERVER[PHP_SELF]");
}
// Function to update the current record
function do_update($link)
{
$labID = $_POST['labID'];
$name = addslashes(htmlentities($_POST['name']));
$contact = addslashes(htmlentities($_POST['contact']));
$query = "UPDATE lab " .
"SET name = '$name', " .
"building = '$contact', " .
"dateUpdated = NOW() " .
"WHERE labID = $labID ";
mysqli_query($link, $query)
or die("Query failed : $query<br />\n" . mysqli_error($link));
header("Location: $_SERVER[PHP_SELF]?ID=$labID");
exit();
}
// Function to create a new record
function do_create($link)
{
$guid = uuid();
$name = addslashes(htmlentities($_POST['name']));
$contact = addslashes(htmlentities($_POST['contact']));
$query = "INSERT INTO lab " .
"SET labGUID = '$guid', " .
"name = '$name', " .
"building = '$contact', " .
"dateUpdated = NOW() ";
mysqli_query($link, $query)
or die("Query failed : $query<br />\n" . mysqli_error($link));
$new = mysqli_insert_id($link);
header("Location: {$_SERVER['PHP_SELF']}?ID=$new");
}
// Function to display and navigate records
function display_record($link)
{
// Find a record to display
$labID = htmlentities(get_id($link));
if ($labID === false)
return;
// Anything other than a number here is a security risk
if (!(is_numeric($labID)))
return;
$query = "SELECT name, building AS contact " .
"FROM lab " .
"WHERE labID = ? ";
// Prepared statement
if ($stmt = mysqli_prepare($link, $query)) {
$stmt->bind_param('i', $labID);
$stmt->execute();
$stmt->store_result();
$num_of_rows = $stmt->num_rows;
$stmt->bind_result($name, $contact);
$stmt->fetch();
$stmt->free_result();
$stmt->close();
}
/* This code was replace by the prepared statement above
$result = mysqli_query($link, $query)
or die("Query failed : $query<br />\n" . mysqli_error($link));
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
// Create local variables; make sure IE displays empty cells properly
foreach ($row as $key => $value)
{
$$key = (empty($value)) ? " " : html_entity_decode( stripslashes( nl2br($value) ) );
}
*/
// Populate a list box to allow user to jump to another record
$nav_listbox = "<select name='nav_box' id='nav_box' " .
" onchange='get_lab(this);' >" .
" <option value='null'>None selected...</option>\n";
$query = "SELECT labID, name FROM lab ORDER BY name";
$result = mysqli_query($link, $query)
or die("Query failed : $query<br />\n" . mysqli_error($link));
while (list($t_id, $t_name) = mysqli_fetch_array($result))
{
$selected = ($labID == $t_id) ? " selected='selected'" : "";
$nav_listbox .= " <option$selected value='$t_id'>$t_name</option>\n";
}
$nav_listbox .= "</select>\n";
echo<<<HTML
<form action="{$_SERVER['PHP_SELF']}" method='post'>
<table cellspacing='0' cellpadding='10' class='style1'>
<thead>
<tr><th colspan='8'>Edit Labs</th></tr>
</thead>
<tfoot>
<tr><td colspan='2'>Jump to: $nav_listbox
<input type='submit' name='prior' value='<' />
<input type='submit' name='next' value='>' />
<input type='submit' name='new' value='New' />
<input type='submit' name='edit' value='Edit' />
<input type='submit' name='delete' value='Delete' />
<input type='hidden' name='labID' value='$labID' />
</td></tr>
</tfoot>
<tbody>
<tr><th>Facility Name:</th>
<td>$name</td></tr>
<tr><th>Contact Information:</th>
<td>$contact</td></tr>
</tbody>
</table>
</form>
HTML;
}
// Function to figure out which record to display
function get_id($link)
{
// See if we are being directed to a particular record
if (isset($_GET['ID']))
return( $_GET['ID'] );
// We don't know which record, so just find the first one
$query = "SELECT labID FROM lab " .
"ORDER BY name " .
"LIMIT 1 ";
$result = mysqli_query($link, $query)
or die("Query failed : $query<br />\n" . mysqli_error($link));
if (mysqli_num_rows($result) == 1)
{
list($labID) = mysqli_fetch_array($result);
return( $labID );
}
// If we're here, there aren't any records
echo<<<HTML
<form action='{$_SERVER[PHP_SELF]}' method='post'>
<table cellspacing='0' cellpadding='10' class='style1'>
<thead>
<tr><th colspan='2'>Edit Labs</th></tr>
</thead>
<tfoot>
<tr><td colspan='2'><input type='submit' name='new' value='New' />
</td></tr>
</tfoot>
<tbody>
<tr><th>Status:</th>
<td>There are no records to display</td></tr>
</tbody>
</table>
</form>
HTML;
return( false );
}
// Function to edit a record
function edit_record($link)
{
// Get the record we need to edit
if ( isset( $_POST['edit'] ) )
$labID = $_POST['labID'];
else if ( isset( $_GET['edit'] ) )
$labID = $_GET['edit'];
else
{
// How did we get here?
echo "<p>There was a problem with the edit request.</p>\n";
return;
}
$query = "SELECT name, building AS contact " .
"FROM lab " .
"WHERE labID = $labID ";
$result = mysqli_query($link, $query)
or die("Query failed : $query<br />\n" . mysqli_error($link));
$row = mysqli_fetch_array($result);
$name = html_entity_decode( stripslashes( $row['name'] ) );
$contact = html_entity_decode( stripslashes( $row['contact'] ) );
echo<<<HTML
<form action="{$_SERVER['PHP_SELF']}" method="post">
<table cellspacing='0' cellpadding='10' class='style1'>
<thead>
<tr><th colspan='8'>Edit Labs</th></tr>
</thead>
<tfoot>
<tr><td colspan='2'><input type='submit' name='update' value='Update' />
<input type='hidden' name='labID' value='$labID' />
<input type='reset' /></td></tr>
</tfoot>
<tbody>
<tr><th>Facility Name:</th>
<td><input type='text' name='name' size='40'
maxlength='255' value='$name' /></td></tr>
<tr><th>Contact Information:</th>
<td><textarea name='contact' rows='6' cols='65'
wrap='virtual'>$contact</textarea></td></tr>
</tbody>
</table>
</form>
HTML;
}
// Function to create a new record
function do_new($link)
{
echo<<<HTML
<form action="{$_SERVER['PHP_SELF']}" method="post">
<table cellspacing='0' cellpadding='10' class='style1'>
<thead>
<tr><th colspan='8'>Edit Labs</th></tr>
</thead>
<tfoot>
<tr><td colspan='2'><input type='submit' name='create' value='Create' />
<input type='reset' /></td></tr>
</tfoot>
<tbody>
<tr><th>Name:</th>
<td><input type='text' name='name' size='40'
maxlength='256' /></td></tr>
<tr><th>Contact Information:</th>
<td><textarea name='contact' rows='6' cols='65'
wrap='virtual'></textarea></td></tr>
</tbody>
</table>
</form>
HTML;
}
?>