user_id = $id; } public function getClient() { require_once APPPATH . '../vendor/autoload.php'; $client = new \Google_Client(); $client->setApplicationName('Google Drive API - MS1'); $client->setScopes([ Google_Service_Drive::DRIVE_FILE, Google_Service_Drive::DRIVE_METADATA_READONLY ]); $client->setAuthConfig(APPPATH . 'credentials/credentials.json'); $client->setAccessType('offline'); $client->setPrompt('select_account consent'); $client->setRedirectUri(base_url('index.php/GoogleDrive/test_google')); $tokenDir = APPPATH . 'credentials/tokens/'; if (!is_dir($tokenDir)) { mkdir($tokenDir, 0700, true); } $user_id =$this->user_id; // à adapter selon ton système d’authentification $tokenPath = $tokenDir . 'token_' . $user_id . '.json'; // 1. Si token.json existe, on le charge if (file_exists($tokenPath)) { $accessToken = json_decode(file_get_contents($tokenPath), true); $client->setAccessToken($accessToken); // 2. Si expiré, on tente de le refresh automatiquement if ($client->isAccessTokenExpired()) { if ($client->getRefreshToken()) { $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken()); file_put_contents($tokenPath, json_encode($client->getAccessToken())); } else { unlink($tokenPath); redirect($client->createAuthUrl()); exit; } } } else { // 3. Pas de token -> redirection OAuth if (isset($_GET['code'])) { $accessToken = $client->fetchAccessTokenWithAuthCode($_GET['code']); if (!isset($accessToken['error'])) { file_put_contents($tokenPath, json_encode($client->getAccessToken())); } else { echo 'Erreur OAuth : ' . print_r($accessToken, true); exit; } } else { redirect($client->createAuthUrl()); exit; } } return $client; } public function creer_document($proj_id) { $this->load->model('Projets_model'); $mem_proj = $this->Projets_model->mod_getRecord($proj_id); if($mem_proj->proj_google_rep_id==""){ return; } $this->load->model('Clients_model'); $contacts2 = $this->Clients_model->mod_getRecords_contacts( $mem_proj->proj_client_id); $mem_link = base_url() . "index.php/projets/form/" . $mem_proj->proj_id; $this->set_user_id($this->session->userdata('id')); $client = $this->getClient(); if (!($client instanceof Google_Client)) { echo "Erreur : Le client Google n'est pas valide ou vous n'êtes plus connecté."; return; } try { $driveService = new Google_Service_Drive($client); $docsService = new Google_Service_Docs($client); $nomFichier = 'Infos Course '.$this->session->username; $parentId = $mem_proj->proj_google_rep_id; $dateHeure = date('Y-m-d H:i'); // Récupère le driveId du répertoire (Drive partagé) $folderInfo = $driveService->files->get($parentId, [ 'fields' => 'driveId', 'supportsAllDrives' => true ]); $driveId = $folderInfo->getDriveId(); // Données à insérer $data = [ ['Nom du projet', $mem_proj->proj_nom], ['Date', $mem_proj->proj_date_debut], ['Client', $mem_proj->compagnie], ['Notes', $mem_proj->proj_notes], ['Lien', "$mem_link"], ['Représentant', $mem_proj->rep_prenom], ['Date de création', $dateHeure], ]; // Ajout dynamique des autres contacts $i = 1; foreach ($contacts2 as $c) { $nomComplet = trim($c['con_prenom'] . ' ' . $c['con_nom']); $email = $c['con_courriel'] ?? ''; $tel = $c['con_telephone2'] ?? ''; $rol_nom = $c['rol_nom'] ?? ''; // Tu peux aussi appeler jira_get_tickets_by_email($email) ici si tu veux inclure les tickets $ligne = $nomComplet; if ($email) $ligne .= '' . $email; if ($tel) $ligne .= ' - ' . $tel; if ($rol_nom) $ligne .= ' - ' . $rol_nom; $data[] = [ $nomComplet, $ligne]; $i++; $tickets = jira_get_tickets_by_email($email); if (!empty($tickets['ouverts'])): $ligne=''; foreach ($tickets['ouverts'] as $t): $ligne .= '
| {$row[0]} | {$row[1]} |