Gbase.php 23 KB

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