-
Notifications
You must be signed in to change notification settings - Fork 0
/
select.php
669 lines (624 loc) · 20.5 KB
/
select.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
<!DOCTYPE HTML>
<?php
include "userauth.php";
include "analyze.php";
include "shifttypes.php";
$AUTO_CONTINUE = 1;
// character used to X the calendar
$X = "✔";
?>
<html>
<head>
<title>View and Edit Schedule</title>
<meta name="viewport" content="width=device-width, user-scalable=no" />
<link href="/trees.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="header">
<h2><?php if (!empty($ADMIN)) {
print "<a href='/admin/index.php'>Admin</a> > ";
print "<a href='/admin/list.php'>List Parents</a> > ";
}
print href("/home.php");
?>Home</a> > View and Edit Schedule</h2>
</div>
<div class="colmask fullpage">
<div class="col1">
<style>
#select { border-collapse: collapse; border: 0px solid black }
tr:nth-child(even) { background: #CCCCCC }
tr:nth-child(odd) { background: #FFFFFF }
tr.cash { font-style: italic }
tr.setup { }
tr.sales { font-weight: bold }
td, th { padding: 2px 2px 2px 2px; }
td.box { text-align: center }
</style>
<?php
function sortshift($a, $b)
{
return $a['start-time'] - $b['start-time'];
if ($a['start-time'] == $b['start-time']) {
return 0;
}
if ($a['start-time'] < $b['start-time']) {
return -1;
}
return 1;
}
// Get the list of shifts
$mysqli = db_connect();
if ($mysqli === FALSE) {
print "<b>Error connecting to database</b><br/>\n";
error_log("Error connecting to database");
}
$ENABLED = false;
$query = "SELECT * FROM options WHERE opt_start < NOW()";
if (($result = $mysqli->query($query)) !== FALSE) {
while ($row = $result->fetch_array()) {
$ENABLED = true;
}
$result->close();
}
if (!empty($ADMIN)) {
$ENABLED = true;
unset($ADD_ONLY);
}
$query = "SELECT opt_start FROM options";
if (($result = $mysqli->query($query)) !== FALSE) {
while ($row = $result->fetch_array()) {
$ts = strtotime($row['opt_start']);
$START = strftime("%l:%M %p %A, %B %e, %Y", $ts);
}
$result->close();
}
$query = "SELECT * FROM shifts WHERE troop = '$TROOP'";
if (($result = $mysqli->query($query)) === FALSE) {
print "<b>Error retrieving shifts</b><br/>\n";
error_log("Error retrieving shifts");;
} else {
while (($arr = $result->fetch_array())) {
$idx = $arr['id'];
$shifts[$idx] = $arr;
$shifts[$idx]['start-time'] = strtotime($arr['start']);
$shifts[$idx]['end-time'] = strtotime($arr['end']);
}
$result->close();
}
uasort($shifts, 'sortshift');
$LIMITS = analyze();
if (isset($SCOUT)) {
$num_scouts = count($SCOUT);
} else {
$num_scouts = 0;
}
$errors = "";
if (isset($_POST['cancel'])) {
print "<script>window.location = '";
redirect("/home.php");
print "';</script>\n";
}
if (isset($_POST['submit'])) {
if (!$ENABLED) {
print "<script>window.location = '";
redirect("/home.php");
print "';</script>\n";
exit;
}
// save post params
foreach ($_POST as $key => $value) {
// [1] = shift, [2] = scout
if (preg_match("/^s(\d+)_(\d+)$/", $key, $matches)) {
$shiftid = $matches[1];
$scoutid = $matches[2];
$scout_shifts[$scoutid][$shiftid] = 1;
}
if (preg_match("/^p(\d+)_(\d+)$/", $key, $matches)) {
$shiftid = $matches[1];
$parent_shifts[$shiftid] = 1;
}
if (preg_match("/^snow(\d+)$/", $key, $matches)) {
$shiftid = $matches[1];
$snow_shifts[$shiftid] = 1;
}
}
//print "<pre>\n";
//print_r($_POST);
//print "</pre>\n";
$query = $MYSQL_LOCK;
if (($result = $mysqli->query($query)) !== FALSE) {
while ($row = $result->fetch_row()) {
if ($row[0] != 1) {
print "<b>Error locking database.</b><br/>\n";
error_log("Error locking database.");
exit;
}
}
$result->close();
} else {
print "<b>Error locking database.</b><br/>\n";
error_log("Error locking database.");
exit;
}
// SUCCESSFUL LOCKING
$num_scouts = count($SCOUT);
$id = $PARENT['id'];
$query = "SELECT * FROM parent_shifts WHERE parentid = '$id'";
if (($result = $mysqli->query($query)) !== FALSE) {
while ($row = $result->fetch_array()) {
$parent_shifts_in_db[$row['shiftid']] = 1;
}
$result->close();
}
$query = "SELECT * FROM snow_shifts WHERE parentid = '$id'";
if (($result = $mysqli->query($query)) !== FALSE) {
while ($row = $result->fetch_array()) {
$snow_shifts_in_db[$row['shiftid']] = 1;
}
$result->close();
}
foreach ($SCOUT as $scoutid => $scout) {
$query = "SELECT * FROM scout_shifts WHERE scoutid = '$scoutid'";
if (($result = $mysqli->query($query)) !== FALSE) {
while ($row = $result->fetch_array()) {
$scout_shifts_in_db[$row['scoutid']][$row['shiftid']] = 1;
}
$result->close();
}
}
// $parent_shifts_in_db are the shifts the parent currently has
// $parent_shifts are the shifts the parent wants
// $scout_shifts_in_db are the shifts the scouts currently have
// $scout_shifts are the shifts the scouts want
// $snow_shifts_in_db are the snow shifts the families currently have
// $snow_shifts are the snow shifts the familite want
// STEP 1: CHECK IF DESIRED SHIFTS ARE AVAILABLE
foreach ($shifts as $shiftid => $shift) {
if (!isset($parent_shifts_in_db[$shiftid]) &&
isset($parent_shifts[$shiftid])) {
// not in db, and is checked
$adults = $shift['adults'];
$query = "SELECT COUNT(*) FROM parent_shifts WHERE shiftid = '$shiftid'";
if (($result = $mysqli->query($query)) !== FALSE) {
$row = $result->fetch_row();
$adults -= $row[0];
$result->close();
}
if ($adults < 1) {
$start_str = strftime("%a, %b %e, %l:%M%P", $shift['start-time']);
$errors .= "Shift $shiftid ($start_err) is unavailable for the parent.<br/>";
error_log("Shift $shiftid ($start_err) is unavailable for the parent.");
}
}
$scouts_in_shift = 0;
foreach ($SCOUT as $scoutid => $scout) {
if (!isset($scout_shifts_in_db[$scoutid][$shiftid]) &&
isset($scout_shifts[$scoutid][$shiftid])) {
// not in db, and is checked
$scouts_in_shift++;
}
}
if ($scouts_in_shift > 0) {
$scouts = $shift['scouts'];
$query = "SELECT COUNT(*) FROM scout_shifts WHERE shiftid = '$shiftid'";
if (($result = $mysqli->query($query)) !== FALSE) {
$row = $result->fetch_row();
$scouts -= $row[0];
$result->close();
}
if ($scouts < $scouts_in_shift) {
$start_str = strftime("%a, %b %e, %l:%M%P", $shift['start-time']);
$errors .= "Shift $shiftid ($start_str) is unavailable for your scouts.<br/>";
error_log("Shift $shiftid ($start_str) is unavailable for your scouts.");
}
}
if (!isset($snow_shifts_in_db[$shiftid]) &&
isset($snow_shifts[$shiftid])) {
// not in db, and is checked
$families = $LIMITS['fps'];
$query = "SELECT COUNT(*) FROM snow_shifts WHERE shiftid = '$shiftid'";
if (($result = $mysqli->query($query)) !== FALSE) {
$row = $result->fetch_row();
$families -= $row[0];
$result->close();
}
if ($families < 1) {
$start_str = strftime("%a, %b %e, %l:%M%P", $shift['start-time']);
$errors .= "Shift $shiftid ($start_str) is unavailable for snow removal.<br/>";
error_log("Shift $shiftid ($start_str) is unavailable for snow removal.");
}
}
}
if (empty($errors)) {
// NO ERRORS... update database and redirect
// print "<p>NO ERRORS!</p>\n";
if (isset($scout_shifts)) {
foreach ($scout_shifts as $scoutid => $value) {
foreach ($value as $shiftid => $one) {
if (!isset($scout_shifts_in_db[$scoutid][$shiftid])) {
$query = "INSERT INTO scout_shifts (shiftid, scoutid, troop) VALUES ($shiftid, $scoutid, $TROOP);";
//print "QUERY: $query<br>\n";
if ($mysqli->query($query) == FALSE) {
$errors .= "INSERT scout_shifts failed<br>";
error_log("INSERT scout_shifts failed: $query");
}
}
}
}
}
if (isset($scout_shifts_in_db)) {
foreach ($scout_shifts_in_db as $scoutid => $value) {
foreach ($value as $shiftid => $one) {
if (!isset($scout_shifts[$scoutid][$shiftid])) {
$query = "DELETE FROM scout_shifts WHERE shiftid=$shiftid AND scoutid=$scoutid;";
//print "QUERY: $query<br>\n";
if ($mysqli->query($query) == FALSE) {
$errors .= "DELETE scout_shifts failed<br>";
error_log("DELETE scout_shifts failed: $query");
}
}
}
}
}
$parentid = $PARENT['id'];
if (isset($parent_shifts)) {
foreach ($parent_shifts as $shiftid => $one) {
if (!isset($parent_shifts_in_db[$shiftid])) {
$query = "INSERT INTO parent_shifts (shiftid, parentid, troop) VALUES ($shiftid, $parentid, $TROOP);";
//print "QUERY: $query<br>\n";
if ($mysqli->query($query) == FALSE) {
$errors .= "INSERT parent_shifts failed<br>";
error_log("INSERT parent_shifts failed: $query");
}
}
}
}
if (isset($parent_shifts_in_db)) {
foreach ($parent_shifts_in_db as $shiftid => $one) {
if (!isset($parent_shifts[$shiftid])) {
$query = "DELETE FROM parent_shifts WHERE shiftid=$shiftid AND parentid=$parentid;";
//print "QUERY: $query<br>\n";
if ($mysqli->query($query) == FALSE) {
$errors .= "DELETE parent_shifts failed<br>";
error_log("DELETE parent_shifts failed: $query");
}
}
}
}
if (isset($snow_shifts)) {
foreach ($snow_shifts as $shiftid => $one) {
if (!isset($snow_shifts_in_db[$shiftid])) {
$query = "INSERT INTO snow_shifts (shiftid, parentid, troop) VALUES ($shiftid, $parentid, $TROOP);";
//print "QUERY: $query<br>\n";
if ($mysqli->query($query) == FALSE) {
$errors .= "INSERT snow_shifts failed<br>";
error_log("INSERT snow_shifts failed: $query");
}
}
}
}
if (isset($snow_shifts_in_db)) {
foreach ($snow_shifts_in_db as $shiftid => $one) {
if (!isset($snow_shifts[$shiftid])) {
$query = "DELETE FROM snow_shifts WHERE shiftid=$shiftid AND parentid=$parentid;";
//print "QUERY: $query<br>\n";
if ($mysqli->query($query) == FALSE) {
$errors .= "DELETE snow_shifts failed<br>";
error_log("DELETE snow_shifts failed: $query");
}
}
}
}
}
// else errors!!!!!!
$query = $MYSQL_UNLOCK;
if (($result = $mysqli->query($query)) !== FALSE) {
while ($row = $result->fetch_row()) {
if ($row[0] != 1) {
print "<b>Error unlocking database.</b><br/>\n";
error_log("Error unlocking database.");
exit;
}
}
$result->close();
} else {
print "<b>Error unlocking database.</b><br/>\n";
error_log("Error unlocking database.");
exit;
}
if (!empty($errors)) {
print "<p><b>$errors</b></p>\n";
} else {
if (isset($AUTO_CONTINUE)) {
print "<script>window.location = '";
redirect("/home.php");
print "';</script>\n";
}
}
}
if ($ENABLED) {
print "<p>Please place a checkbox next to the shift(s) you want. ";
if (isset($ADD_ONLY)) {
print "<b>Removing shifts is not available at this time.</b> ";
print "If you need to remove a shift, please add a shift first, then <a href='mailto:[email protected]'>email me</a>.";
}
print "</p>\n";
} else {
print "<p>Tree Scheduling starts <b>$START</b>.</p>";
}
print '<form method ="post" ';
action("/select.php");
print ">\n";
$shifts_printed = 0;
$shifts_editable = 0;
// Get names, current shifts, limits
// parent is already in $PARENT
// scouts are already in $SCOUT[]
// if $scout_shifts and $parent_shifts are empty (because we didn't POST), then read
// the shifts from the database, if there was an error from above, these should be
// non-zero, so we *should* only read them when first coming to this form
if (!isset($parent_shifts)) {
$id = $PARENT['id'];
$query = "SELECT * FROM parent_shifts WHERE parentid = $id";
if (($result = $mysqli->query($query)) !== FALSE) {
while ($row = $result->fetch_array()) {
$parent_shifts[$row['shiftid']] = 1;
}
$result->close();
}
}
if (!isset($snow_shifts)) {
$query = "SELECT * FROM snow_shifts WHERE parentid = $id";
if (($result = $mysqli->query($query)) !== FALSE) {
while ($row = $result->fetch_array()) {
$snow_shifts[$row['shiftid']] = 1;
}
$result->close();
}
}
$num_scouts = 0;
if (!isset($scout_shifts) && isset($SCOUT)) {
foreach ($SCOUT as $key => $value) {
$num_scouts++;
$id = $value['id'];
$query = "SELECT * FROM scout_shifts WHERE scoutid = $id";
if (($result = $mysqli->query($query)) !== FALSE) {
while ($row = $result->fetch_array()) {
$scout_shifts[$row['scoutid']][$row['shiftid']] = 1;
}
$result->close();
}
}
}
print "<p>Scouts are to fill " . $LIMITS['sps'] . " shifts (minimum), parents are to fill " . $LIMITS['spp'] . " shifts (minimum).<br/>";
if ($LIMITS['min'] > 0) {
print "Your family must fill " . ($LIMITS['min'] * ($num_scouts + 1)) . " shifts total (a parent or scout may take the extra shift).<br/>";
}
print "Each family must select 1 snow removal shift.</p>\n";
print "<table id='select'>\n";
print "<tr>";
print "<th>Parent<br/>(" . $LIMITS['spp'] . ")</th>";
if (isset($SCOUT)) {
foreach ($SCOUT as $key2 => $value2) {
print "<th>" . $value2['fname'] . "<br/>(" . $LIMITS['sps'] . ")</th>";
}
}
print "<th>Snow<br/>Removal</th><th style='vertical-align: bottom'>Date/Time</th><th style='vertical-align:bottom'>Description</th>";
print "</tr>\n";
foreach ($shifts as $shiftid => $shift) {
$adults = $shift['adults'];
$scouts = $shift['scouts'];
$snows = $LIMITS['fps'];
$query = "SELECT COUNT(*) FROM parent_shifts WHERE shiftid = '$shiftid'";
if (($result = $mysqli->query($query)) !== FALSE) {
$row = $result->fetch_row();
$adults -= $row[0];
$result->close();
}
$query = "SELECT COUNT(*) FROM scout_shifts WHERE shiftid = '$shiftid'";
if (($result = $mysqli->query($query)) !== FALSE) {
$row = $result->fetch_row();
$scouts -= $row[0];
$result->close();
}
$query = "SELECT COUNT(*) FROM snow_shifts WHERE shiftid = '$shiftid'";
if (($result = $mysqli->query($query)) !== FALSE) {
$row = $result->fetch_row();
$snows -= $row[0];
$result->close();
}
$adult_is_full = false;
if ($adults <= 0) {
$adult_is_full = true;
}
$scout_is_full = false;
if ($scouts <= 0) {
$scout_is_full = true;
}
$snow_is_full = false;
if ($snows <= 0) {
$snow_is_full = true;
}
$is_past = false;
if ($now >= $shift['start-time']) {
$is_past = true;
}
// true if we want to print the line
$is_interesting = false;
$is_editable = false;
$start = localtime($shift['start-time'], true);
$end = localtime($shift['end-time'], true);
// take the date, subtract the day number to determine the start of the week
$is_cash = false;
$is_snow = false;
$end_str = "";
if ($shift['type'] == $SHIFT_CASH_OPEN) {
$start_str = strftime("%a, %b %e, %l:%M%P", $shift['start-time']);
$is_cash = true;
} else if ($shift['type'] == $SHIFT_CASH_CLOSE) {
$start_str = strftime("%a, %b %e, %l:%M%P", $shift['start-time']);
$is_cash = true;
} else {
$start_str = strftime("%a, %b %e, %l:%M%P-", $shift['start-time']);
$end_str = trim(strftime("%l:%M%P", $shift['end-time'])); // trim any leading %l space
}
if ($shift['description'] == "Open Sales") {
$is_snow = true;
}
// determine how to start the line
if ($shift['type'] == $SHIFT_CASH_CLOSE ||
$shift['type'] == $SHIFT_CASH_OPEN) {
$line = "<tr class='cash'>";
} else if ($shift['type'] == $SHIFT_SETUP) {
$line = "<tr class='setup'>";
} else {
$line = "<tr class='sales'>";
}
// PARENT CELL
$variable = "p" . $shiftid . "_" . $PARENT['id'];
$line .= "<td class='box'>";
if ($ENABLED) {
if ($is_past) {
if (isset($parent_shifts[$shiftid])) {
$is_interesting = true;
$line .= "$X<input type='hidden' value='1' name='$variable' />";
} else {
// else don't put anything in the cell
$line .= " ";
}
} else {
if (isset($parent_shifts[$shiftid])) {
$is_interesting = true;
if (isset($ADD_ONLY) && empty($errors)) {
$line .= "$X<input type='hidden' value='1' name='$variable' />";
} else {
$is_editable = true;
$line .= "<input type='checkbox' checked='checked' name='$variable' />";
}
} else if ($adult_is_full) {
$line .= "FULL";
} else {
$is_interesting = true;
$is_editable = true;
$line .= "<input type='checkbox' name='$variable' />";
}
}
} else {
$is_interesting = true;
}
$line .= "</td>";
// SCOUT CELLS
if (isset($SCOUT)) {
foreach ($SCOUT as $scoutid => $scout) {
$variable = "s" . $shiftid . "_" . $scoutid;
$line .= "<td class='box'>";
if ($ENABLED) {
if ($is_cash) {
// scouts can't do cash shifts
$line .= " ";
} else if ($is_past) {
if (isset($scout_shifts[$scoutid][$shiftid])) {
$is_interesting = true;
$line .= "$X<input type='hidden' value='1' name='$variable' />";
} else {
// else don't put anything in the cell
$line .= " ";
}
} else {
if (isset($scout_shifts[$scoutid][$shiftid])) {
$is_interesting = true;
if (isset($ADD_ONLY) && empty($errors)) {
$line .= "$X<input type='hidden' value='1' name='$variable' />";
} else {
$is_editable = true;
$line .= "<input type='checkbox' checked='checked' name='$variable' />";
}
} else if ($scout_is_full) {
$line .= "FULL";
} else {
$is_interesting = true;
$is_editable = true;
$line .= "<input type='checkbox' name='$variable' />";
}
}
}
$line .= "</td>";
}
}
// SNOW CELL
$variable = "snow" . $shiftid;
$line .= "<td class='box'>";
if ($ENABLED) {
if ($is_past) {
if (isset($snow_shifts[$shiftid])) {
$is_interesting = true;
$line .= "$X<input type='hidden' value='1' name='$variable' />";
} else {
// else don't put anything in the cell
$line .= " ";
}
} else if ($is_snow) {
if (isset($snow_shifts[$shiftid])) {
$is_interesting = true;
if (isset($ADD_ONLY) && empty($errors)) {
$line .= "$X<input type='hidden' value='1' name='$variable' />";
} else {
$is_editable = true;
$line .= "<input type='checkbox' checked='checked' name='$variable' />";
}
} else if ($snow_is_full) {
$line .= "FULL";
} else {
$is_interesting = true;
$is_editable = true;
$line .= "<input type='checkbox' name='$variable' />";
}
} else {
// else don't put anything in the cell
$line .= " ";
}
}
$line .= "</td>";
$line .= "<td>" . $start_str . $end_str . "</td>";
$line .= "<td>" . $shift['description'];
if ($is_snow) {
$line .= " (snow removal)";
}
if ($shift['type'] == $SHIFT_CASH_OPEN ||
$shift['type'] == $SHIFT_CASH_CLOSE) {
$line .= " </i>-- counts as 1/$CASH_VALUE of a shift</i>";
}
if ($scouts > 0 && $scouts < $num_scouts) {
$line .= " </b> -- Only $scouts space";
if ($scouts > 1) {
$line .= "s";
}
$line .= " available for scouts.</b>";
}
$line .= "</td></tr>\n";
if ($is_interesting) {
if ($is_editable) {
$shifts_editable++;
}
$shifts_printed++;
print $line;
}
}
print "</table>\n";
if ($ENABLED) {
if ($shifts_editable > 0) {
print "<input type='submit' name='submit' value='Submit'>\n";
print "<input type='submit' name='cancel' value='Cancel'>\n";
} else {
print "<p>No shifts available!</p>\n";
}
if ($shifts_printed == 0) {
print "<style>\nth { visibility: hidden }\n</style>\n";
}
}
?>
</form>
</div>
</div>
</body>
</html>