This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
/
Spreadsheet-ClientLogin.php
454 lines (415 loc) · 13 KB
/
Spreadsheet-ClientLogin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Gdata
* @subpackage Demos
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
/**
* @see Zend_Loader
*/
require_once 'Zend/Loader.php';
/**
* @see Zend_Gdata
*/
Zend_Loader::loadClass('Zend_Gdata');
/**
* @see Zend_Gdata_ClientLogin
*/
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
/**
* @see Zend_Gdata_Spreadsheets
*/
Zend_Loader::loadClass('Zend_Gdata_Spreadsheets');
/**
* @see Zend_Gdata_App_AuthException
*/
Zend_Loader::loadClass('Zend_Gdata_App_AuthException');
/**
* @see Zend_Http_Client
*/
Zend_Loader::loadClass('Zend_Http_Client');
/**
* SimpleCRUD
*
* @category Zend
* @package Zend_Gdata
* @subpackage Demos
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class SimpleCRUD
{
/**
* Constructor
*
* @param string $email
* @param string $password
* @return void
*/
public function __construct($email, $password)
{
try {
$client = Zend_Gdata_ClientLogin::getHttpClient($email, $password,
Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME);
} catch (Zend_Gdata_App_AuthException $ae) {
exit("Error: ". $ae->getMessage() ."\nCredentials provided were email: [$email] and password [$password].\n");
}
$this->gdClient = new Zend_Gdata_Spreadsheets($client);
$this->currKey = '';
$this->currWkshtId = '';
$this->listFeed = '';
$this->rowCount = 0;
$this->columnCount = 0;
}
/**
* promptForSpreadsheet
*
* @return void
*/
public function promptForSpreadsheet()
{
$feed = $this->gdClient->getSpreadsheetFeed();
print "== Available Spreadsheets ==\n";
$this->printFeed($feed);
$input = getInput("\nSelection");
$currKey = explode('/', $feed->entries[$input]->id->text);
$this->currKey = $currKey[5];
}
/**
* promptForWorksheet
*
* @return void
*/
public function promptForWorksheet()
{
$query = new Zend_Gdata_Spreadsheets_DocumentQuery();
$query->setSpreadsheetKey($this->currKey);
$feed = $this->gdClient->getWorksheetFeed($query);
print "== Available Worksheets ==\n";
$this->printFeed($feed);
$input = getInput("\nSelection");
$currWkshtId = explode('/', $feed->entries[$input]->id->text);
$this->currWkshtId = $currWkshtId[8];
}
/**
* promptForCellsAction
*
* @return void
*/
public function promptForCellsAction()
{
echo "Pick a command:\n";
echo "\ndump -- dump cell information\nupdate {row} {col} {input_value} -- update cell information\n";
$input = getInput('Command');
$command = explode(' ', $input);
if ($command[0] == 'dump') {
$this->cellsGetAction();
} else if (($command[0] == 'update') && (count($command) > 2)) {
$this->getRowAndColumnCount();
if (count($command) == 4) {
$this->cellsUpdateAction($command[1], $command[2], $command[3]);
} elseif (count($command) > 4) {
$newValue = implode(' ', array_slice($command,3));
$this->cellsUpdateAction($command[1], $command[2], $newValue);
} else {
$this->cellsUpdateAction($command[1], $command[2], '');
}
} else {
$this->invalidCommandError($input);
}
}
/**
* promptToResize
*
* @param integer $newRowCount
* @param integer $newColumnCount
* @return boolean
*/
public function promptToResize($newRowCount, $newColumnCount) {
$input = getInput('Would you like to resize the worksheet? [yes | no]');
if ($input == 'yes') {
return $this->resizeWorksheet($newRowCount, $newColumnCount);
} else {
return false;
}
}
/**
* resizeWorksheet
*
* @param integer $newRowCount
* @param integer $newColumnCount
* @return boolean
*/
public function resizeWorksheet($newRowCount, $newColumnCount) {
$query = new Zend_Gdata_Spreadsheets_DocumentQuery();
$query->setSpreadsheetKey($this->currKey);
$query->setWorksheetId($this->currWkshtId);
$currentWorksheet = $this->gdClient->getWorksheetEntry($query);
$currentWorksheet = $currentWorksheet->setRowCount(new Zend_Gdata_Spreadsheets_Extension_RowCount($newRowCount));
$currentWorksheet = $currentWorksheet->setColumnCount(new Zend_Gdata_Spreadsheets_Extension_ColCount($newColumnCount));
$currentWorksheet->save();
$this->getRowAndColumnCount();
print "Worksheet has been resized to $this->rowCount rows and $this->columnCount columns.\n";
return true;
}
/**
* promptForListAction
*
* @return void
*/
public function promptForListAction()
{
echo "\n== Options ==\n".
"dump -- dump row information\n".
"insert {row_data} -- insert data in the next available cell in a given column (example: insert column_header=content)\n".
"update {row_index} {row_data} -- update data in the row provided (example: update row-number column-header=newdata\n".
"delete {row_index} -- delete a row\n\n";
$input = getInput('Command');
$command = explode(' ', $input);
if ($command[0] == 'dump') {
$this->listGetAction();
} else if ($command[0] == 'insert') {
$this->listInsertAction(array_slice($command, 1));
} else if ($command[0] == 'update') {
$this->listUpdateAction($command[1], array_slice($command, 2));
} else if ($command[0] == 'delete') {
$this->listDeleteAction($command[1]);
} else {
$this->invalidCommandError($input);
}
}
/**
* cellsGetAction
*
* @return void
*/
public function cellsGetAction()
{
$query = new Zend_Gdata_Spreadsheets_CellQuery();
$query->setSpreadsheetKey($this->currKey);
$query->setWorksheetId($this->currWkshtId);
$feed = $this->gdClient->getCellFeed($query);
$this->printFeed($feed);
}
/**
* cellsUpdateAction
*
* @param integer $row
* @param integer $col
* @param string $inputValue
* @return void
*/
public function cellsUpdateAction($row, $col, $inputValue)
{
if (($row > $this->rowCount) || ($col > $this->columnCount)) {
print "Current worksheet only has $this->rowCount rows and $this->columnCount columns.\n";
if (!$this->promptToResize($row, $col)) {
return;
}
}
$entry = $this->gdClient->updateCell($row, $col, $inputValue,
$this->currKey, $this->currWkshtId);
if ($entry instanceof Zend_Gdata_Spreadsheets_CellEntry) {
echo "Success!\n";
}
}
/**
* listGetAction
*
* @return void
*/
public function listGetAction()
{
$query = new Zend_Gdata_Spreadsheets_ListQuery();
$query->setSpreadsheetKey($this->currKey);
$query->setWorksheetId($this->currWkshtId);
$this->listFeed = $this->gdClient->getListFeed($query);
print "entry id | row-content in column A | column-header: cell-content\n".
"Please note: The 'dump' command on the list feed only dumps data until the first blank row is encountered.\n\n";
$this->printFeed($this->listFeed);
print "\n";
}
/**
* listInsertAction
*
* @param mixed $rowData
* @return void
*/
public function listInsertAction($rowData)
{
$rowArray = $this->stringToArray($rowData);
$entry = $this->gdClient->insertRow($rowArray, $this->currKey, $this->currWkshtId);
if ($entry instanceof Zend_Gdata_Spreadsheets_ListEntry) {
foreach ($rowArray as $column_header => $value) {
echo "Success! Inserted '$value' in column '$column_header' at row ". substr($entry->getTitle()->getText(), 5) ."\n";
}
}
}
/**
* listUpdateAction
*
* @param integer $index
* @param mixed $rowData
* @return void
*/
public function listUpdateAction($index, $rowData)
{
$query = new Zend_Gdata_Spreadsheets_ListQuery();
$query->setSpreadsheetKey($this->currKey);
$query->setWorksheetId($this->currWkshtId);
$this->listFeed = $this->gdClient->getListFeed($query);
$rowArray = $this->stringToArray($rowData);
$entry = $this->gdClient->updateRow($this->listFeed->entries[$index], $rowArray);
if ($entry instanceof Zend_Gdata_Spreadsheets_ListEntry) {
echo "Success!\n"; $response = $entry->save();
}
}
/**
* listDeleteAction
*
* @param integer $index
* @return void
*/
public function listDeleteAction($index)
{
$query = new Zend_Gdata_Spreadsheets_ListQuery();
$query->setSpreadsheetKey($this->currKey);
$query->setWorksheetId($this->currWkshtId);
$this->listFeed = $this->gdClient->getListFeed($query);
$this->gdClient->deleteRow($this->listFeed->entries[$index]);
}
/**
* stringToArray
*
* @param string $rowData
* @return array
*/
public function stringToArray($rowData)
{
$arr = array();
foreach ($rowData as $row) {
$temp = explode('=', $row);
$arr[$temp[0]] = $temp[1];
}
return $arr;
}
/**
* printFeed
*
* @param Zend_Gdata_Gbase_Feed $feed
* @return void
*/
public function printFeed($feed)
{
$i = 0;
foreach($feed->entries as $entry) {
if ($entry instanceof Zend_Gdata_Spreadsheets_CellEntry) {
print $entry->title->text .' '. $entry->content->text . "\n";
} else if ($entry instanceof Zend_Gdata_Spreadsheets_ListEntry) {
print $i .' '. $entry->title->text .' | '. $entry->content->text . "\n";
} else {
print $i .' '. $entry->title->text . "\n";
}
$i++;
}
}
/**
* getRowAndColumnCount
*
* @return void
*/
public function getRowAndColumnCount()
{
$query = new Zend_Gdata_Spreadsheets_CellQuery();
$query->setSpreadsheetKey($this->currKey);
$query->setWorksheetId($this->currWkshtId);
$feed = $this->gdClient->getCellFeed($query);
if ($feed instanceOf Zend_Gdata_Spreadsheets_CellFeed) {
$this->rowCount = $feed->getRowCount();
$this->columnCount = $feed->getColumnCount();
}
}
/**
* invalidCommandError
*
* @param string $input
* @return void
*/
public function invalidCommandError($input)
{
echo 'Invalid input: '.$input."\n";
}
/**
* promtForFeedtype
*
* @return void
*/
public function promptForFeedtype() {
$input = getInput('Select to use either the cell or the list feed [cells or list]');
if ($input == 'cells') {
while(1) {
$this->promptForCellsAction();
}
} else if ($input == 'list') {
while(1) {
$this->promptForListAction();
}
} else {
print "Invalid input. Please try again.\n";
$this->promptForFeedtype();
}
}
/**
* run
*
* @return void
*/
public function run()
{
$this->promptForSpreadsheet();
$this->promptForWorksheet();
$this->promptForFeedtype();
}
}
/**
* getInput
*
* @param string $text
* @return string
*/
function getInput($text)
{
echo $text.': ';
return trim(fgets(STDIN));
}
$email = null;
$pass = null;
// process command line options
foreach ($argv as $argument) {
$argParts = explode('=', $argument);
if ($argParts[0] == '--email') {
$email = $argParts[1];
} else if ($argParts[0] == '--pass') {
$pass = $argParts[1];
}
}
if (($email == null) || ($pass == null)) {
$email = getInput("Please enter your email address [example: username@gmail.com]");
$pass = getInput("Please enter your password [example: mypassword]");
}
$sample = new SimpleCRUD($email, $pass);
$sample->run();