Files
crm-ms1/application/models/GoogleDrive_model.php
2026-05-27 11:44:10 -04:00

174 lines
5.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class GoogleDrive_model extends CI_Model
{
/** @var object|null */
private $gds = null;
public function __construct()
{
parent::__construct();
$this->gds = $this->boot_google_drive_any();
if (!$this->gds) {
log_message('error', '[GoogleDrive_model] Aucune classe Google Drive trouvée.');
throw new Exception('Librairie Google Drive introuvable (GoogleDrive / GoogleDriveDocs / …)');
}
}
/**
* Essaie dinclure et dinstancier une des classes de ta stack.
* - Fichiers testés: GoogleDrive.php, GoogleDriveDocs.php, DiagGoogle.php, GoogleServiceJWT.php
* - Dossiers testés: application/libraries, application/models, application/
*/
private function boot_google_drive_any()
{
$files = ['GoogleDrive.php','GoogleDriveDocs.php','DiagGoogle.php','GoogleServiceJWT.php'];
$dirs = [APPPATH.'libraries/', APPPATH.'models/', APPPATH];
foreach ($dirs as $dir) {
foreach ($files as $fn) {
$path = $dir.$fn;
if (is_file($path)) {
require_once $path;
}
}
}
// Classes candidates (ajoute ici si ton nom diffère)
$classes = ['GoogleDriveService','GoogleDrive','GoogleDriveDocs','DiagGoogle'];
foreach ($classes as $cls) {
if (class_exists($cls)) {
try {
// Si lun de ces constructeurs a besoin de paramètres, adapte ici.
return new $cls();
} catch (Throwable $e) {
// essaie la suivante
}
}
}
return null;
}
/**
* Liste les sous-dossiers dun parent.
* Retour: array [['id'=>..., 'name'=>...], ...]
*/
public function list_children(string $drive_id, string $parent_id): array
{
// Méthodes possibles dans ta lib (on tente dans cet ordre)
$candidates = [
'listFolders', 'list_children', 'children', 'listFoldersInParent'
];
$results = null;
foreach ($candidates as $m) {
if (method_exists($this->gds, $m)) {
try {
$results = $this->gds->$m($drive_id, $parent_id);
} catch (ArgumentCountError $e) {
// ordre inversé éventuel
$results = $this->gds->$m($parent_id, $drive_id);
} catch (Throwable $t) {
$results = null;
}
if (is_array($results)) break;
}
}
if (!is_array($results)) {
throw new Exception('Aucune méthode compatible pour lister les sous-dossiers dans la librairie GoogleDrive*.');
}
$out = [];
foreach ($results as $f) {
$id = $f['id'] ?? ($f['fileId'] ?? ($f['gid'] ?? null));
$name = $f['name'] ?? ($f['title'] ?? ($f['nom'] ?? null));
if ($id && $name) {
$out[] = ['id'=>$id, 'name'=>$name];
}
}
usort($out, fn($a,$b)=>strcasecmp($a['name'],$b['name']));
return $out;
}
/**
* Construit la chaîne base->...->final (ou [] si final est hors du sous-arbre).
*/
public function build_chain_from_base(string $drive_id, string $base_id, string $final_id): array
{
if ($base_id === $final_id) {
$base = $this->get_file($drive_id, $base_id);
return [['id'=>$base_id, 'name'=>$base['name'] ?? '(racine)']];
}
$nodes = [];
$cur = $final_id;
$guard = 0;
while ($cur && $guard < 100) {
$file = $this->get_file($drive_id, $cur);
if (!$file) break;
$nodes[] = ['id'=>$cur, 'name'=>$file['name'] ?? $cur];
if ($cur === $base_id) break;
$parents = $this->get_parents($drive_id, $cur);
$cur = $parents[0] ?? null;
$guard++;
}
$foundBase = false;
foreach ($nodes as $n) { if ($n['id'] === $base_id) { $foundBase = true; break; } }
if (!$foundBase) return [];
return array_reverse($nodes);
}
private function get_file(string $drive_id, string $file_id): ?array
{
$candidates = ['getFile','get_file','fileGet','get'];
foreach ($candidates as $m) {
if (method_exists($this->gds, $m)) {
try {
$res = $this->gds->$m($drive_id, $file_id);
} catch (ArgumentCountError $e) {
$res = $this->gds->$m($file_id, $drive_id);
} catch (Throwable $t) {
$res = null;
}
if (is_array($res)) {
$name = $res['name'] ?? ($res['title'] ?? ($res['nom'] ?? null));
return ['id'=>$file_id, 'name'=>$name];
}
}
}
return null;
}
private function get_parents(string $drive_id, string $file_id): array
{
$candidates = ['getParents','parents','fileParents'];
foreach ($candidates as $m) {
if (method_exists($this->gds, $m)) {
try {
$res = $this->gds->$m($drive_id, $file_id);
} catch (ArgumentCountError $e) {
$res = $this->gds->$m($file_id, $drive_id);
} catch (Throwable $t) {
$res = null;
}
if (is_array($res)) {
$ids = [];
foreach ($res as $p) {
$ids[] = is_array($p) ? ($p['id'] ?? $p['fileId'] ?? $p['gid'] ?? null) : $p;
}
return array_values(array_filter($ids));
}
}
}
return [];
}
}