ExportToGoogleSS

The Contact Form DB ExportToGoogleSS class.

Defined (1)

The class is defined in the following location(s).

/ExportToGoogleSS.php  
  1. class ExportToGoogleSS extends ExportBase implements CFDBExport { 
  2.  
  3. public function export($formName, $options = null) { 
  4. $this->setOptions($options); 
  5.  
  6. // Security Check 
  7. if (!$this->isAuthorized()) { 
  8. $this->assertSecurityErrorMessage(); 
  9. return; 
  10.  
  11. // Headers 
  12. $this->echoHeaders('Content-Type: text/html; charset=UTF-8'); 
  13.  
  14. if (!CFDBCheckZendFramework::checkIncludeZend()) { 
  15. return; 
  16.  
  17. Zend_Loader::loadClass('Zend_Gdata'); 
  18. Zend_Loader::loadClass('Zend_Gdata_ClientLogin'); 
  19. //Zend_Loader::loadClass('Zend_Gdata_Spreadsheets'); 
  20. Zend_Loader::loadClass('Zend_Gdata_App_AuthException'); 
  21. Zend_Loader::loadClass('Zend_Http_Client'); 
  22. Zend_Loader::loadClass('Zend_Gdata_Docs'); 
  23.  
  24. $guser = $options['guser']; 
  25. $gpwd = $options['gpwd']; 
  26. try { 
  27. $client = Zend_Gdata_ClientLogin::getHttpClient( 
  28. $guser, $gpwd,  
  29. Zend_Gdata_Docs::AUTH_SERVICE_NAME); //Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME 
  30. catch (Zend_Gdata_App_AuthException $ae) { 
  31. CFDBDie::wp_die("<p>Login failed for: '$guser' </p><p>Error: " . $ae->getMessage() . '</p>',  
  32. __('Login Failed', 'contact-form-7-to-database-extension'),  
  33. array('response' => 200, 'back_link' => true)); 
  34.  
  35. try { 
  36. // Generate CSV file contents into buffer 
  37. $exporter = new ExportToCsvUtf8; 
  38. $exporter->setOptions($options); 
  39. $exporter->setCommonOptions(); 
  40. $exporter->setUseBom(false); 
  41.  
  42. ob_start(); 
  43. $exporter->echoCsv($formName); 
  44. $csvFileContents = ob_get_contents(); 
  45. ob_end_clean(); 
  46.  
  47. // Put the contents in a tmp file because Google upload API only reads from a file 
  48. $tmpfname = tempnam(sys_get_temp_dir(), "$formName.csv"); 
  49. $handle = fopen($tmpfname, 'w'); 
  50. fwrite($handle, $csvFileContents); 
  51. fclose($handle); 
  52.  
  53. // Upload the tmp file to Google Docs 
  54. $docs = new Zend_Gdata_Docs($client); 
  55. $newDocumentEntry = $docs->uploadFile($tmpfname, $formName, 'text/csv'); 
  56. unlink($tmpfname); // delete tmp file 
  57.  
  58. // Get the URL of the new Google doc 
  59. $alternateLink = ''; 
  60. foreach ($newDocumentEntry->link as $link) { 
  61. if ($link->getRel() === 'alternate') { 
  62. $alternateLink = $link->getHref(); 
  63. break; 
  64.  
  65. //header("Location: $alternateLink"); 
  66. //$title = $newDocumentEntry->title; 
  67.  
  68. $title = __('New Google Spreadsheet', 'contact-form-7-to-database-extension'); 
  69. $output = 
  70. utf8_encode("$title: <a target=\"_blank\" href=\"$alternateLink\">") . 
  71. $formName . 
  72. utf8_encode('</a>'); 
  73. CFDBDie::wp_die($output, $title, array('response' => 200, 'back_link' => true)); 
  74. catch (Exception $ex) { 
  75. CFDBDie::wp_die($ex->getMessage() . '<pre>' . $ex->getTraceAsString() . '</pre>',  
  76. __('Error', 'contact-form-7-to-database-extension'),  
  77. array('back_link' => true));