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.
19 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
20 * @license http://framework.zend.com/license/new-bsd New BSD License
27 require_once 'Zend/Gdata.php';
30 * @see Zend_Gdata_Docs_DocumentListFeed
32 require_once 'Zend/Gdata/Docs/DocumentListFeed.php';
35 * @see Zend_Gdata_Docs_DocumentListEntry
37 require_once 'Zend/Gdata/Docs/DocumentListEntry.php';
40 * @see Zend_Gdata_App_Extension_Category
42 require_once 'Zend/Gdata/App/Extension/Category.php';
45 * @see Zend_Gdata_App_Extension_Title
47 require_once 'Zend/Gdata/App/Extension/Title.php';
50 * Service class for interacting with the Google Document List data API
51 * @link http://code.google.com/apis/documents/
56 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
57 * @license http://framework.zend.com/license/new-bsd New BSD License
59 class Zend_Gdata_Docs
extends Zend_Gdata
62 const DOCUMENTS_LIST_FEED_URI
= 'http://docs.google.com/feeds/documents/private/full';
63 const DOCUMENTS_FOLDER_FEED_URI
= 'http://docs.google.com/feeds/folders/private/full';
64 const DOCUMENTS_CATEGORY_SCHEMA
= 'http://schemas.google.com/g/2005#kind';
65 const DOCUMENTS_CATEGORY_TERM
= 'http://schemas.google.com/docs/2007#folder';
66 const AUTH_SERVICE_NAME
= 'writely';
68 protected $_defaultPostUri = self
::DOCUMENTS_LIST_FEED_URI
;
70 private static $SUPPORTED_FILETYPES = array(
73 'TSV'=>'text/tab-separated-values',
74 'TAB'=>'text/tab-separated-values',
77 'DOC'=>'application/msword',
78 'ODS'=>'application/vnd.oasis.opendocument.spreadsheet',
79 'ODT'=>'application/vnd.oasis.opendocument.text',
80 'RTF'=>'application/rtf',
81 'SXW'=>'application/vnd.sun.xml.writer',
82 'XLS'=>'application/vnd.ms-excel',
83 'XLSX'=>'application/vnd.ms-excel',
84 'PPT'=>'application/vnd.ms-powerpoint',
85 'PPS'=>'application/vnd.ms-powerpoint');
88 * Create Gdata_Docs object
90 * @param Zend_Http_Client $client (optional) The HTTP client to use when
91 * when communicating with the Google servers.
92 * @param string $applicationId The identity of the app in the form of Company-AppName-Version
94 public function __construct($client = null, $applicationId = 'MyCompany-MyApp-1.0')
96 $this->registerPackage('Zend_Gdata_Docs');
97 parent
::__construct($client, $applicationId);
98 $this->_httpClient
->setParameterPost('service', self
::AUTH_SERVICE_NAME
);
102 * Looks up the mime type based on the file name extension. For example,
103 * calling this method with 'csv' would return
104 * 'text/comma-separated-values'. The Mime type is sent as a header in
105 * the upload HTTP POST request.
107 * @param string $fileExtension
108 * @return string The mime type to be sent to the server to tell it how the
109 * multipart mime data should be interpreted.
111 public static function lookupMimeType($fileExtension) {
112 return self
::$SUPPORTED_FILETYPES[strtoupper($fileExtension)];
116 * Retreive feed object containing entries for the user's documents.
118 * @param mixed $location The location for the feed, as a URL or Query
119 * @return Zend_Gdata_Docs_DocumentListFeed
121 public function getDocumentListFeed($location = null)
123 if ($location === null) {
124 $uri = self
::DOCUMENTS_LIST_FEED_URI
;
125 } else if ($location instanceof Zend_Gdata_Query
) {
126 $uri = $location->getQueryUrl();
130 return parent
::getFeed($uri, 'Zend_Gdata_Docs_DocumentListFeed');
134 * Retreive entry object representing a single document.
136 * @param mixed $location The location for the entry, as a URL or Query
137 * @return Zend_Gdata_Docs_DocumentListEntry
139 public function getDocumentListEntry($location = null)
141 if ($location === null) {
142 require_once 'Zend/Gdata/App/InvalidArgumentException.php';
143 throw new Zend_Gdata_App_InvalidArgumentException(
144 'Location must not be null');
145 } else if ($location instanceof Zend_Gdata_Query
) {
146 $uri = $location->getQueryUrl();
150 return parent
::getEntry($uri, 'Zend_Gdata_Docs_DocumentListEntry');
154 * Retreive entry object representing a single document.
156 * This method builds the URL where this item is stored using the type
157 * and the id of the document.
158 * @param string $docId The URL key for the document. Examples:
159 * dcmg89gw_62hfjj8m, pKq0CzjiF3YmGd0AIlHKqeg
160 * @param string $docType The type of the document as used in the Google
161 * Document List URLs. Examples: document, spreadsheet, presentation
162 * @return Zend_Gdata_Docs_DocumentListEntry
164 public function getDoc($docId, $docType) {
165 $location = 'http://docs.google.com/feeds/documents/private/full/' .
166 $docType . '%3A' . $docId;
167 return $this->getDocumentListEntry($location);
171 * Retreive entry object for the desired word processing document.
173 * @param string $id The URL id for the document. Example:
176 public function getDocument($id) {
177 return $this->getDoc($id, 'document');
181 * Retreive entry object for the desired spreadsheet.
183 * @param string $id The URL id for the document. Example:
184 * pKq0CzjiF3YmGd0AIlHKqeg
186 public function getSpreadsheet($id) {
187 return $this->getDoc($id, 'spreadsheet');
191 * Retreive entry object for the desired presentation.
193 * @param string $id The URL id for the document. Example:
196 public function getPresentation($id) {
197 return $this->getDoc($id, 'presentation');
201 * Upload a local file to create a new Google Document entry.
203 * @param string $fileLocation The full or relative path of the file to
205 * @param string $title The name that this document should have on the
206 * server. If set, the title is used as the slug header in the
207 * POST request. If no title is provided, the location of the
208 * file will be used as the slug header in the request. If no
209 * mimeType is provided, this method attempts to determine the
210 * mime type based on the slugHeader by looking for .doc,
211 * .csv, .txt, etc. at the end of the file name.
212 * Example value: 'test.doc'.
213 * @param string $mimeType Describes the type of data which is being sent
214 * to the server. This must be one of the accepted mime types
215 * which are enumerated in SUPPORTED_FILETYPES.
216 * @param string $uri (optional) The URL to which the upload should be
218 * Example: 'http://docs.google.com/feeds/documents/private/full'.
219 * @return Zend_Gdata_Docs_DocumentListEntry The entry for the newly
220 * created Google Document.
222 public function uploadFile($fileLocation, $title=null, $mimeType=null,
225 // Set the URI to which the file will be uploaded.
227 $uri = $this->_defaultPostUri
;
230 // Create the media source which describes the file.
231 $fs = $this->newMediaFileSource($fileLocation);
232 if ($title !== null) {
233 $slugHeader = $title;
235 $slugHeader = $fileLocation;
238 // Set the slug header to tell the Google Documents server what the
239 // title of the document should be and what the file extension was
240 // for the original file.
241 $fs->setSlug($slugHeader);
243 // Set the mime type of the data.
244 if ($mimeType === null) {
245 $filenameParts = explode('.', $fileLocation);
246 $fileExtension = end($filenameParts);
247 $mimeType = self
::lookupMimeType($fileExtension);
250 // Set the mime type for the upload request.
251 $fs->setContentType($mimeType);
253 // Send the data to the server.
254 return $this->insertDocument($fs, $uri);
258 * Creates a new folder in Google Docs
260 * @param string $folderName The folder name to create
261 * @param string|null $folderResourceId The parent folder to create it in
262 * ("folder%3Amy_parent_folder")
263 * @return Zend_Gdata_Entry The folder entry created.
264 * @todo ZF-8732: This should return a *subclass* of Zend_Gdata_Entry, but
265 * the appropriate type doesn't exist yet.
267 public function createFolder($folderName, $folderResourceId=null) {
268 $category = new Zend_Gdata_App_Extension_Category(self
::DOCUMENTS_CATEGORY_TERM
,
269 self
::DOCUMENTS_CATEGORY_SCHEMA
);
270 $title = new Zend_Gdata_App_Extension_Title($folderName);
271 $entry = new Zend_Gdata_Entry();
273 $entry->setCategory(array($category));
274 $entry->setTitle($title);
276 $uri = self
::DOCUMENTS_LIST_FEED_URI
;
277 if ($folderResourceId != null) {
278 $uri = self
::DOCUMENTS_FOLDER_FEED_URI
. '/' . $folderResourceId;
281 return $this->insertEntry($entry, $uri);
285 * Inserts an entry to a given URI and returns the response as an Entry.
287 * @param mixed $data The Zend_Gdata_Docs_DocumentListEntry or media
288 * source to post. If it is a DocumentListEntry, the mediaSource
289 * should already have been set. If $data is a mediaSource, it
290 * should have the correct slug header and mime type.
291 * @param string $uri POST URI
292 * @param string $className (optional) The class of entry to be returned.
293 * The default is a 'Zend_Gdata_Docs_DocumentListEntry'.
294 * @return Zend_Gdata_Docs_DocumentListEntry The entry returned by the
295 * service after insertion.
297 public function insertDocument($data, $uri,
298 $className='Zend_Gdata_Docs_DocumentListEntry')
300 return $this->insertEntry($data, $uri, $className);