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
|
<?php
/* DATA RETRIEVAL
*
* Retrieves events from communi and puts them into the database
*/
function communievents_scrape_events() {
global $wpdb;
$tablename = $wpdb->prefix . 'communievents_events';
$sync_dt = new DateTimeImmutable('now', new DateTimeZone('utc'));
$sync_ts = $sync_dt->getTimestamp();
$api = CommuniApi::defaultInstance();
$events = $api->events($api->userGroupIds());
// falls es gar keine events gibt ist etwas faul!
// lieber hier abbrechen und keine alten Events löschen
if (empty($events))
return;
foreach ($events as $e) {
if ($e->allday()) {
// wir speichern Ganztags-Veranstaltungen als Veranstaltung von 00:00:01 bis 23:59:59
// damit die Queries mit < und > die Veranstaltung den ganzen Tag lang ausspucken
$start_str = $e->start()->format('Y-m-d 00:00:01');
$end_str = $e->end()->format('Y-m-d 23:59:59');
} else {
$start_str = $e->start()->format('Y-m-d H:i:s');
$end_str = $e->end()->format('Y-m-d H:i:s');
}
$wpdb->replace($tablename,
array(
'id' => $e->id(),
'group' => $e->groupId(),
'title' => $e->title(),
'location' => $e->location(),
'description' => $e->description(),
'start' => $start_str,
'end' => $end_str,
'allday' => $e->allday(),
'official' => $e->official(),
'url' => $e->webappUrl(),
'lastsync' => $sync_ts
));
if ($wpdb->last_error !== '')
$wpdb->print_error();
}
// remove all old events
$wpdb->query($wpdb->prepare("DELETE FROM $tablename WHERE lastsync < %d", $sync_ts));
if ($wpdb->last_error !== '')
$wpdb->print_error();
}
function communievents_setup_cron_hook() {
add_action('communievents_cron_hook', 'communievents_scrape_events');
}
|