newItemEntry(); // Add title $newEntry->title = $gdata->newTitle(trim($_POST['recipe_title'])); // Add some content $newEntry->content = $gdata->newContent($_POST['recipe_text']); $newEntry->content->type = 'text'; // Define item type $newEntry->itemType = 'testrecipes'; $newEntry->itemType->type = 'text'; // Add item-specific attributes $newEntry->addGbaseAttribute('cuisine', $_POST['cuisine'], 'text'); $newEntry->addGbaseAttribute('cooking_time', $_POST['time_val'] . ' ' . $_POST['time_units'], 'intUnit'); $newEntry->addGbaseAttribute('main_ingredient', $_POST['main_ingredient'], 'text'); $newEntry->addGbaseAttribute('serving_count', $_POST['serves'], 'number'); // Post the item $createdEntry = $gdata->insertGbaseItem($newEntry, $dryRun); return $createdEntry; } /** * Updates an existing recipe by performing an HTTP PUT * on its feed URI, using the updated values as the data. * @return true */ function updateItem() { $client = Zend_Gdata_AuthSub::getHttpClient($_POST['token']); $gdata = new Zend_Gdata_Gbase($client); $itemUrl = $_POST['link']; $updatedEntry = $gdata->getGbaseItemEntry($itemUrl); // Update title $updatedEntry->title = $gdata->newTitle(trim($_POST['recipe_title'])); // Update content $updatedEntry->content = $gdata->newContent($_POST['recipe_text']); $updatedEntry->content->type = 'text'; // Update item-specific attributes $baseAttributeArr = $updatedEntry->getGbaseAttribute('cuisine'); if (is_object($baseAttributeArr[0])) { $baseAttributeArr[0]->text = $_POST['cuisine']; } $baseAttributeArr = $updatedEntry->getGbaseAttribute('cooking_time'); if (is_object($baseAttributeArr[0])) { $baseAttributeArr[0]->text = $_POST['time_val'] . ' ' . $_POST['time_units']; } $baseAttributeArr = $updatedEntry->getGbaseAttribute('main_ingredient'); if (is_object($baseAttributeArr[0])) { $baseAttributeArr[0]->text = $_POST['main_ingredient']; } $baseAttributeArr = $updatedEntry->getGbaseAttribute('serving_count'); if (is_object($baseAttributeArr[0])) { $baseAttributeArr[0]->text = $_POST['serves']; } $dryRun = false; $gdata->updateGbaseItem($updatedEntry, $dryRun); // Alternatively, you can call the save() method directly on the entry // $updatedEntry->save(); return true; } /** * Deletes a recipe by performing an HTTP DELETE on its feed URI. * @return void */ function deleteItem() { $client = Zend_Gdata_AuthSub::getHttpClient($_POST['token']); $gdata = new Zend_Gdata_Gbase($client); $itemUrl = $_POST['link']; $deleteEntry = $gdata->getGbaseItemEntry($itemUrl); $dryRun = false; $gdata->deleteGbaseItem($deleteEntry, $dryRun); // Alternatively, you can call the save() method directly on the entry // $gdata->delete($itemUrl); } /** * Creates the XML content used to perform a batch delete. * @return string The constructed XML to be used for the batch delete */ function buildBatchXML() { $result = '' . "\n" . '' . "\n"; $counter = 0; foreach($_POST as $key => $value) { if(substr($key, 0, 5) == "link_") { $counter++; $result .= '' . "\n" . '' . $value . '' . "\n" . '' . "\n" . '' . $counter . '' . "\n" . '' . "\n"; } } $result .= '' . "\n"; return $result; } /** * Deletes all recipes by performing an HTTP POST to the * batch URI. * @return Zend_Http_Response The reponse of the post */ function batchDelete() { $client = Zend_Gdata_AuthSub::getHttpClient($_POST['token']); $gdata = new Zend_Gdata_Gbase($client); $response = $gdata->post(buildBatchXML(), ITEMS_FEED_URI . '/batch'); return $response; } /** * Writes the HTML header for the demo. * * NOTE: We would normally keep the HTML/CSS markup separate from the business * logic above, but have decided to include it here for simplicity of * having a single-file sample. * @return void */ function printHTMLHeader() { print '' . "\n" . '' . "\n" . '' . "\n" . 'PHP Demo: Google Base API' . "\n" . '' . "\n" . '' . "\n" . '
' . "\n"; } /** * Writes the HTML footer for the demo. * * NOTE: We would normally keep the HTML/CSS markup separate from the business * logic above, but have decided to include it here for simplicity of * having a single-file sample. * @return void */ function printHTMLFooter() { print '
' . "\n"; } /** * We arrive here when the user first comes to the form. The first step is * to have them get a single-use token. */ function showIntroPage() { $next_url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; $scope = ITEMS_FEED_URI; $secure = false; $session = true; $redirect_url = Zend_Gdata_AuthSub::getAuthSubTokenUri($next_url, $scope, $secure, $session); printHTMLHeader(); print '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n" . '
' . 'PHP Demo: Google Base data API
' . 'Powered By' . ' ' . 'Zend Google Data Client Library
Before you get started, please sign in to your personal Google Base account.
' . "\n"; printHTMLFooter(); } /** * Prints the table of recipes the user has already entered * on the left-hand side of the page. * @param string $token The session token * @return void */ function showRecipeListPane($token) { $client = Zend_Gdata_AuthSub::getHttpClient($token); $gdata = new Zend_Gdata_Gbase($client); try { $feed = $gdata->getGbaseItemFeed(ITEMS_FEED_URI . '/-/testrecipes'); print '' . "\n" . '' . 'View all of your published items' . '' . "\n" . '' . "\n"; if ($feed->count() == 0) { print '' . '' . '' . "\n"; } else { print '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n"; foreach ($feed->entries as $feed_entry) { $href = $feed_entry->link[0]->href; $title = $feed_entry->title->text; $id = $feed_entry->id->text; $baseAttributeArr = $feed_entry->getGbaseAttribute('cuisine'); // Only want first cuisine if (isset($baseAttributeArr[0]) && is_object($baseAttributeArr[0])) { $cuisine = $baseAttributeArr[0]->text; } $baseAttributeArr = $feed_entry->getGbaseAttribute('serving_count'); // Only want first serving_count if (isset($baseAttributeArr[0]) && is_object($baseAttributeArr[0])) { $serving_count = $baseAttributeArr[0]->text; } print '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n"; /* Create an Edit button for each existing recipe. */ print '' . "\n"; /* Create a Delete button for each existing recipe. */ print '' . "\n" . '' . "\n"; } } /* Create a "Delete all" button" to demonstrate batch requests. */ print '' . "\n"; print '
' . 'Recipes you have added that searchable via the API
(none)
NameCuisineServesActions
' . $title . '' . $cuisine . '' . $serving_count . '' . "\n" . '
' . "\n" . '' . "\n" . "" . "\n" . '' . "\n" . '' . "\n" . '
' . "\n" . '
' . "\n" . '
' . "\n" . '' . "\n" . "" . "\n" . '' . "\n" . '' . "\n" . '
' . "\n" . '
' . "\n" . '
' . "\n" . '' . "\n" . '' . "\n"; $i = 0; foreach ($feed as $feed_entry) { print '' . "\n"; $i++; } print 'count() == 0) { print ' disabled="true"'; } print '>
' . "\n"; print '' . "\n"; } catch (Zend_Gdata_App_Exception $e) { showMainMenu("Error: " . $e->getMessage(), $token); } } /** * Prints a small form allowing the user to insert a new * recipe. * @param string $sessionToken A session token * @return void */ function showRecipeInsertPane($sessionToken) { global $cuisines; print '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n" . "\n" . '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n" . '' . '' . "\n" . '' . "\n" . '' . '' . "\n" . '' . "\n" . '' . '' . "\n" . '' . "\n" . '
' . 'Insert a new recipe
Title:' . '
Main ingredient:' . '
Cuisine:
Cooking Time: ' . '
Serves:
Recipe:
 
' . "\n" . '' . "\n"; } /** * Shows a menu allowing the user to update an existing * recipe with the Base API update feature. * @return void */ function showEditMenu() { global $cuisines; $client = Zend_Gdata_AuthSub::getHttpClient($_POST['token']); $gdata = new Zend_Gdata_Gbase($client); try { $feed = $gdata->getGbaseItemFeed(ITEMS_FEED_URI); foreach ($feed->entries as $feed_entry) { $editLink = $feed_entry->link[2]->href; if ($editLink == $_POST['edit']) { $baseAttributeArr = $feed_entry->getGbaseAttribute('cooking_time'); if (isset($baseAttributeArr[0]) && is_object($baseAttributeArr[0])) { $splitCookingTime = explode(' ', $baseAttributeArr[0]->text); } $baseAttributeArr = $feed_entry->getGbaseAttribute('cuisine'); // Cuisine can have multiple entries if (isset($baseAttributeArr[0]) && is_object($baseAttributeArr[0])) { $cuisine = $baseAttributeArr[0]->text; } $baseAttributeArr = $feed_entry->getGbaseAttribute('serving_count'); // $serving_count can have multiple entries if (isset($baseAttributeArr[0]) && is_object($baseAttributeArr[0])) { $serving_count = $baseAttributeArr[0]->text; } $main_ingredient = $feed_entry->getGbaseAttribute('main_ingredient'); // Main_ingredient can have multiple entries if (is_array($main_ingredient)) { $main_ingredient = $main_ingredient[0]->text; } printHTMLHeader(); print '' . "\n"; print '' . '' . '' . "\n"; print "\n" . '' . "\n" . '' . "\n" . '' . "\n"; print '' . "\n" . '' . "\n"; print '' . "\n" . '' . "\n"; print '' . "\n" . '' . "\n"; print '' . '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n" . '
Edit recipe:
Title:' . '' . '
Main ingredient:
Cuisine:
Cooking Time: ' . "\n" . '
Serves:
Recipe:
 ' . '
' . "\n"; printHTMLFooter(); break; } } } catch (Zend_Gdata_App_Exception $e) { showMainMenu($e->getMessage(), $_POST['token']); } } /** * Displays both the "List of current recipes" and * "Insert a new recipe" panels in a single table. * @param string $tableTitle The title to display in the html table * @param string $sessionToken A session token * @return void */ function showMainMenu($tableTitle, $sessionToken) { printHTMLHeader(); print '' . "\n" . '' . "\n" . '' . "\n" . '' . "\n" . '\n" . '' . "\n"; // Create the two sub-tables. showRecipeListPane($sessionToken); showRecipeInsertPane($sessionToken); // Add a "Sign out" link. print '' . "\n"; // Close the master table. print '
' . 'PHP Demo: Google Base data API
' . '' . 'Powered By ' . '' . 'Zend Google Data Client Library
' . $tableTitle . "
Or click here to' . ' sign out' . ' of your Google account.
' . "\n"; printHTMLFooter(); } /** * Exchanges the given single-use token for a session * token using AuthSubSessionToken, and returns the result. * @param string $token The single-use token from AuthSubRequest * @return string The upgraded (session) token */ function exchangeToken($token) { return Zend_Gdata_AuthSub::getAuthSubSessionToken($token); } /** * We arrive here after the user first authenticates and we get back * a single-use token. * @return void */ function showFirstAuthScreen() { $singleUseToken = $_GET['token']; $sessionToken = exchangeToken($singleUseToken); if (!$sessionToken) { showIntroPage(); } else { $tableTitle = "Here's your single use token: " . "$singleUseToken
" . "\n" . "And here's the session token: $sessionToken"; showMainMenu($tableTitle, $sessionToken); } } /** * Main logic to handle the POST operation of inserting an item. * @return void */ function handlePost() { try { $newEntry= postItem(); if ($newEntry) { showMainMenu('Recipe inserted! It will be searchable by the API soon...', $_POST['token']); } } catch (Zend_Gdata_App_Exception $e) { showMainMenu('Recipe insertion failed: ' . $e->getMessage(), $_POST['token']); } } /** * Main logic to handle deleting an item. * @return void */ function handleDelete() { try { deleteItem(); showMainMenu('Recipe deleted.', $_POST['token']); } catch (Zend_Gdata_App_Exception $e) { showMainMenu('Recipe deletion failed: ' . $e->getMessage(), $_POST['token']); } } /** * Main logic to handle a batch deletion of items. * @return void */ function handleBatch() { try { $batch_response = batchDelete(); if ($batch_response->isSuccessful()) { showMainMenu('All recipes deleted.', $_POST['token']); } else { showMainMenu('Batch deletion failed: ' . $batch_response->getMessage(), $_POST['token']); } } catch (Zend_Gdata_App_Exception $e) { showMainMenu('Batch deletion failed: ' . $e->getMessage(), $_POST['token']); } } /** * Main logic to handle updating an item * @return void */ function handleUpdate() { try { if (updateItem()) { showMainMenu('Recipe successfully updated.', $_POST['token']); } else { showMainMenu('Recipe update failed.', $_POST['token']); } } catch (Zend_Gdata_App_Exception $e) { showMainMenu('Recipe update failed: ' . $e->getMessage(), $_POST['token']); } } /** * Main logic to handle requests */ if (count($_GET) == 1 && array_key_exists('token', $_GET)) { showFirstAuthScreen(); } else { if (count($_POST) == 0) { showIntroPage(); } else { if ($_POST['action'] == 'insert') { handlePost(); } else if ($_POST['action'] == 'delete') { handleDelete(); } else if ($_POST['action'] == 'delete_all') { handleBatch(); } else if ($_POST['action'] == 'edit') { showEditMenu(); } else if ($_POST['action'] == 'update') { handleUpdate(); } else { showIntroPage(); } } } ?>