contact

or cancel

Adding Google Quick-add to Anything: The Textdate PHP Class

Earlier this year we were asked by a client to create a tee time scheduling application for their golf course, and we decided it would be best to use Google Calendar within a Google Apps account as the “backend” management tool, and the Google Calendar API to make it all tick. One of the fun late-night features we wanted to add was something to replicate Google’s “Quick-add” functionality, so a golfer could just type in slang like “tomorrow at noon” or “next Tuesday at 3pm” to schedule a tee time. We created a class to do this, and think it might be useful to share.

This class takes a string/sentence as an input, and outputs several code-friendly values- bamb, your own quick-add. We use the Zend Gdata library to integrate with the Google Calendar API, which [lucky for us] supports creating events by the quick-add method. The basic function of this class is to retrieve start-time, end-time, and get a title for a “slang” event description.

The Details of Textdate

The textdate.class.php creates an event with the description you input using the Quick-add API, stores the needed details about the event, then deletes the event. We assume you will need a double opt-in system for scheduling, and don’t want to schedule an event without either the user or yourself making sure the text-to-date conversion went as planned. We have added an “add_event” function to handle the actual scheduling, which would be step two after [maybe] checking the event against a database of existing events or schedules.

Setup and Use textdate.class.php

You will need to download the Zend Gdata Library first. For this example, I simple extracted the folder “Zend” inside the “library” folder, and uploaded that in my root directory. There are many ways to make sure the library is accessible to the class, and we take care of it opting for the simple way, but if you are using the library extensively and want to include it by default you can use a PHP include in your PHP configurations, in the .htaccess, or using a line of PHP (read the Zend “INSTALL.txt” for more info).

Assuming you have a Google Apps or Gmail account, you should have access to a Google Calendar, and you will share your login information in the textdate.class.php file to access the calendar API. Below is the basic usage of the class:

<?php
include('textdate.class.php');

$td = new Textdate;
$event_details = $td->convert('Meet Jeff tomorrow at 5pm for 3 hours');

if(!isset($event_details['error']))	{
	echo 'Title: '.$event_details['title'];
	echo '<br/>';
	echo 'Start: '.date('g:sa M j, Y',$event_details['start']);
	echo '<br/>';
	echo 'End: '.date('g:sa M j, Y',$event_details['end']);
}	else	{
	echo $event_details['error'];
}

Title: Meet Jeff
Start: 5:00pm Sep 1, 2010
End: 8:00pm Sep 1, 2010

We are passing around a Unix time-stamp for the “start” and “end”, we find that is easiest to start with for most development. After you run some checks, or give the user a view of some of the details to verify, you can use the “add_event” to actually put the event into the calendar:

$td->add_event($event_details);

There are a couple other things you can look to add, like notes for the event, people/email addresses, where the event will be, etc. We find this should serve as a good starting point for the basic text-to-date conversion though. Here is the class itself, and a ZIP package is given below of both the class and the test file.

<?php
require_once 'Zend/Loader.php';
date_default_timezone_set('America/New_York');

class Textdate	{
	function convert($event_string)
	{
		$gcal = $this->load_calender();

		//add quick event
		$event = $gcal->newEventEntry();
		$event->content = $gcal->newContent($event_string);
		$event->quickAdd = $gcal->newQuickAdd('true');
		$newEvent = $gcal->insertEvent($event);

		$query = $gcal->newEventQuery();
		$query->setUser('default');
		$query->setVisibility('private');
		$query->setProjection('full');
		$query->setOrderby('lastmodified');

		//get feed, or catch error
		try {
			$feed = $gcal->getCalendarEventFeed($query);
		}
		catch(Zend_Gdata_App_Exception $e) {
			$event_details = array(
								'error'=>'Error: '.$e->getResponse()
								);
			return $event_details;
		}

		//store details
		foreach($feed as $event) 	{
			$event_details = array(
								'start'=>strtotime($event->when[0]->getStartTime()),
								'end'=>strtotime($event->when[0]->getEndTime()),
								'title'=>$event->title,
								'url'=>$event->getEditLink()->href
								);
			break;
		}

		//delete event
		$event = $gcal->getCalendarEventEntry($event_details['url']);
		$event->delete();
		return $event_details;
	}

	function add_event($event_details)	{
		$gcal = $this->load_calender();

		try {
			$event = $gcal->newEventEntry();
			$event->title = $gcal->newTitle($event_details['title']);
			$when = $gcal->newWhen();
			$when->startTime = date(DATE_ATOM,$event_details['start']);
			$when->endTime = date(DATE_ATOM,$event_details['end']);
			$event->when = array($when);
			$gcal->insertEvent($event);
		} catch (Zend_Gdata_App_Exception $e) 	{
			return "Error: ".$e->getResponse();
		}
		return TRUE;
	}	

	function load_calender()	{
		// load library
		Zend_Loader::loadClass('Zend_Gdata');
		Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
		Zend_Loader::loadClass('Zend_Gdata_Calendar');
		Zend_Loader::loadClass('Zend_Http_Client');

		// create authenticated HTTP client for Calendar service
		$gcal = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
		$user = "YOUREMAIL";
		$pass = "YOURPASSWORD";
		$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $gcal);
		return $returnCal = new Zend_Gdata_Calendar($client);
	}
}

Download the ZIP Package

Isn’t that Neat?

Google is basically awesome for letting everyone tap into their text-to-event algorithm work, and hopefully you find it at least intriguing, and maybe useful on an application down the road. We encourage you to leave a comment if you like this or need any help getting it running, happy coding ya’ll.

Comments