103 lines
4.1 KiB
PHP
103 lines
4.1 KiB
PHP
<?php
|
|
require_once dirname(__DIR__, 2) . '/vendor/autoload.php';
|
|
require_once __DIR__ . '/class/VisualLimsApiClient.class.php';
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
// Disable PHP error display
|
|
ini_set('display_errors', '0');
|
|
error_reporting(E_ALL);
|
|
|
|
try {
|
|
$api = VisualLimsApiClient::getInstance(); // also loads dotenv
|
|
|
|
// In simulate mode: return fake clients built from idclient values already in datadb.
|
|
// This ensures client dropdowns auto-select the correct row idclient, which in turn
|
|
// triggers the ClienteResponsabile select to populate via the mock.
|
|
if (($_ENV['SIMULATE_EXPORT_LIMS'] ?? '') === 'true') {
|
|
require_once __DIR__ . '/class/db-functions.php';
|
|
$pdo = DBHandlerSelect::getInstance()->getConnection();
|
|
$stmt = $pdo->query("SELECT DISTINCT idclient FROM datadb WHERE idclient IS NOT NULL AND idclient > 0 ORDER BY idclient ASC");
|
|
$ids = $stmt->fetchAll(PDO::FETCH_COLUMN);
|
|
$fakeClients = array_map(fn($id) => [
|
|
'IdCliente' => (int) $id,
|
|
'Nominativo' => "Cliente Simulato {$id}",
|
|
'CodiceCliente' => "SIM_{$id}",
|
|
], $ids);
|
|
echo json_encode(['value' => $fakeClients]);
|
|
exit;
|
|
}
|
|
|
|
// Parametri OData
|
|
$params = [
|
|
'$select' => 'IdCliente,Nominativo,CodiceCliente',
|
|
'$orderby' => 'Nominativo asc'
|
|
];
|
|
|
|
// Costruisce query string con encoding corretto
|
|
$queryString = http_build_query($params);
|
|
|
|
// Componi endpoint finale
|
|
$endpoint = "Cliente?$queryString";
|
|
|
|
// Funzione per eseguire la chiamata con retry
|
|
function makeApiRequest($api, $endpoint, $maxRetries = 3)
|
|
{
|
|
for ($retry = 0; $retry < $maxRetries; $retry++) {
|
|
try {
|
|
// Tenta la chiamata API
|
|
$data = $api->get($endpoint);
|
|
// Salva risposta per debug
|
|
file_put_contents(__DIR__ . '/clienti_response.json', json_encode($data, JSON_PRETTY_PRINT));
|
|
return $data;
|
|
} catch (Exception $e) {
|
|
$errorMessage = $e->getMessage();
|
|
// Controlla se l'errore è legato all'autenticazione (HTTP 400 con messaggio specifico)
|
|
if (strpos($errorMessage, 'HTTP 400') !== false && strpos($errorMessage, 'Cannot persist the object') !== false) {
|
|
// Forza il refresh del token
|
|
try {
|
|
// Assumi che VisualLimsApiClient abbia un metodo per il refresh del token
|
|
$api->refreshToken(); // Da implementare in VisualLimsApiClient se non esiste
|
|
error_log("Tentativo $retry: Refresh token eseguito per endpoint $endpoint");
|
|
} catch (Exception $refreshEx) {
|
|
error_log("Errore durante il refresh del token: " . $refreshEx->getMessage());
|
|
throw new Exception("Impossibile eseguire il refresh del token: " . $refreshEx->getMessage());
|
|
}
|
|
// Ritarda leggermente prima del retry
|
|
usleep(500000); // 500ms
|
|
continue;
|
|
}
|
|
// Altri errori non gestiti dal retry
|
|
throw $e;
|
|
}
|
|
}
|
|
throw new Exception("Massimo numero di tentativi raggiunto per $endpoint");
|
|
}
|
|
|
|
// Cache file (1 hour TTL)
|
|
$cacheFile = __DIR__ . '/cache/clienti.json';
|
|
if (file_exists($cacheFile) && (time() - filemtime($cacheFile) < 3600)) {
|
|
readfile($cacheFile);
|
|
exit;
|
|
}
|
|
|
|
// Esegui la chiamata con retry
|
|
$data = makeApiRequest($api, $endpoint);
|
|
|
|
$json = json_encode($data);
|
|
if (!is_dir(__DIR__ . '/cache')) mkdir(__DIR__ . '/cache', 0777, true);
|
|
file_put_contents($cacheFile, $json);
|
|
|
|
echo $json;
|
|
} catch (Exception $e) {
|
|
http_response_code(500);
|
|
$errorResponse = [
|
|
'error' => $e->getMessage(),
|
|
'file' => $e->getFile(),
|
|
'line' => $e->getLine(),
|
|
'trace' => $e->getTraceAsString()
|
|
];
|
|
error_log("Errore in get_clienti.php: " . json_encode($errorResponse));
|
|
echo json_encode($errorResponse);
|
|
}
|