Benjamin Harris 2 týždňov pred
rodič
commit
58dd420dca
4 zmenil súbory, kde vykonal 70 pridanie a 41 odobranie
  1. 19 14
      client-brief.php
  2. 38 25
      old_g_letter.php
  3. 7 1
      process_form.php
  4. 6 1
      simple-file-upload.php

+ 19 - 14
client-brief.php

@@ -440,23 +440,24 @@ if ($__action === 'loa_lookup') {
 /* -------------------------------------------------------------------------- */
 use Google\Client;
 use Google\Service\Drive;
-function createFolder()
+function createFolder(string $access_token, string $folder_name, string $parent_folder_id = ''): ?string
 {
     try {
         $client = new Google\Client();
-        $client->useApplicationDefaultCredentials();
-        $client->addScope(Drive::DRIVE);
+        $client->setAccessToken($access_token);
         $driveService = new Drive($client);
-        $fileMetadata = new Drive\DriveFile(array(
-            'name' => '[03]Projects 2023',
-            'mimeType' => 'application/vnd.google-apps.folder'));
-        $file = $driveService->files->create($fileMetadata, array(
-            'fields' => 'id'));
-        printf("Folder ID: %s\n", $file->id);
+        $metadata = new Drive\DriveFile([
+            'name'     => $folder_name,
+            'mimeType' => 'application/vnd.google-apps.folder',
+        ]);
+        if ($parent_folder_id) {
+            $metadata->setParents([$parent_folder_id]);
+        }
+        $file = $driveService->files->create($metadata, ['fields' => 'id,name,webViewLink']);
         return $file->id;
-
-    }catch(Exception $e) {
-        echo "Error Message: ".$e;
+    } catch (Exception $e) {
+        error_log('createFolder error: ' . $e->getMessage());
+        return null;
     }
 }
 
@@ -1818,8 +1819,12 @@ Preview above. Copy the HTML if you paste into an HTML-capable composer such as
                 const DISCOVERY_DOC = 'https://www.googleapis.com/discovery/v1/apis/drive/v3/rest';
                 var SCOPES = 'https://www.googleapis.com/auth/drive';
 
-                // parent folder: supply a folder ID if you want a parent; leave '' for My Drive
-                const PROJECTS_PARENT_FOLDER_ID = ''; // e.g. '1AbCxyz...'
+                // TODO: Set this to the Google Drive folder ID of the root projects directory
+                // (e.g. the "[03] Projects 20XX" folder). Without this, new job folders are
+                // created in the signed-in user's Drive root, not the shared projects directory.
+                // Find the ID by opening the folder in Drive and copying the ID from the URL:
+                //   https://drive.google.com/drive/folders/<FOLDER_ID_HERE>
+                const PROJECTS_PARENT_FOLDER_ID = ''; // <-- paste folder ID here
 
                 let tokenClient, gapiInited = false, gisInited = false;
 

+ 38 - 25
old_g_letter.php

@@ -14,6 +14,7 @@ ini_set('display_errors', 1);
 //error_reporting(E_ERROR | E_PARSE);
 //
 require_once 'vendor/autoload.php';
+session_start();
 
 date_default_timezone_set("Australia/Hobart");
 
@@ -58,7 +59,12 @@ if (isset($_GET['code'])) {
 if (!empty($_SESSION['upload_token'])) {
     $client->setAccessToken($_SESSION['upload_token']);
     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 {
     $_SESSION['code_verifier'] = $client->getOAuth2Service()->generateCodeVerifier();
@@ -90,33 +96,40 @@ if (count($files->getFiles()) > 0) {
 
 // Replace with your Google Doc's Template file ID
 $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);
 
+// 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,
 

+ 7 - 1
process_form.php

@@ -1,4 +1,5 @@
 <?php
+session_start();
 error_reporting(E_ALL);
 ini_set('display_errors', 1);
 
@@ -36,7 +37,12 @@ if (isset($_GET['code'])) {
 if (!empty($_SESSION['upload_token'])) {
     $client->setAccessToken($_SESSION['upload_token']);
     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 {
     $_SESSION['code_verifier'] = $client->getOAuth2Service()->generateCodeVerifier();

+ 6 - 1
simple-file-upload.php

@@ -67,7 +67,12 @@ if (isset($_GET['code'])) {
 if (!empty($_SESSION['upload_token'])) {
     $client->setAccessToken($_SESSION['upload_token']);
     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 {
     $_SESSION['code_verifier'] = $client->getOAuth2Service()->generateCodeVerifier();