104 lines
4.3 KiB
PHP
104 lines
4.3 KiB
PHP
<?php
|
|
require_once dirname(__DIR__, 3) . '/vendor/autoload.php'; // Risale a root/vendor/
|
|
require_once dirname(__FILE__) . '/../class/VisualLimsApiClient.class.php'; // In root/public/userarea/class/
|
|
|
|
use Dotenv\Dotenv;
|
|
|
|
// Debug: Log the path where we expect the .env file
|
|
$envPath = dirname(__DIR__, 3);
|
|
file_put_contents(__DIR__ . '/debug_log.txt', date('Y-m-d H:i:s') . ' - Expected .env path: ' . $envPath . PHP_EOL, FILE_APPEND);
|
|
|
|
// Carica il file .env dalla root del progetto
|
|
try {
|
|
$dotenv = Dotenv::createImmutable($envPath);
|
|
$dotenv->load();
|
|
} catch (Exception $e) {
|
|
file_put_contents(__DIR__ . '/error_log.txt', date('Y-m-d H:i:s') . ' - Errore caricamento .env: ' . $e->getMessage() . PHP_EOL, FILE_APPEND);
|
|
exit(1);
|
|
}
|
|
|
|
// Recupera le variabili d'ambiente
|
|
$dbHost = $_ENV['DB_HOST'];
|
|
$dbName = $_ENV['DB_DATABASE'];
|
|
$dbUser = $_ENV['DB_USERNAME'];
|
|
$dbPass = $_ENV['DB_PASSWORD'];
|
|
$dbPrefix = $_ENV['DB_PREFIX'];
|
|
|
|
// Debug: Log database connection details (excluding password)
|
|
file_put_contents(__DIR__ . '/debug_log.txt', date('Y-m-d H:i:s') . " - DB Connection: host=$dbHost, dbname=$dbName, user=$dbUser, prefix=$dbPrefix" . PHP_EOL, FILE_APPEND);
|
|
|
|
// Connessione al database MySQL
|
|
try {
|
|
$pdo = new PDO("mysql:host=$dbHost;dbname=$dbName;charset=utf8mb4", $dbUser, $dbPass);
|
|
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
} catch (PDOException $e) {
|
|
file_put_contents(__DIR__ . '/error_log.txt', date('Y-m-d H:i:s') . ' - Errore connessione DB: ' . $e->getMessage() . PHP_EOL, FILE_APPEND);
|
|
exit(1);
|
|
}
|
|
|
|
try {
|
|
$api = VisualLimsApiClient::getInstance();
|
|
|
|
// Endpoint per recuperare le Matrici
|
|
$endpoint = 'Matrice';
|
|
|
|
// (Opzionale) aggiungi parametri
|
|
$options = []; // es. ['$top' => 100]
|
|
|
|
// Debug: salva URL usato
|
|
$base_url = 'https://93.43.5.102/limsapi/api/odata/';
|
|
$query = http_build_query($options);
|
|
$full_url = $base_url . $endpoint . ($query ? '?' . $query : '');
|
|
file_put_contents(__DIR__ . '/last_url.txt', $full_url . PHP_EOL, FILE_APPEND);
|
|
|
|
// Chiamata API
|
|
$data = $api->get($endpoint, $options);
|
|
|
|
// Debug: Log the API response size
|
|
file_put_contents(__DIR__ . '/debug_log.txt', date('Y-m-d H:i:s') . ' - API response received, value count: ' . (isset($data['value']) ? count($data['value']) : 0) . PHP_EOL, FILE_APPEND);
|
|
|
|
// Salva il JSON in locale (opzionale, per debug)
|
|
file_put_contents(__DIR__ . '/matrici_response.json', json_encode($data, JSON_PRETTY_PRINT));
|
|
|
|
// Svuota la tabella (dump rapido)
|
|
$pdo->exec("TRUNCATE TABLE {$dbPrefix}matrici");
|
|
|
|
// Debug: Log after truncate
|
|
file_put_contents(__DIR__ . '/debug_log.txt', date('Y-m-d H:i:s') . ' - Table truncated: ' . $dbPrefix . 'matrici' . PHP_EOL, FILE_APPEND);
|
|
|
|
// Prepara l'insert
|
|
$stmt = $pdo->prepare("
|
|
INSERT INTO {$dbPrefix}matrici (
|
|
IdMatrice, NomeMatriceTraduzione, DescrizioneTraduzione, MacroMatrice, NomeMatrice, Descrizione
|
|
) VALUES (
|
|
:IdMatrice, :NomeMatriceTraduzione, :DescrizioneTraduzione, :MacroMatrice, :NomeMatrice, :Descrizione
|
|
)
|
|
");
|
|
|
|
// Inserisci i dati
|
|
$insertedRows = 0;
|
|
if (isset($data['value']) && is_array($data['value'])) {
|
|
foreach ($data['value'] as $item) {
|
|
$stmt->execute([
|
|
':IdMatrice' => $item['IdMatrice'],
|
|
':NomeMatriceTraduzione' => $item['NomeMatriceTraduzione'],
|
|
':DescrizioneTraduzione' => $item['DescrizioneTraduzione'] ?? null,
|
|
':MacroMatrice' => $item['MacroMatrice'] ?? null,
|
|
':NomeMatrice' => $item['NomeMatrice'],
|
|
':Descrizione' => $item['Descrizione'] ?? null,
|
|
]);
|
|
$insertedRows++;
|
|
// Debug: Log each inserted row
|
|
file_put_contents(__DIR__ . '/debug_log.txt', date('Y-m-d H:i:s') . ' - Inserted row with IdMatrice: ' . $item['IdMatrice'] . PHP_EOL, FILE_APPEND);
|
|
}
|
|
}
|
|
|
|
// Log successo
|
|
file_put_contents(__DIR__ . '/success_log.txt', date('Y-m-d H:i:s') . ' - Aggiornamento completato: ' . $insertedRows . ' record inseriti.' . PHP_EOL, FILE_APPEND);
|
|
} catch (Exception $e) {
|
|
file_put_contents(__DIR__ . '/error_log.txt', date('Y-m-d H:i:s') . ' - ' . $e->getMessage() . PHP_EOL, FILE_APPEND);
|
|
exit(1);
|
|
}
|
|
|
|
exit(0); // Esci con successo per cron
|