initially import ZendFramework-1.9.5 into repository
[cacert-mgr.git] / external / ZendFramework-1.9.5 / demos / Zend / Gdata / MyLibrary / demo.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 /**
23 * @see Zend_Loader
24 */
25 require_once 'Zend/Loader.php';
26
27 /**
28 * @see Zend_Gdata_Books
29 */
30 Zend_Loader::loadClass('Zend_Gdata_Books');
31
32 /**
33 * @see Zend_Gdata_ClientLogin
34 */
35 Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
36
37 /**
38 * @see Zend_Gdata_App_AuthException
39 */
40 Zend_Loader::loadClass('Zend_Gdata_App_AuthException');
41
42
43 class SimpleDemo {
44 /**
45 * Constructor
46 *
47 * @param string $email
48 * @param string $password
49 * @return void
50 */
51 public function __construct($email, $password)
52 {
53 try {
54 $client = Zend_Gdata_ClientLogin::getHttpClient($email, $password,
55 Zend_Gdata_Books::AUTH_SERVICE_NAME);
56 } catch (Zend_Gdata_App_AuthException $ae) {
57 exit("Error: ". $ae->getMessage() ."\nCredentials provided were ".
58 "email: [$email] and password [$password].\n");
59 }
60 $this->gdClient = new Zend_Gdata_Books($client);
61 }
62
63 /**
64 * Print the content of a feed
65 *
66 * @param Zend_Gdata_Gbase_Feed $feed
67 * @return void
68 */
69 public function printFeed($feed)
70 {
71 $i = 0;
72 foreach($feed as $entry) {
73 $titles = $entry->getTitles();
74 $rating = $entry->getRating();
75 if (count($titles)) {
76 if (!is_object($rating)) {
77 $rating_str = "?";
78 } else {
79 $rating_str = $rating->getAverage();
80 }
81 print $i." ".$titles[0]->getText().
82 ", Rating: ".$rating_str."\n";
83 $i++;
84 }
85 }
86 }
87
88 /**
89 * List books in the My library feed
90 *
91 * @return void
92 */
93 public function listLibrary()
94 {
95 $feed = $this->gdClient->getUserLibraryFeed();
96 print "== Books in my library ==\n";
97 $this->printFeed($feed);
98 print "\n";
99 }
100
101 /**
102 * List books in the annotation feed.
103 *
104 * @return void
105 */
106 public function listReviewed()
107 {
108 $feed = $this->gdClient->getUserLibraryFeed(
109 Zend_Gdata_Books::MY_ANNOTATION_FEED_URI);
110 print "== Books I annotated ==\n";
111 $this->printFeed($feed);
112 print "\n";
113 }
114
115 /**
116 * Add an arbitrary book to the library feed.
117 *
118 * @param string $volumeId Volume to the library
119 * @return void
120 */
121 public function addBookToLibrary($volumeId)
122 {
123 $entry = new Zend_Gdata_Books_VolumeEntry();
124 $entry->setId(
125 new Zend_Gdata_App_Extension_Id($volumeId));
126 print "Inserting ".$volumeId."\n\n";
127 return $this->gdClient->insertVolume($entry);
128 }
129
130 /**
131 * Add an arbitrary book to the library feed.
132 *
133 * @param string $volumeId Volume to add a rating to
134 * @param float $rating Numeric rating from 0 to 5
135 * @return void
136 */
137 public function addRating($volumeId, $rating)
138 {
139 $entry = new Zend_Gdata_Books_VolumeEntry();
140 $entry->setId(
141 new Zend_Gdata_App_Extension_Id($volumeId));
142 $entry->setRating(
143 new Zend_Gdata_Extension_Rating($rating, "0", 5, 1));
144 print "Inserting a rating of ".$rating." for ".$volumeId."\n\n";
145 return $this->gdClient->insertVolume($entry,
146 Zend_Gdata_Books::MY_ANNOTATION_FEED_URI);
147 }
148
149 /**
150 * Remove an an arbitrary book from a feed (either remove
151 * from library feed or remove the annotations from annotation
152 * feed).
153 *
154 * @param Zend_Gdata_Books_VolumeEntry $entry
155 * @return void
156 */
157 public function removeBook($entry)
158 {
159 print "Deleting ".$entry->getId()->getText()."\n\n";
160 $this->gdClient->deleteVolume($entry);
161 }
162
163 /**
164 * Main logic for the demo.
165 *
166 * @return void
167 */
168 public function run()
169 {
170 $test_volume = "8YEAAAAAYAAJ";
171
172 // Playing with the library feed
173 $this->listLibrary();
174
175 $entry = $this->addBookToLibrary($test_volume);
176 $this->listLibrary();
177
178 $this->removeBook($entry);
179 $this->listLibrary();
180
181 // Playing with the annotation feed
182 $this->listReviewed();
183
184 $entry = $this->addRating($test_volume, 4.0);
185 $this->listReviewed();
186
187 $this->removeBook($entry);
188 $this->listReviewed();
189 }
190 }
191
192 /**
193 * getInput
194 *
195 * @param string $text
196 * @return string
197 */
198 function getInput($text)
199 {
200 echo $text.': ';
201 return trim(fgets(STDIN));
202 }
203
204 echo "Books Gdata API - my library demo\n\n";
205 $email = null;
206 $pass = null;
207
208 // process command line options
209 foreach ($argv as $argument) {
210 $argParts = explode('=', $argument);
211 if ($argParts[0] == '--email') {
212 $email = $argParts[1];
213 } else if ($argParts[0] == '--pass') {
214 $pass = $argParts[1];
215 }
216 }
217
218 if (($email == null) || ($pass == null)) {
219 $email = getInput(
220 "Please enter your email address [example: username@gmail.com]");
221 $pass = getInput(
222 "Please enter your password [example: mypassword]");
223 }
224
225 $demo = new SimpleDemo($email, $pass);
226 $demo->run();