-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquando.php
More file actions
463 lines (377 loc) · 14.9 KB
/
quando.php
File metadata and controls
463 lines (377 loc) · 14.9 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
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
<?php
namespace Grav\Plugin;
use Grav\Common\Plugin;
use RocketTheme\Toolbox\Event\Event;
/**
* Class QuandoPlugin
* @package Grav\Plugin
*/
class QuandoPlugin extends Plugin
{
public static function getSubscribedEvents() {
return [
'onPluginsInitialized' => ['initializeIfRequired', 0],
];
}
/**
* Initialize the plugin
*/
public function initializeIfRequired() {
// Don't proceed if we are in the admin plugin
if ($this->isAdmin()) {
return;
}
// Enable the main event we are interested in
$this->enable([
'onTwigSiteVariables' => ['initializePlugin', 0],
'onTwigTemplatePaths' => ['addTwigTemplatePaths', 0],
'onTwigExtensions' => ['addTwigExtensions', 0],
]);
}
public function addTwigExtensions() {
// require_once(__DIR__ . '/twig/ExampleTwigExtension.php');
$this->grav['twig']->twig->addExtension(new DatetimeFormatExtension());
$this->grav['twig']->twig->addExtension(new DatetimeFilterExtension());
$this->grav['twig']->twig->addExtension(new TranslateExtension());
}
/**
* Add current directory to twig lookup paths.
*/
public function addTwigTemplatePaths()
{
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
}
public function initializePlugin() {
if (!array_key_exists('quando', $this->grav['twig']->twig_vars)) { // may have been explicitly called earlier
// NB: $services_times not $service_times !!
$this->grav['twig']->twig_vars['quando'] = [];
if (array_key_exists('hours', $this->config['plugins']['quando'])) {
$services_times = $this->config['plugins']['quando']['hours'];
$calendars = [];
foreach($services_times as $name => $service_times) {
$service_times['name'] = $name;
$calendars[$name] = new ServiceTimes($service_times);
}
$this->grav['twig']->twig_vars['quando'] = $calendars;
}
$this->grav['twig']->twig_vars['openhrs'] = $this->grav['twig']->twig_vars['quando']; // TODO: remove deprecated name, only needed for legacy support
}
}
}
class DatetimeFormatExtension extends \Twig_Extension {
public function getName() {
return 'DatetimeFormatExtension';
}
public function getFilters() {
return [
new \Twig_SimpleFilter('briefTime', 'Grav\Plugin\ServiceTimes::briefTime'),
];
}
}
class DatetimeFilterExtension extends \Twig_Extension {
public function getName() {
return 'DatetimeFilterExtension';
}
public function getFilters() {
return [
new \Twig_SimpleFilter('only_dates_from', [$this, 'excludeDatesBefore']),
new \Twig_SimpleFilter('only_periods_from', [$this, 'excludePeriodsBefore']),
];
}
private function defaultToday(&$dstr) {
if(is_null($dstr)) {
$dstr = (new \DateTime())->format('Y-m-d');
}
}
public function excludeDatesBefore($dates, $date_ymd=NULL) {
$this->defaultToday($date_ymd);
return array_filter($dates, function($dt) use ($date_ymd) {
return ($dt >= $date_ymd);
}, ARRAY_FILTER_USE_KEY);
}
public function excludePeriodsBefore($periods, $date_ymd=NULL) {
$this->defaultToday($date_ymd);
return array_filter($periods, function($prd) use ($date_ymd) {
$ends = array_key_exists('end', $prd) ? $prd['end'] : $prd['finish']; // TODO: deprecated array member 'finish'
return ($ends >= $date_ymd);
});
}
}
class TranslateExtension extends \Twig_Extension {
public function getName() {
return 'TranslateExtension';
}
public function getFilters() {
return [
new \Twig_SimpleFilter('tav', [$this, 'translateArrayUsingValue']),
];
}
public function translateArrayUsingValue($key, $term, $lang=NULL, $case_sensitive=FALSE, $default=NULL) {
global $grav;
if (is_null($lang)) {
$lang = $grav['language']->getActive();
if(is_null($lang)) {
$lang = 'en'; // FIXME: yeah, I don't know about this workaround or why default language can be false (or active language null). For now, this prevents a fatal error when language settings are not provided in system.yaml. OTOH there's precedent in core for this kind of kludge: https://github.com/getgrav/grav/blob/42084ea0cb5f3aebcb385a1a797af72000b6d1e1/system/src/Grav/Common/Language/Language.php#L421
}
}
$vocab = $grav['language']->getTranslation($lang, $key, TRUE);
if (!$case_sensitive) {
$term = strtolower($term);
$vocab = array_map('strtolower', $vocab);
}
$position = array_search($term, $vocab);
if ($position === FALSE) {
return $default;
}
return $grav['language']->translateArray($key, $position);
}
}
class ServiceTimes {
private $calendar;
const DOW = [
'sunday' => 0,
'monday' => 1,
'tuesday' => 2,
'wednesday' => 3,
'thursday' => 4,
'friday' => 5,
'saturday' => 6,
]; // from PHP 7, we should be able to do this like const DOW = array_flip(['sunday','monday',....]);
function __construct($calendar) {
global $grav;
$this->grav = $grav;
$this->load($calendar);
return $this;
}
private function load($calendar) {
$this->calendar = $calendar;
$this->timezone = new \DateTimeZone($this->calendar['timezone']);
// TODO - validate the times ??
}
private function deprecatedMethodWarning($method_name) {
$this->grav['debugger']->addMessage("Warning: Call to deprecated $method_name rendering template \"{$this->grav['page']->template()}\"");
}
public function availableOn($day_name, $timetable=NULL) {
if (is_null($timetable)) {
$timetable = $this->calendar['regular'];
}
return ( array_key_exists($day_name, $timetable) AND !empty($timetable[$day_name]) );
}
public function scheduleOn($day_name, $timetable=NULL) {
if (is_null($timetable)) {
$timetable = $this->regularTimetable();
}
$ret = [];
if ($this->availableOn($day_name, $timetable)) {
$ret = $timetable[$day_name];
}
return $ret;
}
private function availableLaterOnDay($dto, $schedule) { // "beforeCOB" ??
if (empty($schedule)) {
return FALSE;
}
else {
$timeOfDay = $dto->format('H:i');
$last_window = array_pop($schedule);
$window_stops = array_key_exists('stops', $last_window) ? $last_window['stops'] : (array_key_exists('finishes', $last_window) ? $last_window['finishes'] : $last_window['shuts']); // TODO: remove deprecated property 'shuts' eventually
return ( $window_stops > $timeOfDay );
}
}
public static function briefTime($timeOfDay, $pattern='g.ia', $truncateZeroComponents=['.i']) {
$to = \DateTime::createFromFormat('Y-m-d H:i', "1970-01-01 $timeOfDay"); // $to is "time object" (no significant date component)
$mod_pattern = $pattern;
foreach ($truncateZeroComponents as $truncateableComponent) {
if (intval(strtr($to->format($truncateableComponent),'.-',' '), 10) == 0) {
$mod_pattern = str_replace($truncateableComponent, '', $mod_pattern);
}
}
return $to->format($mod_pattern);
}
public static function formatSchedule($schedule, $pattern, $truncateZeroComponents=[]) {
$ret = [];
foreach ($schedule as $window) {
foreach ($window as $activity => $timeOfDay) {
$window[$activity] = self::briefTime($timeOfDay, $pattern, $truncateZeroComponents);
}
$ret[] = $window;
}
return $ret;
}
public function statusAt($dto, $includeNext=TRUE) {
$this->grav['debugger']->addMessage("Notice: Confirm template {$this->grav['page']->template()} or its inclusions do not use deprecated properties 'hours', 'open', 'open_on_day', or 'open_later_on_day'"); // TODO: remove deprecation notice
$ret = [];
$dto->setTimezone($this->timezone);
$ret['hours'] = $ret['schedule'] = $this->scheduleAt($dto); // TODO: remove deprecated property
$ret['open'] = $ret['available'] = $this->withinSchedule($dto, $ret['schedule']); // TODO: remove deprecated property
$ret['open_on_day'] = $ret['available_on_day'] = !empty($ret['schedule']); // this isn't worth a function - "tradingDay" ?? // TODO: remove deprecated property
$ret['open_later_on_day'] = $ret['available_later_on_day'] = $this->availableLaterOnDay($dto, $ret['schedule']); // TODO: remove deprecated property
if ($includeNext) {
$ret['until'] = $this->nextChange($dto, $ret['schedule']);
}
return $ret;
}
public function availableAt($dto) {
return $this->statusAt($dto, FALSE)['open'];
}
public function isAvailable() {
$dto = new \DateTime();
return $this->availableAt($dto);
}
private function nextChange($dto, $schedule=NULL) {
if (is_null($schedule)) {
$schedule = $this->scheduleAt($dto);
}
$timeOfDay = $dto->format('H:i');
foreach($schedule as $window) {
$starts = array_key_exists('starts', $window) ? $window['starts'] : (array_key_exists('begins', $window) ? $window['begins'] : $window['opens']); // TODO: remove deprecated property 'opens' eventually
$stops = array_key_exists('stops', $window) ? $window['stops'] : (array_key_exists('finishes', $window) ? $window['finishes'] : $window['shuts']); // TODO: remove deprecated property 'shuts' eventually
// test for easy case where $timeOfDay is before this opening
if ( $timeOfDay < $starts ) {
return \DateTime::createFromFormat('Y-m-d H:i', $dto->format('Y-m-d ') . $starts);
}
if ( $starts <= $timeOfDay AND $stops > $timeOfDay ) {
return \DateTime::createFromFormat('Y-m-d H:i', $dto->format('Y-m-d ') . $stops);
// TODO: handle times going over midnight here, not needed now
}
}
// still here? let's try the next day until we get something (recursive calls)
$nextDay = $dto->add(new \DateInterval('P1D'));
$nextDaySchedule = $this->scheduleAt($nextDay);
$nextDay = \DateTime::createFromFormat('Y-m-d H:i', $nextDay->format('Y-m-d') . ' 00:00'); // bring it back to midnight
return $this->nextChange($nextDay, $nextDaySchedule);
}
private function withinSchedule($dto, $schedule) {
$timeOfDay = $dto->format('H:i');
foreach($schedule as $window) {
$starts = array_key_exists('starts', $window) ? $window['starts'] : (array_key_exists('begins', $window) ? $window['begins'] : $window['opens']); // TODO: remove deprecated property 'opens' eventually
$stops = array_key_exists('stops', $window) ? $window['stops'] : (array_key_exists('finishes', $window) ? $window['finishes'] : $window['shuts']); // TODO: remove deprecated property 'shuts' eventually
if ( $starts <= $timeOfDay AND $stops > $timeOfDay ) {
return TRUE;
}
}
return FALSE;
}
private function dayStringFromNumber($day_number) {
return array_search($day_number, self::DOW);
}
private function dayStringFromDateTime($dto) {
return $this->dayStringFromNumber($dto->format('w'));
}
private function scheduleAt($dto) {
$date_ymd = $dto->format('Y-m-d');
$day_name = $this->dayStringFromDateTime($dto);
if ($day_name) {
// look for period matches
if(array_key_exists('periods', $this->calendar)) {
// NB - we are taking first match from periods array, so that's the precedence
foreach($this->calendar['periods'] as $period) {
$begins = array_key_exists('begin', $period) ? $period['begin'] : $period['start']; // TODO: remove deprecated property 'start' eventually
$ends = array_key_exists('end', $period) ? $period['end'] : $period['finish']; // TODO: remove deprecated property 'finish' eventually
if ( $begins <= $date_ymd AND $ends >= $date_ymd ) {
return $this->findInSchedule($day_name, $date_ymd, $period);
}
}
}
// look for global matches
return $this->findInSchedule($day_name, $date_ymd, $this->calendar);
}
return [];
}
/*
public function schedulesBetween($start, $finish) { // TODO: stub, make this wrap schedulesAfter()
}
*/
public function schedulesAfter($start_dto, $days_duration) {
$ret = []; $day = [];
for ($i=0; $i < $days_duration ; $i++) {
$day[] = ( $i == 0 ? clone $start_dto : clone $day[$i-1] );
if ($i > 0) {
$day[$i]->add(new \DateInterval('P1D')); // thanks PHP, WTF were you thinking when you created this class API ??
}
$ret[] = ['day' => $day[$i], 'schedule' => $this->scheduleAt($day[$i]), 'windows' => $this->scheduleAt($day[$i])]; // TODO: remove deprecated 'schedule' property here
}
return $ret;
}
public function regularTimetable() {
return $this->calendar['regular'];
}
public function getTimetable($timetable=NULL) {
if (is_null($timetable)) {
return $this->calendar;
}
elseif (!array_key_exists($timetable, $this->calendar)) {
return [];
}
else {
return($this->calendar[$timetable]);
}
}
public function schedulesWeek($start_dto, $days_context=NULL) {
$day = clone $start_dto;
if ($days_context) {
$date_method = ( $days_context < 0 ? 'sub' : 'add' );
call_user_func( [$day, $date_method], new \DateInterval('P' . abs($days_context) . 'D'));
}
return $this->schedulesAfter($day, 7);
}
private function findInSchedule($day_name, $date_ymd, $schedule) {
// look for exception matches
if (array_key_exists('exceptions', $schedule)) {
if(array_key_exists($date_ymd, $schedule['exceptions'])) {
return $schedule['exceptions'][$date_ymd]['hours'];
}
}
// look for regular day matches
return $this->scheduleOn($day_name, $schedule['regular']);
}
/* retrieve (only!) specific metadata properties from calendar, or an indexed array of all allowed metadata properties' values */
public function getMeta($property=NULL) {
$allowed_properties = ['headings', 'labels', 'microdata'];
if (is_null($property)) {
$ret = [];
foreach($allowed_properties as $ap) {
if (array_key_exists($ap, $this->calendar)) { // prevents a Twig error if labels is not declared, not sure if I should be requiring it instead
$ret[$ap] = $this->calendar[$ap];
}
}
return $ret;
}
if (!array_key_exists($property, $this->calendar)) {
$this->grav['debugger']->addMessage("Warning: ServiceTimes::getMeta() called for non-existent property \"$property\" rendering template \"{$this->grav['page']->template()}\"");
return [];
}
if (!in_array($property, $allowed_properties)) {
$this->grav['debugger']->addMessage("Warning: ServiceTimes::getMeta() called for disallowed property \"$property\" rendering template \"{$this->grav['page']->template()}\"");
return [];
}
// else
return($this->calendar[$property]);
}
/* ******************************************************************* */
/* Deprecated methods below retained for API compatibility: DO NOT USE */
public function opensOn($day_name, $calendar=NULL) { // TODO: remove deprecated name
$this->deprecatedMethodWarning(__METHOD__);
return $this->availableOn($day_name, $calendar);
}
public function hoursOn($day_name, $calendar=NULL) { // TODO: remove deprecated name
$this->deprecatedMethodWarning(__METHOD__);
return $this->scheduleOn($day_name, $calendar);
}
public function openAt($dto) { // TODO: remove deprecated name
$this->deprecatedMethodWarning(__METHOD__);
return $this->availableAt($dto);
}
public function isOpen() { // TODO: remove deprecated name
$this->deprecatedMethodWarning(__METHOD__);
return $this->isAvailable();
}
public function getSchedule($member=NULL) { // TODO: remove deprecated name
$this->deprecatedMethodWarning(__METHOD__);
return $this->getTimetable($member);
}
public function regularSchedule() { // TODO: remove deprecated name
$this->deprecatedMethodWarning(__METHOD__);
return $this->regularTimetable();
}
}