]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - Zend/Gdata/Docs.php
Release 6.5.0
[Github/sugarcrm.git] / Zend / Gdata / Docs.php
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-2010 Zend Technologies USA Inc. (http://www.zend.com)
20  * @license    http://framework.zend.com/license/new-bsd     New BSD License
21  */
22
23 /**
24  * Modifications by SugarCRM
25  * 
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
29  */
30
31
32 /**
33  * @see Zend_Gdata
34  */
35 require_once 'Zend/Gdata.php';
36
37 /**
38  * @see Zend_Gdata_Docs_DocumentListFeed
39  */
40 require_once 'Zend/Gdata/Docs/DocumentListFeed.php';
41
42 /**
43  * @see Zend_Gdata_Docs_DocumentListEntry
44  */
45 require_once 'Zend/Gdata/Docs/DocumentListEntry.php';
46
47 /**
48  * Service class for interacting with the Google Document List data API
49  * @link http://code.google.com/apis/documents/
50  *
51  * @category   Zend
52  * @package    Zend_Gdata
53  * @subpackage Docs
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
56  */
57 class Zend_Gdata_Docs extends Zend_Gdata
58 {
59
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;
63
64     protected $_defaultPostUri = self::DOCUMENTS_LIST_FEED_URI;
65
66     /**
67          * Namespaces used for Zend_Gdata_Docs
68          *
69          * @var array
70          */
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)
76     );
77
78     private static $SUPPORTED_FILETYPES = array(
79       'CSV' => 'text/csv',
80       'DOC' => 'application/msword',
81       'DOCX' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
82       'HTML' =>'text/html',
83       'HTM' => 'text/html',
84       'JPG' => 'image/jpeg',
85       'ODS' => 'application/vnd.oasis.opendocument.spreadsheet',
86       'ODT' => 'application/vnd.oasis.opendocument.text',
87       'PDF' => 'application/pdf',
88       'PNG' => 'image/png',
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');
99
100     /**
101      * Create Gdata_Docs object
102      *
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
106      */
107     public function __construct($client = null, $applicationName)
108     {
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);
114     }
115
116     /**
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.
121      *
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.
125      */
126     public static function lookupMimeType($fileExtension) {
127       return self::$SUPPORTED_FILETYPES[strtoupper($fileExtension)];
128     }
129
130     /**
131      * Retrieve feed object containing entries for the user's documents.
132      *
133      * @param mixed $location The location for the feed, as a URL or Query
134      * @return Zend_Gdata_Docs_DocumentListFeed
135      */
136     public function getDocumentListFeed($location = null)
137     {
138         if ($location === null) {
139             $uri = self::DOCUMENTS_LIST_FEED_URI;
140         } else if ($location instanceof Zend_Gdata_Query) {
141             $uri = $location->getQueryUrl();
142         } else {
143             $uri = $location;
144         }
145         return parent::getFeed($uri, 'Zend_Gdata_Docs_DocumentListFeed');
146     }
147
148     /**
149      * Retrieve entry object representing a single document.
150      *
151      * @param mixed $location The location for the entry, as a URL or Query
152      * @return Zend_Gdata_Docs_DocumentListEntry
153      */
154     public function getDocumentListEntry($location = null)
155     {
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();
162         } else {
163             $uri = $location;
164         }
165         return parent::getEntry($uri, 'Zend_Gdata_Docs_DocumentListEntry');
166     }
167
168     /**
169          * Retrieve a document entry representing a single document.
170          *
171          * @param string $resourceId The document resource id. Examples:
172          *     document:dcmg89gw_62hfjj8m, spreadsheet:pKq0CzjiF3YmGd0AIlHKqeg,
173          *     pdf:asdf89hfjjddfg
174          * @return Zend_Gdata_Docs_DocumentListEntry
175          */
176         public function getResource($resourceId) {
177                 $uri = 'https://docs.google.com/feeds/documents/private/full/' . $resourceId;
178                 return $this->getDocumentListEntry($uri);
179     }
180
181     /**
182      * Retrieve entry object representing a single document.
183      *
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.
192      */
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);
197     }
198
199     /**
200      * Retrieve entry object for the desired word processing document.
201      *
202      * @param string $id The URL id for the document. Example:
203      *     dcmg89gw_62hfjj8m
204      * @deprecated Use getResource($resourceId) instead.
205      */
206     public function getDocument($id) {
207         return $this->getDoc('document%3A' . $id);
208     }
209
210     /**
211      * Retrieve entry object for the desired spreadsheet.
212      *
213      * @param string $id The URL id for the spreadsheet. Example:
214      *     pKq0CzjiF3YmGd0AIlHKqeg
215      * @deprecated Use getResource($resourceId) instead.
216      */
217     public function getSpreadsheet($id) {
218         return $this->getDoc('spreadsheet%3A' . $id);
219     }
220
221     /**
222      * Retrieve entry object for the desired presentation.
223      *
224      * @param string $id The URL id for the presentation. Example:
225      *     dcmg89gw_21gtrjcn
226      * @deprecated Use getResource($resourceId) instead.
227      */
228     public function getPresentation($id) {
229         return $this->getDoc('presentation%3A' . $id);
230     }
231
232     /**
233      * Upload a local file to create a new Google Document entry.
234      *
235      * @param string $fileLocation The full or relative path of the file to
236      *         be uploaded.
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
249      *         made.
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.
253      */
254     public function uploadFile($fileLocation, $title=null, $mimeType=null,
255                                $uri=null)
256     {
257         // Set the URI to which the file will be uploaded.
258         if ($uri === null) {
259             $uri = $this->_defaultPostUri;
260         }
261
262         // Create the media source which describes the file.
263         $fs = $this->newMediaFileSource($fileLocation);
264         if ($title !== null) {
265             $slugHeader = $title;
266         } else {
267             $slugHeader = $fileLocation;
268         }
269
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);
274
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);
281         }
282
283         // Set the mime type for the upload request.
284         $fs->setContentType($mimeType);
285
286         // Send the data to the server.
287         return $this->insertDocument($fs, $uri);
288     }
289
290     /**
291      * Inserts an entry to a given URI and returns the response as an Entry.
292      *
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.
302      */
303     public function insertDocument($data, $uri,
304         $className='Zend_Gdata_Docs_DocumentListEntry')
305     {
306         return $this->insertEntry($data, $uri, $className);
307     }
308     
309     /**
310      * Return the supported mime types and file extensions.
311      *
312      * @return array
313      * @author Andreas Sandberg
314      */
315     public static function getSupportedMimeTypes() {
316       return self::$SUPPORTED_FILETYPES;
317     }
318
319 }