Docs.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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-2015 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. * @see Zend_Gdata_App_Extension_Category
  36. */
  37. require_once 'Zend/Gdata/App/Extension/Category.php';
  38. /**
  39. * @see Zend_Gdata_App_Extension_Title
  40. */
  41. require_once 'Zend/Gdata/App/Extension/Title.php';
  42. /**
  43. * Service class for interacting with the Google Document List data API
  44. * @link http://code.google.com/apis/documents/
  45. *
  46. * @category Zend
  47. * @package Zend_Gdata
  48. * @subpackage Docs
  49. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  50. * @license http://framework.zend.com/license/new-bsd New BSD License
  51. */
  52. class Zend_Gdata_Docs extends Zend_Gdata
  53. {
  54. const DOCUMENTS_LIST_FEED_URI = 'https://docs.google.com/feeds/documents/private/full';
  55. const DOCUMENTS_FOLDER_FEED_URI = 'https://docs.google.com/feeds/folders/private/full';
  56. const DOCUMENTS_CATEGORY_SCHEMA = 'http://schemas.google.com/g/2005#kind';
  57. const DOCUMENTS_CATEGORY_TERM = 'http://schemas.google.com/docs/2007#folder';
  58. const AUTH_SERVICE_NAME = 'writely';
  59. /**
  60. * @var string
  61. */
  62. protected $_defaultPostUri = self::DOCUMENTS_LIST_FEED_URI;
  63. /**
  64. * @var array
  65. */
  66. protected static $SUPPORTED_FILETYPES = array(
  67. 'TXT' => 'text/plain',
  68. 'CSV' => 'text/csv',
  69. 'TSV' => 'text/tab-separated-values',
  70. 'TAB' => 'text/tab-separated-values',
  71. 'HTML' => 'text/html',
  72. 'HTM' => 'text/html',
  73. 'DOC' => 'application/msword',
  74. 'DOCX' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  75. 'ODS' => 'application/vnd.oasis.opendocument.spreadsheet',
  76. 'ODT' => 'application/vnd.oasis.opendocument.text',
  77. 'RTF' => 'application/rtf',
  78. 'SXW' => 'application/vnd.sun.xml.writer',
  79. 'XLS' => 'application/vnd.ms-excel',
  80. 'XLSX' => 'application/vnd.ms-excel',
  81. 'PPT' => 'application/vnd.ms-powerpoint',
  82. 'PPS' => 'application/vnd.ms-powerpoint'
  83. );
  84. /**
  85. * Create Gdata_Docs object
  86. *
  87. * @param Zend_Http_Client $client (optional) The HTTP client to use when
  88. * when communicating with the Google servers.
  89. * @param string $applicationId The identity of the app in the form of Company-AppName-Version
  90. */
  91. public function __construct($client = null, $applicationId = 'MyCompany-MyApp-1.0')
  92. {
  93. $this->registerPackage('Zend_Gdata_Docs');
  94. parent::__construct($client, $applicationId);
  95. $this->_httpClient->setParameterPost('service', self::AUTH_SERVICE_NAME);
  96. }
  97. /**
  98. * Looks up the mime type based on the file name extension. For example,
  99. * calling this method with 'csv' would return
  100. * 'text/comma-separated-values'. The Mime type is sent as a header in
  101. * the upload HTTP POST request.
  102. *
  103. * @param string $fileExtension
  104. * @return string The mime type to be sent to the server to tell it how the
  105. * multipart mime data should be interpreted.
  106. */
  107. public static function lookupMimeType($fileExtension)
  108. {
  109. return self::$SUPPORTED_FILETYPES[strtoupper($fileExtension)];
  110. }
  111. /**
  112. * Retreive feed object containing entries for the user's documents.
  113. *
  114. * @param mixed $location The location for the feed, as a URL or Query
  115. * @return Zend_Gdata_Docs_DocumentListFeed
  116. */
  117. public function getDocumentListFeed($location = null)
  118. {
  119. if ($location === null) {
  120. $uri = self::DOCUMENTS_LIST_FEED_URI;
  121. } else if ($location instanceof Zend_Gdata_Query) {
  122. $uri = $location->getQueryUrl();
  123. } else {
  124. $uri = $location;
  125. }
  126. return parent::getFeed($uri, 'Zend_Gdata_Docs_DocumentListFeed');
  127. }
  128. /**
  129. * Retreive entry object representing a single document.
  130. *
  131. * @param mixed $location The location for the entry, as a URL or Query
  132. * @return Zend_Gdata_Docs_DocumentListEntry
  133. * @throws Zend_Gdata_App_InvalidArgumentException
  134. */
  135. public function getDocumentListEntry($location = null)
  136. {
  137. if ($location === null) {
  138. require_once 'Zend/Gdata/App/InvalidArgumentException.php';
  139. throw new Zend_Gdata_App_InvalidArgumentException(
  140. 'Location must not be null'
  141. );
  142. } else if ($location instanceof Zend_Gdata_Query) {
  143. $uri = $location->getQueryUrl();
  144. } else {
  145. $uri = $location;
  146. }
  147. return parent::getEntry($uri, 'Zend_Gdata_Docs_DocumentListEntry');
  148. }
  149. /**
  150. * Retreive entry object representing a single document.
  151. *
  152. * This method builds the URL where this item is stored using the type
  153. * and the id of the document.
  154. * @param string $docId The URL key for the document. Examples:
  155. * dcmg89gw_62hfjj8m, pKq0CzjiF3YmGd0AIlHKqeg
  156. * @param string $docType The type of the document as used in the Google
  157. * Document List URLs. Examples: document, spreadsheet, presentation
  158. * @return Zend_Gdata_Docs_DocumentListEntry
  159. */
  160. public function getDoc($docId, $docType)
  161. {
  162. $location = 'https://docs.google.com/feeds/documents/private/full/' .
  163. $docType . '%3A' . $docId;
  164. return $this->getDocumentListEntry($location);
  165. }
  166. /**
  167. * Retreive entry object for the desired word processing document.
  168. *
  169. * @param string $id The URL id for the document. Example: dcmg89gw_62hfjj8m
  170. * @return Zend_Gdata_Docs_DocumentListEntry
  171. */
  172. public function getDocument($id)
  173. {
  174. return $this->getDoc($id, 'document');
  175. }
  176. /**
  177. * Retreive entry object for the desired spreadsheet.
  178. *
  179. * @param string $id The URL id for the document. Example: pKq0CzjiF3YmGd0AIlHKqeg
  180. * @return Zend_Gdata_Docs_DocumentListEntry
  181. */
  182. public function getSpreadsheet($id)
  183. {
  184. return $this->getDoc($id, 'spreadsheet');
  185. }
  186. /**
  187. * Retreive entry object for the desired presentation.
  188. *
  189. * @param string $id The URL id for the document. Example: dcmg89gw_21gtrjcn
  190. * @return Zend_Gdata_Docs_DocumentListEntry
  191. */
  192. public function getPresentation($id)
  193. {
  194. return $this->getDoc($id, 'presentation');
  195. }
  196. /**
  197. * Upload a local file to create a new Google Document entry.
  198. *
  199. * @param string $fileLocation The full or relative path of the file to
  200. * be uploaded.
  201. * @param string $title The name that this document should have on the
  202. * server. If set, the title is used as the slug header in the
  203. * POST request. If no title is provided, the location of the
  204. * file will be used as the slug header in the request. If no
  205. * mimeType is provided, this method attempts to determine the
  206. * mime type based on the slugHeader by looking for .doc,
  207. * .csv, .txt, etc. at the end of the file name.
  208. * Example value: 'test.doc'.
  209. * @param string $mimeType Describes the type of data which is being sent
  210. * to the server. This must be one of the accepted mime types
  211. * which are enumerated in SUPPORTED_FILETYPES.
  212. * @param string $uri (optional) The URL to which the upload should be
  213. * made.
  214. * Example: 'https://docs.google.com/feeds/documents/private/full'.
  215. * @return Zend_Gdata_Docs_DocumentListEntry The entry for the newly
  216. * created Google Document.
  217. */
  218. public function uploadFile($fileLocation, $title = null, $mimeType = null,
  219. $uri = null
  220. )
  221. {
  222. // Set the URI to which the file will be uploaded.
  223. if ($uri === null) {
  224. $uri = $this->_defaultPostUri;
  225. }
  226. // Create the media source which describes the file.
  227. $fs = $this->newMediaFileSource($fileLocation);
  228. if ($title !== null) {
  229. $slugHeader = $title;
  230. } else {
  231. $slugHeader = $fileLocation;
  232. }
  233. // Set the slug header to tell the Google Documents server what the
  234. // title of the document should be and what the file extension was
  235. // for the original file.
  236. $fs->setSlug($slugHeader);
  237. // Set the mime type of the data.
  238. if ($mimeType === null) {
  239. $filenameParts = explode('.', $fileLocation);
  240. $fileExtension = end($filenameParts);
  241. $mimeType = self::lookupMimeType($fileExtension);
  242. }
  243. // Set the mime type for the upload request.
  244. $fs->setContentType($mimeType);
  245. // Send the data to the server.
  246. return $this->insertDocument($fs, $uri);
  247. }
  248. /**
  249. * Creates a new folder in Google Docs
  250. *
  251. * @param string $folderName The folder name to create
  252. * @param string|null $folderResourceId The parent folder to create it in
  253. * ("folder%3Amy_parent_folder")
  254. * @return Zend_Gdata_Entry The folder entry created.
  255. * @todo ZF-8732: This should return a *subclass* of Zend_Gdata_Entry, but
  256. * the appropriate type doesn't exist yet.
  257. */
  258. public function createFolder($folderName, $folderResourceId = null)
  259. {
  260. $category = new Zend_Gdata_App_Extension_Category(
  261. self::DOCUMENTS_CATEGORY_TERM,
  262. self::DOCUMENTS_CATEGORY_SCHEMA
  263. );
  264. $title = new Zend_Gdata_App_Extension_Title($folderName);
  265. $entry = new Zend_Gdata_Entry();
  266. $entry->setCategory(array($category));
  267. $entry->setTitle($title);
  268. $uri = self::DOCUMENTS_LIST_FEED_URI;
  269. if ($folderResourceId != null) {
  270. $uri = self::DOCUMENTS_FOLDER_FEED_URI . '/' . $folderResourceId;
  271. }
  272. return $this->insertEntry($entry, $uri);
  273. }
  274. /**
  275. * Inserts an entry to a given URI and returns the response as an Entry.
  276. *
  277. * @param mixed $data The Zend_Gdata_Docs_DocumentListEntry or media
  278. * source to post. If it is a DocumentListEntry, the mediaSource
  279. * should already have been set. If $data is a mediaSource, it
  280. * should have the correct slug header and mime type.
  281. * @param string $uri POST URI
  282. * @param string $className (optional) The class of entry to be returned.
  283. * The default is a 'Zend_Gdata_Docs_DocumentListEntry'.
  284. * @return Zend_Gdata_Docs_DocumentListEntry The entry returned by the
  285. * service after insertion.
  286. */
  287. public function insertDocument($data, $uri,
  288. $className = 'Zend_Gdata_Docs_DocumentListEntry')
  289. {
  290. return $this->insertEntry($data, $uri, $className);
  291. }
  292. }