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