Source for file calendar.php

Documentation is available at calendar.php

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

Documentation generated on Tue, 19 Nov 2013 14:54:57 +0100 by phpDocumentor 1.4.3