1 | <?php
|
---|
2 |
|
---|
3 | /**
|
---|
4 | * Zend Framework
|
---|
5 | *
|
---|
6 | * LICENSE
|
---|
7 | *
|
---|
8 | * This source file is subject to the new BSD license that is bundled
|
---|
9 | * with this package in the file LICENSE.txt.
|
---|
10 | * It is also available through the world-wide-web at this URL:
|
---|
11 | * http://framework.zend.com/license/new-bsd
|
---|
12 | * If you did not receive a copy of the license and are unable to
|
---|
13 | * obtain it through the world-wide-web, please send an email
|
---|
14 | * to license@zend.com so we can send you a copy immediately.
|
---|
15 | *
|
---|
16 | * @category Zend
|
---|
17 | * @package Zend_Gdata
|
---|
18 | * @subpackage Docs
|
---|
19 | * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
---|
20 | * @license http://framework.zend.com/license/new-bsd New BSD License
|
---|
21 | */
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * @see Zend_Gdata
|
---|
25 | */
|
---|
26 | require_once 'Zend/Gdata.php';
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * @see Zend_Gdata_Docs_DocumentListFeed
|
---|
30 | */
|
---|
31 | require_once 'Zend/Gdata/Docs/DocumentListFeed.php';
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * @see Zend_Gdata_Docs_DocumentListEntry
|
---|
35 | */
|
---|
36 | require_once 'Zend/Gdata/Docs/DocumentListEntry.php';
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Service class for interacting with the Google Document List data API
|
---|
40 | * @link http://code.google.com/apis/documents/
|
---|
41 | *
|
---|
42 | * @category Zend
|
---|
43 | * @package Zend_Gdata
|
---|
44 | * @subpackage Docs
|
---|
45 | * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
---|
46 | * @license http://framework.zend.com/license/new-bsd New BSD License
|
---|
47 | */
|
---|
48 | class Zend_Gdata_Docs extends Zend_Gdata
|
---|
49 | {
|
---|
50 |
|
---|
51 | const DOCUMENTS_LIST_FEED_URI = 'http://docs.google.com/feeds/documents/private/full';
|
---|
52 | const AUTH_SERVICE_NAME = 'writely';
|
---|
53 |
|
---|
54 | protected $_defaultPostUri = self::DOCUMENTS_LIST_FEED_URI;
|
---|
55 |
|
---|
56 | private static $SUPPORTED_FILETYPES = array(
|
---|
57 | 'CSV'=>'text/csv',
|
---|
58 | 'DOC'=>'application/msword',
|
---|
59 | 'ODS'=>'application/vnd.oasis.opendocument.spreadsheet',
|
---|
60 | 'ODT'=>'application/vnd.oasis.opendocument.text',
|
---|
61 | 'RTF'=>'application/rtf',
|
---|
62 | 'SXW'=>'application/vnd.sun.xml.writer',
|
---|
63 | 'TXT'=>'text/plain',
|
---|
64 | 'XLS'=>'application/vnd.ms-excel');
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Create Gdata_Docs object
|
---|
68 | *
|
---|
69 | * @param Zend_Http_Client $client (optional) The HTTP client to use when
|
---|
70 | * when communicating with the Google servers.
|
---|
71 | * @param string $applicationId The identity of the app in the form of Company-AppName-Version
|
---|
72 | */
|
---|
73 | public function __construct($client = null, $applicationId = 'MyCompany-MyApp-1.0')
|
---|
74 | {
|
---|
75 | $this->registerPackage('Zend_Gdata_Docs');
|
---|
76 | parent::__construct($client, $applicationId);
|
---|
77 | $this->_httpClient->setParameterPost('service', self::AUTH_SERVICE_NAME);
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Looks up the mime type based on the file name extension. For example,
|
---|
82 | * calling this method with 'csv' would return
|
---|
83 | * 'text/comma-separated-values'. The Mime type is sent as a header in
|
---|
84 | * the upload HTTP POST request.
|
---|
85 | *
|
---|
86 | * @param string $fileExtension
|
---|
87 | * @return string The mime type to be sent to the server to tell it how the
|
---|
88 | * multipart mime data should be interpreted.
|
---|
89 | */
|
---|
90 | public static function lookupMimeType($fileExtension) {
|
---|
91 | return self::$SUPPORTED_FILETYPES[strtoupper($fileExtension)];
|
---|
92 | }
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Retreive feed object containing entries for the user's documents.
|
---|
96 | *
|
---|
97 | * @param mixed $location The location for the feed, as a URL or Query
|
---|
98 | * @return Zend_Gdata_Docs_DocumentListFeed
|
---|
99 | */
|
---|
100 | public function getDocumentListFeed($location = null)
|
---|
101 | {
|
---|
102 | if ($location === null) {
|
---|
103 | $uri = self::DOCUMENTS_LIST_FEED_URI;
|
---|
104 | } else if ($location instanceof Zend_Gdata_Query) {
|
---|
105 | $uri = $location->getQueryUrl();
|
---|
106 | } else {
|
---|
107 | $uri = $location;
|
---|
108 | }
|
---|
109 | return parent::getFeed($uri, 'Zend_Gdata_Docs_DocumentListFeed');
|
---|
110 | }
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Retreive entry object representing a single document.
|
---|
114 | *
|
---|
115 | * @param mixed $location The location for the entry, as a URL or Query
|
---|
116 | * @return Zend_Gdata_Docs_DocumentListEntry
|
---|
117 | */
|
---|
118 | public function getDocumentListEntry($location = null)
|
---|
119 | {
|
---|
120 | if ($location === null) {
|
---|
121 | require_once 'Zend/Gdata/App/InvalidArgumentException.php';
|
---|
122 | throw new Zend_Gdata_App_InvalidArgumentException(
|
---|
123 | 'Location must not be null');
|
---|
124 | } else if ($location instanceof Zend_Gdata_Query) {
|
---|
125 | $uri = $location->getQueryUrl();
|
---|
126 | } else {
|
---|
127 | $uri = $location;
|
---|
128 | }
|
---|
129 | return parent::getEntry($uri, 'Zend_Gdata_Docs_DocumentListEntry');
|
---|
130 | }
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * Retreive entry object representing a single document.
|
---|
134 | *
|
---|
135 | * This method builds the URL where this item is stored using the type
|
---|
136 | * and the id of the document.
|
---|
137 | * @param string $docId The URL key for the document. Examples:
|
---|
138 | * dcmg89gw_62hfjj8m, pKq0CzjiF3YmGd0AIlHKqeg
|
---|
139 | * @param string $docType The type of the document as used in the Google
|
---|
140 | * Document List URLs. Examples: document, spreadsheet, presentation
|
---|
141 | * @return Zend_Gdata_Docs_DocumentListEntry
|
---|
142 | */
|
---|
143 | public function getDoc($docId, $docType) {
|
---|
144 | $location = 'http://docs.google.com/feeds/documents/private/full/' .
|
---|
145 | $docType . '%3A' . $docId;
|
---|
146 | return $this->getDocumentListEntry($location);
|
---|
147 | }
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Retreive entry object for the desired word processing document.
|
---|
151 | *
|
---|
152 | * @param string $id The URL id for the document. Example:
|
---|
153 | * dcmg89gw_62hfjj8m
|
---|
154 | */
|
---|
155 | public function getDocument($id) {
|
---|
156 | return $this->getDoc($id, 'document');
|
---|
157 | }
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * Retreive entry object for the desired spreadsheet.
|
---|
161 | *
|
---|
162 | * @param string $id The URL id for the document. Example:
|
---|
163 | * pKq0CzjiF3YmGd0AIlHKqeg
|
---|
164 | */
|
---|
165 | public function getSpreadsheet($id) {
|
---|
166 | return $this->getDoc($id, 'spreadsheet');
|
---|
167 | }
|
---|
168 |
|
---|
169 | /**
|
---|
170 | * Retreive entry object for the desired presentation.
|
---|
171 | *
|
---|
172 | * @param string $id The URL id for the document. Example:
|
---|
173 | * dcmg89gw_21gtrjcn
|
---|
174 | */
|
---|
175 | public function getPresentation($id) {
|
---|
176 | return $this->getDoc($id, 'presentation');
|
---|
177 | }
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * Upload a local file to create a new Google Document entry.
|
---|
181 | *
|
---|
182 | * @param string $fileLocation The full or relative path of the file to
|
---|
183 | * be uploaded.
|
---|
184 | * @param string $title The name that this document should have on the
|
---|
185 | * server. If set, the title is used as the slug header in the
|
---|
186 | * POST request. If no title is provided, the location of the
|
---|
187 | * file will be used as the slug header in the request. If no
|
---|
188 | * mimeType is provided, this method attempts to determine the
|
---|
189 | * mime type based on the slugHeader by looking for .doc,
|
---|
190 | * .csv, .txt, etc. at the end of the file name.
|
---|
191 | * Example value: 'test.doc'.
|
---|
192 | * @param string $mimeType Describes the type of data which is being sent
|
---|
193 | * to the server. This must be one of the accepted mime types
|
---|
194 | * which are enumerated in SUPPORTED_FILETYPES.
|
---|
195 | * @param string $uri (optional) The URL to which the upload should be
|
---|
196 | * made.
|
---|
197 | * Example: 'http://docs.google.com/feeds/documents/private/full'.
|
---|
198 | * @return Zend_Gdata_Docs_DocumentListEntry The entry for the newly
|
---|
199 | * created Google Document.
|
---|
200 | */
|
---|
201 | public function uploadFile($fileLocation, $title=null, $mimeType=null,
|
---|
202 | $uri=null)
|
---|
203 | {
|
---|
204 | // Set the URI to which the file will be uploaded.
|
---|
205 | if ($uri === null) {
|
---|
206 | $uri = $this->_defaultPostUri;
|
---|
207 | }
|
---|
208 |
|
---|
209 | // Create the media source which describes the file.
|
---|
210 | $fs = $this->newMediaFileSource($fileLocation);
|
---|
211 | if ($title !== null) {
|
---|
212 | $slugHeader = $title;
|
---|
213 | } else {
|
---|
214 | $slugHeader = $fileLocation;
|
---|
215 | }
|
---|
216 |
|
---|
217 | // Set the slug header to tell the Google Documents server what the
|
---|
218 | // title of the document should be and what the file extension was
|
---|
219 | // for the original file.
|
---|
220 | $fs->setSlug($slugHeader);
|
---|
221 |
|
---|
222 | // Set the mime type of the data.
|
---|
223 | if ($mimeType === null) {
|
---|
224 | $slugHeader = $fs->getSlug();
|
---|
225 | $filenameParts = explode('.', $slugHeader);
|
---|
226 | $fileExtension = end($filenameParts);
|
---|
227 | $mimeType = self::lookupMimeType($fileExtension);
|
---|
228 | }
|
---|
229 |
|
---|
230 | // Set the mime type for the upload request.
|
---|
231 | $fs->setContentType($mimeType);
|
---|
232 |
|
---|
233 | // Send the data to the server.
|
---|
234 | return $this->insertDocument($fs, $uri);
|
---|
235 | }
|
---|
236 |
|
---|
237 | /**
|
---|
238 | * Inserts an entry to a given URI and returns the response as an Entry.
|
---|
239 | *
|
---|
240 | * @param mixed $data The Zend_Gdata_Docs_DocumentListEntry or media
|
---|
241 | * source to post. If it is a DocumentListEntry, the mediaSource
|
---|
242 | * should already have been set. If $data is a mediaSource, it
|
---|
243 | * should have the correct slug header and mime type.
|
---|
244 | * @param string $uri POST URI
|
---|
245 | * @param string $className (optional) The class of entry to be returned.
|
---|
246 | * The default is a 'Zend_Gdata_Docs_DocumentListEntry'.
|
---|
247 | * @return Zend_Gdata_Docs_DocumentListEntry The entry returned by the
|
---|
248 | * service after insertion.
|
---|
249 | */
|
---|
250 | public function insertDocument($data, $uri,
|
---|
251 | $className='Zend_Gdata_Docs_DocumentListEntry')
|
---|
252 | {
|
---|
253 | return $this->insertEntry($data, $uri, $className);
|
---|
254 | }
|
---|
255 |
|
---|
256 | }
|
---|