-
Notifications
You must be signed in to change notification settings - Fork 0
/
ics.php
141 lines (128 loc) · 3.49 KB
/
ics.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
<?php
include "userauth.php";
include "analyze.php";
include "shifttypes.php";
header('Content-type: text/calendar; charset=utf-8');
header('Content-Disposition:attachment; filename=trees.ics');
function dateToCal($timestamp)
{
//return date('Ymd\THis\Z', $timestamp);
return date('Ymd\THis', $timestamp);
}
function escapeString($string)
{
return preg_replace('/([\,;])/', '\\\$1', $string);
}
function vcalstart()
{
print "BEGIN:VCALENDAR\n";
print "VERSION:2.0\n";
print "PRODID:-//tshort/tree//NONSGML v1.0//EN\n";
print "CALSCALE:GREGORIAN\n";
}
function vcalend()
{
print "END:VCALENDAR\n";
}
function vevent($datestart, $dateend, $address, $summary, $description)
{
print "BEGIN:VEVENT\n";
print "DTEND:" . dateToCal($dateend) . "\n";
print "UID:" . uniqid() . "\n";
print "DTSAMP:" . dateToCal(time()) . "\n";
print "LOCATION:" . escapeString($address) . "\n";
print "DESCRIPTION:" . escapeString($description) . "\n";
print "SUMMARY:" . escapeString($summary) . "\n";
print "DTSTART:" . dateToCal($datestart) . "\n";
print "END:VEVENT\n";
}
$mysqli = db_connect();
if ($mysqli === FALSE) {
print "<b>Error connecting to database</b><br/>\n";
}
$query = "SELECT * FROM shifts WHERE troop = '$TROOP'";
if (($result = $mysqli->query($query)) === FALSE) {
print "<b>Error retrieving shifts</b><br/>\n";
} 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();
}
$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[$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[$row['shiftid']] = 1;
}
$result->close();
}
foreach ($SCOUT as $id => $value) {
$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();
}
}
vcalstart();
foreach ($shifts as $shiftid => $shift)
{
$found = false;
if (isset($parent_shifts[$shiftid])) {
$found = true;
}
if (isset($snow_shifts[$shiftid])) {
$found = true;
}
foreach ($SCOUT as $scoutid => $scout) {
if (isset($scout_shifts[$scoutid][$shiftid])) {
if (isset($counts[$scoutid])) {
$counts[$scoutid]++;
} else {
$counts[$scoutid] = 1;
}
$found = true;
}
}
if ($found == false) {
continue;
}
$people = "";
if (isset($parent_shifts[$shiftid])) {
$people = $PARENT['pname'];
}
foreach ($SCOUT as $scoutid => $scout) {
if (isset($scout_shifts[$scoutid][$shiftid])) {
if (!empty($people)) {
$people .= " + ";
}
$people .= $scout['sname'];
}
}
if (isset($snow_shifts[$shiftid])) {
if (!empty($people)) {
$people .= " + ";
}
$people = "Snow Removal";
}
vevent($shift['start-time'],
$shift['end-time'],
"Sullivan Tire Parking Lot",
$shift['description'],
$people);
}
vcalend();
?>