-
Notifications
You must be signed in to change notification settings - Fork 0
/
createevent.php
388 lines (315 loc) · 12.9 KB
/
createevent.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
<?php include("include.php");
$MONTH = array("","January","February","March","April","May","June","July","August","September","October","November","December");
$DAYS = array(-1, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); // Max days in month
// Not going to take leap years into account, too much unnecessary effort - february has 29 max days for this.
?>
<?php
$actions = "";
$invalid = 0;
if(isset($_GET["id"])){
$group_id = intval($_GET["id"]);
if($stmt = $mysqli->prepare("SELECT g.group_name FROM `group` g WHERE g.group_id=?")){
$stmt->bind_param("i",$group_id);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($group_name);
$group = $stmt;
if($group->fetch()){
$auth_user = 0;
// Check if user is authorized
if($stmt = $mysqli->prepare("SELECT authorized FROM groupuser WHERE group_id=? AND username=?")){
$stmt->bind_param("is",$group_id, $username);
$stmt->execute();
$stmt->bind_result($auth_user);
$stmt->fetch();
$stmt->close();
}
if($auth_user == 1){
$actions = '<div class="actions"><a href="group.php?id='.$group_id.'">Back to Group: '.htmlentities($group_name).'</a></div>';
// If submitted
if(isset($_POST["name"])){
// Make sure event name is specified
if($_POST["name"] == "") $error[] = "You must enter an event name";
// Start date
$start_month = intval($_POST["start_month"]);
$start_day = intval($_POST["start_day"]);
$start_year = intval($_POST["start_year"]);
$start_hour = intval($_POST["start_hour"]);
$start_minute = intval($_POST["start_minute"]);
$start_ampm = intval($_POST["start_ampm"]);
if($start_month < 1 || $start_month > 12 || $start_day < 1 || $start_day > $DAYS[$start_month]){
if($start_day > $DAYS[$start_month]) $error[] = "[Start Date] ".$MONTH[$start_month]." ".$start_day." does not exist.";
else $error[] = "Invalid start date/time";
} else {
if($start_ampm != 1 && $start_ampm != 2) $error[] = "[Start Date] Invalid AM/PM input";
else if($start_hour < 1 || $start_hour > 12 || $start_minute < 0 || $start_minute > 60) $error[] = "[Start Date] ".$_POST["start_hour"].":".$_POST["start_minute"]." is not a valid time.";
}
// End date
$end_month = intval($_POST["end_month"]);
$end_day = intval($_POST["end_day"]);
$end_year = intval($_POST["end_year"]);
$end_hour = intval($_POST["end_hour"]);
$end_minute = intval($_POST["end_minute"]);
$end_ampm = intval($_POST["end_ampm"]);
if($end_month < 1 || $end_month > 12 || $end_day < 1 || $end_day > $DAYS[$end_month]){
if($end_day > $DAYS[$end_month]) $error[] = "[End Date] ".$MONTH[$end_month]." ".$end_day." does not exist.";
else $error[] = "Invalid end date/time";
} else {
if($end_ampm != 1 && $end_ampm != 2) $error[] = "[End Date] Invalid AM/PM input";
else if($end_hour < 1 || $end_hour > 12 || $end_minute < 0 || $end_minute > 60) $error[] = "[End Date] ".$_POST["end_hour"].":".$_POST["end_minute"]." is not a valid time.";
}
// Verify event location
// if other, create new location
if($_POST["location"] == "other"){
if($_POST["loc_name"] == "") $error[] = "[Location] You must specify a location name";
// Assume location description is optional
if($_POST["loc_street"] == "") $error[] = "[Location] You must specify a street address";
if($_POST["loc_city"] == "") $error[] = "[Location] You must specify a city";
if($_POST["loc_zip"] == "") $error[] = "[Location] You must specify a zipcode";
else {
$zipcode = intval($_POST["loc_zip"]);
if($zipcode > 99999 || $zipcode < 1) $error[] = "[Location] You must specify a valid 5-digit zipcode.";
}
if($_POST["loc_latitude"] == "") $error[] = "[Location] You must specify a latitude";
if($_POST["loc_longitude"] == "") $error[] = "[Location] You must specify a longitude";
} else {
if(strlen($_POST["location"]) < 6) $error[] = "Invalid Location";
else {
$location_zipcode = substr($_POST["location"],0,5);
$location_name = substr($_POST["location"],5);
}
}
// If no errors, begin adding event
if(count($error) == 0){
if($start_ampm == 2 && $start_hour < 11) $start_hour += 12;
$start_date = new DateTime();
$start_date->setDate($start_year, $start_month, $start_day);
$start_date->setTime($start_hour, $start_minute, 0);
if($end_ampm == 2 && $end_hour < 11) $end_hour += 12;
$end_date = new DateTime();
$end_date->setDate($end_year, $end_month, $end_day);
$end_date->setTime($end_hour, $end_minute, 0);
$start_date_string = $start_date->format("Y-m-d H:i:s");
$end_date_string = $end_date->format("Y-m-d H:i:s");
if($stmt = $mysqli->prepare("INSERT INTO event (title, description, start_time, end_time, group_id, lname, zip) VALUES(?,?,?,?,?,?,?)")){
$stmt->bind_param("ssssisi", $_POST["name"], $_POST["description"], $start_date_string, $end_date_string, $group_id, $location_name, $location_zipcode);
$stmt->execute();
$stmt->close();
// Get new id and redirect
if($stmt = $mysqli->prepare("SELECT max(event_id) FROM event")){
$stmt->execute();
$stmt->bind_result($event_id);
if($stmt->fetch()){
header("Location: event.php?id=".$event_id."&action=join");
}
}
}
}
}
?>
<html>
<head>
<title>Create New Event</title>
<?php include("header.php"); ?>
</head>
<body>
<?php include("body_header.php"); ?>
<div id="title">Create New Event</div>
<?php print_errors($error, $success); ?>
<?php echo $actions; ?>
<form action="createevent.php?id=<?php echo $group_id; ?>" method="post">
<table cellspacing="0">
<tr>
<th colspan="2" class="table_header">Create New Event</th>
</tr>
<tr>
<th>Group</th>
<td><?php echo htmlentities($group_name); ?>
</tr>
<tr>
<th>Event Name</th>
<td><input name="name" <?php if(isset($_POST["name"])) echo 'value="'.$_POST["name"].'" '; ?>/></td>
</tr>
<tr>
<th>Description</th>
<td>
<textarea name="description" style="width:100%;" rows="10"><?php if(isset($_POST["description"])) echo $_POST["description"]; ?></textarea>
</td>
</tr>
<tr>
<th>Start Time</th>
<td>
<select name="start_month">
<?php
$current_month = date("n");
for($x=1;$x<=12;$x++){
echo '<option value="'.$x.'"';
if(isset($_POST["start_month"])){
if($start_month == $x) echo ' selected';
} else if($current_month == $x) echo ' selected';
echo '>'.$MONTH[$x].'</option>';
}
?>
</select>
<select name="start_day">
<?php
$current_day = date("d");
for($x=1;$x<=31;$x++){
echo '<option value="'.$x.'"';
if(isset($_POST["start_day"])){
if($start_day == $x) echo ' selected';
} else if($current_day == $x) echo ' selected';
echo '>'.$x.'</option>';
}
?>
</select>
<!--<input name="start_day" size="2" placeholder="DD" /> / -->
<input name="start_year" size="4" placeholder="YYYY" <?php if(isset($_POST["start_year"])) echo 'value="'.$_POST["start_year"].'" '; ?>;/> at
<input name="start_hour" size="2" placeholder="hh" <?php if(isset($_POST["start_hour"])) echo 'value="'.$_POST["start_hour"].'" '; ?>/>:
<input name="start_minute" size="2" placeholder="mm" <?php if(isset($_POST["start_minute"])) echo 'value="'.$_POST["start_minute"].'" '; ?>/>
<select name="start_ampm">
<option value="1">AM</option>
<option value="2"<?php if(isset($_POST["start_ampm"]) && $_POST["start_ampm"] == 2) echo ' selected'; ?>>PM</option>
</select>
</td>
</tr>
<tr>
<th>End Time</th>
<td>
<select name="end_month">
<?php
$current_month = date("n");
for($x=1;$x<=12;$x++){
echo '<option value="'.$x.'"';
if(isset($_POST["end_month"])){
if($end_month == $x) echo ' selected';
} else if($current_month == $x) echo ' selected';
echo '>'.$MONTH[$x].'</option>';
}
?>
</select>
<select name="end_day">
<?php
$current_day = date("d");
for($x=1;$x<=31;$x++){
echo '<option value="'.$x.'"';
if(isset($_POST["end_day"])){
if($end_day == $x) echo ' selected';
} else if($current_day == $x) echo ' selected';
echo '>'.$x.'</option>';
}
?>
</select>
<!--<input name="start_day" size="2" placeholder="DD" /> / -->
<input name="end_year" size="4" placeholder="YYYY" <?php if(isset($_POST["end_year"])) echo 'value="'.$_POST["end_year"].'" '; ?>/> at
<input name="end_hour" size="2" placeholder="hh" <?php if(isset($_POST["end_hour"])) echo 'value="'.$_POST["end_hour"].'" '; ?>/>:
<input name="end_minute" size="2" placeholder="mm" <?php if(isset($_POST["end_minute"])) echo 'value="'.$_POST["end_minute"].'" '; ?>/>
<select name="end_ampm">
<option value="1">AM</option>
<option value="2"<?php if(isset($_POST["end_ampm"]) && $_POST["end_ampm"] == 2) echo ' selected'; ?>>PM</option>
</select>
</td>
</tr>
<tr>
<th>Location</th>
<td>
<select name="location" onchange="locationSelector(this)">
<option value="-1">Select Location...</option>
<?php
if($stmt = $mysqli->prepare("SELECT lname, zip, city FROM location")){
$stmt->execute();
$stmt->bind_result($location_name, $location_zipcode, $location_city);
while($stmt->fetch()){
echo '<option value="'.$location_zipcode.$location_name.'"';
if(isset($_POST["location"]) && $_POST["location"] == $location_zipcode.$location_name) echo ' selected';
echo '>'.$location_name.' - '.$location_zipcode.', '.$location_city.'</option>';
}
}
?>
<option value="other"<?php if(isset($_POST["location"]) && $_POST["location"] == "other") echo ' selected'; ?>>Add New Location...</option>
</select>
</td>
</tr>
<tr class="location_row">
<th>Location Name</th>
<td><input name="loc_name" size="20" maxlength="20" <?php if(isset($_POST["loc_name"])) echo 'value="'.$_POST["loc_name"].'" '; ?>/></td>
</tr>
<tr class="location_row">
<th>Location Description</th>
<td><textarea name="loc_description" style="width:100%;" rows="5"><?php if(isset($_POST["loc_description"])) echo $_POST["loc_description"]; ?></textarea></td>
</tr>
<tr class="location_row">
<th>Location Street Address</th>
<td><input name="loc_street" size="30" maxlength="50" <?php if(isset($_POST["loc_street"])) echo 'value="'.$_POST["loc_street"].'" '; ?>/></td>
</tr>
<tr class="location_row">
<th>Location City</th>
<td><input name="loc_city" size="20" maxlength="20" /></td>
</tr>
<tr class="location_row">
<th>Location Zipcode</th>
<td><input name="loc_zip" size="5" maxlength="5" /></td>
</tr>
<tr class="location_row">
<th>Location Latitude</th>
<td><input name="loc_latitude" size="20" /></td>
</tr>
<tr class="location_row">
<th>Location Longitude</th>
<td><input name="loc_longitude" size="20" /></td>
</tr>
<tr>
<th colspan="2">
<input type="submit" value="Create Event" />
</th>
</tr>
</table>
</form>
<?php echo $actions; ?>
<script type="text/javascript">
function setLocationRows(n){
var a = document.getElementsByTagName("tr");
for(var x=0;x<a.length;x++){
if(a[x].className == "location_row"){
a[x].style.display = n;
}
}
}
function locationSelector(e){
if(e.value == "other"){
setLocationRows("");
} else {
setLocationRows("none");
}
}
<?php if(!isset($_POST["location"]) || $_POST["location"] != "other") echo 'setLocationRows("none");'; ?>
</script>
<?php include("body_footer.php"); ?>
</body>
</html>
<?php
} else $invalid = 1;
} else $invalid = 1;
} else $invalid = 1;
} else $invalid = 1;
if($invalid == 1){
?>
<html>
<head>
<title>Create New Event</title>
<?php include("header.php"); ?>
</head>
<body>
<?php include("body_header.php"); ?>
<div id="title">Error</div>
<?php echo $actions; ?>
<div id="main_box">
You are not authorized to perform this action.
<br /><br />
<a href="groups.php">Back to List of Groups</a>
</div>
<?php echo $actions; ?>
<?php include("body_footer.php"); ?>
</body>
</html>
<?php } ?>