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