/****************************************************************/
/**
 * This file returns events from Google Calendar in JSON format.
 */
$_user_location = 'public';
require_once 'includes/classes/googlecalendar.class.php';
define('AT_INCLUDE_PATH', '../../../include/');
require AT_INCLUDE_PATH . 'vitals.inc.php';
//Default values
define('CALENDAR_DEF_COLOR', '#3399FF');
$gcalobj = new GoogleCalendar();
$qry = "SELECT * FROM %scalendar_google_sync WHERE userid=%d";
$row = queryDB($qry, array(TABLE_PREFIX, $_SESSION['member_id']), TRUE);
if (count($row) > 0) {
    $_SESSION['sessionToken'] = $row['token'];
    if ($gcalobj->isvalidtoken($_SESSION['sessionToken'])) {
        $client = $gcalobj->getAuthSubHttpClient();
        $query = "SELECT * FROM %scalendar_google_sync WHERE userid= %d";
        $rowval = queryDB($query, array(TABLE_PREFIX, $_SESSION['member_id']), TRUE);
        $prevval = $rowval['calids'];
        outputCalendarByDateRange($client, $_GET['start'], $_GET['end'], $prevval, $gcalobj);
    }
}
/**
 * Iterate through all the Google Calendars and create a JSON encoded array of events.
 *
 * @return array of events in JSON format
 */
function outputCalendarByDateRange($client, $startDate, $endDate, $idsofcal, $gcalobj)
{
    $gdataCal = new Zend_Gdata_Calendar($client);