Using Google Calendar on CakePHP website

1)Download following

Zend Framework’s Library and save in your_cake_folder/app/vendors/

Here is Zend Component save in app/Controller/Components/

<?php
/**
* Zend Component
**/
class ZendComponent extends Component {

public $components = array();

function __construct(ComponentCollection $collection, $settings = array()) {
parent::__construct($collection, $settings);
}

public function import($key) {
$this->__setIncludePath();
App::import(‘Vendor’, “Zend.$key”, array(
‘file’ => ‘Zend’ . DS . str_replace(DS, ‘/’, $key) . ‘.php’,
));
}

private function __setIncludePath() {
$separator = PATH_SEPARATOR;
$includePath = explode($separator, ini_get(“include_path”));
$includePath[] = dirname(dirname(dirname(__FILE__))) . DS . ‘Vendor’ . DS;
ini_set(“include_path”, implode($separator, $includePath));
}
}

The Calevent component

 

<?php

/**
* User Controller class
* PHP versions 5.1.4
* @date 12-Aug-2011
* @Purpose:This controller handles all the functionalities regarding Google calendar events.
* @filesource
* @author PORIA
* @revision
**/
class CaleventComponent extends Component
{

var $components= array(‘Zend’);
//method to show the calendar events feed
public function showcal($user,$pass)
{

$this->Zend->import(‘Gdata_AuthSub’);
$this->Zend->import(‘Gdata_ClientLogin’);
$this->Zend->import(‘Gdata_Calendar’);

App::import(‘Vendor’, ‘Zend_Gdata_AuthSub’, true, false, ‘Zend/Gdata/AuthSub.php’);
App::import(‘Vendor’, ‘Zend_Gdata_Calendar’, true, false, ‘Zend/Gdata/Calendar.php’);
App::import(‘Vendor’, ‘Zend_Gdata_ClientLogin’, true, false, ‘Zend/Gdata/ClientLogin.php’);

$service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME; // predefined service name for calendar
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
$service = new Zend_Gdata_Calendar($client); // Get the calendar list feed
$query = $service->newEventQuery(); // Set different query parameters
$query->setUser(‘default’);
$query->setVisibility(‘private’);
$query->setProjection(‘basic’);
$query->setOrderby(‘starttime’); // Get the event list
$query->setSortOrder(‘d’);
try
{
$eventFeed = $service->getCalendarEventFeed($query);
return($eventFeed);
}
catch (Zend_Gdata_App_Exception $e)
{
echo “Error: ” . $e->getMessage();
}
}

//function adds the events to database and google account as well
public function createevent($a,$user,$pass)
{

$this->Zend->import(‘Gdata_AuthSub’);
$this->Zend->import(‘Gdata_ClientLogin’);
$this->Zend->import(‘Gdata_Calendar’);

App::import(‘Vendor’, ‘Zend_Gdata_AuthSub’, true, false, ‘Zend/Gdata/AuthSub.php’);
App::import(‘Vendor’, ‘Zend_Gdata_Calendar’, true, false, ‘Zend/Gdata/Calendar.php’);
App::import(‘Vendor’, ‘Zend_Gdata_ClientLogin’, true, false, ‘Zend/Gdata/ClientLogin.php’);
$user = ’emailwhosecalendarevents@youareaccessing’;
$pass = ”;
$servicename = Zend_Gdata_Calendar::AUTH_SERVICE_NAME; // predefined service name for calendar
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $servicename);
$service = new Zend_Gdata_Calendar($client); // Get the calendar list feed
$query = $service->newEventQuery(); // Set different query parameters

// construct event object
// save to server
try {
$event = $service->newEventEntry();
$event->title = $service->newTitle(“hello”);
$when = $service->newWhen();
$user=$this->_userID;
$start = substr($start,0,19);
$end = substr($end,0,19);
$when->startTime = $start;
$when->endTime = $end;
$event->when = array($when);
$createdEvent = $service->insertEvent($event);
$event_id = $createdEvent->id->text;
return $event_id;
}
catch (Zend_Gdata_App_Exception $e)
{
echo “Error: ” . $e->getResponse();
}

}

}
//deleting an event
public function delete($id,$user,$pass)
{

$this->Zend->import(‘Gdata_AuthSub’);
$this->Zend->import(‘Gdata_ClientLogin’);
$this->Zend->import(‘Gdata_Calendar’);

App::import(‘Vendor’, ‘Zend_Gdata_AuthSub’, true, false, ‘Zend/Gdata/AuthSub.php’);
App::import(‘Vendor’, ‘Zend_Gdata_Calendar’, true, false, ‘Zend/Gdata/Calendar.php’);
App::import(‘Vendor’, ‘Zend_Gdata_ClientLogin’, true, false, ‘Zend/Gdata/ClientLogin.php’);

$user = ”;
$pass = ”;
$servicename = Zend_Gdata_Calendar::AUTH_SERVICE_NAME; // predefined service name for calendar
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $servicename);
$service = new Zend_Gdata_Calendar($client);
// Get the event
$event = $service->getCalendarEventEntry(‘http://www.google.com/calendar/feeds/default/private/full/&#8217; . $idofevent);
// Delete the event
$event->delete();

 

}
public function edit($uri)
{

$this->Zend->import(‘Gdata_AuthSub’);
$this->Zend->import(‘Gdata_ClientLogin’);
$this->Zend->import(‘Gdata_Calendar’);

App::import(‘Vendor’, ‘Zend_Gdata_AuthSub’, true, false, ‘Zend/Gdata/AuthSub.php’);
App::import(‘Vendor’, ‘Zend_Gdata_Calendar’, true, false, ‘Zend/Gdata/Calendar.php’);
App::import(‘Vendor’, ‘Zend_Gdata_ClientLogin’, true, false, ‘Zend/Gdata/ClientLogin.php’);
$user = ”;
$pass = ”;
$servicename = Zend_Gdata_Calendar::AUTH_SERVICE_NAME; // predefined service name for calendar
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $servicename);
$service = new Zend_Gdata_Calendar($client);
// Get the event
$event = $service->getCalendarEventEntry(‘http://www.google.com/calendar/feeds/default/private/full/&#8217; . $eventuri);
return $event;

}
}

?>

Conroller

<?php

/**
* User Controller class
* PHP versions 5.1.4
* @date 12-Aug-2011
* @Purpose:This controller handles all the functionalities regarding Google calendar events.
* @filesource
* @author PORIA
* @revision
**/

App::uses(‘Sanitize’, ‘Utility’);
class CalendarsController extends AppController
{
var $name = “Calendars”;
var $helpers = array(‘Html’, ‘Form’, ‘Session’,’General’);
var $components = array(‘RequestHandler’,’Email’,’Upload’,’Zend’,’Calevent’);
var $paginate = array();
var $uses = array(‘Calendar’,’Manager’,’User’);

/******************************* START FUNCTIONS **************************/
function admin_showcal()
{
$a=$this->Calevent->showcal($user,$pass);
}
function admin_createevent()
{
$user=””;
$pass=””;
$event_id=$this->Calevent->createevent($this->data,$user,$pass); //sending data to save event to google database
}

function admin_editevent($id)
{

$this->Zend->import(‘Gdata_AuthSub’);
$this->Zend->import(‘Gdata_ClientLogin’);
$this->Zend->import(‘Gdata_Calendar’);
App::import(‘Vendor’, ‘Zend_Gdata_AuthSub’, true, false, ‘Zend/Gdata/AuthSub.php’);
App::import(‘Vendor’, ‘Zend_Gdata_Calendar’, true, false, ‘Zend/Gdata/Calendar.php’);
App::import(‘Vendor’, ‘Zend_Gdata_ClientLogin’, true, false, ‘Zend/Gdata/ClientLogin.php’);
$user=””;
$pass=””;
$servicename = Zend_Gdata_Calendar::AUTH_SERVICE_NAME; // predefined service name for calendar
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $servicename);
$service = new Zend_Gdata_Calendar($client);
// Get the event
$event = $service->getCalendarEventEntry(‘http://www.google.com/calendar/feeds/default/private/full/&#8217; . @$eventid);

}

function admin_updateevents()
{
$this->Zend->import(‘Gdata_AuthSub’);
$this->Zend->import(‘Gdata_ClientLogin’);
$this->Zend->import(‘Gdata_Calendar’);
$this->Zend->import(‘Gdata_HttpClient’);
App::import(‘Vendor’, ‘Zend_Gdata_AuthSub’, true, false, ‘Zend/Gdata/AuthSub.php’);
App::import(‘Vendor’, ‘Zend_Gdata_Calendar’, true, false, ‘Zend/Gdata/Calendar.php’);
App::import(‘Vendor’, ‘Zend_Gdata_ClientLogin’, true, false, ‘Zend/Gdata/ClientLogin.php’);
App::import(‘Vendor’, ‘Zend_Gdata_ClientLogin’, true, false, ‘Zend/Gdata/HttpClient.php’);
$user = ”;
$pass = ”;
$servicename = Zend_Gdata_Calendar::AUTH_SERVICE_NAME; // predefined service name for calendar
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $servicename);
$service = new Zend_Gdata_Calendar($client);
// Get the event
$event = $service->getCalendarEventEntry(‘http://www.google.com/calendar/feeds/default/private/full/&#8217; . $eventuri);
$event->title = $service->newTitle($title);
$when = $service->newWhen();
$when->startTime = $sdate;
$when->endTime = $edate;
$event->when = array($when);
$event->save();

}

function admin_deleteevent($id)
{
$user=””;
$pass=””;
$this->Calevent->delete($id,$user,$pass);
}
}

AND create view files for each method .

I have imported the files in each method ..these can be imported inside class only once and can be used in every method.

Enjoy!!!

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s