|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace smtech\CanvasICSSync\SyncIntoCanvas; |
|
4
|
|
|
|
|
5
|
|
|
use vevent; |
|
6
|
|
|
|
|
7
|
|
|
class Event extends Syncable |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Wrapped VEVENT |
|
11
|
|
|
* @var vevent |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $vevent; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Calendar ID |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $calendar; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Unique hash identifying this event |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $hash; |
|
26
|
|
|
|
|
27
|
|
|
protected $canvasId; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct($veventOrHash, $calendar, $calendarEventId = null) |
|
30
|
|
|
{ |
|
31
|
|
|
if (empty($veventOrHash)) { |
|
32
|
|
|
throw new Exception("Valid VEVENT or hash required"); |
|
33
|
|
|
} |
|
34
|
|
|
if (is_a($veventOrHash, vevent::class)) { |
|
35
|
|
|
$this->vevent = $veventOrHash; |
|
36
|
|
|
} else { |
|
37
|
|
|
$this->hash = (string) $veventOrHash; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
if (empty($calendar)) { |
|
41
|
|
|
throw new Exception("Calendar ID required"); |
|
42
|
|
|
} |
|
43
|
|
|
$this->calendar = (string) $calendar; |
|
44
|
|
|
|
|
45
|
|
|
$this->getHash(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function getProperty($property) |
|
49
|
|
|
{ |
|
50
|
|
|
return (empty($this->vevent) ? false : $this->vevent->getProperty($property)); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
const FIELD_MAP = [ |
|
54
|
|
|
'calendar_event[title]' => 'SUMMARY', |
|
55
|
|
|
'calendar_event[description]' => 'DESCRIPTION', |
|
56
|
|
|
'calendar_event[start_at]' => [ |
|
57
|
|
|
0 => 'X-CURRENT-DTSTART', |
|
58
|
|
|
1 => 'DTSTART' |
|
59
|
|
|
], |
|
60
|
|
|
'calendar_event[end_at]' => [ |
|
61
|
|
|
0 => 'X-CURRENT-DTEND', |
|
62
|
|
|
1 => 'DTEND' |
|
63
|
|
|
], |
|
64
|
|
|
'calendar_event[location_name]' => 'LOCATION' |
|
65
|
|
|
]; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Generate a hash of this version of an event to cache in the database |
|
69
|
|
|
**/ |
|
70
|
|
|
public function getHash($algorithm = 'md5') |
|
71
|
|
|
{ |
|
72
|
|
|
if (empty($this->hash)) { |
|
73
|
|
|
$blob = ''; |
|
74
|
|
|
foreach (static::$FIELD_MAP as $field) { |
|
75
|
|
|
if (is_array($field)) { |
|
76
|
|
|
foreach ($field as $option) { |
|
77
|
|
|
if (!empty($property = $this->getProperty($option))) { |
|
78
|
|
|
$blob .= serialize($property); |
|
79
|
|
|
break; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
} else { |
|
83
|
|
|
if (!empty($property = $this->getProperty($field))) { |
|
84
|
|
|
$blob .= serialize($property); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
$this->hash = hash($algorithm, $blob); |
|
89
|
|
|
} |
|
90
|
|
|
return $this->hash; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function isCached() |
|
94
|
|
|
{ |
|
95
|
|
|
return !empty(static::load($this->getHash(), $this->calendar)); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function save() |
|
99
|
|
|
{ |
|
100
|
|
|
$db = static::getDatabase(); |
|
101
|
|
|
|
|
102
|
|
|
$find = $db->prepare( |
|
103
|
|
|
"SELECT * |
|
104
|
|
|
FROM `events` |
|
105
|
|
|
WHERE |
|
106
|
|
|
`event_hash` = :event_hash AND |
|
107
|
|
|
`calendar` = :calendar" |
|
108
|
|
|
); |
|
109
|
|
|
$update = $db->prepare( |
|
110
|
|
|
"UPDATE `events` |
|
111
|
|
|
SET |
|
112
|
|
|
`calendar` = :calendar, |
|
113
|
|
|
`calendar_event[id]` = :calendar_event_id" |
|
114
|
|
|
) |
|
115
|
|
|
} |
|
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
public static function load($id, $calendar) |
|
118
|
|
|
{ |
|
119
|
|
|
$find = static::getDatabase()->prepare( |
|
120
|
|
|
"SELECT * |
|
121
|
|
|
FROM `events` |
|
122
|
|
|
WHERE |
|
123
|
|
|
`event_hash` = :event_hash AND |
|
124
|
|
|
`calendar` = :calendar" |
|
125
|
|
|
); |
|
126
|
|
|
$find->execute([ |
|
127
|
|
|
'event_hash' => $id, |
|
128
|
|
|
'calendar' => $calendar |
|
129
|
|
|
]); |
|
130
|
|
|
if (($cache = $find->fetch()) !== false) { |
|
131
|
|
|
return new Event($cache['event_hash'], $cache['calendar']); |
|
132
|
|
|
} |
|
133
|
|
|
return null; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|