10 defined(
'JPATH_PLATFORM') or die;
31 parent::__construct($options, $auth);
33 if (isset($this->auth) && !$this->auth->getOption(
'scope'))
35 $this->auth->setOption(
'scope',
'https://www.googleapis.com/auth/calendar');
49 public function removeCalendar($calendarID)
51 if ($this->isAuthenticated())
53 $jdata = $this->query(
'https://www.googleapis.com/calendar/v3/users/me/calendarList/' . urlencode($calendarID), null, null,
'delete');
55 if ($jdata->body !=
'')
57 throw new UnexpectedValueException(
"Unexpected data received from Google: `{$jdata->body}`.");
77 public function getCalendar($calendarID)
79 if ($this->isAuthenticated())
81 $jdata = $this->query(
'https://www.googleapis.com/calendar/v3/users/me/calendarList/' . urlencode($calendarID));
83 if ($data = json_decode($jdata->body,
true))
89 throw new UnexpectedValueException(
"Unexpected data received from Google: `{$jdata->body}`.");
109 public function addCalendar($calendarID, $options = array())
111 if ($this->isAuthenticated())
113 $options[
'id'] = $calendarID;
114 $url =
'https://www.googleapis.com/calendar/v3/users/me/calendarList';
115 $jdata = $this->query($url, json_encode($options), array(
'Content-type' =>
'application/json'),
'post');
117 if ($data = json_decode($jdata->body,
true))
123 throw new UnexpectedValueException(
"Unexpected data received from Google: `{$jdata->body}`.");
143 public function listCalendars($options = array(), $maxpages = 1)
145 if ($this->isAuthenticated())
147 $next = array_key_exists(
'nextPageToken', $options) ? $options[
'nextPage'] : null;
148 unset($options[
'nextPageToken']);
149 $url =
'https://www.googleapis.com/calendar/v3/users/me/calendarList?' . http_build_query($options);
151 return $this->listGetData($url, $maxpages, $next);
170 public function editCalendarSettings($calendarID, $options)
172 if ($this->isAuthenticated())
174 $url =
'https://www.googleapis.com/calendar/v3/users/me/calendarList/' . urlencode($calendarID);
175 $jdata = $this->query($url, json_encode($options), array(
'Content-type' =>
'application/json'),
'put');
177 if ($data = json_decode($jdata->body,
true))
183 throw new UnexpectedValueException(
"Unexpected data received from Google: `{$jdata->body}`.");
202 public function clearCalendar($calendarID)
204 if ($this->isAuthenticated())
206 $data = $this->query(
'https://www.googleapis.com/calendar/v3/users/me/calendars/' . urlencode($calendarID) .
'/clear', null, null,
'post');
208 if ($data->body !=
'')
210 throw new UnexpectedValueException(
"Unexpected data received from Google: `{$data->body}`.");
230 public function deleteCalendar($calendarID)
232 if ($this->isAuthenticated())
234 $data = $this->query(
'https://www.googleapis.com/calendar/v3/users/me/calendars/' . urlencode($calendarID), null, null,
'delete');
236 if ($data->body !=
'')
238 throw new UnexpectedValueException(
"Unexpected data received from Google: `{$data->body}`.");
259 public function createCalendar($title, $options = array())
261 if ($this->isAuthenticated())
263 $options[
'summary'] = $title;
264 $url =
'https://www.googleapis.com/calendar/v3/calendars';
265 $jdata = $this->query($url, json_encode($options), array(
'Content-type' =>
'application/json'),
'post');
267 if ($data = json_decode($jdata->body,
true))
273 throw new UnexpectedValueException(
"Unexpected data received from Google: `{$jdata->body}`.");
293 public function editCalendar($calendarID, $options)
295 if ($this->isAuthenticated())
297 $url =
'https://www.googleapis.com/calendar/v3/users/me/calendars/' . urlencode($calendarID);
298 $jdata = $this->query($url, json_encode($options), array(
'Content-type' =>
'application/json'),
'put');
299 $data = json_decode($jdata->body,
true);
301 if ($data && array_key_exists(
'items', $data))
307 throw new UnexpectedValueException(
"Unexpected data received from Google: `{$jdata->body}`.");
327 public function deleteEvent($calendarID, $eventID)
329 if ($this->isAuthenticated())
331 $url =
'https://www.googleapis.com/calendar/v3/users/me/calendars/' . urlencode($calendarID) .
'/events/' . urlencode($eventID);
332 $data = $this->query($url, null, null,
'delete');
334 if ($data->body !=
'')
336 throw new UnexpectedValueException(
"Unexpected data received from Google: `{$data->body}`.");
358 public function getEvent($calendarID, $eventID, $options = array())
360 if ($this->isAuthenticated())
362 $url =
'https://www.googleapis.com/calendar/v3/users/me/calendarList/';
363 $url .= urlencode($calendarID) .
'/events/' . urlencode($eventID) .
'?' . http_build_query($options);
364 $jdata = $this->query($url);
366 if ($data = json_decode($jdata->body,
true))
372 throw new UnexpectedValueException(
"Unexpected data received from Google: `{$jdata->body}`.");
398 public function createEvent($calendarID, $start, $end =
false, $options = array(), $timezone =
false, $allday =
false, $notify =
false)
400 if ($this->isAuthenticated())
404 $startobj =
new DateTime;
406 elseif (is_int($start))
408 $startobj =
new DateTime;
409 $startobj->setTimestamp($start);
411 elseif (is_string($start))
413 $startobj =
new DateTime($start);
415 elseif (is_a($start,
'DateTime'))
421 throw new InvalidArgumentException(
'Invalid event start time.');
428 elseif (is_int($end))
430 $endobj =
new DateTime;
431 $endobj->setTimestamp($end);
433 elseif (is_string($end))
435 $endobj =
new DateTime($end);
437 elseif (is_a($end,
'DateTime'))
443 throw new InvalidArgumentException(
'Invalid event end time.');
448 $options[
'start'] = array(
'date' => $startobj->format(
'Y-m-d'));
449 $options[
'end'] = array(
'date' => $endobj->format(
'Y-m-d'));
453 $options[
'start'] = array(
'dateTime' => $startobj->format(DateTime::RFC3339));
454 $options[
'end'] = array(
'dateTime' => $endobj->format(DateTime::RFC3339));
457 if ($timezone ===
true)
459 $options[
'start'][
'timeZone'] = $startobj->getTimezone()->getName();
460 $options[
'end'][
'timeZone'] = $endobj->getTimezone()->getName();
462 elseif (is_a($timezone,
'DateTimeZone'))
464 $options[
'start'][
'timeZone'] = $timezone->getName();
465 $options[
'end'][
'timeZone'] = $timezone->getName();
467 elseif (is_string($timezone))
469 $options[
'start'][
'timeZone'] = $timezone;
470 $options[
'end'][
'timeZone'] = $timezone;
473 $url =
'https://www.googleapis.com/calendar/v3/calendars/' . urlencode($calendarID) .
'/events' . ($notify ?
'?sendNotifications=true' :
'');
474 $jdata = $this->query($url, json_encode($options), array(
'Content-type' =>
'application/json'),
'post');
476 if ($data = json_decode($jdata->body,
true))
482 throw new UnexpectedValueException(
"Unexpected data received from Google: `{$jdata->body}`.");
504 public function listRecurrences($calendarID, $eventID, $options = array(), $maxpages = 1)
506 if ($this->isAuthenticated())
508 $next = array_key_exists(
'nextPageToken', $options) ? $options[
'nextPage'] : null;
509 unset($options[
'nextPageToken']);
510 $url =
'https://www.googleapis.com/calendar/v3/users/me/calendars/' . urlencode($calendarID) .
'/events/' . urlencode($eventID) .
'/instances';
511 $url .=
'?' . http_build_query($options);
513 return $this->listGetData($url, $maxpages, $next);
533 public function listEvents($calendarID, $options = array(), $maxpages = 1)
535 if ($this->isAuthenticated())
537 $next = array_key_exists(
'nextPageToken', $options) ? $options[
'nextPage'] : null;
538 unset($options[
'nextPageToken']);
539 $url =
'https://www.googleapis.com/calendar/v3/calendars/' . urlencode($calendarID) .
'/events?' . http_build_query($options);
541 return $this->listGetData($url, $maxpages, $next);
562 public function moveEvent($calendarID, $eventID, $destID, $notify =
false)
564 if ($this->isAuthenticated())
566 $url =
'https://www.googleapis.com/calendar/v3/calendars/' . urlencode($calendarID) .
'/events/' . urlencode($eventID) .
'/move';
567 $url .=
'?destination=' . $destID . ($notify ?
'&sendNotifications=true' :
'');
568 $jdata = $this->query($url, null, null,
'post');
570 if ($data = json_decode($jdata->body,
true))
576 throw new UnexpectedValueException(
"Unexpected data received from Google: `{$jdata->body}`.");
598 public function editEvent($calendarID, $eventID, $options, $notify =
false)
600 if ($this->isAuthenticated())
602 $url =
'https://www.googleapis.com/calendar/v3/calendars/';
603 $url .= urlencode($calendarID) .
'/events/' . urlencode($eventID) . ($notify ?
'?sendNotifications=true' :
'');
604 $jdata = $this->query($url, json_encode($options), array(
'Content-type' =>
'application/json'),
'put');
606 if ($data = json_decode($jdata->body,
true))
612 throw new UnexpectedValueException(
"Unexpected data received from Google: `{$jdata->body}`.");