From 12c6cc5f9526c80dbe90724b0157c6a456d91955 Mon Sep 17 00:00:00 2001 From: Claudio Date: Tue, 7 Oct 2025 09:12:54 +0200 Subject: [PATCH] lazy load modal parts and matrici cron --- .../userarea/cronfiles/get_matrici_cron.php | 103 ++++++++++++++++++ public/userarea/import_edit2.php | 36 +++++- 2 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 public/userarea/cronfiles/get_matrici_cron.php diff --git a/public/userarea/cronfiles/get_matrici_cron.php b/public/userarea/cronfiles/get_matrici_cron.php new file mode 100644 index 0000000..b118694 --- /dev/null +++ b/public/userarea/cronfiles/get_matrici_cron.php @@ -0,0 +1,103 @@ +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 diff --git a/public/userarea/import_edit2.php b/public/userarea/import_edit2.php index 94f2340..e7044b5 100644 --- a/public/userarea/import_edit2.php +++ b/public/userarea/import_edit2.php @@ -712,7 +712,7 @@ foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) { - +
@@ -1181,6 +1181,40 @@ foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) { }); }); +