Docs.php 9.1 KB

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