-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddsheet.php
More file actions
286 lines (248 loc) · 8.77 KB
/
addsheet.php
File metadata and controls
286 lines (248 loc) · 8.77 KB
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
<?php
// Code for Processing Create Account Form.
require "includes/config.php";
// Database Connection
require "includes/library.php";
$pdo = connectDB();
$errors = [];
if(isset($_POST['submit'])) {
$title = $_POST['title'];
$description = $_POST['description'];
$start_event_time = $_POST['start-event-time'];
$end_event_time = $_POST['end-event-time'] ?? null;
$meeting_time_start = $_POST['meeting-time-start'];
$meeting_time_end = $_POST['meeting-time-end'];
$signups = $_POST['signups'];
$searchable = isset($_POST['searchable']) ? 1 : 0;
$weekly = isset($_POST['weekly']) ? 1 : 0;
$creator_id = $_SESSION['user_id'];
$location = $_POST['location'];
// Validate title
if(empty($_POST['title'])) {
$errors['title'] = 'Title is required.';
}
// Validate description
if(empty($_POST['description'])) {
$errors['description'] = 'Description is required.';
}
// Validate start time
if(empty($_POST['start-event-time'])) {
$errors['start-event-time'] = 'Start event time is required.';
}
// Default to 50 if signups is empty
if(empty($signups)) {
$signups = 50;
}
// Validate total signups
if(!is_numeric($signups) || $signups < 0) {
$errors['signups'] = 'Total number of signups must be a number greater than or equal to 0.';
}
// Validate Meeting Start Time
if(empty($_POST['meeting-time-start'])) {
$errors['meeting-time-start'] = 'Meeting start time is required.';
}
// Validate Meeting End Time
if(empty($_POST['meeting-time-end'])) {
$errors['meeting-time-end'] = 'Meeting end time is required.';
}
// Validate Location
if(empty($_POST['location'])) {
$errors['location'] = 'Location is required.';
}
// Sanitize the inputs
$title = htmlspecialchars($title);
$description = htmlspecialchars($description);
$start_event_time = htmlspecialchars($start_event_time);
$end_event_time = htmlspecialchars($end_event_time);
$signups = htmlspecialchars($signups);
$meeting_time_start = htmlspecialchars($meeting_time_start);
$meeting_time_end = htmlspecialchars($meeting_time_end);
$location = htmlspecialchars($location);
if(empty($errors)){
// User data array
$data = [
'title' => $title,
'description' => $description,
'start_event_time' => $start_event_time,
'end_event_time' => $end_event_time,
'total_signups' => $signups,
'is_searchable' => $searchable,
'creator_id' => $creator_id,
'meeting_time_start' => $meeting_time_start,
'meeting_time_end' => $meeting_time_end,
'is_weekly' => $weekly,
'creator_id' => $creator_id,
'location' => $location
];
$fields = array_keys($data);
$values = array_values($data);
$placeholders = array_fill(0, count($data), '?');
$sql = sprintf(
'INSERT INTO A3_3420_Events (%s) VALUES (%s)',
implode(', ', $fields),
implode(', ', $placeholders)
);
$stmt = $pdo->prepare($sql);
$stmt->execute($values);
// Update the table so the remaining signups is equal to the total signups.
$query = "UPDATE A3_3420_Events SET remaining_signups = total_signups WHERE remaining_signups IS NULL";
$stmt = $pdo->prepare($query);
$stmt->execute();
}
}
?>
<!DOCTYPE html>
<html lang="en">
<!--The head tag, contains the meta information along with the title describing the page.-->
<head>
<?php
$PAGE_TITLE = "Add Sheet";
include "includes/metadata.php";
?>
</head>
<!--The body tag, contains the body of the html file-->
<body>
<!-- HEADER -->
<?php include "includes/header.php"?>
<!--The main tag, contains the majority of the form contents for creating an account.-->
<main class="main-content">
<h1>Add a Sign-up Sheet</h1>
<p>
Here you can create a sign-up sheet by entering the relevant details.
</p>
<p>
A Title, Description, the starting time-slot and the number of signups are all required for
this form.
</p>
<!--A post method for data integrity going to an arbitrary createaccount.php file contains divs for
appropriate seperation along with well defined labels and id's to name each section of the form. According to
the assignment document, name, username, email and password are all required.-->
<form action="<?= htmlentities($_SERVER['PHP_SELF']) ?>" method="post">
<div class="form-group">
<label for="title">Event Title:</label>
<input
type="text"
id="title"
name="title"
placeholder="Example Title"
class="form-control"
required
/>
<?php if(isset($errors['title'])): ?>
<span class="error-text"><?php echo $errors['title']; ?></span>
<?php endif; ?>
</div>
<div class="form-group">
<label for="description">Event Description:</label>
<textarea
type="text"
id="description"
name="description"
placeholder="Example Event Description"
class="form-control"
required
></textarea>
<?php if(isset($errors['description'])): ?>
<span class="error-text"><?php echo $errors['description']; ?></span>
<?php endif; ?>
</div>
<div class="form-group">
<label for="location">Location:</label>
<input
type="text"
id="location"
name="location"
placeholder="Stohn Hall"
class="form-control"
required
/>
<?php if(isset($errors['location'])): ?>
<span class="error-text"><?php echo $errors['location']; ?></span>
<?php endif; ?>
</div>
<!--I ideally would like to perform some real-time date validation here but thats not possible within HTML alone.-->
<div class="form-group">
<label for="start-event-time">Application Open Time:</label>
<input
type="date"
id="start-event-time"
name="start-event-time"
class="form-control"
required
/>
<?php if(isset($errors['start-event-time'])): ?>
<span class="error-text"><?php echo $errors['start-event-time']; ?></span>
<?php endif; ?>
</div>
<div class="form-group">
<label for="end-event-time">Application Closing Time:</label>
<input
type="date"
id="end-event-time"
name="end-event-time"
class="form-control"
/>
</div>
<div class="form-group">
<label for="meeting-time-start">Meeting Time Start:</label>
<input
type="datetime-local"
id="meeting-time-start"
name="meeting-time-start"
class="form-control"
required
/>
</div>
<div class="form-group">
<label for="meeting-time-end">Meeting Time End:</label>
<input
type="datetime-local"
id="meeting-time-end"
name="meeting-time-end"
class="form-control"
required
/>
</div>
<div class="form-group">
<label for="signups">Total Number of Signups:</label>
<input
type="text"
id="signups"
name="signups"
placeholder="50"
class="form-control"
/>
<?php if(isset($errors['signups'])): ?>
<span class="error-text"><?php echo $errors['signups']; ?></span>
<?php endif; ?>
<small>Enter 0 to have unlimited registrations. Default is 0.</small>
</div>
<!--Check boxes for dictating if the event should be searchable-->
<div class="form-check">
<input
type="checkbox"
id="searchable"
name="searchable"
value="1"
class="form-check-input"
/>
<label for="searchable" class="form-check-label">Make Event Searchable?</label>
</div>
<div class="form-check">
<input
type="checkbox"
id="weekly"
name="weekly"
value="1"
class="form-check-input"
/>
<label for="weekly" class="form-check-label">Make Event Weekly?</label>
</div>
<!--Submit button sends out everything in the form using the post method.-->
<input class="btn" type="submit" value="Create Event" name="submit" />
</form>
</main>
<!-- FOOTER -->
<?php include "includes/footer.php"?>
</body>
</html>