-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathEventObject.php
More file actions
178 lines (158 loc) · 6 KB
/
EventObject.php
File metadata and controls
178 lines (158 loc) · 6 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
<?php
/**
* @category Parser
* @package ics-parser
*/
namespace ICal;
class EventObject
{
public $summary;
public $dtstart;
public $dtend;
public $duration;
public $dtstamp;
public $uid;
public $created;
public $lastmodified;
public $description;
public $location;
public $sequence;
public $status;
public $transp;
public $organizer;
public $attendee;
public $url;
public $categories;
public $xWrSource;
public $xWrSourceUrl;
// Array properties to store timezone info
public $dtstart_array;
public $dtend_array;
public function __construct($data = array())
{
if (!empty($data)) {
foreach ($data as $key => $value) {
if (!property_exists($this, $key)) {
$variable = lcfirst(str_replace(' ', '', ucwords(str_replace('-', ' ', strtolower($key)))));
} else {
$variable = $key;
}
if (is_array($value)) {
$this->{$variable} = $value;
} else {
$this->{$variable} = stripslashes(trim(str_replace('\n', "\n", $value)));
}
}
}
}
public static function __set_state($anArray)
{
$eventObject = new EventObject($anArray);
return $eventObject;
}
/**
* Return Event data excluding anything blank
* as ICS format with proper timezone handling
*
* @return string
*/
public function printIcs()
{
$crlf = "\r\n";
$output = "BEGIN:VEVENT".$crlf;
// Get default timezone
$defaultTimezone = 'Europe/Berlin';
// Handle DTSTART with timezone information
if (!empty($this->dtstart)) {
// Skip values that end with Z (already in UTC)
$isUtc = (substr($this->dtstart, -1) === 'Z');
if (isset($this->dtstart_array) && isset($this->dtstart_array[0]['TZID'])) {
// Use timezone from the event data
$output .= sprintf("DTSTART;TZID=%s:%s%s", $this->dtstart_array[0]['TZID'], $this->dtstart, $crlf);
} elseif (!$isUtc && preg_match("/^\d{8}T\d{6}/", $this->dtstart)) {
// Add default timezone for datetime values without timezone
$output .= sprintf("DTSTART;TZID=%s:%s%s", $defaultTimezone, $this->dtstart, $crlf);
} else {
// Keep all-day events and UTC times as is
$output .= sprintf("DTSTART:%s%s", $this->dtstart, $crlf);
}
}
// Handle DTEND with timezone information
if (!empty($this->dtend)) {
// Skip values that end with Z (already in UTC)
$isUtc = (substr($this->dtend, -1) === 'Z');
if (isset($this->dtend_array) && isset($this->dtend_array[0]['TZID'])) {
// Use timezone from the event data
$output .= sprintf("DTEND;TZID=%s:%s%s", $this->dtend_array[0]['TZID'], $this->dtend, $crlf);
} elseif (!$isUtc && preg_match("/^\d{8}T\d{6}/", $this->dtend)) {
// Add default timezone for datetime values without timezone
$output .= sprintf("DTEND;TZID=%s:%s%s", $defaultTimezone, $this->dtend, $crlf);
} else {
// Keep all-day events and UTC times as is
$output .= sprintf("DTEND:%s%s", $this->dtend, $crlf);
}
}
// Process other properties
$data = array(
'SUMMARY' => $this->summary,
'DURATION' => $this->duration,
'DTSTAMP' => $this->dtstamp,
'UID' => $this->uid,
'CREATED' => $this->created,
'LAST-MODIFIED' => $this->lastmodified,
'DESCRIPTION' => $this->description,
'LOCATION' => $this->location,
'SEQUENCE' => $this->sequence,
'STATUS' => $this->status,
'TRANSP' => $this->transp,
'ORGANISER' => $this->organizer,
'URL' => $this->url,
'CATEGORIES' => $this->categories,
'X-WR-SOURCE' => $this->xWrSource,
'X-WR-SOURCE-URL' => $this->xWrSourceUrl,
);
$data = array_map('trim', $data); // Trim all values
$data = array_filter($data); // Remove any blank values
foreach ($data as $key => $value) {
$output .= sprintf("%s:%s%s", $key, $value, $crlf);
}
$output .= "END:VEVENT".$crlf;
return $output;
}
/**
* Return Event data excluding anything blank
* within an HTML template
*
* @param string $html HTML template to use
* @return string
*/
public function printData($html = '<p>%s: %s</p>')
{
$data = array(
'SUMMARY' => $this->summary,
'DTSTART' => $this->dtstart,
'DTEND' => $this->dtend,
'DTSTART_TZ' => $this->dtstart_tz,
'DTEND_TZ' => $this->dtend_tz,
'DURATION' => $this->duration,
'DTSTAMP' => $this->dtstamp,
'UID' => $this->uid,
'CREATED' => $this->created,
'LAST-MODIFIED' => $this->lastmodified,
'DESCRIPTION' => $this->description,
'LOCATION' => $this->location,
'SEQUENCE' => $this->sequence,
'STATUS' => $this->status,
'TRANSP' => $this->transp,
'ORGANISER' => $this->organizer,
'ATTENDEE(S)' => $this->attendee,
);
$data = array_map('trim', $data); // Trim all values
$data = array_filter($data); // Remove any blank values
$output = '';
foreach ($data as $key => $value) {
$output .= sprintf($html, $key, $value);
}
return $output;
}
}