/libraries/joomla/google/data/calendar.php

https://bitbucket.org/pastor399/newcastleunifc · PHP · 620 lines · 373 code · 39 blank · 208 comment · 40 complexity · 2f66eabf45dcf0ae8eb7f0b6a75db895 MD5 · raw file

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