|
@@ -14,6 +14,7 @@ ini_set('display_errors', 1);
|
|
|
//error_reporting(E_ERROR | E_PARSE);
|
|
//error_reporting(E_ERROR | E_PARSE);
|
|
|
//
|
|
//
|
|
|
require_once 'vendor/autoload.php';
|
|
require_once 'vendor/autoload.php';
|
|
|
|
|
+session_start();
|
|
|
|
|
|
|
|
date_default_timezone_set("Australia/Hobart");
|
|
date_default_timezone_set("Australia/Hobart");
|
|
|
|
|
|
|
@@ -58,7 +59,12 @@ if (isset($_GET['code'])) {
|
|
|
if (!empty($_SESSION['upload_token'])) {
|
|
if (!empty($_SESSION['upload_token'])) {
|
|
|
$client->setAccessToken($_SESSION['upload_token']);
|
|
$client->setAccessToken($_SESSION['upload_token']);
|
|
|
if ($client->isAccessTokenExpired()) {
|
|
if ($client->isAccessTokenExpired()) {
|
|
|
- unset($_SESSION['upload_token']);
|
|
|
|
|
|
|
+ if ($client->getRefreshToken()) {
|
|
|
|
|
+ $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
|
|
|
|
|
+ $_SESSION['upload_token'] = $client->getAccessToken();
|
|
|
|
|
+ } else {
|
|
|
|
|
+ unset($_SESSION['upload_token']);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
} else {
|
|
} else {
|
|
|
$_SESSION['code_verifier'] = $client->getOAuth2Service()->generateCodeVerifier();
|
|
$_SESSION['code_verifier'] = $client->getOAuth2Service()->generateCodeVerifier();
|
|
@@ -90,33 +96,40 @@ if (count($files->getFiles()) > 0) {
|
|
|
|
|
|
|
|
// Replace with your Google Doc's Template file ID
|
|
// Replace with your Google Doc's Template file ID
|
|
|
$documentId = '1RaPRLceht4bElaIqwFVWBaH92428PJciu9sCZ5lQimU';
|
|
$documentId = '1RaPRLceht4bElaIqwFVWBaH92428PJciu9sCZ5lQimU';
|
|
|
-// Retrieve the Google Doc
|
|
|
|
|
-$doc = $docsService->documents->get($documentId);
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
|
-// Replace variables
|
|
|
|
|
-$content = $doc->getBody()->getContent();
|
|
|
|
|
-$content = str_replace('<<DATE>>', $date, $content);
|
|
|
|
|
-$content = str_replace('<<NAME>>', $name, $content);
|
|
|
|
|
-$content = str_replace('<<FIRST_NAME>>', $first_name, $content);
|
|
|
|
|
-$content = str_replace('<<COMPANY>>', $company, $content);
|
|
|
|
|
-$content = str_replace('<<POSTAL_ADDRESS>>', $postal_address, $content);
|
|
|
|
|
-
|
|
|
|
|
-$content = str_replace('<<DRG>>', $drg, $content);
|
|
|
|
|
-$content = str_replace('<<BUILDING_DESCRTIPTION>>', $building_description, $content);
|
|
|
|
|
-$content = str_replace('<<SITE_ADDRESS>>', $site_address, $content);
|
|
|
|
|
-
|
|
|
|
|
-$content = str_replace('<<AGENT>>', $agent, $content);
|
|
|
|
|
-$content = str_replace('<<AGENT_COMPANY>>', $agent_company, $content);
|
|
|
|
|
-
|
|
|
|
|
-// Create a new Google Doc
|
|
|
|
|
-$newDoc = new Google_Service_Docs_Document();
|
|
|
|
|
-$newDoc->setBody(new Google_Service_Docs_Body(['content' => $content]));
|
|
|
|
|
-
|
|
|
|
|
-// Save the new Google Doc to Google Drive
|
|
|
|
|
-$driveFile = new Google_Service_Drive_DriveFile(['name' => $file_name]);
|
|
|
|
|
|
|
+// Copy the template into a new file — replacements happen on the copy, not the template
|
|
|
|
|
+$driveFile = new Google\Service\Drive\DriveFile(['name' => $file_name]);
|
|
|
$driveFile = $driveService->files->copy($documentId, $driveFile);
|
|
$driveFile = $driveService->files->copy($documentId, $driveFile);
|
|
|
|
|
|
|
|
|
|
+// Replace template placeholders using the Docs batchUpdate API.
|
|
|
|
|
+// getBody()->getContent() returns an array of objects, not a string — str_replace cannot be used here.
|
|
|
|
|
+$replacements = [
|
|
|
|
|
+ '<<DATE>>' => $date,
|
|
|
|
|
+ '<<NAME>>' => $name,
|
|
|
|
|
+ '<<FIRST_NAME>>' => $first_name,
|
|
|
|
|
+ '<<COMPANY>>' => $company,
|
|
|
|
|
+ '<<POSTAL_ADDRESS>>' => $postal_address,
|
|
|
|
|
+ '<<DRG>>' => $drg,
|
|
|
|
|
+ '<<BUILDING_DESCRTIPTION>>' => $building_description,
|
|
|
|
|
+ '<<SITE_ADDRESS>>' => $site_address,
|
|
|
|
|
+ '<<AGENT>>' => $agent,
|
|
|
|
|
+ '<<AGENT_COMPANY>>' => $agent_company,
|
|
|
|
|
+];
|
|
|
|
|
+
|
|
|
|
|
+$requests = [];
|
|
|
|
|
+foreach ($replacements as $placeholder => $value) {
|
|
|
|
|
+ $requests[] = new Google\Service\Docs\Request([
|
|
|
|
|
+ 'replaceAllText' => [
|
|
|
|
|
+ 'containsText' => ['text' => $placeholder, 'matchCase' => true],
|
|
|
|
|
+ 'replaceText' => $value,
|
|
|
|
|
+ ],
|
|
|
|
|
+ ]);
|
|
|
|
|
+}
|
|
|
|
|
+$docsService->documents->batchUpdate(
|
|
|
|
|
+ $driveFile->getId(),
|
|
|
|
|
+ new Google\Service\Docs\BatchUpdateDocumentRequest(['requests' => $requests])
|
|
|
|
|
+);
|
|
|
|
|
+
|
|
|
|
|
|
|
|
// Find the Specified Folder to save documents in,
|
|
// Find the Specified Folder to save documents in,
|
|
|
|
|
|