Also apply the __toString fix to UserInfo
[cacert-mgr.git] / external / ZendFramework-1.9.5 / demos / Zend / Gdata / Gbase.php
1 <?php
2 /**
3 * Zend Framework
4 *
5 * LICENSE
6 *
7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
14 *
15 * @category Zend
16 * @package Zend_Gdata
17 * @subpackage Demos
18 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 */
21
22 /* Load the Zend Gdata classes. */
23 require_once 'Zend/Loader.php';
24 Zend_Loader::loadClass('Zend_Gdata_AuthSub');
25 Zend_Loader::loadClass('Zend_Gdata_Gbase');
26
27 /* The items feed URL, used for queries, insertions and batch commands. */
28 define('ITEMS_FEED_URI', 'http://www.google.com/base/feeds/items');
29
30 /* Types of cuisine the user may select when inserting a recipe. */
31 $cuisines = array('African', 'American', 'Asian', 'Caribbean', 'Chinese',
32 'French', 'Greek', 'Indian', 'Italian', 'Japanese', 'Jewish',
33 'Mediterranean', 'Mexican', 'Middle Eastern', 'Moroccan',
34 'North American', 'Spanish', 'Thai', 'Vietnamese', 'Other');
35
36
37 /**
38 * Inserts a new recipe by performing an HTTP POST to the
39 * items feed.
40 * @param boolean $dryRun (optional) True if this should be a dry run insert
41 * @return Zend_Gdata_Gbase_ItemFeed The newly created entry
42 */
43 function postItem($dryRun = false) {
44 $client = Zend_Gdata_AuthSub::getHttpClient($_POST['token']);
45 $gdata = new Zend_Gdata_Gbase($client);
46
47 $newEntry = $gdata->newItemEntry();
48
49 // Add title
50 $newEntry->title = $gdata->newTitle(trim($_POST['recipe_title']));
51
52 // Add some content
53 $newEntry->content = $gdata->newContent($_POST['recipe_text']);
54 $newEntry->content->type = 'text';
55
56 // Define item type
57 $newEntry->itemType = 'testrecipes';
58 $newEntry->itemType->type = 'text';
59
60 // Add item-specific attributes
61 $newEntry->addGbaseAttribute('cuisine', $_POST['cuisine'], 'text');
62 $newEntry->addGbaseAttribute('cooking_time', $_POST['time_val'] . ' ' .
63 $_POST['time_units'], 'intUnit');
64 $newEntry->addGbaseAttribute('main_ingredient',
65 $_POST['main_ingredient'],
66 'text');
67 $newEntry->addGbaseAttribute('serving_count', $_POST['serves'], 'number');
68
69 // Post the item
70 $createdEntry = $gdata->insertGbaseItem($newEntry, $dryRun);
71
72 return $createdEntry;
73 }
74
75 /**
76 * Updates an existing recipe by performing an HTTP PUT
77 * on its feed URI, using the updated values as the data.
78 * @return true
79 */
80 function updateItem() {
81 $client = Zend_Gdata_AuthSub::getHttpClient($_POST['token']);
82 $gdata = new Zend_Gdata_Gbase($client);
83
84 $itemUrl = $_POST['link'];
85 $updatedEntry = $gdata->getGbaseItemEntry($itemUrl);
86
87 // Update title
88 $updatedEntry->title = $gdata->newTitle(trim($_POST['recipe_title']));
89
90 // Update content
91 $updatedEntry->content = $gdata->newContent($_POST['recipe_text']);
92 $updatedEntry->content->type = 'text';
93
94 // Update item-specific attributes
95 $baseAttributeArr = $updatedEntry->getGbaseAttribute('cuisine');
96 if (is_object($baseAttributeArr[0])) {
97 $baseAttributeArr[0]->text = $_POST['cuisine'];
98 }
99
100 $baseAttributeArr = $updatedEntry->getGbaseAttribute('cooking_time');
101 if (is_object($baseAttributeArr[0])) {
102 $baseAttributeArr[0]->text =
103 $_POST['time_val'] . ' ' . $_POST['time_units'];
104 }
105
106 $baseAttributeArr = $updatedEntry->getGbaseAttribute('main_ingredient');
107 if (is_object($baseAttributeArr[0])) {
108 $baseAttributeArr[0]->text = $_POST['main_ingredient'];
109 }
110
111 $baseAttributeArr = $updatedEntry->getGbaseAttribute('serving_count');
112 if (is_object($baseAttributeArr[0])) {
113 $baseAttributeArr[0]->text = $_POST['serves'];
114 }
115
116 $dryRun = false;
117 $gdata->updateGbaseItem($updatedEntry, $dryRun);
118
119 // Alternatively, you can call the save() method directly on the entry
120 // $updatedEntry->save();
121
122 return true;
123 }
124
125 /**
126 * Deletes a recipe by performing an HTTP DELETE on its feed URI.
127 * @return void
128 */
129 function deleteItem() {
130 $client = Zend_Gdata_AuthSub::getHttpClient($_POST['token']);
131 $gdata = new Zend_Gdata_Gbase($client);
132
133 $itemUrl = $_POST['link'];
134 $deleteEntry = $gdata->getGbaseItemEntry($itemUrl);
135
136 $dryRun = false;
137 $gdata->deleteGbaseItem($deleteEntry, $dryRun);
138
139 // Alternatively, you can call the save() method directly on the entry
140 // $gdata->delete($itemUrl);
141 }
142
143 /**
144 * Creates the XML content used to perform a batch delete.
145 * @return string The constructed XML to be used for the batch delete
146 */
147 function buildBatchXML() {
148 $result = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
149 '<feed xmlns="http://www.w3.org/2005/Atom"' . "\n" .
150 ' xmlns:g="http://base.google.com/ns/1.0"' . "\n" .
151 ' xmlns:batch="http://schemas.google.com/gdata/batch">' . "\n";
152
153 $counter = 0;
154 foreach($_POST as $key => $value) {
155 if(substr($key, 0, 5) == "link_") {
156 $counter++;
157
158 $result .= '<entry>' . "\n" .
159 '<id>' . $value . '</id>' . "\n" .
160 '<batch:operation type="delete"/>' . "\n" .
161 '<batch:id>' . $counter . '</batch:id>' . "\n" .
162 '</entry>' . "\n";
163 }
164 }
165 $result .= '</feed>' . "\n";
166
167 return $result;
168 }
169
170 /**
171 * Deletes all recipes by performing an HTTP POST to the
172 * batch URI.
173 * @return Zend_Http_Response The reponse of the post
174 */
175 function batchDelete() {
176 $client = Zend_Gdata_AuthSub::getHttpClient($_POST['token']);
177 $gdata = new Zend_Gdata_Gbase($client);
178
179 $response = $gdata->post(buildBatchXML(), ITEMS_FEED_URI . '/batch');
180
181 return $response;
182 }
183
184 /**
185 * Writes the HTML header for the demo.
186 *
187 * NOTE: We would normally keep the HTML/CSS markup separate from the business
188 * logic above, but have decided to include it here for simplicity of
189 * having a single-file sample.
190 * @return void
191 */
192 function printHTMLHeader()
193 {
194 print '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"' . "\n" .
195 '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' . "\n" .
196 '<html xmlns="http://www.w3.org/1999/xhtml" lang="en">' . "\n" .
197 '<head><meta http-equiv="Content-Type" ' .
198 'content="text/html;charset=utf-8"/>' . "\n" .
199 '<title>PHP Demo: Google Base API</title>' . "\n" .
200 '<link rel="stylesheet" type="text/css" ' .
201 'href="http://code.google.com/css/dev_docs.css">' . "\n" .
202 '</head>' . "\n" .
203 '<body><center>' . "\n";
204 }
205
206 /**
207 * Writes the HTML footer for the demo.
208 *
209 * NOTE: We would normally keep the HTML/CSS markup separate from the business
210 * logic above, but have decided to include it here for simplicity of
211 * having a single-file sample.
212 * @return void
213 */
214 function printHTMLFooter() {
215 print '</center></body></html>' . "\n";
216 }
217
218 /**
219 * We arrive here when the user first comes to the form. The first step is
220 * to have them get a single-use token.
221 */
222 function showIntroPage() {
223 $next_url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
224 $scope = ITEMS_FEED_URI;
225 $secure = false;
226 $session = true;
227 $redirect_url = Zend_Gdata_AuthSub::getAuthSubTokenUri($next_url,
228 $scope,
229 $secure,
230 $session);
231
232 printHTMLHeader();
233
234 print '<table style="width:50%;">' . "\n" .
235 '<tr>' . "\n" .
236 '<th colspan="2" style="text-align:center;">' .
237 'PHP Demo: Google Base data API<br/>' .
238 '<small><span style="font-variant: small-caps;">Powered By</span>' .
239 ' <a href="http://framework.zend.com/download/gdata">' .
240 'Zend Google Data Client Library</a></small></th>' . "\n" .
241 '</tr>' . "\n" .
242 '<tr><td>Before you get started, please <a href="' . $redirect_url .
243 '">sign in</a> to your personal Google Base account.</td></tr>' . "\n" .
244 '</table>' . "\n";
245
246 printHTMLFooter();
247 }
248
249 /**
250 * Prints the table of recipes the user has already entered
251 * on the left-hand side of the page.
252 * @param string $token The session token
253 * @return void
254 */
255 function showRecipeListPane($token) {
256 $client = Zend_Gdata_AuthSub::getHttpClient($token);
257 $gdata = new Zend_Gdata_Gbase($client);
258 try {
259 $feed = $gdata->getGbaseItemFeed(ITEMS_FEED_URI . '/-/testrecipes');
260
261 print '<td style="width:50%;text-align:center;vertical-align:top">' . "\n" .
262 '<a href="http://www.google.com/base/dashboard" target="_blank">' .
263 'View all of your published items</a>' .
264 '<table>' . "\n" .
265 '<tr><th colspan="5" style="text-align:center">' .
266 'Recipes you have added that searchable via the API</th></tr>' . "\n";
267
268 if ($feed->count() == 0) {
269 print '<tr style="font-style:italic">' .
270 '<td colspan="5" style="text-align:center">(none)</td>' .
271 '</tr>' . "\n";
272 } else {
273 print '<tr style="font-style:italic">' . "\n" .
274 '<td style="text-align:center">Name</td>' . "\n" .
275 '<td style="text-align:center">Cuisine</td>' . "\n" .
276 '<td style="text-align:center">Serves</td>' . "\n" .
277 '<td colspan="2" style="text-align:center">Actions</td>' . "\n" .
278 '</tr>' . "\n";
279
280 foreach ($feed->entries as $feed_entry) {
281 $href = $feed_entry->link[0]->href;
282 $title = $feed_entry->title->text;
283 $id = $feed_entry->id->text;
284
285 $baseAttributeArr = $feed_entry->getGbaseAttribute('cuisine');
286 // Only want first cuisine
287 if (isset($baseAttributeArr[0]) && is_object($baseAttributeArr[0])) {
288 $cuisine = $baseAttributeArr[0]->text;
289 }
290
291 $baseAttributeArr = $feed_entry->getGbaseAttribute('serving_count');
292 // Only want first serving_count
293 if (isset($baseAttributeArr[0]) && is_object($baseAttributeArr[0])) {
294 $serving_count = $baseAttributeArr[0]->text;
295 }
296
297 print '<tr>' . "\n" .
298 '<td align="left" valign="top"><b><a href="' . $href . '">' .
299 $title . '</a></b></td>' . "\n" .
300 '<td style="text-align:center;vertical-align:top">' .
301 $cuisine . '</td>' . "\n" .
302 '<td style="text-align:center;vertical-align:top">' .
303 $serving_count . '</td>' . "\n";
304
305 /* Create an Edit button for each existing recipe. */
306 print '<td style="text-align:center;vertical-align:top">' . "\n" .
307 '<form method="post" action="' . $_SERVER['PHP_SELF'] .
308 '" style="margin-top:0;margin-bottom:0;">' . "\n" .
309 '<input type="hidden" name="action" value="edit">' . "\n" .
310 "<input type=\"hidden\" name=\"token\" value=\"$token\">" . "\n" .
311 '<input type="hidden" name="edit" value="' . $id . '">' . "\n" .
312 '<input type="submit" value="Edit">' . "\n" .
313 '</form>' . "\n" .
314 '</td>' . "\n";
315
316 /* Create a Delete button for each existing recipe. */
317 print '<td style="text-align:center; vertical-align:top">' . "\n" .
318 '<form method="post" action="' . $_SERVER['PHP_SELF'] .
319 '" style="margin-top:0;margin-bottom:0;">' . "\n" .
320 '<input type="hidden" name="action" value="delete">' . "\n" .
321 "<input type=\"hidden\" name=\"token\" value=\"$token\">" . "\n" .
322 '<input type="hidden" name="link" value="' . $id . '">' . "\n" .
323 '<input type="submit" value="Delete">' . "\n" .
324 '</form>' . "\n" .
325 '</td>' . "\n" .
326 '</tr>' . "\n";
327 }
328 }
329
330 /* Create a "Delete all" button" to demonstrate batch requests. */
331 print '<tr><td colspan="5" style="text-align:center">' . "\n" .
332 '<form method="post" action="' . $_SERVER['PHP_SELF'] .
333 '" style="margin-top:0;margin-bottom:0">' . "\n" .
334 '<input type="hidden" name="action" value="delete_all">' . "\n" .
335 '<input type="hidden" name="token" value="' . $token . '">' . "\n";
336
337 $i = 0;
338 foreach ($feed as $feed_entry) {
339 print '<input type="hidden" name="link_' . $i . '" value="' .
340 $feed_entry->id->text . '">' . "\n";
341 $i++;
342 }
343
344 print '<input type="submit" value="Delete All"';
345 if ($feed->count() == 0) {
346 print ' disabled="true"';
347 }
348 print '></form></td></tr>' . "\n";
349 print '</table>' . "\n";
350
351 print '</td>' . "\n";
352 } catch (Zend_Gdata_App_Exception $e) {
353 showMainMenu("Error: " . $e->getMessage(), $token);
354 }
355 }
356
357 /**
358 * Prints a small form allowing the user to insert a new
359 * recipe.
360 * @param string $sessionToken A session token
361 * @return void
362 */
363 function showRecipeInsertPane($sessionToken) {
364 global $cuisines;
365
366 print '<td valign="top" width="50%">' . "\n" .
367 '<table width="90%">' . "\n" .
368 '<tr><th colspan="2" style="text-align:center">' .
369 'Insert a new recipe</th></tr>' . "\n" .
370 '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">' . "\n" .
371 '<input type="hidden" name="action" value="insert">' . "\n" .
372 "<input type=\"hidden\" name=\"token\" value=\"$sessionToken\">\n" .
373 '<tr><td align="right">Title:</td>' . "\n" .
374 '<td><input type="text" name="recipe_title" class="half">' .
375 '</td></tr>' . "\n" .
376 '<tr><td align="right">Main ingredient:</td>' . "\n" .
377 '<td><input type="text" name="main_ingredient" class="half">' .
378 '</td></tr>' . "\n" .
379 '<tr><td align="right">Cuisine:</td>' . "\n" .
380 '<td><select name="cuisine" class="half">' . "\n";
381
382 foreach ($cuisines as $curCuisine) {
383 print "<option value=\"$curCuisine\">$curCuisine</option>\n";
384 }
385
386 print '</select></td></tr>' . "\n" .
387 '<tr><td align="right">Cooking Time:</td>' .
388 '<td><input type="text" name="time_val" size=2 maxlength=2>&nbsp;' .
389 '<select name="time_units"><option value="minutes">minutes</option>' .
390 '<option value="hours">hours</option></select></td></tr>' . "\n" .
391 '<tr><td align="right">Serves:</td>' . "\n" .
392 '<td><input type="text" name="serves" size=2 maxlength=3></td>' .
393 '</tr>' . "\n" .
394 '<tr><td align="right">Recipe:</td>' . "\n" .
395 '<td><textarea class="full" name="recipe_text"></textarea></td>' .
396 '</tr>' . "\n" .
397 '<td>&nbsp;</td><td><input type="submit" value="Submit"></td>' . "\n" .
398 '</form></tr></table>' . "\n" .
399 '</td>' . "\n";
400 }
401
402 /**
403 * Shows a menu allowing the user to update an existing
404 * recipe with the Base API update feature.
405 * @return void
406 */
407 function showEditMenu() {
408 global $cuisines;
409
410 $client = Zend_Gdata_AuthSub::getHttpClient($_POST['token']);
411 $gdata = new Zend_Gdata_Gbase($client);
412
413 try {
414 $feed = $gdata->getGbaseItemFeed(ITEMS_FEED_URI);
415 foreach ($feed->entries as $feed_entry) {
416 $editLink = $feed_entry->link[2]->href;
417
418 if ($editLink == $_POST['edit']) {
419 $baseAttributeArr = $feed_entry->getGbaseAttribute('cooking_time');
420 if (isset($baseAttributeArr[0]) && is_object($baseAttributeArr[0])) {
421 $splitCookingTime = explode(' ', $baseAttributeArr[0]->text);
422 }
423
424 $baseAttributeArr = $feed_entry->getGbaseAttribute('cuisine');
425 // Cuisine can have multiple entries
426 if (isset($baseAttributeArr[0]) && is_object($baseAttributeArr[0])) {
427 $cuisine = $baseAttributeArr[0]->text;
428 }
429
430 $baseAttributeArr = $feed_entry->getGbaseAttribute('serving_count');
431 // $serving_count can have multiple entries
432 if (isset($baseAttributeArr[0]) && is_object($baseAttributeArr[0])) {
433 $serving_count = $baseAttributeArr[0]->text;
434 }
435
436 $main_ingredient = $feed_entry->getGbaseAttribute('main_ingredient');
437 // Main_ingredient can have multiple entries
438 if (is_array($main_ingredient)) {
439 $main_ingredient = $main_ingredient[0]->text;
440 }
441
442 printHTMLHeader();
443
444 print '<table style="width:50%">' . "\n";
445 print '<tr>' .
446 '<th colspan="2" style="text-align:center">Edit recipe:</th>' .
447 '</tr>' . "\n";
448
449 print "<form method=\"post\" action=\"{$_SERVER['PHP_SELF']}\">\n" .
450 '<input type="hidden" name="action" value="update">' . "\n" .
451 '<input type="hidden" name="link" value="' .
452 $_POST['edit'] . '">' . "\n" .
453 '<input type="hidden" name="token" value="' .
454 $_POST['token'] . '">' . "\n";
455
456 print '<tr><td align="right">Title:</td>' . "\n" .
457 '<td>' .
458 '<input type="text" name="recipe_title" class="half" value="' .
459 $feed_entry->title->text . '">' .
460 '</td></tr>' . "\n";
461
462 print '<tr><td align="right">Main ingredient:</td>' . "\n" .
463 '<td><input type="text" name="main_ingredient" value="' .
464 $main_ingredient . '" class="half"></td></tr>' . "\n";
465
466 print '<tr><td align="right">Cuisine:</td>' . "\n" .
467 '<td><select name="cuisine" class="half">' . "\n";
468
469 foreach ($cuisines as $curCuisine) {
470 print '<option value="' . $curCuisine . '"';
471 if ($curCuisine == $cuisine) {
472 print ' selected="selected"';
473 }
474 print '>' . $curCuisine . "</option>\n";
475 }
476
477 print '</select></td></tr>' . "\n";
478 print '<tr><td align="right">Cooking Time:</td>' .
479 '<td><input type="text" name="time_val" size="2" maxlength="2" ' .
480 'value="' . $splitCookingTime[0] . '">&nbsp;' . "\n" .
481 '<select name="time_units">' . "\n";
482 if ($splitCookingTime[1] == "minutes") {
483 print '<option value="minutes" selected="selected">minutes</option>' .
484 "\n";
485 print '<option value="hours">hours</option>' . "\n";
486 } else {
487 print '<option value="minutes">minutes</option>' . "\n";
488 print '<option value="hours" selected="selected">hours</option>' .
489 "\n";
490 }
491
492 print '</select></td></tr>' . "\n" .
493 '<tr><td align="right">Serves:</td>' . "\n" .
494 '<td><input type="text" name="serves" value="' .
495 $serving_count . '" size="2" maxlength="3"></td></tr>' . "\n" .
496 '<tr><td align="right">Recipe:</td>' . "\n" .
497 '<td><textarea class="full" name="recipe_text">' .
498 $feed_entry->content->text . '</textarea></td></tr>' . "\n" .
499 '<td>&nbsp;</td><td><input type="submit" value="Update">' .
500 '</td>' . "\n" .
501 '</form></tr></table>' . "\n";
502
503 printHTMLFooter();
504
505 break;
506 }
507 }
508 } catch (Zend_Gdata_App_Exception $e) {
509 showMainMenu($e->getMessage(), $_POST['token']);
510 }
511 }
512
513 /**
514 * Displays both the "List of current recipes" and
515 * "Insert a new recipe" panels in a single table.
516 * @param string $tableTitle The title to display in the html table
517 * @param string $sessionToken A session token
518 * @return void
519 */
520 function showMainMenu($tableTitle, $sessionToken) {
521 printHTMLHeader();
522
523 print '<table style="width: 75%;text-align:center">' . "\n" .
524 '<tr>' . "\n" .
525 '<th colspan="2" style="text-align:center;">' .
526 'PHP Demo: Google Base data API<br />' .
527 '<font size="-1">' .
528 '<span style="font-variant: small-caps;">Powered By</span> ' .
529 '<a href="http://framework.zend.com/download/gdata">' .
530 'Zend Google Data Client Library</a></font></th>' . "\n" .
531 '</tr>' . "\n" .
532 '<tr><td colspan="2" align="center">' . $tableTitle . "</td></tr>\n" .
533 '<tr>' . "\n";
534
535 // Create the two sub-tables.
536 showRecipeListPane($sessionToken);
537 showRecipeInsertPane($sessionToken);
538
539 // Add a "Sign out" link.
540 print '<tr><th colspan="2" style="text-align: center">Or click here to' .
541 ' <a href="http://www.google.com/accounts/Logout">sign out</a>' .
542 ' of your Google account.</th></tr>' . "\n";
543
544 // Close the master table.
545 print '</table>' . "\n";
546
547 printHTMLFooter();
548 }
549
550 /**
551 * Exchanges the given single-use token for a session
552 * token using AuthSubSessionToken, and returns the result.
553 * @param string $token The single-use token from AuthSubRequest
554 * @return string The upgraded (session) token
555 */
556 function exchangeToken($token) {
557 return Zend_Gdata_AuthSub::getAuthSubSessionToken($token);
558 }
559
560 /**
561 * We arrive here after the user first authenticates and we get back
562 * a single-use token.
563 * @return void
564 */
565 function showFirstAuthScreen() {
566 $singleUseToken = $_GET['token'];
567 $sessionToken = exchangeToken($singleUseToken);
568
569 if (!$sessionToken) {
570 showIntroPage();
571 } else {
572 $tableTitle = "Here's your <b>single use token:</b> " .
573 "<code>$singleUseToken</code><br/>" . "\n" .
574 "And here's the <b>session token:</b> <code>$sessionToken</code>";
575
576 showMainMenu($tableTitle, $sessionToken);
577 }
578 }
579
580 /**
581 * Main logic to handle the POST operation of inserting an item.
582 * @return void
583 */
584 function handlePost() {
585 try {
586 $newEntry= postItem();
587 if ($newEntry) {
588 showMainMenu('Recipe inserted! It will be searchable by the API soon...',
589 $_POST['token']);
590 }
591 } catch (Zend_Gdata_App_Exception $e) {
592 showMainMenu('Recipe insertion failed: ' . $e->getMessage(),
593 $_POST['token']);
594 }
595 }
596
597 /**
598 * Main logic to handle deleting an item.
599 * @return void
600 */
601 function handleDelete() {
602 try {
603 deleteItem();
604 showMainMenu('Recipe deleted.', $_POST['token']);
605 } catch (Zend_Gdata_App_Exception $e) {
606 showMainMenu('Recipe deletion failed: ' . $e->getMessage(),
607 $_POST['token']);
608 }
609 }
610
611 /**
612 * Main logic to handle a batch deletion of items.
613 * @return void
614 */
615 function handleBatch() {
616 try {
617 $batch_response = batchDelete();
618 if ($batch_response->isSuccessful()) {
619 showMainMenu('All recipes deleted.', $_POST['token']);
620 } else {
621 showMainMenu('Batch deletion failed: ' . $batch_response->getMessage(),
622 $_POST['token']);
623 }
624 } catch (Zend_Gdata_App_Exception $e) {
625 showMainMenu('Batch deletion failed: ' . $e->getMessage(), $_POST['token']);
626 }
627 }
628
629 /**
630 * Main logic to handle updating an item
631 * @return void
632 */
633 function handleUpdate() {
634 try {
635 if (updateItem()) {
636 showMainMenu('Recipe successfully updated.', $_POST['token']);
637 } else {
638 showMainMenu('Recipe update failed.', $_POST['token']);
639 }
640 } catch (Zend_Gdata_App_Exception $e) {
641 showMainMenu('Recipe update failed: ' . $e->getMessage(), $_POST['token']);
642 }
643 }
644
645 /**
646 * Main logic to handle requests
647 */
648 if (count($_GET) == 1 && array_key_exists('token', $_GET)) {
649 showFirstAuthScreen();
650 } else {
651 if (count($_POST) == 0) {
652 showIntroPage();
653 } else {
654 if ($_POST['action'] == 'insert') {
655 handlePost();
656 } else if ($_POST['action'] == 'delete') {
657 handleDelete();
658 } else if ($_POST['action'] == 'delete_all') {
659 handleBatch();
660 } else if ($_POST['action'] == 'edit') {
661 showEditMenu();
662 } else if ($_POST['action'] == 'update') {
663 handleUpdate();
664 } else {
665 showIntroPage();
666 }
667 }
668 }
669
670 ?>