1 <?php
2 3 4 5 6 7 8
9
10 defined('JPATH_PLATFORM') or die;
11
12 use Joomla\Registry\Registry;
13
14 15 16 17 18 19
20 class JGoogleDataCalendar extends JGoogleData
21 {
22 23 24 25 26 27 28 29
30 public function __construct(Registry $options = null, JGoogleAuth $auth = null)
31 {
32 parent::__construct($options, $auth);
33
34 if (isset($this->auth) && !$this->auth->getOption('scope'))
35 {
36 $this->auth->setOption('scope', 'https://www.googleapis.com/auth/calendar');
37 }
38 }
39
40 41 42 43 44 45 46 47 48 49
50 public function removeCalendar($calendarID)
51 {
52 if ($this->isAuthenticated())
53 {
54 $jdata = $this->query('https://www.googleapis.com/calendar/v3/users/me/calendarList/' . urlencode($calendarID), null, null, 'delete');
55
56 if ($jdata->body != '')
57 {
58 throw new UnexpectedValueException("Unexpected data received from Google: `{$jdata->body}`.");
59 }
60
61 return true;
62 }
63 else
64 {
65 return false;
66 }
67 }
68
69 70 71 72 73 74 75 76 77 78
79 public function getCalendar($calendarID)
80 {
81 if ($this->isAuthenticated())
82 {
83 $jdata = $this->query('https://www.googleapis.com/calendar/v3/users/me/calendarList/' . urlencode($calendarID));
84
85 if ($data = json_decode($jdata->body, true))
86 {
87 return $data;
88 }
89 else
90 {
91 throw new UnexpectedValueException("Unexpected data received from Google: `{$jdata->body}`.");
92 }
93 }
94 else
95 {
96 return false;
97 }
98 }
99
100 101 102 103 104 105 106 107 108 109 110
111 public function addCalendar($calendarID, $options = array())
112 {
113 if ($this->isAuthenticated())
114 {
115 $options['id'] = $calendarID;
116 $url = 'https://www.googleapis.com/calendar/v3/users/me/calendarList';
117 $jdata = $this->query($url, json_encode($options), array('Content-type' => 'application/json'), 'post');
118
119 if ($data = json_decode($jdata->body, true))
120 {
121 return $data;
122 }
123 else
124 {
125 throw new UnexpectedValueException("Unexpected data received from Google: `{$jdata->body}`.");
126 }
127 }
128 else
129 {
130 return false;
131 }
132 }
133
134 135 136 137 138 139 140 141 142 143 144
145 public function listCalendars($options = array(), $maxpages = 1)
146 {
147 if ($this->isAuthenticated())
148 {
149 $next = array_key_exists('nextPageToken', $options) ? $options['nextPage'] : null;
150 unset($options['nextPageToken']);
151 $url = 'https://www.googleapis.com/calendar/v3/users/me/calendarList?' . http_build_query($options);
152
153 return $this->listGetData($url, $maxpages, $next);
154 }
155 else
156 {
157 return false;
158 }
159 }
160
161 162 163 164 165 166 167 168 169 170 171
172 public function editCalendarSettings($calendarID, $options)
173 {
174 if ($this->isAuthenticated())
175 {
176 $url = 'https://www.googleapis.com/calendar/v3/users/me/calendarList/' . urlencode($calendarID);
177 $jdata = $this->query($url, json_encode($options), array('Content-type' => 'application/json'), 'put');
178
179 if ($data = json_decode($jdata->body, true))
180 {
181 return $data;
182 }
183 else
184 {
185 throw new UnexpectedValueException("Unexpected data received from Google: `{$jdata->body}`.");
186 }
187 }
188 else
189 {
190 return false;
191 }
192 }
193
194 195 196 197 198 199 200 201 202 203
204 public function clearCalendar($calendarID)
205 {
206 if ($this->isAuthenticated())
207 {
208 $data = $this->query('https://www.googleapis.com/calendar/v3/users/me/calendars/' . urlencode($calendarID) . '/clear', null, null, 'post');
209
210 if ($data->body != '')
211 {
212 throw new UnexpectedValueException("Unexpected data received from Google: `{$data->body}`.");
213 }
214
215 return true;
216 }
217 else
218 {
219 return false;
220 }
221 }
222
223 224 225 226 227 228 229 230 231 232
233 public function deleteCalendar($calendarID)
234 {
235 if ($this->isAuthenticated())
236 {
237 $data = $this->query('https://www.googleapis.com/calendar/v3/users/me/calendars/' . urlencode($calendarID), null, null, 'delete');
238
239 if ($data->body != '')
240 {
241 throw new UnexpectedValueException("Unexpected data received from Google: `{$data->body}`.");
242 }
243
244 return true;
245 }
246 else
247 {
248 return false;
249 }
250 }
251
252 253 254 255 256 257 258 259 260 261 262
263 public function createCalendar($title, $options = array())
264 {
265 if ($this->isAuthenticated())
266 {
267 $options['summary'] = $title;
268 $url = 'https://www.googleapis.com/calendar/v3/calendars';
269 $jdata = $this->query($url, json_encode($options), array('Content-type' => 'application/json'), 'post');
270
271 if ($data = json_decode($jdata->body, true))
272 {
273 return $data;
274 }
275 else
276 {
277 throw new UnexpectedValueException("Unexpected data received from Google: `{$jdata->body}`.");
278 }
279 }
280 else
281 {
282 return false;
283 }
284 }
285
286 287 288 289 290 291 292 293 294 295 296
297 public function editCalendar($calendarID, $options)
298 {
299 if ($this->isAuthenticated())
300 {
301 $url = 'https://www.googleapis.com/calendar/v3/users/me/calendars/' . urlencode($calendarID);
302 $jdata = $this->query($url, json_encode($options), array('Content-type' => 'application/json'), 'put');
303 $data = json_decode($jdata->body, true);
304
305 if ($data && array_key_exists('items', $data))
306 {
307 return $data;
308 }
309 else
310 {
311 throw new UnexpectedValueException("Unexpected data received from Google: `{$jdata->body}`.");
312 }
313 }
314 else
315 {
316 return false;
317 }
318 }
319
320 321 322 323 324 325 326 327 328 329 330
331 public function deleteEvent($calendarID, $eventID)
332 {
333 if ($this->isAuthenticated())
334 {
335 $url = 'https://www.googleapis.com/calendar/v3/users/me/calendars/' . urlencode($calendarID) . '/events/' . urlencode($eventID);
336 $data = $this->query($url, null, null, 'delete');
337
338 if ($data->body != '')
339 {
340 throw new UnexpectedValueException("Unexpected data received from Google: `{$data->body}`.");
341 }
342
343 return true;
344 }
345 else
346 {
347 return false;
348 }
349 }
350
351 352 353 354 355 356 357 358 359 360 361 362
363 public function getEvent($calendarID, $eventID, $options = array())
364 {
365 if ($this->isAuthenticated())
366 {
367 $url = 'https://www.googleapis.com/calendar/v3/users/me/calendarList/';
368 $url .= urlencode($calendarID) . '/events/' . urlencode($eventID) . '?' . http_build_query($options);
369 $jdata = $this->query($url);
370
371 if ($data = json_decode($jdata->body, true))
372 {
373 return $data;
374 }
375 else
376 {
377 throw new UnexpectedValueException("Unexpected data received from Google: `{$jdata->body}`.");
378 }
379 }
380 else
381 {
382 return false;
383 }
384 }
385
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
403 public function createEvent($calendarID, $start, $end = false, $options = array(), $timezone = false, $allday = false, $notify = false)
404 {
405 if ($this->isAuthenticated())
406 {
407 if (!$start)
408 {
409 $startobj = new DateTime;
410 }
411 elseif (is_int($start))
412 {
413 $startobj = new DateTime;
414 $startobj->setTimestamp($start);
415 }
416 elseif (is_string($start))
417 {
418 $startobj = new DateTime($start);
419 }
420 elseif (is_a($start, 'DateTime'))
421 {
422 $startobj = $start;
423 }
424 else
425 {
426 throw new InvalidArgumentException('Invalid event start time.');
427 }
428
429 if (!$end)
430 {
431 $endobj = $startobj;
432 }
433 elseif (is_int($end))
434 {
435 $endobj = new DateTime;
436 $endobj->setTimestamp($end);
437 }
438 elseif (is_string($end))
439 {
440 $endobj = new DateTime($end);
441 }
442 elseif (is_a($end, 'DateTime'))
443 {
444 $endobj = $end;
445 }
446 else
447 {
448 throw new InvalidArgumentException('Invalid event end time.');
449 }
450
451 if ($allday)
452 {
453 $options['start'] = array('date' => $startobj->format('Y-m-d'));
454 $options['end'] = array('date' => $endobj->format('Y-m-d'));
455 }
456 else
457 {
458 $options['start'] = array('dateTime' => $startobj->format(DateTime::RFC3339));
459 $options['end'] = array('dateTime' => $endobj->format(DateTime::RFC3339));
460 }
461
462 if ($timezone === true)
463 {
464 $options['start']['timeZone'] = $startobj->getTimezone()->getName();
465 $options['end']['timeZone'] = $endobj->getTimezone()->getName();
466 }
467 elseif (is_a($timezone, 'DateTimeZone'))
468 {
469 $options['start']['timeZone'] = $timezone->getName();
470 $options['end']['timeZone'] = $timezone->getName();
471 }
472 elseif (is_string($timezone))
473 {
474 $options['start']['timeZone'] = $timezone;
475 $options['end']['timeZone'] = $timezone;
476 }
477
478 $url = 'https://www.googleapis.com/calendar/v3/calendars/' . urlencode($calendarID) . '/events' . ($notify ? '?sendNotifications=true' : '');
479 $jdata = $this->query($url, json_encode($options), array('Content-type' => 'application/json'), 'post');
480
481 if ($data = json_decode($jdata->body, true))
482 {
483 return $data;
484 }
485 else
486 {
487 throw new UnexpectedValueException("Unexpected data received from Google: `{$jdata->body}`.");
488 }
489 }
490 else
491 {
492 return false;
493 }
494 }
495
496 497 498 499 500 501 502 503 504 505 506 507 508
509 public function listRecurrences($calendarID, $eventID, $options = array(), $maxpages = 1)
510 {
511 if ($this->isAuthenticated())
512 {
513 $next = array_key_exists('nextPageToken', $options) ? $options['nextPage'] : null;
514 unset($options['nextPageToken']);
515 $url = 'https://www.googleapis.com/calendar/v3/users/me/calendars/' . urlencode($calendarID) . '/events/' . urlencode($eventID) . '/instances';
516 $url .= '?' . http_build_query($options);
517
518 return $this->listGetData($url, $maxpages, $next);
519 }
520 else
521 {
522 return false;
523 }
524 }
525
526 527 528 529 530 531 532 533 534 535 536 537
538 public function listEvents($calendarID, $options = array(), $maxpages = 1)
539 {
540 if ($this->isAuthenticated())
541 {
542 $next = array_key_exists('nextPageToken', $options) ? $options['nextPage'] : null;
543 unset($options['nextPageToken']);
544 $url = 'https://www.googleapis.com/calendar/v3/calendars/' . urlencode($calendarID) . '/events?' . http_build_query($options);
545
546 return $this->listGetData($url, $maxpages, $next);
547 }
548 else
549 {
550 return false;
551 }
552 }
553
554 555 556 557 558 559 560 561 562 563 564 565 566
567 public function moveEvent($calendarID, $eventID, $destID, $notify = false)
568 {
569 if ($this->isAuthenticated())
570 {
571 $url = 'https://www.googleapis.com/calendar/v3/calendars/' . urlencode($calendarID) . '/events/' . urlencode($eventID) . '/move';
572 $url .= '?destination=' . $destID . ($notify ? '&sendNotifications=true' : '');
573 $jdata = $this->query($url, null, null, 'post');
574
575 if ($data = json_decode($jdata->body, true))
576 {
577 return $data;
578 }
579 else
580 {
581 throw new UnexpectedValueException("Unexpected data received from Google: `{$jdata->body}`.");
582 }
583 }
584 else
585 {
586 return false;
587 }
588 }
589
590 591 592 593 594 595 596 597 598 599 600 601 602
603 public function editEvent($calendarID, $eventID, $options, $notify = false)
604 {
605 if ($this->isAuthenticated())
606 {
607 $url = 'https://www.googleapis.com/calendar/v3/calendars/';
608 $url .= urlencode($calendarID) . '/events/' . urlencode($eventID) . ($notify ? '?sendNotifications=true' : '');
609 $jdata = $this->query($url, json_encode($options), array('Content-type' => 'application/json'), 'put');
610
611 if ($data = json_decode($jdata->body, true))
612 {
613 return $data;
614 }
615 else
616 {
617 throw new UnexpectedValueException("Unexpected data received from Google: `{$jdata->body}`.");
618 }
619 }
620 else
621 {
622 return false;
623 }
624 }
625 }
626