old_g_letter.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /* *************************************************** */
  3. // Google Docs Creator
  4. // Gets a Tempalate copies it and inserts data,
  5. // Then saves into the specified folder as a Doc and as a PDF
  6. // In future it will email the pdf to customer email
  7. // (c) 2023 Benjamin Harris ben@tazz.com.au
  8. /* *************************************************** */
  9. //phpinfo();
  10. error_reporting(E_ALL);
  11. ini_set('display_errors', 1);
  12. //error_reporting(E_ERROR | E_PARSE);
  13. //
  14. require_once 'vendor/autoload.php';
  15. date_default_timezone_set("Australia/Hobart");
  16. $today = date("Y-m-d");
  17. $datetime = date("D dS M y @ h:i a");
  18. //Variables to insert into Google Doc
  19. $date = date("D dS M y");
  20. $first_name = 'John';
  21. $last_name = 'Doe';
  22. $name = $first_name . ' ' . $last_name;
  23. $company = 'ABC Company';
  24. $postal_address = '123 Some Street, Some Town, Some State, Some PC';
  25. $drg = '301003';
  26. $building_description = 'Commercial Extension';
  27. $site_address = '456 black Street, Black Town, Black State, Black PC';
  28. $street = explode(",", $site_address);
  29. $site_address_street = substr($street[0], 0);
  30. $agent = 'Benjamin Harris';
  31. $agent_company = 'Agent Company';
  32. $file_name = $drg . ' - ' . $name . ' - LOA - ' . $today;
  33. // Set up Google API Client
  34. $client = new Google\Client();
  35. $client->setAuthConfig('oauth-credentials.json');
  36. $client->setScopes([Google_Service_Drive::DRIVE, Google_Service_Docs::DOCUMENTS]);
  37. $service = new Google\Service\Drive($client);
  38. // Authorize with Google
  39. if (isset($_GET['code'])) {
  40. $token = $client->fetchAccessTokenWithAuthCode($_GET['code'], $_SESSION['code_verifier']);
  41. $client->setAccessToken($token);
  42. // store in the session also
  43. $_SESSION['upload_token'] = $token;
  44. // redirect back to the example
  45. header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
  46. }
  47. // set the access token as part of the client
  48. if (!empty($_SESSION['upload_token'])) {
  49. $client->setAccessToken($_SESSION['upload_token']);
  50. if ($client->isAccessTokenExpired()) {
  51. unset($_SESSION['upload_token']);
  52. }
  53. } else {
  54. $_SESSION['code_verifier'] = $client->getOAuth2Service()->generateCodeVerifier();
  55. $authUrl = $client->createAuthUrl();
  56. }
  57. // Create Google Drive and Google Docs service
  58. $driveService = new Google\Service\Drive($client);
  59. $docsService = new Google\Service\Docs($client);
  60. // Following may be required to future proof, here you can specify the title of the document instead of the document id
  61. /*
  62. // Specify the title (name) of your Google Doc
  63. $docTitle = 'Your Google Doc Title';
  64. // List files in your Google Drive
  65. $files = $driveService->files->listFiles([
  66. 'q' => "name='$docTitle' and mimeType='application/vnd.google-apps.document'",
  67. ]);
  68. if (count($files->getFiles()) > 0) {
  69. // Get the first matching document
  70. $documentId = $files->getFiles()[0]->getId();
  71. } else {
  72. echo 'Google Doc not found.';
  73. die;
  74. }
  75. */
  76. // Replace with your Google Doc's Template file ID
  77. $documentId = '1RaPRLceht4bElaIqwFVWBaH92428PJciu9sCZ5lQimU';
  78. // Retrieve the Google Doc
  79. $doc = $docsService->documents->get($documentId);
  80. // Replace variables
  81. $content = $doc->getBody()->getContent();
  82. $content = str_replace('<<DATE>>', $date, $content);
  83. $content = str_replace('<<NAME>>', $name, $content);
  84. $content = str_replace('<<FIRST_NAME>>', $first_name, $content);
  85. $content = str_replace('<<COMPANY>>', $company, $content);
  86. $content = str_replace('<<POSTAL_ADDRESS>>', $postal_address, $content);
  87. $content = str_replace('<<DRG>>', $drg, $content);
  88. $content = str_replace('<<BUILDING_DESCRTIPTION>>', $building_description, $content);
  89. $content = str_replace('<<SITE_ADDRESS>>', $site_address, $content);
  90. $content = str_replace('<<AGENT>>', $agent, $content);
  91. $content = str_replace('<<AGENT_COMPANY>>', $agent_company, $content);
  92. // Create a new Google Doc
  93. $newDoc = new Google_Service_Docs_Document();
  94. $newDoc->setBody(new Google_Service_Docs_Body(['content' => $content]));
  95. // Save the new Google Doc to Google Drive
  96. $driveFile = new Google_Service_Drive_DriveFile(['name' => $file_name]);
  97. $driveFile = $driveService->files->copy($documentId, $driveFile);
  98. // Find the Specified Folder to save documents in,
  99. // Specify the title (name) of your Google Doc
  100. $folderTitle = $drg . ' - ' . $site_address_street;
  101. // List files in your Google Drive
  102. $files = $driveService->files->listFiles([
  103. 'q' => "name='$folderTitle' and mimeType='application/vnd.google-apps.folder'",
  104. ]);
  105. if (count($files->getFiles()) > 0) {
  106. // Get the first matching document
  107. $folderId = $files->getFiles()[0]->getId();
  108. } else {
  109. echo 'Google folder not found.';
  110. // Create the folder if it doesnt exist,
  111. // folder needs to be created inside folder called [03]Projects 2023 which is "[0Y] Projects YYYY"
  112. // $parent_folder = '[0' . substr(date(y), -1) . '] Projects' . date(Y);
  113. }
  114. // Move the copied file to a specific folder
  115. $driveService->files->update($driveFile->getId(), null, [
  116. 'addParents' => '1ZwFTZ-YzFwYs1mN772WSET570E9ec-7O',
  117. 'removeParents' => $driveFile->getParents()[0],
  118. ]);
  119. // Create PDF
  120. $pdfFile = new Google_Service_Drive_DriveFile(['name' => $file_name . '.pdf']);
  121. $pdfFile = $driveService->files->copy($driveFile->getId(), $pdfFile, ['mimeType' => 'application/pdf']);
  122. echo 'Letter copied, variables replaced, and saved in the client folder.';