PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/application/libraries/Zend/Gdata/Spreadsheets.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 445 lines | 217 code | 47 blank | 181 comment | 39 complexity | cbff90d950aba5e382ccb81f223dc482 MD5 | raw file
  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 Spreadsheets
  18. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Spreadsheets.php 23805 2011-03-16 00:55:40Z tjohns $
  21. */
  22. /**
  23. * Zend_Gdata
  24. */
  25. require_once('Zend/Gdata.php');
  26. /**
  27. * Zend_Gdata_Spreadsheets_SpreadsheetFeed
  28. */
  29. require_once('Zend/Gdata/Spreadsheets/SpreadsheetFeed.php');
  30. /**
  31. * Zend_Gdata_Spreadsheets_WorksheetFeed
  32. */
  33. require_once('Zend/Gdata/Spreadsheets/WorksheetFeed.php');
  34. /**
  35. * Zend_Gdata_Spreadsheets_CellFeed
  36. */
  37. require_once('Zend/Gdata/Spreadsheets/CellFeed.php');
  38. /**
  39. * Zend_Gdata_Spreadsheets_ListFeed
  40. */
  41. require_once('Zend/Gdata/Spreadsheets/ListFeed.php');
  42. /**
  43. * Zend_Gdata_Spreadsheets_SpreadsheetEntry
  44. */
  45. require_once('Zend/Gdata/Spreadsheets/SpreadsheetEntry.php');
  46. /**
  47. * Zend_Gdata_Spreadsheets_WorksheetEntry
  48. */
  49. require_once('Zend/Gdata/Spreadsheets/WorksheetEntry.php');
  50. /**
  51. * Zend_Gdata_Spreadsheets_CellEntry
  52. */
  53. require_once('Zend/Gdata/Spreadsheets/CellEntry.php');
  54. /**
  55. * Zend_Gdata_Spreadsheets_ListEntry
  56. */
  57. require_once('Zend/Gdata/Spreadsheets/ListEntry.php');
  58. /**
  59. * Zend_Gdata_Spreadsheets_DocumentQuery
  60. */
  61. require_once('Zend/Gdata/Spreadsheets/DocumentQuery.php');
  62. /**
  63. * Zend_Gdata_Spreadsheets_ListQuery
  64. */
  65. require_once('Zend/Gdata/Spreadsheets/ListQuery.php');
  66. /**
  67. * Zend_Gdata_Spreadsheets_CellQuery
  68. */
  69. require_once('Zend/Gdata/Spreadsheets/CellQuery.php');
  70. /**
  71. * Gdata Spreadsheets
  72. *
  73. * @link http://code.google.com/apis/gdata/spreadsheets.html
  74. *
  75. * @category Zend
  76. * @package Zend_Gdata
  77. * @subpackage Spreadsheets
  78. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  79. * @license http://framework.zend.com/license/new-bsd New BSD License
  80. */
  81. class Zend_Gdata_Spreadsheets extends Zend_Gdata
  82. {
  83. const SPREADSHEETS_FEED_URI = 'https://spreadsheets.google.com/feeds/spreadsheets';
  84. const SPREADSHEETS_POST_URI = 'https://spreadsheets.google.com/feeds/spreadsheets/private/full';
  85. const WORKSHEETS_FEED_LINK_URI = 'http://schemas.google.com/spreadsheets/2006#worksheetsfeed';
  86. const LIST_FEED_LINK_URI = 'http://schemas.google.com/spreadsheets/2006#listfeed';
  87. const CELL_FEED_LINK_URI = 'http://schemas.google.com/spreadsheets/2006#cellsfeed';
  88. const AUTH_SERVICE_NAME = 'wise';
  89. /**
  90. * Namespaces used for Zend_Gdata_Photos
  91. *
  92. * @var array
  93. */
  94. public static $namespaces = array(
  95. array('gs', 'http://schemas.google.com/spreadsheets/2006', 1, 0),
  96. array(
  97. 'gsx', 'http://schemas.google.com/spreadsheets/2006/extended', 1, 0)
  98. );
  99. /**
  100. * Create Gdata_Spreadsheets object
  101. *
  102. * @param Zend_Http_Client $client (optional) The HTTP client to use when
  103. * when communicating with the Google servers.
  104. * @param string $applicationId The identity of the app in the form of Company-AppName-Version
  105. */
  106. public function __construct($client = null, $applicationId = 'MyCompany-MyApp-1.0')
  107. {
  108. $this->registerPackage('Zend_Gdata_Spreadsheets');
  109. $this->registerPackage('Zend_Gdata_Spreadsheets_Extension');
  110. parent::__construct($client, $applicationId);
  111. $this->_httpClient->setParameterPost('service', self::AUTH_SERVICE_NAME);
  112. $this->_server = 'spreadsheets.google.com';
  113. }
  114. /**
  115. * Gets a spreadsheet feed.
  116. *
  117. * @param mixed $location A DocumentQuery or a string URI specifying the feed location.
  118. * @return Zend_Gdata_Spreadsheets_SpreadsheetFeed
  119. */
  120. public function getSpreadsheetFeed($location = null)
  121. {
  122. if ($location == null) {
  123. $uri = self::SPREADSHEETS_FEED_URI;
  124. } else if ($location instanceof Zend_Gdata_Spreadsheets_DocumentQuery) {
  125. if ($location->getDocumentType() == null) {
  126. $location->setDocumentType('spreadsheets');
  127. }
  128. $uri = $location->getQueryUrl();
  129. } else {
  130. $uri = $location;
  131. }
  132. return parent::getFeed($uri, 'Zend_Gdata_Spreadsheets_SpreadsheetFeed');
  133. }
  134. /**
  135. * Gets a spreadsheet entry.
  136. *
  137. * @param string $location A DocumentQuery or a URI specifying the entry location.
  138. * @return SpreadsheetEntry
  139. */
  140. public function getSpreadsheetEntry($location)
  141. {
  142. if ($location instanceof Zend_Gdata_Spreadsheets_DocumentQuery) {
  143. if ($location->getDocumentType() == null) {
  144. $location->setDocumentType('spreadsheets');
  145. }
  146. $uri = $location->getQueryUrl();
  147. } else {
  148. $uri = $location;
  149. }
  150. return parent::getEntry($uri, 'Zend_Gdata_Spreadsheets_SpreadsheetEntry');
  151. }
  152. /**
  153. * Gets a worksheet feed.
  154. *
  155. * @param mixed $location A DocumentQuery, SpreadsheetEntry, or a string URI
  156. * @return Zend_Gdata_Spreadsheets_WorksheetFeed The feed of worksheets
  157. */
  158. public function getWorksheetFeed($location)
  159. {
  160. if ($location instanceof Zend_Gdata_Spreadsheets_DocumentQuery) {
  161. if ($location->getDocumentType() == null) {
  162. $location->setDocumentType('worksheets');
  163. }
  164. $uri = $location->getQueryUrl();
  165. } else if ($location instanceof Zend_Gdata_Spreadsheets_SpreadsheetEntry) {
  166. $uri = $location->getLink(self::WORKSHEETS_FEED_LINK_URI)->href;
  167. } else {
  168. $uri = $location;
  169. }
  170. return parent::getFeed($uri, 'Zend_Gdata_Spreadsheets_WorksheetFeed');
  171. }
  172. /**
  173. * Gets a worksheet entry.
  174. *
  175. * @param string $location A DocumentQuery or a URI specifying the entry location.
  176. * @return WorksheetEntry
  177. */
  178. public function GetWorksheetEntry($location)
  179. {
  180. if ($location instanceof Zend_Gdata_Spreadsheets_DocumentQuery) {
  181. if ($location->getDocumentType() == null) {
  182. $location->setDocumentType('worksheets');
  183. }
  184. $uri = $location->getQueryUrl();
  185. } else {
  186. $uri = $location;
  187. }
  188. return parent::getEntry($uri, 'Zend_Gdata_Spreadsheets_WorksheetEntry');
  189. }
  190. /**
  191. * Gets a cell feed.
  192. *
  193. * @param string $location A CellQuery, WorksheetEntry or a URI specifying the feed location.
  194. * @return CellFeed
  195. */
  196. public function getCellFeed($location)
  197. {
  198. if ($location instanceof Zend_Gdata_Spreadsheets_CellQuery) {
  199. $uri = $location->getQueryUrl();
  200. } else if ($location instanceof Zend_Gdata_Spreadsheets_WorksheetEntry) {
  201. $uri = $location->getLink(self::CELL_FEED_LINK_URI)->href;
  202. } else {
  203. $uri = $location;
  204. }
  205. return parent::getFeed($uri, 'Zend_Gdata_Spreadsheets_CellFeed');
  206. }
  207. /**
  208. * Gets a cell entry.
  209. *
  210. * @param string $location A CellQuery or a URI specifying the entry location.
  211. * @return CellEntry
  212. */
  213. public function getCellEntry($location)
  214. {
  215. if ($location instanceof Zend_Gdata_Spreadsheets_CellQuery) {
  216. $uri = $location->getQueryUrl();
  217. } else {
  218. $uri = $location;
  219. }
  220. return parent::getEntry($uri, 'Zend_Gdata_Spreadsheets_CellEntry');
  221. }
  222. /**
  223. * Gets a list feed.
  224. *
  225. * @param mixed $location A ListQuery, WorksheetEntry or string URI specifying the feed location.
  226. * @return ListFeed
  227. */
  228. public function getListFeed($location)
  229. {
  230. if ($location instanceof Zend_Gdata_Spreadsheets_ListQuery) {
  231. $uri = $location->getQueryUrl();
  232. } else if ($location instanceof Zend_Gdata_Spreadsheets_WorksheetEntry) {
  233. $uri = $location->getLink(self::LIST_FEED_LINK_URI)->href;
  234. } else {
  235. $uri = $location;
  236. }
  237. return parent::getFeed($uri, 'Zend_Gdata_Spreadsheets_ListFeed');
  238. }
  239. /**
  240. * Gets a list entry.
  241. *
  242. * @param string $location A ListQuery or a URI specifying the entry location.
  243. * @return ListEntry
  244. */
  245. public function getListEntry($location)
  246. {
  247. if ($location instanceof Zend_Gdata_Spreadsheets_ListQuery) {
  248. $uri = $location->getQueryUrl();
  249. } else {
  250. $uri = $location;
  251. }
  252. return parent::getEntry($uri, 'Zend_Gdata_Spreadsheets_ListEntry');
  253. }
  254. /**
  255. * Updates an existing cell.
  256. *
  257. * @param int $row The row containing the cell to update
  258. * @param int $col The column containing the cell to update
  259. * @param int $inputValue The new value for the cell
  260. * @param string $key The key for the spreadsheet to be updated
  261. * @param string $wkshtId (optional) The worksheet to be updated
  262. * @return CellEntry The updated cell entry.
  263. */
  264. public function updateCell($row, $col, $inputValue, $key, $wkshtId = 'default')
  265. {
  266. $cell = 'R'.$row.'C'.$col;
  267. $query = new Zend_Gdata_Spreadsheets_CellQuery();
  268. $query->setSpreadsheetKey($key);
  269. $query->setWorksheetId($wkshtId);
  270. $query->setCellId($cell);
  271. $entry = $this->getCellEntry($query);
  272. $entry->setCell(new Zend_Gdata_Spreadsheets_Extension_Cell(null, $row, $col, $inputValue));
  273. $response = $entry->save();
  274. return $response;
  275. }
  276. /**
  277. * Inserts a new row with provided data.
  278. *
  279. * @param array $rowData An array of column header to row data
  280. * @param string $key The key of the spreadsheet to modify
  281. * @param string $wkshtId (optional) The worksheet to modify
  282. * @return ListEntry The inserted row
  283. */
  284. public function insertRow($rowData, $key, $wkshtId = 'default')
  285. {
  286. $newEntry = new Zend_Gdata_Spreadsheets_ListEntry();
  287. $newCustomArr = array();
  288. foreach ($rowData as $k => $v) {
  289. $newCustom = new Zend_Gdata_Spreadsheets_Extension_Custom();
  290. $newCustom->setText($v)->setColumnName($k);
  291. $newEntry->addCustom($newCustom);
  292. }
  293. $query = new Zend_Gdata_Spreadsheets_ListQuery();
  294. $query->setSpreadsheetKey($key);
  295. $query->setWorksheetId($wkshtId);
  296. $feed = $this->getListFeed($query);
  297. $editLink = $feed->getLink('http://schemas.google.com/g/2005#post');
  298. return $this->insertEntry($newEntry->saveXML(), $editLink->href, 'Zend_Gdata_Spreadsheets_ListEntry');
  299. }
  300. /**
  301. * Updates an existing row with provided data.
  302. *
  303. * @param ListEntry $entry The row entry to update
  304. * @param array $newRowData An array of column header to row data
  305. */
  306. public function updateRow($entry, $newRowData)
  307. {
  308. $newCustomArr = array();
  309. foreach ($newRowData as $k => $v) {
  310. $newCustom = new Zend_Gdata_Spreadsheets_Extension_Custom();
  311. $newCustom->setText($v)->setColumnName($k);
  312. $newCustomArr[] = $newCustom;
  313. }
  314. $entry->setCustom($newCustomArr);
  315. return $entry->save();
  316. }
  317. /**
  318. * Deletes an existing row .
  319. *
  320. * @param ListEntry $entry The row to delete
  321. */
  322. public function deleteRow($entry)
  323. {
  324. $entry->delete();
  325. }
  326. /**
  327. * Returns the content of all rows as an associative array
  328. *
  329. * @param mixed $location A ListQuery or string URI specifying the feed location.
  330. * @return array An array of rows. Each element of the array is an associative array of data
  331. */
  332. public function getSpreadsheetListFeedContents($location)
  333. {
  334. $listFeed = $this->getListFeed($location);
  335. $listFeed = $this->retrieveAllEntriesForFeed($listFeed);
  336. $spreadsheetContents = array();
  337. foreach ($listFeed as $listEntry) {
  338. $rowContents = array();
  339. $customArray = $listEntry->getCustom();
  340. foreach ($customArray as $custom) {
  341. $rowContents[$custom->getColumnName()] = $custom->getText();
  342. }
  343. $spreadsheetContents[] = $rowContents;
  344. }
  345. return $spreadsheetContents;
  346. }
  347. /**
  348. * Returns the content of all cells as an associative array, indexed
  349. * off the cell location (ie 'A1', 'D4', etc). Each element of
  350. * the array is an associative array with a 'value' and a 'function'.
  351. * Only non-empty cells are returned by default. 'range' is the
  352. * value of the 'range' query parameter specified at:
  353. * http://code.google.com/apis/spreadsheets/reference.html#cells_Parameters
  354. *
  355. * @param mixed $location A CellQuery, WorksheetEntry or a URL (w/o query string) specifying the feed location.
  356. * @param string $range The range of cells to retrieve
  357. * @param boolean $empty Whether to retrieve empty cells
  358. * @return array An associative array of cells
  359. */
  360. public function getSpreadsheetCellFeedContents($location, $range = null, $empty = false)
  361. {
  362. $cellQuery = null;
  363. if ($location instanceof Zend_Gdata_Spreadsheets_CellQuery) {
  364. $cellQuery = $location;
  365. } else if ($location instanceof Zend_Gdata_Spreadsheets_WorksheetEntry) {
  366. $url = $location->getLink(self::CELL_FEED_LINK_URI)->href;
  367. $cellQuery = new Zend_Gdata_Spreadsheets_CellQuery($url);
  368. } else {
  369. $url = $location;
  370. $cellQuery = new Zend_Gdata_Spreadsheets_CellQuery($url);
  371. }
  372. if ($range != null) {
  373. $cellQuery->setRange($range);
  374. }
  375. $cellQuery->setReturnEmpty($empty);
  376. $cellFeed = $this->getCellFeed($cellQuery);
  377. $cellFeed = $this->retrieveAllEntriesForFeed($cellFeed);
  378. $spreadsheetContents = array();
  379. foreach ($cellFeed as $cellEntry) {
  380. $cellContents = array();
  381. $cell = $cellEntry->getCell();
  382. $cellContents['formula'] = $cell->getInputValue();
  383. $cellContents['value'] = $cell->getText();
  384. $spreadsheetContents[$cellEntry->getTitle()->getText()] = $cellContents;
  385. }
  386. return $spreadsheetContents;
  387. }
  388. /**
  389. * Alias for getSpreadsheetFeed
  390. *
  391. * @param mixed $location A DocumentQuery or a string URI specifying the feed location.
  392. * @return Zend_Gdata_Spreadsheets_SpreadsheetFeed
  393. */
  394. public function getSpreadsheets($location = null)
  395. {
  396. return $this->getSpreadsheetFeed($location = null);
  397. }
  398. }