-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathevent_type.php
408 lines (360 loc) · 11.5 KB
/
event_type.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
<?php
/**
* Event type
*
* This file contains the Event class which represents an event.
* It also contains the events array which contains all events.
*
* To use this file, include it in your PHP file:
* require_once "event_type.php";
* global $events;
*/
// Import config because it needs $fp
require_once "config.php";
require_once "i18n/i18n.php";
// Load the Spyc library
require __DIR__ . '/lib/spyc/Spyc.php';
// Metas contains email addresses of people who should be notified when someone registers for an event
if (file_exists(__DIR__ . '/metas.php')) {
include_once __DIR__ . '/metas.php';
// import the variable mail_handles from metas.php if existant
global $mail_handles;
}
global $fp;
/**
* Represents a FSI Event.
*/
class Event
{
public string $link;
public string $name;
public string $location;
private int $maxParticipants;
public bool $dinosAllowed;
public bool $cancelled;
public string $text;
public string $info;
private array $eventDate;
private array $registrationDate;
public array $form;
public string $csvPath;
public string $icon;
public array $metas;
public function __construct(array $data)
{
global $i18n;
$this->link = $data['link'];
// If the name is set, use it, otherwise use the translation
$this->name = Event::i18n($data, 'name') ?? $i18n->translate(strtolower($this->link) . '_name') ?? '';
$this->location = $data['location'];
$this->maxParticipants = $data['max_participants'] ?? false;
$this->dinosAllowed = $data['dinos'] ?? false;
$this->cancelled = $data['cancelled'] ?? false;
// Text and info
$this->text = Event::i18n($data, 'text') ?? $i18n->translate(strtolower($this->link) . '_text') ?? '';
$this->info = Event::i18n($data, 'info') ?? $i18n->translate(strtolower($this->link) . '_info') ?? '';
// Date and time of the event
$this->eventDate = [
'onTime' => true
];
$this->eventDate = array_merge($this->eventDate, $data['event_date'] ?? []);
$this->registrationDate = $data['registration_date'] ?? [];
// Form fields
$this->form = [
'course_required' => true,
'food' => false,
'breakfast' => false,
];
$this->form = array_merge($this->form, $data['form'] ?? []);
// Misc
$this->csvPath = $data['csv_path'];
$this->icon = $data['icon'];
$this->metas = $data['metas'] ?? [];
}
/**
* Translate the given key if it is an i18n key.
* @param array $data - The data array.
* @param string $key - The key to translate.
* @return string|null - The translated string or null if the key does not exist.
*/
private static function i18n(array $data, string $key)
{
global $i18n;
// Check if the key exists in the data array
if (!isset($data[$key])) {
return null;
}
$value = $data[$key];
if ($i18n::isI18nKey($value)) {
return $i18n->translate($value);
}
return $value;
}
/**
* Load events from a YAML file.
* @param string $filepath - The path to the YAML file.
* @return array - An array of Event objects.
*/
public static function fromYaml(string $filepath): array
{
global $mail_handles, $fp;
// Load all events from the events.yaml file and initialize the $events array
$events = spyc_load_file($filepath);
$events = $events['events'] ?? [];
// Get keys of the events array
$keys = array_keys($events);
// Create an array of Event objects where the link is the key of the event
$event_map = [];
for ($i = 0; $i < count($events); $i++) {
$event = $events[$keys[$i]];
$event['link'] = $keys[$i];
$event_map[$keys[$i]] = new Event($event);
}
$events = $event_map;
// Parse the date and time of each event
$dateFormat = 'd.m.Y H:i';
foreach ($events as /* @var Event $event */ $event) {
///// Date parsing /////
// Event
// 1. Parse the start and end date of the event
// 2. Convert the date to a Unix timestamp
// If the date is invalid, set the timestamp to 0
$startDate = DateTime::createFromFormat($dateFormat, $event->eventDate['start']);
$endDate = DateTime::createFromFormat($dateFormat, $event->eventDate['end'] ?? '');
$event->eventDate['startUTS'] = $startDate ? $startDate->getTimestamp() : 0;
$event->eventDate['endUTS'] = $endDate ? $endDate->getTimestamp() : 0;
// Registration
$startRegDate = DateTime::createFromFormat($dateFormat, $event->registrationDate['start'] ?? '');
$endRegDate = DateTime::createFromFormat($dateFormat, $event->registrationDate['end'] ?? '');
$event->registrationDate['startUTS'] = $startRegDate ? $startRegDate->getTimestamp() : 0;
$event->registrationDate['endUTS'] = $endRegDate ? $endRegDate->getTimestamp() : 0;
///// Metas /////
$metas = $event->metas;
$event->metas = [];
foreach ($metas as $value) {
// Check if the value exists in $mail_handles and append it to the list in $event->metas.
if (isset($mail_handles[$value])) {
$event->metas[] = $mail_handles[$value];
}
}
///// CSV path /////
$event->csvPath = $fp . $event->csvPath;
}
return $events;
}
/**
* Check if the user can register for the event.
* This is the case if the event is not cancelled, the registration is open and there are spots left.
* @return bool
*/
public function canRegister(): bool
{
$now = time();
return (
($now >= $this->getRegistrationStartUTS() && $this->getRegistrationStartUTS() > 0)
|| $this->getRegistrationStartUTS() == 0
)
&& $now <= $this->getRegistrationEndUTS()
&& $this->getRemainingSpots() > 0
&& !$this->cancelled
&& $this->isUpcoming();
}
/**
* Check if the event is past.
* An event is past if the current time is after the event end time.
* @return bool
*/
public function isPast(): bool
{
$now = time();
return !$this->isTakingPlace(
array(
'startUTS' => $this->getEventStartUTS(),
'endUTS' => $this->getEventEndUTS())) &&
(($this->getEventEndUTS() !== 0 && $now > $this->getEventEndUTS()) ||
($this->getEventEndUTS() === 0 && $this->getEventStartUTS() < $now));
}
/**
* Check if the event is upcoming.
* An event is upcoming if the current time is before the event start time.
* @return bool
*/
public function isUpcoming(): bool
{
return !$this->isPast();
}
/**
* Check if the event is active.
* An event is active if the current time is between the event start and end time.
* @param bool $exact - If true, the event is only active if the current time is exactly between the start
* and end time. If false, the event is active if it's the day of the event.
* @return bool
*/
private static function isTakingPlace(array $date, bool $exact = false): bool
{
$startUTS = $date['startUTS'];
$endUTS = $date['endUTS'] ?? 0;
$now = time();
$start_time = $exact ? $startUTS : strtotime('today midnight', $startUTS);
$end_time = $endUTS !== 0 ? $endUTS : strtotime('tomorrow midnight', $startUTS);
return $now >= $start_time && time() <= $end_time;
}
/**
* Check if the event is taking place.
* @param bool $exact - If true, the event is only taking place if the current time is exactly between the start
* and end time. If false, the event is taking place if it's the day of the event.
* @return bool
*/
public function eventIsTakingPlace(bool $exact = false): bool
{
return Event::isTakingPlace(array(
'startUTS' => $this->getEventStartUTS(),
'endUTS' => $this->getEventEndUTS()
), $exact);
}
/**
* Get the start date of the registration.
* @return int
*/
public function getRegistrationStartUTS(): int
{
return $this->registrationDate['startUTS'];
}
/**
* Get the end date of the registration.
* @return int
*/
public function getRegistrationEndUTS(): int
{
return $this->registrationDate['endUTS'];
}
/**
* Get the start date of the event.
* @return int
*/
public function getEventStartUTS(): int
{
return $this->eventDate['startUTS'] ?? 0;
}
/**
* Get the end date of the event.
* @return int
*/
public function getEventEndUTS(): int
{
return $this->eventDate['endUTS'] ?? 0;
}
/**
* Get the event date as a string.
* @param array $options - array of options
* <ul>
* <li>compact: show date in compact mode</li>
* </ul>
* @return string
*/
public function getEventDateString(array $options = array()): string
{
return $this->dateTimeToString(array(
$this->getEventStartUTS(),
$this->getEventEndUTS(),
$this->eventDate['onTime'] ?? true
), $options);
}
/**
* Get the registration date as a string.
* @return string
*/
public function getRegistrationDateString(): string
{
return $this->dateTimeToString(array(
$this->getRegistrationStartUTS(),
$this->getRegistrationEndUTS(),
true
), array('compact' => false, 'noEnd' => true));
}
/**
* Build a date and time string for the given start and end date.
*
* @param array $date
* @param array $options - array of options
* <ul>
* <li>compact: show date in compact mode</li>
* </ul>
*
* @return string
*/
public static function dateTimeToString(array $date, array $options = array()): string
{
global $i18n;
$isGerman = $i18n->getLanguage() == 'de';
$dateFormat = $isGerman ? 'd.m.y' : 'y-m-d';
$timeFormat = $isGerman ? 'H:i' : 'h:i A';
$startDate = date($dateFormat, $date[0] ?? 0);
$startTime = date($timeFormat, $date[0] ?? 0);
$endDate = date($dateFormat, $date[1] ?? 0);
$hasEndDate = $date[1] !== 0;
$onTime = $date[2] ?? true;
// Parse options
$compact = isset($options['compact']) && $options['compact'];
$noEnd = isset($options['noEnd']) && $options['noEnd'];
$noTime = isset($options['no_time']) && $options['no_time'];
// Check if the start and end date are on different days
$isStartEndDiffDays = $startDate != $endDate && $hasEndDate;
$time = time();
$today = date($dateFormat, $time);
if ($today === $startDate) {
$startDate = $i18n['time_today'];
}
// Check if the event is taking place
$isTakingPlace = Event::isTakingPlace(array('startUTS' => $date[0], 'endUTS' => $date[1]));
$dateAndTime = $startDate;
if ($compact) {
// compact mode
// 1.1.2017
// 1.1.2017 - 2.1.2017
// 1.1.2017 12:00 Uhr
if (Event::isTakingPlace(array('startUTS' => $date[0], 'endUTS' => $date[1]), true)) {
$dateAndTime = $i18n['time_takingPlace'];
} elseif ($isTakingPlace) {
$dateAndTime = $i18n['time_today'];
} elseif (!$isStartEndDiffDays && !$noTime) {
$dateAndTime = $dateAndTime . '<br>' . $startTime;
}
} else {
// full date and time
// 1.1.2017 um 12:00 Uhr
// 1.1.2017 ab 12:00 Uhr
// 1.1.2017 um 12:00 Uhr - 2.1.2017
if (!$noTime) {
$startTimeKey = $onTime ? 'time_at' : 'time_from';
$dateAndTime = "$dateAndTime {$i18n->translate($startTimeKey, array('TIME' => $startTime))}";
}
}
if ($isStartEndDiffDays && !$noEnd && $hasEndDate && !$isTakingPlace) {
$dateAndTime = "$dateAndTime " . $i18n->translate('time_to') . " $endDate";
}
return $dateAndTime;
}
public function getRemainingSpots(): int
{
if ($this->maxParticipants) {
$filepath = $this->csvPath;
// Check if the file exists
if (!file_exists($filepath)) {
return $this->maxParticipants;
}
$header_line_count = 1;
$csv_file = file($filepath, FILE_SKIP_EMPTY_LINES);
$spots = $this->maxParticipants - (count($csv_file) - $header_line_count);
if ($spots <= 0) {
$spots = 0;
}
return $spots;
}
return 0;
}
}
// Load the events from the events.yaml file
// The events must be imported using the following code:
// global $events;
$events = Event::fromYaml(__DIR__ . '/events.yml');