Spreadsheet-ClientLogin.php 13 KB

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