File indexing completed on 2024-12-22 05:36:46
0001 <?php 0002 0003 /** 0004 * Zend Framework 0005 * 0006 * LICENSE 0007 * 0008 * This source file is subject to the new BSD license that is bundled 0009 * with this package in the file LICENSE.txt. 0010 * It is also available through the world-wide-web at this URL: 0011 * http://framework.zend.com/license/new-bsd 0012 * If you did not receive a copy of the license and are unable to 0013 * obtain it through the world-wide-web, please send an email 0014 * to license@zend.com so we can send you a copy immediately. 0015 * 0016 * @category Zend 0017 * @package Zend_Gdata 0018 * @subpackage Docs 0019 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0020 * @license http://framework.zend.com/license/new-bsd New BSD License 0021 * @version $Id$ 0022 */ 0023 0024 /** 0025 * @see Zend_Gdata 0026 */ 0027 // require_once 'Zend/Gdata.php'; 0028 0029 /** 0030 * @see Zend_Gdata_Docs_DocumentListFeed 0031 */ 0032 // require_once 'Zend/Gdata/Docs/DocumentListFeed.php'; 0033 0034 /** 0035 * @see Zend_Gdata_Docs_DocumentListEntry 0036 */ 0037 // require_once 'Zend/Gdata/Docs/DocumentListEntry.php'; 0038 0039 /** 0040 * @see Zend_Gdata_App_Extension_Category 0041 */ 0042 // require_once 'Zend/Gdata/App/Extension/Category.php'; 0043 0044 /** 0045 * @see Zend_Gdata_App_Extension_Title 0046 */ 0047 // require_once 'Zend/Gdata/App/Extension/Title.php'; 0048 0049 /** 0050 * Service class for interacting with the Google Document List data API 0051 * @link http://code.google.com/apis/documents/ 0052 * 0053 * @category Zend 0054 * @package Zend_Gdata 0055 * @subpackage Docs 0056 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0057 * @license http://framework.zend.com/license/new-bsd New BSD License 0058 */ 0059 class Zend_Gdata_Docs extends Zend_Gdata 0060 { 0061 const DOCUMENTS_LIST_FEED_URI = 'https://docs.google.com/feeds/documents/private/full'; 0062 const DOCUMENTS_FOLDER_FEED_URI = 'https://docs.google.com/feeds/folders/private/full'; 0063 const DOCUMENTS_CATEGORY_SCHEMA = 'http://schemas.google.com/g/2005#kind'; 0064 const DOCUMENTS_CATEGORY_TERM = 'http://schemas.google.com/docs/2007#folder'; 0065 const AUTH_SERVICE_NAME = 'writely'; 0066 0067 /** 0068 * @var string 0069 */ 0070 protected $_defaultPostUri = self::DOCUMENTS_LIST_FEED_URI; 0071 0072 /** 0073 * @var array 0074 */ 0075 protected static $SUPPORTED_FILETYPES = array( 0076 'TXT' => 'text/plain', 0077 'CSV' => 'text/csv', 0078 'TSV' => 'text/tab-separated-values', 0079 'TAB' => 'text/tab-separated-values', 0080 'HTML' => 'text/html', 0081 'HTM' => 'text/html', 0082 'DOC' => 'application/msword', 0083 'DOCX' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 0084 'ODS' => 'application/vnd.oasis.opendocument.spreadsheet', 0085 'ODT' => 'application/vnd.oasis.opendocument.text', 0086 'RTF' => 'application/rtf', 0087 'SXW' => 'application/vnd.sun.xml.writer', 0088 'XLS' => 'application/vnd.ms-excel', 0089 'XLSX' => 'application/vnd.ms-excel', 0090 'PPT' => 'application/vnd.ms-powerpoint', 0091 'PPS' => 'application/vnd.ms-powerpoint' 0092 ); 0093 0094 /** 0095 * Create Gdata_Docs object 0096 * 0097 * @param Zend_Http_Client $client (optional) The HTTP client to use when 0098 * when communicating with the Google servers. 0099 * @param string $applicationId The identity of the app in the form of Company-AppName-Version 0100 */ 0101 public function __construct($client = null, $applicationId = 'MyCompany-MyApp-1.0') 0102 { 0103 $this->registerPackage('Zend_Gdata_Docs'); 0104 parent::__construct($client, $applicationId); 0105 $this->_httpClient->setParameterPost('service', self::AUTH_SERVICE_NAME); 0106 } 0107 0108 /** 0109 * Looks up the mime type based on the file name extension. For example, 0110 * calling this method with 'csv' would return 0111 * 'text/comma-separated-values'. The Mime type is sent as a header in 0112 * the upload HTTP POST request. 0113 * 0114 * @param string $fileExtension 0115 * @return string The mime type to be sent to the server to tell it how the 0116 * multipart mime data should be interpreted. 0117 */ 0118 public static function lookupMimeType($fileExtension) 0119 { 0120 return self::$SUPPORTED_FILETYPES[strtoupper($fileExtension)]; 0121 } 0122 0123 /** 0124 * Retreive feed object containing entries for the user's documents. 0125 * 0126 * @param mixed $location The location for the feed, as a URL or Query 0127 * @return Zend_Gdata_Docs_DocumentListFeed 0128 */ 0129 public function getDocumentListFeed($location = null) 0130 { 0131 if ($location === null) { 0132 $uri = self::DOCUMENTS_LIST_FEED_URI; 0133 } else if ($location instanceof Zend_Gdata_Query) { 0134 $uri = $location->getQueryUrl(); 0135 } else { 0136 $uri = $location; 0137 } 0138 return parent::getFeed($uri, 'Zend_Gdata_Docs_DocumentListFeed'); 0139 } 0140 0141 /** 0142 * Retreive entry object representing a single document. 0143 * 0144 * @param mixed $location The location for the entry, as a URL or Query 0145 * @return Zend_Gdata_Docs_DocumentListEntry 0146 * @throws Zend_Gdata_App_InvalidArgumentException 0147 */ 0148 public function getDocumentListEntry($location = null) 0149 { 0150 if ($location === null) { 0151 // require_once 'Zend/Gdata/App/InvalidArgumentException.php'; 0152 throw new Zend_Gdata_App_InvalidArgumentException( 0153 'Location must not be null' 0154 ); 0155 } else if ($location instanceof Zend_Gdata_Query) { 0156 $uri = $location->getQueryUrl(); 0157 } else { 0158 $uri = $location; 0159 } 0160 return parent::getEntry($uri, 'Zend_Gdata_Docs_DocumentListEntry'); 0161 } 0162 0163 /** 0164 * Retreive entry object representing a single document. 0165 * 0166 * This method builds the URL where this item is stored using the type 0167 * and the id of the document. 0168 * @param string $docId The URL key for the document. Examples: 0169 * dcmg89gw_62hfjj8m, pKq0CzjiF3YmGd0AIlHKqeg 0170 * @param string $docType The type of the document as used in the Google 0171 * Document List URLs. Examples: document, spreadsheet, presentation 0172 * @return Zend_Gdata_Docs_DocumentListEntry 0173 */ 0174 public function getDoc($docId, $docType) 0175 { 0176 $location = 'https://docs.google.com/feeds/documents/private/full/' . 0177 $docType . '%3A' . $docId; 0178 0179 return $this->getDocumentListEntry($location); 0180 } 0181 0182 /** 0183 * Retreive entry object for the desired word processing document. 0184 * 0185 * @param string $id The URL id for the document. Example: dcmg89gw_62hfjj8m 0186 * @return Zend_Gdata_Docs_DocumentListEntry 0187 */ 0188 public function getDocument($id) 0189 { 0190 return $this->getDoc($id, 'document'); 0191 } 0192 0193 /** 0194 * Retreive entry object for the desired spreadsheet. 0195 * 0196 * @param string $id The URL id for the document. Example: pKq0CzjiF3YmGd0AIlHKqeg 0197 * @return Zend_Gdata_Docs_DocumentListEntry 0198 */ 0199 public function getSpreadsheet($id) 0200 { 0201 return $this->getDoc($id, 'spreadsheet'); 0202 } 0203 0204 /** 0205 * Retreive entry object for the desired presentation. 0206 * 0207 * @param string $id The URL id for the document. Example: dcmg89gw_21gtrjcn 0208 * @return Zend_Gdata_Docs_DocumentListEntry 0209 */ 0210 public function getPresentation($id) 0211 { 0212 return $this->getDoc($id, 'presentation'); 0213 } 0214 0215 /** 0216 * Upload a local file to create a new Google Document entry. 0217 * 0218 * @param string $fileLocation The full or relative path of the file to 0219 * be uploaded. 0220 * @param string $title The name that this document should have on the 0221 * server. If set, the title is used as the slug header in the 0222 * POST request. If no title is provided, the location of the 0223 * file will be used as the slug header in the request. If no 0224 * mimeType is provided, this method attempts to determine the 0225 * mime type based on the slugHeader by looking for .doc, 0226 * .csv, .txt, etc. at the end of the file name. 0227 * Example value: 'test.doc'. 0228 * @param string $mimeType Describes the type of data which is being sent 0229 * to the server. This must be one of the accepted mime types 0230 * which are enumerated in SUPPORTED_FILETYPES. 0231 * @param string $uri (optional) The URL to which the upload should be 0232 * made. 0233 * Example: 'https://docs.google.com/feeds/documents/private/full'. 0234 * @return Zend_Gdata_Docs_DocumentListEntry The entry for the newly 0235 * created Google Document. 0236 */ 0237 public function uploadFile($fileLocation, $title = null, $mimeType = null, 0238 $uri = null 0239 ) 0240 { 0241 // Set the URI to which the file will be uploaded. 0242 if ($uri === null) { 0243 $uri = $this->_defaultPostUri; 0244 } 0245 0246 // Create the media source which describes the file. 0247 $fs = $this->newMediaFileSource($fileLocation); 0248 if ($title !== null) { 0249 $slugHeader = $title; 0250 } else { 0251 $slugHeader = $fileLocation; 0252 } 0253 0254 // Set the slug header to tell the Google Documents server what the 0255 // title of the document should be and what the file extension was 0256 // for the original file. 0257 $fs->setSlug($slugHeader); 0258 0259 // Set the mime type of the data. 0260 if ($mimeType === null) { 0261 $filenameParts = explode('.', $fileLocation); 0262 $fileExtension = end($filenameParts); 0263 $mimeType = self::lookupMimeType($fileExtension); 0264 } 0265 0266 // Set the mime type for the upload request. 0267 $fs->setContentType($mimeType); 0268 0269 // Send the data to the server. 0270 return $this->insertDocument($fs, $uri); 0271 } 0272 0273 /** 0274 * Creates a new folder in Google Docs 0275 * 0276 * @param string $folderName The folder name to create 0277 * @param string|null $folderResourceId The parent folder to create it in 0278 * ("folder%3Amy_parent_folder") 0279 * @return Zend_Gdata_Entry The folder entry created. 0280 * @todo ZF-8732: This should return a *subclass* of Zend_Gdata_Entry, but 0281 * the appropriate type doesn't exist yet. 0282 */ 0283 public function createFolder($folderName, $folderResourceId = null) 0284 { 0285 $category = new Zend_Gdata_App_Extension_Category( 0286 self::DOCUMENTS_CATEGORY_TERM, 0287 self::DOCUMENTS_CATEGORY_SCHEMA 0288 ); 0289 $title = new Zend_Gdata_App_Extension_Title($folderName); 0290 $entry = new Zend_Gdata_Entry(); 0291 0292 $entry->setCategory(array($category)); 0293 $entry->setTitle($title); 0294 0295 $uri = self::DOCUMENTS_LIST_FEED_URI; 0296 if ($folderResourceId != null) { 0297 $uri = self::DOCUMENTS_FOLDER_FEED_URI . '/' . $folderResourceId; 0298 } 0299 0300 return $this->insertEntry($entry, $uri); 0301 } 0302 0303 /** 0304 * Inserts an entry to a given URI and returns the response as an Entry. 0305 * 0306 * @param mixed $data The Zend_Gdata_Docs_DocumentListEntry or media 0307 * source to post. If it is a DocumentListEntry, the mediaSource 0308 * should already have been set. If $data is a mediaSource, it 0309 * should have the correct slug header and mime type. 0310 * @param string $uri POST URI 0311 * @param string $className (optional) The class of entry to be returned. 0312 * The default is a 'Zend_Gdata_Docs_DocumentListEntry'. 0313 * @return Zend_Gdata_Docs_DocumentListEntry The entry returned by the 0314 * service after insertion. 0315 */ 0316 public function insertDocument($data, $uri, 0317 $className = 'Zend_Gdata_Docs_DocumentListEntry') 0318 { 0319 return $this->insertEntry($data, $uri, $className); 0320 } 0321 }