Tutorial Zend Framework

22
Create a productivity package with the Zend Framework V1.5 and Google applications, Part 3: Integrate with Google Apps Google Calendar, Spreadsheets, Picasa, and other applications Skill Level: Intermediate John Mertic Software Engineer SugarCRM 05 Aug 2008 In this three-part "Create a productivity package with the Zend Framework V1.5 and Google Apps" tutorial series, you will build an example Web site using Zend Framework V1.5 to connect to several Google productivity applications. So far, we've built a basic Web site using the new features of the Zend Framework V1.5, such as enhancements to the Zend_View and Zend_Form components. We then looked at Zend_Gdata and how to link our Web site to Google Base data. Here in the final part of the series, we see how to link to Google Calendar, Google Docs, spreadsheets, Picasa, and YouTube. We use these tools to provide a dashboard for our Web site. This dashboard allows users to display upcoming events and documents, as well as giving them the ability to add events and documents to the Google account. Section 1. Before you start This tutorial is intended for PHP developers looking to take advantage of the many features of the Zend Framework and integrate with the various Google applications. About this series Integrate with Google Apps Trademarks © Copyright IBM Corporation 2008. All rights reserved. Page 1 of 22

Transcript of Tutorial Zend Framework

Page 1: Tutorial Zend Framework

Create a productivity package with the ZendFramework V1.5 and Google applications, Part 3:Integrate with Google AppsGoogle Calendar, Spreadsheets, Picasa, and other applications

Skill Level: Intermediate

John MerticSoftware EngineerSugarCRM

05 Aug 2008

In this three-part "Create a productivity package with the Zend Framework V1.5 andGoogle Apps" tutorial series, you will build an example Web site using ZendFramework V1.5 to connect to several Google productivity applications. So far, we'vebuilt a basic Web site using the new features of the Zend Framework V1.5, such asenhancements to the Zend_View and Zend_Form components. We then looked atZend_Gdata and how to link our Web site to Google Base data. Here in the final partof the series, we see how to link to Google Calendar, Google Docs, spreadsheets,Picasa, and YouTube. We use these tools to provide a dashboard for our Web site.This dashboard allows users to display upcoming events and documents, as well asgiving them the ability to add events and documents to the Google account.

Section 1. Before you start

This tutorial is intended for PHP developers looking to take advantage of the manyfeatures of the Zend Framework and integrate with the various Google applications.

About this series

Integrate with Google Apps Trademarks© Copyright IBM Corporation 2008. All rights reserved. Page 1 of 22

Page 2: Tutorial Zend Framework

In this series, we will build an application using some of the new Zend FrameworkV1.5 features that also make use of some powerful Google applications:

• In Part 1, learn how to create a Web site using the Zend Framework. Inparticular, many of new features in V1.5 with advanced Web-site layoutfunctionality will be introduced.

• In Part 2, extend the basic site in Part 1 and extend its functionality. Learnabout the many features of the Zend_Form component, including the newAjax features. Also learn about using Google Base with the Zend_Gdatacomponent, and integrate it with our site.

• Here in Part 3, see how to integrate the various Google applications intoour site, including Google Calendar, Google Docs and spreadsheets,Picasa, and YouTube.

About this tutorial

Now that you're familiar with the Zend_Gdata component, we'll show you how toget into the meat of using Google Apps. In this tutorial, you will learn how to create,read, and display Google Calendar information so users can manage their owncalendars from the site. You also learn how to use Google Spreadsheets from withina Zend Framework application, and how to list and search documents stored inGoogle Documents. We also touch on integrating a Zend Framework applicationwith YouTube and Picasa, providing a front end to finding videos and photos withinthese services.

System requirements

You need these tools to follow along:

Web serverAny operating system and any Web server can be used. Feel free to useApache V2.X or IBM's HTTP Server. Download from Apache.org or from IBM.

PHPPHP V5.1.4 or later is required for this tutorial.

Zend FrameworkV1.5 can be downloaded from Zend Technologies.

MySQL Community ServerV5.0 is available from MySQL.com.

jQuery

developerWorks® ibm.com/developerWorks

Integrate with Google Apps Trademarks© Copyright IBM Corporation 2008. All rights reserved. Page 2 of 22

Page 3: Tutorial Zend Framework

V1.2.5 is available from jQuery.com.

Section 2. Google Calendar

In Part 2, we began working with the Zend_Gdata component of the ZendFramework. This component provides integration with the Google API, which opensup the ability to integrate an application with the various Google services, such asGoogle Base. We continue to use this component in the final part of the series tointegrate with other Google services, beginning with Calendar.

Enabling Google Calendar

Google Calendar is an easy-to-use, yet very powerful shared-calendar Webapplication. It's similar to the calendaring features in Microsoft® Outlook® andLotus® Notes®, and it is built to take advantage of shared calendars and the sharingof calendars with the open-standards iCal. And just like with Google Base, we canuse the Zend_Gdata component to connect to Google Calendar, reading public andprivate calendars, and posting new events to an existing Google Calendar. Thisallows users to manage their calendars in the their Google accounts directly fromour site.

For our Web site, we expand on the rather-vacant home page to add a section thatwill display today's events on your Google Calendar. To handle this, we need aGoogle account and place to enter the username and password for that account,separate from the credentials the user has for our Web site. For this, we add anoption to the already-existing user preferences: We add two columns,google_user and google_pass, to the users database table created in Part 2.We also modify the user registration and user preferences forms to grab thisinformation from the user.

Figure 1. Updated user registration page

ibm.com/developerWorks developerWorks®

Integrate with Google Apps Trademarks© Copyright IBM Corporation 2008. All rights reserved. Page 3 of 22

Page 4: Tutorial Zend Framework

Getting Google Calendar events

For the index page, we show the next 10 events upcoming in your calendar inchronological order, similar to a to-do list or a listing of upcoming events, which willbe displayed on the user's index page. We begin by authenticating with the givenuser's Google account, as shown in Listing 1. To handle searching for the events,we make a query to the your calendar, looking for all events (both public eventsanyone can see, as well as private events only the user can see), that occur in thefuture, listed in chronological order.

Listing 1. Getting the Google controller events action

public function geteventsAction(){

Zend_Loader::loadClass('Zend_Gdata');Zend_Loader::loadClass('Zend_Gdata_ClientLogin');Zend_Loader::loadClass('Zend_Gdata_Calendar');Zend_Loader::loadClass('Zend_Http_Client');

$client = Zend_Gdata_ClientLogin::getHttpClient(Zend_Auth::getInstance()->getIdentity()->google_user,Zend_Auth::getInstance()->getIdentity()->google_pass,

developerWorks® ibm.com/developerWorks

Integrate with Google Apps Trademarks© Copyright IBM Corporation 2008. All rights reserved. Page 4 of 22

Page 5: Tutorial Zend Framework

Zend_Gdata_Calendar::AUTH_SERVICE_NAME);

$service = new Zend_Gdata_Calendar($client);

$query = $service->newEventQuery();$query->setUser('default');$query->setVisibility('private');$query->setProjection('full');$query->setOrderby('starttime');$query->setFutureevents('true');

// Retrieve the event list from the calendar servertry {

$this->view->events = $service->getCalendarEventFeed($query);}catch (Zend_Gdata_App_Exception $e) {

return "Error: " . $e->getResponse();}

}

Looking at the view template for this action, shown in Listing 2, we iterate over thereturned list of events. Each event is an object with corresponding properties for thetitle, date/time, and ID for the event itself. The effect is that we create an at-a-glancelist of upcoming events in the calendar of the Google account user.

Listing 2. View template for the event list, getevent.phtml

<ul><?php foreach ($this->events as $event): ?><li><a href="<?php echo $event->getLink('alternate'); ?>"><?php echo $this->escape($event->title); ?> -<?php echo $this->escape(date("l jS \o\f F Y - h:i A",

strtotime($event->when->startTime))); ?></a></li><?php endforeach; ?></ul>

We also do some date formatting on the returned startTime attribute. This isbecause the date is returned in the RFC 3339 date format. This date format isparseable using the strtotime() function, but not very user-friendly, so wereformatted the date in Listing 2. Similar to the Google Base records, a link to theitem can be retrieved using the getLink() method on each event (look for the linktitled "alternate"), which provides a link to access an actual event on GoogleCalendar, where you can view details about the event and edit the event if needed.

Recall from Part 1 when we were building the top menu of our site, we used actionhelpers to return the content produced by another action in the site within the currentview template. We use this technique again here on the index page, returning theoutput from the getevent view template inside our index view template, as shownin Listing 3. This will provide us with a new view of our dashboard showing upcomingevents.

ibm.com/developerWorks developerWorks®

Integrate with Google Apps Trademarks© Copyright IBM Corporation 2008. All rights reserved. Page 5 of 22

Page 6: Tutorial Zend Framework

Listing 3. Updated index.phtml template to include calendar event listing

<?php $this->headTitle()->append('My Zend Framework Application'); ?><p>Hello world!</p><?php echo $this->action('google', 'getevents');

Now that we've added the at-a-glance feature for upcoming events, let's look atsimple way to add events.

Add events to Google Calendar

Google Calendar has two ways to add an event to a calendar. One is similar to howyou add events in traditional calendaring applications by specifying the details of theevent. The details not only include the title, description, start date and time, and enddate and time but also the ability to make this event reoccurring and a way to setreminders for upcoming events. For a user wanting to quickly add an event to hiscalendar, this traditional way can often be cumbersome. Fortunately, GoogleCalendar has a solution to this: Quick Add.

What Quick Add provides is a way to enter events using a free-form text-entry field.We could enter "Proposal Meeting at XYZ Corporation on Monday at 9:30 a.m." inthe text-entry field, which, when submitted, will parse the string into an actual event.So for the previous example, it would create an event called "Proposal Meeting,"with the location "XYZ Corporation," and the date and time "Monday" at "9:30 a.m."This powerful tool makes it easier for users to enter events in a more natural way.

Let's incorporate the Quick Add feature into the site. For this, we add a new action,quickaddevent, to the Google controller, as shown in Listing 4. We take thecontent sent to us by way of an Ajax request and create a Google calendar eventfrom it.

Listing 4. quickaddevent action

public function quickaddeventAction(){

Zend_Loader::loadClass('Zend_Gdata');Zend_Loader::loadClass('Zend_Gdata_ClientLogin');Zend_Loader::loadClass('Zend_Gdata_Calendar');Zend_Loader::loadClass('Zend_Http_Client');

$client = Zend_Gdata_ClientLogin::getHttpClient(Zend_Auth::getInstance()->getIdentity()->google_user,Zend_Auth::getInstance()->getIdentity()->google_pass,Zend_Gdata_Calendar::AUTH_SERVICE_NAME);

$service = new Zend_Gdata_Calendar($client);

$event= $service->newEventEntry();$event->content= $service->newContent($this->_getParam('event',''));$event->quickAdd = $service->newQuickAdd("true");

developerWorks® ibm.com/developerWorks

Integrate with Google Apps Trademarks© Copyright IBM Corporation 2008. All rights reserved. Page 6 of 22

Page 7: Tutorial Zend Framework

$newEvent = $service->insertEvent($event);

if(!$this->_request->isXmlHttpRequest())$this->_redirect('');

else {$this->_helper->layout->disableLayout();$this->_helper->viewRenderer->setNoRender();echo "Added event '{$newEvent->title}' successfully!";

}}

Once the quickaddevent action is added, the Google Calendar service returns afull event object to us immediately. This is handy since it allows us to give the userimmediate feedback on the results of the event being added.

We put the Quick Add form right below the list of events on the index page, so wemodify that view, as shown in Listing 5. We add a text-entry field that, uponsubmitting, sends an Ajax call to the action shown in Listing 4.

Listing 5. Updated index.phtml with the Quick Add functionality

<?php $this->headTitle()->append('My Zend Framework Application'); ?><h1>My Events</h1><?php echo $this->action('google', 'getevents');?><p>Add New Event</p><form id="addquickevent" action="<?php echo WEBROOT; ?>google/quickaddevent"

class="add" method="POST"><input type="text" name="event" /><input type="submit" value="Add Event" /></form><script type="text/javascript"><!--$(document).ready(function(){

$("#addquickevent").submit(function(){posturl = this.action;postdata = { event: this.event.value };$.post(posturl, postdata, function(data){

alert(data);}, 'text');return false;

});});--></script>

Once again, we used the jQuery library to handle our Ajax calls. In this instance, itwas used to handle posting the Ajax data to the quickaddevent action. Uponsuccess, a pop-up will inform us that the name of the added event will be shown tothe user. This gives us the ability to add events easily on the fly for the Web site.

Figure 2. Event added successfully

ibm.com/developerWorks developerWorks®

Integrate with Google Apps Trademarks© Copyright IBM Corporation 2008. All rights reserved. Page 7 of 22

Page 8: Tutorial Zend Framework

We've seen how easy it is to work with our Google Calendar events using theZend_Gdata component of the Zend Framework. We used this integration to addan at-a-glance list of upcoming events for the user, as well allowing users to QuickAdd functionality that allows them to easily add events in a free-form text-entry field.Now let's look at working with Google spreadsheets to add the ability to look atspreadsheets from our site.

Section 3. Google spreadsheets

Google pushed the envelope with what could be done in a Web application withGoogle Docs, a productivity suite that contains an online spreadsheet application, anonline word-processing application, and a presentation application. The Google

developerWorks® ibm.com/developerWorks

Integrate with Google Apps Trademarks© Copyright IBM Corporation 2008. All rights reserved. Page 8 of 22

Page 9: Tutorial Zend Framework

spreadsheets feature set is robust enough to replace the functionality of MicrosoftExcel® for the average user, with support for formulas and complex calculations.Given that the Google spreadsheets feature is Web-based, you have the ability toshare spreadsheets between users and track changes each user has made, whicheases collaboration between users on a single spreadsheet. Once again, theZend_Gdata component allows us to tap into this resource and integrate it into ourWeb site.

For our Web site, we display a list of spreadsheets available for the current Googleaccount user. Like we used for the Google Calendar example above, we also usethe Google account information stored in the current user's profile. The Googleaccount username and password in our Web site's user preferences is used toauthenticate to the Google spreadsheets service.

Getting a list of spreadsheets

In Listing 6, we add an action to grab the list of spreadsheets for the current Googleaccount. Using the Zend_Gdata component in a similar fashion to before, weconnect to the Google spreadsheets service and get the list of spreadsheets, asshown below.

Figure 3. Link to spreadsheets

Listing 6 shows the getspreadsheets action.

Listing 6. getspreadsheets action

public function getspreadsheetsAction(){

Zend_Loader::loadClass('Zend_Gdata');Zend_Loader::loadClass('Zend_Gdata_ClientLogin');Zend_Loader::loadClass('Zend_Gdata_Spreadsheets');Zend_Loader::loadClass('Zend_Http_Client');

$client = Zend_Gdata_ClientLogin::getHttpClient(

ibm.com/developerWorks developerWorks®

Integrate with Google Apps Trademarks© Copyright IBM Corporation 2008. All rights reserved. Page 9 of 22

Page 10: Tutorial Zend Framework

Zend_Auth::getInstance()->getIdentity()->google_user,Zend_Auth::getInstance()->getIdentity()->google_pass,Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME);

$spreadsheetService = new Zend_Gdata_Spreadsheets($client);

$this->view->items = $spreadsheetService->getSpreadsheetFeed();}

We set the results coming back to the $items view template variable, whichcontains an object with the properties for each spreadsheet in the current Googleaccount. The returned structure is similar to the calendar event list object, so wehave several of the same properties and methods as before.

In Listing 7, we create a view template for the list of spreadsheets in the currentGoogle account. Once again, we provide a link to the actual spreadsheet so the usercan go directly to the spreadsheet to view or modify it.

Listing 7. getspreadsheets.phtml view template

<ul><?php foreach ($this->items as $item): ?><li><a href="<?php echo $item->getLink('alternate'); ?>"><?php echo $this->escape($item->title); ?> -</a></li><?php endforeach; ?></ul>

We also update our index view template accordingly to show the new list ofspreadsheets after the list of events in Listing 8. The Google spreadsheets APIdoesn't provide a way to create new spreadsheets. So unfortunately, we will not beable to add new spreadsheets to the user's Google account through our Web siteusing the Google spreadsheets API. However, in the next section, where we dealwith Google Docs, we show how you can upload a spreadsheet and access thatspreadsheet using the Google API.

Listing 8. index.phtml with the listing of the user's spreadsheets

<?php $this->headTitle()->append('My Zend Framework Application'); ?><h1>My Events</h1><?php echo $this->action('google', 'getevents');?><p>Add New Event</p><form id="addquickevent" action="<?php echo WEBROOT; ?>google/quickaddevent"class="add" method="POST"><input type="text" name="event" /><input type="submit" value="Add Event" /></form><h1>My Spreadsheets</h1><?php echo $this->action('google', 'getspreadsheets');?><script type="text/javascript"><!--

developerWorks® ibm.com/developerWorks

Integrate with Google Apps Trademarks© Copyright IBM Corporation 2008. All rights reserved. Page 10 of 22

Page 11: Tutorial Zend Framework

$(document).ready(function(){$("#addquickevent").submit(function(){

posturl = this.action;postdata = { event: this.event.value };$.post(posturl, postdata, function(data){

alert(data);}, 'text');return false;

});});--></script>

We've seen how to integrate Google spreadsheets into our Web site. Now let's lookat the another powerful application in the Google Apps arsenal: Google Docs.

Section 4. Google Docs

Google Docs also contains word-processing functionality, similar to MicrosoftWord®. Google Docs facilitates collaboration on documents with change control andrevision tracking. It is supported with the Zend_Gdata component of the ZendFramework.

Getting a list of documents

Just like we've done with Google spreadsheets, we also display a list of documentson the user's site home page, extending the dashboard concept to includedocuments.

Figure 4. Dashboard with documents

Getting the list of documents, as shown in Listing 9, is pretty much identical to

ibm.com/developerWorks developerWorks®

Integrate with Google Apps Trademarks© Copyright IBM Corporation 2008. All rights reserved. Page 11 of 22

Page 12: Tutorial Zend Framework

getting the list of spreadsheets. We retrieve the list of documents and push it into anobject list, where each item represents a document in the user's account.

Listing 9. getdocuments action

public function getdocumentsAction(){

Zend_Loader::loadClass('Zend_Gdata');Zend_Loader::loadClass('Zend_Gdata_ClientLogin');Zend_Loader::loadClass('Zend_Gdata_Docs');Zend_Loader::loadClass('Zend_Http_Client');

$client = Zend_Gdata_ClientLogin::getHttpClient(Zend_Auth::getInstance()->getIdentity()->google_user,Zend_Auth::getInstance()->getIdentity()->google_pass,Zend_Gdata_Docs::AUTH_SERVICE_NAME);

$docService = new Zend_Gdata_Docs($client);

$this->view->items = $docService->getDocumentListFeed();$this->render('getspreadsheets');

}

Everything works so much the same as with the spreadsheets list that we evenreuse the view template. Therefore, in the spirit of code reuse, we push the list ofdocuments into the $items view template variable, then specifically render thegetspreadsheets view template.

Adding a document

Unlike with the Google spreadsheets API, the Google Docs API allows you to uploaddocuments. We can use this same functionality to not only upload documents butalso Google spreadsheets and presentations, which allows you to use one uploadform to upload documents and spreadsheets to the user's Google account.

Let's look at the adddocuments action in Listing 10. This handles the incoming datavia an Ajax request, then returns the name of the created document to the user.

Listing 10. adddocuments action

public function adddocumentsAction(){

Zend_Loader::loadClass('Zend_Gdata');Zend_Loader::loadClass('Zend_Gdata_ClientLogin');Zend_Loader::loadClass('Zend_Gdata_Docs');Zend_Loader::loadClass('Zend_Http_Client');

$client = Zend_Gdata_ClientLogin::getHttpClient(Zend_Auth::getInstance()->getIdentity()->google_user,Zend_Auth::getInstance()->getIdentity()->google_pass,Zend_Gdata_Docs::AUTH_SERVICE_NAME);

$docService = new Zend_Gdata_Docs($client);

developerWorks® ibm.com/developerWorks

Integrate with Google Apps Trademarks© Copyright IBM Corporation 2008. All rights reserved. Page 12 of 22

Page 13: Tutorial Zend Framework

$newDocument = $docService->uploadFile($_FILES['newdoc']['tmp_name'],$_FILES['newdoc']['name'],null,Zend_Gdata_Docs::DOCUMENTS_LIST_FEED_URI);

if(!$this->_request->isXmlHttpRequest())$this->_redirect('');

else {$this->_helper->layout->disableLayout();$this->_helper->viewRenderer->setNoRender();echo "Added document '{$newDocument->title}' successfully!";

}}

We update our index view template to have the documents list and the ability toupload documents, as shown in Listing 11. This provides a tool for addingdocuments and spreadsheets to the user's Google account, which, in turn, willappear in the list of documents. Uploading a file with Ajax uses a hidden iframe asthe target of the form, instead of doing an XMLHttpRequest to the action. We thenadd a hook when the iframe is fully loaded, which will display an alert to the user ofthe document being uploaded successfully.

Listing 11. Updated index view template with document list and file uploading

<?php $this->headTitle()->append('My Zend Framework Application'); ?><h1>My Events</h1><?php echo $this->action('google', 'getevents');?><p>Add New Event</p><form id="addquickevent" action="<?php echo WEBROOT;

?>google/quickaddevent" method="POST"><input type="text" name="event" /><input type="submit" value="Add Event" /></form><h1>My Spreadsheets</h1><?php echo $this->action('google', 'getspreadsheets');?><h1>My Documents</h1><?php echo $this->action('google', 'getdocuments');?><form id="adddocument" action="<?php echo WEBROOT; ?>google/adddocument"

method="POST"><input type="file" name="newdoc" /><input type="submit" value="Add Document" /></form><iframe id="upload_target" name="upload_target" src=""

style="width:0;height:0;border:0px solid #fff;"></iframe><script type="text/javascript"><!--$(document).ready(function(){

$("#addquickevent").submit(function(){posturl = this.action;postdata = { event: this.event.value };$.post(posturl, postdata, function(data){

alert(data);}, 'text');return false;

});$("#adddocument").submit(function(){

this.target = 'upload_target';});$("#upload_target").load(function(){

alert(this.innerHTML);

ibm.com/developerWorks developerWorks®

Integrate with Google Apps Trademarks© Copyright IBM Corporation 2008. All rights reserved. Page 13 of 22

Page 14: Tutorial Zend Framework

});});--></script>

Figure 5 shows the newly added upload document functionality.

Figure 5. Upload document functionality added to the dashboard

We built a rather handy front end to the Google account, where the user can see adashboard-like view of current events, documents, and spreadsheets on one form.In addition, users can add events and documents to their Google accounts with easethrough their home pages.

Now let's look at interfacing our Web site with two other popular Google services:Picasa and YouTube. Hooking into Picasa and YouTube allows users to showphotos in their accounts, and search for videos and record them in their accounts onour Web site.

Section 5. Picasa and YouTube

Two of the most popular Google services are Picasa and YouTube. Picasa allowsusers to upload photos to a free Web-based album for sharing them with family andfriends, as well as the world. YouTube is the revolutionary video broadcastingservice that allows its users to publish videos for anyone to see. Google hasextended both of these services with powerful APIs that can be used to embedcontent within any Web site, and the Zend_Gdata component in the ZendFramework can be used to add this to our Web site with ease.

developerWorks® ibm.com/developerWorks

Integrate with Google Apps Trademarks© Copyright IBM Corporation 2008. All rights reserved. Page 14 of 22

Page 15: Tutorial Zend Framework

Searching Picasa

For our Web site, you be listing the last 10 photos added to the user's Picasa Webalbum. We start by adding a new action: searchpicasa.

Listing 12. searchpicasa action

public function getphotosAction(){

Zend_Loader::loadClass('Zend_Gdata');Zend_Loader::loadClass('Zend_Gdata_ClientLogin');Zend_Loader::loadClass('Zend_Gdata_Photos');Zend_Loader::loadClass('Zend_Http_Client');

$client = Zend_Gdata_ClientLogin::getHttpClient(Zend_Auth::getInstance()->getIdentity()->google_user,Zend_Auth::getInstance()->getIdentity()->google_pass,Zend_Gdata_Photos::AUTH_SERVICE_NAME);

$service = new Zend_Gdata_Photos($client);

$query = new Zend_Gdata_Photos_AlbumQuery();$query->setType("entry");

try {$this->view->entries = $service->getAlbumFeed($query);

}catch (Zend_Gdata_App_Exception $e) {

echo "Error: " . $e->getResponse();}

}

Now we create a view template to show the list of photos, shown in Listing 13. Here,we cap the number of photos returned to the user at 10, and we render the photos inthe album.

Listing 13. showpicasa view template

<?php for ( $i = 0; $i < 10; $i++ ): ?><img src="<?php echo $photos[$i]->thumbnaillink ;?>" /><br /><?php endfor; ?>

We can add this to display on the index page, just like we've done with thedocuments and spreadsheets earlier. Doing this helps complete the dashboard onthe home page of our Web site, containing widgets for our upcoming events, lists ofdocuments and spreadsheets, and recently added photos.

Figure 6. Recently added photos on the dashboard

ibm.com/developerWorks developerWorks®

Integrate with Google Apps Trademarks© Copyright IBM Corporation 2008. All rights reserved. Page 15 of 22

Page 16: Tutorial Zend Framework

Searching YouTube

For YouTube, we use a similar search engine-style interface like we used in Part 2,when we searched Google Base records. For our Web site, we search YouTubevideo clip titles for particular clips, then add them to our record list, intermixed withexisting Google Base records.

We begin by adding a searchyoutube action to the existing Google controller (seeListing 14). This search tool works similar to how you search Google Base, allowingyou to order the search results and only return a certain number of results.

Listing 14. searchyoutube action

public function searchyoutubeAction(){

Zend_Loader::loadClass('Zend_Gdata_YouTube');

$service = new Zend_Gdata_YouTube();$query = $service->newVideoQuery();$query->videoQuery = $this->_getParam('search','');$query->startIndex = 0;$query->maxResults = 10;$query->orderBy = 'viewCount';

$this->view->videos = $service->getVideoFeed($query);}

Now we create a view to allow searching YouTube videos, along with the ability toadd them to our records database we created in Part 2. Again, we use Ajax to addthe records in a slick way, just like we did for deleting records in a list and addingrecords from Google Base in Part 2.

Listing 15. searchyoutube view template

developerWorks® ibm.com/developerWorks

Integrate with Google Apps Trademarks© Copyright IBM Corporation 2008. All rights reserved. Page 16 of 22

Page 17: Tutorial Zend Framework

<?php $this->headTitle()->append('YouTube Search'); ?><form action="<?php echo WEBROOT; ?>google/searchyoutube" method="post"

id="search"><p>Search for: <?php echo $this->formText("search",$this->search);

?></p><p><?php echo $this->formSubmit("submit","Search"); ?></p></form><ul><?php foreach ($this->videos as $entry): ?><li>

<?php echo $this->escape($entry->title); ?><br /><?php echo $this->escape($entry->content); ?><br /><a href="<?php echo $this->escape($entry->id); ?>"><?php

echo $this->escape($entry->id); ?></a><br /><form action="<?php echo WEBROOT; ?>record/save" class="add"

method="POST"><input type="hidden" name="link" value="<?php echo

$this->escape($entry->id); ?>" /><input type="hidden" name="title" value="<?php echo

$this->escape($entry->title); ?>" /><input type="hidden" name="description" value="<?php echo

$this->escape($entry->content); ?>" /><input type="submit" value="Add to My Records" /></form>

</li><?php endforeach; ?></ul><script type="text/javascript"><!--$(document).ready(function(){

$("form[id!=search]").submit(function(){posturl = this.action;postdata = { link: this.link.value, title: this.title.value,

description: this.description.value };$.post(posturl, postdata, function(data){

alert("Record Successfully Added!");});return false;

});});--></script>

Look at Figure 7 to see how our site allows you to search by keywords for GoogleBase and YouTube entries, allowing you to save them and even edit or delete themlater, as well.

Figure 7. Ability to save Google Base and YouTube search entries

ibm.com/developerWorks developerWorks®

Integrate with Google Apps Trademarks© Copyright IBM Corporation 2008. All rights reserved. Page 17 of 22

Page 18: Tutorial Zend Framework

You have seen how easy it is to integrate these popular social Web services with ourWeb site. For Picasa, we can display the most recently added pictures to our homepage as a decorative feature alongside the Google Calendar, Google Docs, andspreadsheets listings. For YouTube, we added the ability for users to bookmarkvideos within the application for fast retrieval.

Summary

We began this "Create a productivity package with the Zend Framework V1.5 andGoogle Apps" series looking at the Zend Framework and how we could use it tobuild an extensible and powerful Web application using the various componentsincluded in the Zend Framework and sound design principles, such as MVC. Wethen looked at turning the rough Web site we initially built into a full-featured Webapplication with authentication and Web-services integration, namely with Googleusing the Zend_Gdata component, which we used to interacting with Google Base.

developerWorks® ibm.com/developerWorks

Integrate with Google Apps Trademarks© Copyright IBM Corporation 2008. All rights reserved. Page 18 of 22

Page 19: Tutorial Zend Framework

Here, we saw how easy it is to integrate the other popular Google services within ourWeb site. We saw how to access public and private data, as well as publish contentinto the user's Google account.

ibm.com/developerWorks developerWorks®

Integrate with Google Apps Trademarks© Copyright IBM Corporation 2008. All rights reserved. Page 19 of 22

Page 20: Tutorial Zend Framework

Downloads

Description Name Size Downloadmethod

Source code os-php-zend-google-pt3.samplecode.zip45KB HTTP

Information about download methods

developerWorks® ibm.com/developerWorks

Integrate with Google Apps Trademarks© Copyright IBM Corporation 2008. All rights reserved. Page 20 of 22

Page 21: Tutorial Zend Framework

Resources

Learn

• The Zend Framework Web site maintains the latest documentation, as well asPHP installation FAQs.

• See the Zend Framework's Programmer's Reference Guide.

• Read an overview of the new features in V1.5.

• See the Components section of the Zend Framework manual for coverage of allthe framework components.

• Read the Zend_Layout Component Proposal.

• Thought Storms Model-View-Controller explains the MVC and the controversyand confusion surrounding it.

• The phpPatterns Web site explains MVC from the PHP point of view.

• Visit Wikipedia for a high-level view of software frameworks.

• Get on overview of the MVC architecture from Wikipedia.

• What's new in Zend Framework V1.5.

• PHP.net is the central resource for PHP developers.

• Check out the "Recommended PHP reading list."

• Browse all the PHP content on developerWorks.

• Expand your PHP skills by checking out IBM developerWorks' PHP projectresources.

• To listen to interesting interviews and discussions for software developers,check out developerWorks podcasts.

• Using a database with PHP? Check out the Zend Core for IBM, a seamless,out-of-the-box, easy-to-install PHP development and production environmentthat supports IBM DB2 V9.

• Stay current with developerWorks' Technical events and webcasts.

• Check out upcoming conferences, trade shows, webcasts, and other Eventsaround the world that are of interest to IBM open source developers.

• Visit the developerWorks Open source zone for extensive how-to information,tools, and project updates to help you develop with open source technologiesand use them with IBM's products.

• Watch and learn about IBM and open source technologies and product

ibm.com/developerWorks developerWorks®

Integrate with Google Apps Trademarks© Copyright IBM Corporation 2008. All rights reserved. Page 21 of 22

Page 22: Tutorial Zend Framework

functions with the no-cost developerWorks On demand demos.

Get products and technologies

• Download Zend Framework V1.5.1 from Zend Technologies.

• Download PHP V5.x from PHP.net.

• Innovate your next open source development project with IBM trial software,available for download or on DVD.

• Download IBM product evaluation versions, and get your hands on applicationdevelopment tools and middleware products from DB2®, Lotus®, Rational®,Tivoli®, and WebSphere®.

Discuss

• Participate in developerWorks blogs and get involved in the developerWorkscommunity.

• Participate in the developerWorks PHP Forum: Developing PHP applicationswith IBM Information Management products (DB2, IDS).

About the author

John MerticJohn Mertic graduated with a degree in computer science from KentState University and is currently a software engineer at SugarCRM. Hehas contributed to many open source projects, most notably PHPprojects; he is the creator and maintainer of the PHP Windows Installer.

developerWorks® ibm.com/developerWorks

Integrate with Google Apps Trademarks© Copyright IBM Corporation 2008. All rights reserved. Page 22 of 22