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