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
|
<?php
/**
* Copyright (c) 2012 Georg Ehrke <ownclouddev@georgswebsite.de>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
/*
* This class does import and converts all times to the users current timezone
*/
class OC_Calendar_Import{
/*
* @brief counts the absolute number of parsed elements
*/
private $abscount;
/*
* @brief var saves if the percentage should be saved with OC_Cache
*/
private $cacheprogress;
/*
* @brief Sabre_VObject_Component_VCalendar object - for documentation see http://code.google.com/p/sabredav/wiki/Sabre_VObject_Component_VCalendar
*/
private $calobject;
/*
* @brief var counts the number of imported elements
*/
private $count;
/*
* @brief var to check if errors happend while initialization
*/
private $error;
/*
* @brief var saves the ical string that was submitted with the __construct function
*/
private $ical;
/*
* @brief calendar id for import
*/
private $id;
/*
* @brief var saves the percentage of the import's progress
*/
private $progress;
/*
* @brief var saves the key for the percentage of the import's progress
*/
private $progresskey;
/*
* @brief var saves the timezone the events shell converted to
*/
private $tz;
/*
* @brief var saves the userid
*/
private $userid;
/*
* public methods
*/
/*
* @brief does general initialization for import object
* @param string $calendar content of ical file
* @param string $tz timezone of the user
* @return boolean
*/
public function __construct($ical){
$this->error = null;
$this->ical = $ical;
$this->abscount = 0;
$this->count = 0;
try{
$this->calobject = OC_VObject::parse($this->ical);
}catch(Exception $e){
//MISSING: write some log
$this->error = true;
return false;
}
return true;
}
/*
* @brief imports a calendar
* @return boolean
*/
public function import(){
if(!$this->isValid()){
return false;
}
$numofcomponents = count($this->calobject->getComponents());
foreach($this->calobject->getComponents() as $object){
if(!($object instanceof Sabre_VObject_Component_VEvent) && !($object instanceof Sabre_VObject_Component_VJournal) && !($object instanceof Sabre_VObject_Component_VTodo)){
continue;
}
$dtend = OC_Calendar_Object::getDTEndFromVEvent($object);
$object->DTSTART->getDateTime()->setTimezone(new DateTimeZone($this->tz));
$object->DTEND->setDateTime($dtend->getDateTime(), $object->DTSTART->getDateType());
$object->DTEND->getDateTime()->setTimezone(new DateTimeZone($this->tz));
$vcalendar = $this->createVCalendar($object->serialize());
$insertid = OC_Calendar_Object::add($this->id, $vcalendar);
$this->abscount++;
if($this->isDuplicate($insertid)){
OC_Calendar_Object::delete($insertid);
}else{
$this->count++;
}
$this->updateProgress(intval(($this->abscount / $numofcomponents)*100));
}
OC_Cache::remove($this->progresskey);
return true;
}
/*
* @brief sets the timezone
* @return boolean
*/
public function setTimeZone($tz){
$this->tz = $tz;
return true;
}
/*
* @brief sets the progresskey
* @return boolean
*/
public function setProgresskey($progresskey){
$this->progresskey = $progresskey;
return true;
}
/*
* @brief checks if something went wrong while initialization
* @return boolean
*/
public function isValid(){
if(is_null($this->error)){
return true;
}
return false;
}
/*
* @brief returns the percentage of progress
* @return integer
*/
public function getProgress(){
return $this->progress;
}
/*
* @brief enables the cache for the percentage of progress
* @return boolean
*/
public function enableProgressCache(){
$this->cacheprogress = true;
return true;
}
/*
* @brief disables the cache for the percentage of progress
* @return boolean
*/
public function disableProgressCache(){
$this->cacheprogress = false;
return false;
}
/*
* @brief generates a new calendar name
* @return string
*/
public function createCalendarName(){
$calendars = OC_Calendar_Calendar::allCalendars($this->userid);
$calendarname = $guessedcalendarname = !is_null($this->guessCalendarName())?($this->guessCalendarName()):(OC_Calendar_App::$l10n->t('New Calendar'));
$i = 1;
while(!OC_Calendar_Calendar::isCalendarNameavailable($calendarname, $this->userid)){
$calendarname = $guessedcalendarname . ' (' . $i . ')';
$i++;
}
return $calendarname;
}
/*
* @brief generates a new calendar color
* @return string
*/
public function createCalendarColor(){
if(is_null($this->guessCalendarColor())){
return '#9fc6e7';
}
return $this->guessCalendarColor();
}
/*
* @brief sets the id for the calendar
* @param integer $id of the calendar
* @return boolean
*/
public function setCalendarID($id){
$this->id = $id;
return true;
}
/*
* @brief sets the userid to import the calendar
* @param string $id of the user
* @return boolean
*/
public function setUserID($userid){
$this->userid = $userid;
return true;
}
/*
* @brief returns the private
* @param string $id of the user
* @return boolean
*/
public function getCount(){
return $this->count;
}
/*
* private methods
*/
/*
* @brief generates an unique ID
* @return string
*/
//private function createUID(){
// return substr(md5(rand().time()),0,10);
//}
/*
* @brief checks is the UID is already in use for another event
* @param string $uid uid to check
* @return boolean
*/
//private function isUIDAvailable($uid){
//
//}
/*
* @brief generates a proper VCalendar string
* @param string $vobject
* @return string
*/
private function createVCalendar($vobject){
if(is_object($vobject)){
$vobject = @$vobject->serialize();
}
$vcalendar = "BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:ownCloud Calendar " . OCP\App::getAppVersion('calendar') . "\n";
$vcalendar .= $vobject;
$vcalendar .= "END:VCALENDAR";
return $vcalendar;
}
/*
* @brief checks if an event already exists in the user's calendars
* @param integer $insertid id of the new object
* @return boolean
*/
private function isDuplicate($insertid){
$newobject = OC_Calendar_Object::find($insertid);
$stmt = OCP\DB::prepare('SELECT COUNT(*) as count FROM *PREFIX*calendar_objects
INNER JOIN *PREFIX*calendar_calendars ON calendarid=*PREFIX*calendar_calendars.id
WHERE objecttype=? AND startdate=? AND enddate=? AND repeating=? AND summary=? AND calendardata=? AND userid = ?');
$result = $stmt->execute(array($newobject['objecttype'],$newobject['startdate'],$newobject['enddate'],$newobject['repeating'],$newobject['summary'],$newobject['calendardata'], $this->userid));
$result = $result->fetchRow();
if($result['count'] >= 2){
return true;
}
return false;
}
/*
* @brief updates the progress var
* @param integer $percentage
* @return boolean
*/
private function updateProgress($percentage){
$this->progress = $percentage;
if($this->cacheprogress){
OC_Cache::set($this->progresskey, $this->progress, 300);
}
return true;
}
/*
* public methods for (pre)rendering of X-... Attributes
*/
/*
* @brief guesses the calendar color
* @return mixed - string or boolean
*/
public function guessCalendarColor(){
if(!is_null($this->calobject->__get('X-APPLE-CALENDAR-COLOR'))){
return $this->calobject->__get('X-APPLE-CALENDAR-COLOR');
}
return null;
}
/*
* @brief guesses the calendar description
* @return mixed - string or boolean
*/
public function guessCalendarDescription(){
if(!is_null($this->calobject->__get('X-WR-CALDESC'))){
return $this->calobject->__get('X-WR-CALDESC');
}
return null;
}
/*
* @brief guesses the calendar name
* @return mixed - string or boolean
*/
public function guessCalendarName(){
if(!is_null($this->calobject->__get('X-WR-CALNAME'))){
return $this->calobject->__get('X-WR-CALNAME');
}
return null;
}
}
|