Check event conflicts in Google calendar using PHP Zend Framework

6 12 2009

In order to insert an event in to the Google calendar, i wanted to first check the calendar to make sure that there are no conflicts with existing events.

I wanted to be able to search either primary or secondary calendars. So i decided to write the code below to retrieve all events falling within the date-time range, then detect the number of events found. If even one event is found, then there is a conflict.

Remember that All-Day events within the date-time range also appear as conflicts.

<?php

/**
 * @author Siddhant
 * @copyright 2009
 */

$dateTimeStart="2009-12-05T16:00:00-05:00";
$dateTimeEnd="2009-12-05T18:00:01-05:00";

//Connect to google calendar
	//If you do not have access to the PHP.INI file on the remote server, you should use the following statement to set the path information for Zend framework
	$clientLibraryPath = 'libraries';	
	$oldPath = set_include_path(get_include_path() . PATH_SEPARATOR . $clientLibraryPath); 
	// load ZEND classes
	require_once 'Zend/Loader.php';
	Zend_Loader::loadClass('Zend_Gdata');
	Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
	Zend_Loader::loadClass('Zend_Gdata_Calendar');
	Zend_Loader::loadClass('Zend_Http_Client');
	// connect to calendar service
	$gcal = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
	$user = "test@gmail.com"; //Insert your google username
	$pass = "testPassword"; //Insert your Google password
	$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $gcal);
	$gcal = new Zend_Gdata_Calendar($client);
//Search the calendar database to see if there is/are existing event(s) in the date/time range
	//generate a query to retrive events
	$query = $gcal->newEventQuery();
    //This sets the default calendar id
	//$query->setUser('default');
	//This sets the calendar to secondary or non-default calendar
	$query->setUser('secondaryCalendarID@group.calendar.google.com'); //The secondary id can be found from the link which looks like: the https://www.google.com/calendar/feeds/secondaryCalendarID@group.calendar.google.com/private/full
    $query->setVisibility('private');
    $query->setProjection('basic');
    //Order the events found by start time in ascending order
    $query->setOrderby('starttime');
    //Set date range
	$query->setStartMin($dateTimeStart);
	$query->setStartMax($dateTimeEnd);
	// Retrieve the event list from the calendar server
	//Remember that all-day events will show up while detecting conflicts
    try {
      $feed = $gcal->getCalendarEventFeed($query);
    } catch (Zend_Gdata_App_Exception $e) {
      echo "Error: " . $e->getResponse();
    }
    //If even one event is found in the date-time range, then there is a conflict.
	if($feed->totalResults!='0')
	{
		echo($feed->totalResults." Conflicts Found <br/>");	
	 	echo("<ol>");
	    foreach ($feed as $event) 
		{
	      echo "<li>\n";
	      echo "<h2>" . stripslashes($event->title) . "</h2>\n";
	      echo stripslashes($event->summary) . " <br/>\n";
	      $id = substr($event->id, strrpos($event->id, '/')+1);
	      echo "</li>\n";
	    }
	    echo "</ul>";
	    echo("</ol>");
	}    
	else
	{
		echo("No Conflicts");	
	}
?>








Follow

Get every new post delivered to your Inbox.

Join 180 other followers