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
24 * Modifications by SugarCRM
26 * April 12, 2011 - asandberg: Changed mime-type for jpg files to image/jpeg
27 * March 14, 2011 - asandberg: Added support for Google API v3: http://code.google.com/p/gdata-samples/source/browse/trunk/doclist/OCRDemo/DocsBeta.php
28 * March 10, 2011 - asandberg: Added getSupportedMimeTypes function
35 require_once 'Zend/Gdata.php';
38 * @see Zend_Gdata_Docs_DocumentListFeed
40 require_once 'Zend/Gdata/Docs/DocumentListFeed.php';
43 * @see Zend_Gdata_Docs_DocumentListEntry
45 require_once 'Zend/Gdata/Docs/DocumentListEntry.php';
48 * Service class for interacting with the Google Document List data API
49 * @link http://code.google.com/apis/documents/
54 * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
55 * @license http://framework.zend.com/license/new-bsd New BSD License
57 class Zend_Gdata_Docs extends Zend_Gdata
60 const DOCUMENTS_LIST_FEED_URI = 'https://docs.google.com/feeds/default/private/full';
61 const AUTH_SERVICE_NAME = 'writely';
62 const DEFAULT_MAJOR_PROTOCOL_VERSION = 3;
64 protected $_defaultPostUri = self::DOCUMENTS_LIST_FEED_URI;
67 * Namespaces used for Zend_Gdata_Docs
71 public static $namespaces = array(
72 array('batch', 'http://schemas.google.com/gdata/batch', self::DEFAULT_MAJOR_PROTOCOL_VERSION, 0),
73 array('docs', 'http://schemas.google.com/docs/2007', self::DEFAULT_MAJOR_PROTOCOL_VERSION, 0),
74 array('gAcl', 'http://schemas.google.com/acl/2007', self::DEFAULT_MAJOR_PROTOCOL_VERSION, 0),
75 array('gd', 'http://schemas.google.com/g/2005', self::DEFAULT_MAJOR_PROTOCOL_VERSION, 0)
78 private static $SUPPORTED_FILETYPES = array(
80 'DOC' => 'application/msword',
81 'DOCX' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
84 'JPG' => 'image/jpeg',
85 'ODS' => 'application/vnd.oasis.opendocument.spreadsheet',
86 'ODT' => 'application/vnd.oasis.opendocument.text',
87 'PDF' => 'application/pdf',
89 'PPT' => 'application/vnd.ms-powerpoint',
90 'PPS' => 'application/vnd.ms-powerpoint',
91 'RTF' => 'application/rtf',
92 'SXW' => 'application/vnd.sun.xml.writer',
93 'TAB' => 'text/tab-separated-values',
94 'TXT' => 'text/plain',
95 'TEXT' => 'text/plain',
96 'TSV' => 'text/tab-separated-values',
97 'XLS' => 'application/vnd.ms-excel',
98 'XLSX' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
101 * Create Gdata_Docs object
103 * @param Zend_Http_Client $client (optional) The HTTP client to use when
104 * when communicating with the Google servers.
105 * @param string $applicationId The identity of the app in the form of Company-AppName-Version
107 public function __construct($client = null, $applicationName)
109 $this->registerPackage('Zend_Gdata_Docs');
110 $this->registerPackage('Zend_Gdata_Docs_Extension_WritersCanInvite');
111 parent::__construct($client, $applicationName);
112 $this->_httpClient->setParameterPost('service', self::AUTH_SERVICE_NAME);
113 $this->setMajorProtocolVersion(self::DEFAULT_MAJOR_PROTOCOL_VERSION);
117 * Looks up the mime type based on the file name extension. For example,
118 * calling this method with 'csv' would return
119 * 'text/comma-separated-values'. The Mime type is sent as a header in
120 * the upload HTTP POST request.
122 * @param string $fileExtension
123 * @return string The mime type to be sent to the server to tell it how the
124 * multipart mime data should be interpreted.
126 public static function lookupMimeType($fileExtension) {
127 return self::$SUPPORTED_FILETYPES[strtoupper($fileExtension)];
131 * Retrieve feed object containing entries for the user's documents.
133 * @param mixed $location The location for the feed, as a URL or Query
134 * @return Zend_Gdata_Docs_DocumentListFeed
136 public function getDocumentListFeed($location = null)
138 if ($location === null) {
139 $uri = self::DOCUMENTS_LIST_FEED_URI;
140 } else if ($location instanceof Zend_Gdata_Query) {
141 $uri = $location->getQueryUrl();
145 return parent::getFeed($uri, 'Zend_Gdata_Docs_DocumentListFeed');
149 * Retrieve entry object representing a single document.
151 * @param mixed $location The location for the entry, as a URL or Query
152 * @return Zend_Gdata_Docs_DocumentListEntry
154 public function getDocumentListEntry($location = null)
156 if ($location === null) {
157 require_once 'Zend/Gdata/App/InvalidArgumentException.php';
158 throw new Zend_Gdata_App_InvalidArgumentException(
159 'Location must not be null');
160 } else if ($location instanceof Zend_Gdata_Query) {
161 $uri = $location->getQueryUrl();
165 return parent::getEntry($uri, 'Zend_Gdata_Docs_DocumentListEntry');
169 * Retrieve a document entry representing a single document.
171 * @param string $resourceId The document resource id. Examples:
172 * document:dcmg89gw_62hfjj8m, spreadsheet:pKq0CzjiF3YmGd0AIlHKqeg,
174 * @return Zend_Gdata_Docs_DocumentListEntry
176 public function getResource($resourceId) {
177 $uri = 'https://docs.google.com/feeds/documents/private/full/' . $resourceId;
178 return $this->getDocumentListEntry($uri);
182 * Retrieve entry object representing a single document.
184 * This method builds the URL where this item is stored using the type
185 * and the id of the document.
186 * @param string $docId The URL key for the document. Examples:
187 * dcmg89gw_62hfjj8m, pKq0CzjiF3YmGd0AIlHKqeg
188 * @param string $docType The type of the document as used in the Google
189 * Document List URLs. Examples: document, spreadsheet, presentation
190 * @return Zend_Gdata_Docs_DocumentListEntry
191 * @deprecated Use getResource($resourceId) instead.
193 public function getDoc($docId, $docType) {
194 $location = 'https://docs.google.com/feeds/documents/private/full/' .
195 $docType . '%3A' . $docId;
196 return $this->getDocumentListEntry($location);
200 * Retrieve entry object for the desired word processing document.
202 * @param string $id The URL id for the document. Example:
204 * @deprecated Use getResource($resourceId) instead.
206 public function getDocument($id) {
207 return $this->getDoc('document%3A' . $id);
211 * Retrieve entry object for the desired spreadsheet.
213 * @param string $id The URL id for the spreadsheet. Example:
214 * pKq0CzjiF3YmGd0AIlHKqeg
215 * @deprecated Use getResource($resourceId) instead.
217 public function getSpreadsheet($id) {
218 return $this->getDoc('spreadsheet%3A' . $id);
222 * Retrieve entry object for the desired presentation.
224 * @param string $id The URL id for the presentation. Example:
226 * @deprecated Use getResource($resourceId) instead.
228 public function getPresentation($id) {
229 return $this->getDoc('presentation%3A' . $id);
233 * Upload a local file to create a new Google Document entry.
235 * @param string $fileLocation The full or relative path of the file to
237 * @param string $title The name that this document should have on the
238 * server. If set, the title is used as the slug header in the
239 * POST request. If no title is provided, the location of the
240 * file will be used as the slug header in the request. If no
241 * mimeType is provided, this method attempts to determine the
242 * mime type based on the slugHeader by looking for .doc,
243 * .csv, .txt, etc. at the end of the file name.
244 * Example value: 'test.doc'.
245 * @param string $mimeType Describes the type of data which is being sent
246 * to the server. This must be one of the accepted mime types
247 * which are enumerated in SUPPORTED_FILETYPES.
248 * @param string $uri (optional) The URL to which the upload should be
250 * Example: 'http://docs.google.com/feeds/default/private/full'.
251 * @return Zend_Gdata_Docs_DocumentListEntry The entry for the newly
252 * created Google Document.
254 public function uploadFile($fileLocation, $title=null, $mimeType=null,
257 // Set the URI to which the file will be uploaded.
259 $uri = $this->_defaultPostUri;
262 // Create the media source which describes the file.
263 $fs = $this->newMediaFileSource($fileLocation);
264 if ($title !== null) {
265 $slugHeader = $title;
267 $slugHeader = $fileLocation;
270 // Set the slug header to tell the Google Documents server what the
271 // title of the document should be and what the file extension was
272 // for the original file.
273 $fs->setSlug($slugHeader);
275 // Set the mime type of the data.
276 if ($mimeType === null) {
277 $slugHeader = $fs->getSlug();
278 $filenameParts = explode('.', $slugHeader);
279 $fileExtension = end($filenameParts);
280 $mimeType = self::lookupMimeType($fileExtension);
283 // Set the mime type for the upload request.
284 $fs->setContentType($mimeType);
286 // Send the data to the server.
287 return $this->insertDocument($fs, $uri);
291 * Inserts an entry to a given URI and returns the response as an Entry.
293 * @param mixed $data The Zend_Gdata_Docs_DocumentListEntry or media
294 * source to post. If it is a DocumentListEntry, the mediaSource
295 * should already have been set. If $data is a mediaSource, it
296 * should have the correct slug header and mime type.
297 * @param string $uri POST URI
298 * @param string $className (optional) The class of entry to be returned.
299 * The default is a 'Zend_Gdata_Docs_DocumentListEntry'.
300 * @return Zend_Gdata_Docs_DocumentListEntry The entry returned by the
301 * service after insertion.
303 public function insertDocument($data, $uri,
304 $className='Zend_Gdata_Docs_DocumentListEntry')
306 return $this->insertEntry($data, $uri, $className);
310 * Return the supported mime types and file extensions.
313 * @author Andreas Sandberg
315 public static function getSupportedMimeTypes() {
316 return self::$SUPPORTED_FILETYPES;