Calendar Class in Zend Framework

0
3070
Calendar Class in Zend Framework

Calendar Class in Zend FrameworkIn our previous tutorial of Zend Framework we had covered views in Zend Framework, today in this tutorial we will learn to use a Calendar Class in Zend Framework

Zend framework has an interface to access Google Data. This is one of the easiest ways to access Google data API using PHP.
To use Google calendar we first have to authenticate on Google calendar, list all the calendars and create a new calendar.
Zend_Gdata_Calendar is the service class for interacting with the Google Calendar data API.
The packages needed in the use of Calendar are Zend/Gdata.php and Zend/Loader.php.

  • Authentication process:
    • The Google calendar API allows access to both public and private calendar feeds.
    • Public feeds do not require authentication, but are read-only and provide reduced functionality.
    • Private feeds provide complete functionality, but require an authenticated connection to the calendar server.
    • There are 3 authentication schemas supported by Google calendar:
      1. ClientAuth: It provides direct username/password authentication to the calendar servers. This type is only recommended, when other authentication schemas are insufficient.
      2. AuthSub: It allows authentication to the calendar servers via a Google proxy server.
      3. MagicCookie: It allows authentication based on a semi-random URL available from within the Google Calendar interface. This is the simplest authentication scheme to implement, but requires that the users manually retrieve their secure URL before they can authenticate.
    • An example code for authentication is given below, which uses the ClientAuth schema for authentication.
    • /* Defining Email and Password for Google Accounts access
      */
      define(GOOGLE_ACCOUNTS_EMAIL','');
      define(GOOGLE_ACCOUNTS_PWD','');
       
      require_once 'Zend/Gdata.php';
      require_once 'Zend/Loader.php';
       
      Zend_Loader::registerAutoload();
      $EmailID = GOOGLE_ACCOUNTS_EMAIL;
      $PWD  = GOOGLE_ACCOUNTS_PWD;
       
      // Parameters for ClientAuth authentication
      $service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
      $client = Zend_Gdata_ClientLogin::getHttpClient($EmailID, $PWD, $service);
      
    • After authentication we are now get an access to Google Calendar.
    • Now we can verify a calendar we want to use from the Calendar list or else create a new calendar.
    • The sample code is given below:
    • /* Create the Service instance, to interact with Google Data Api
      */
      $service = new Zend_Gdata_Calendar($client);
       
      $calendarName = 'My Calendar’;
      $useCalendarFeed = FALSE;
       
      try 
      {
      // Get the calendar list from Google calendar API
      $listFeed= $service->getCalendarListFeed();
      }
       catch (Zend_Gdata_App_Exception $e) 
      {
      	echo "Error: " . $e->getMessage();
      }
       
      /* 
      *Need to run through all arrays to search for our calendar name
      * Calendar Listing Feed is available on $listFeed variable
      */
       
      foreach ($listFeed as $calendar) {
      if($calendar->title == $calendarName)
      $useCalendarFeed = $calendar->content->src;
      }
       
      /*
      * If we have not found the calendar we want to use,
      * we need to create a new one
      */
      if(FALSE === $useCalendarFeed)
      {
      $appCal = $service->newListEntry();
      $appCal->title = $service->newTitle($calendarName);
      $own_cal = "http://www.google.com/calendar/feeds/default/owncalendars/full";
      $service->insertEvent($appCal, $own_cal);
       
      /*
      * Need to grab the Google Calendar feed again, with the newly inserted calendar
      * We'll need the refreshed feed in order to get the newly added calendar ID in order to add the event into the created calendar
      */
      try 
      {
      $listFeed= $service->getCalendarListFeed();
      } 
      catch (Zend_Gdata_App_Exception $e) 
      {
      	echo "Error: " . $e->getMessage();
      }
       
      }
      

In this way we learned how to use a calendar and work with it this Calendar Class in Zend Framework tutorial.

LEAVE A REPLY

Please enter your comment!
Please enter your name here