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