diff --git a/public/userarea/delete_record.php b/public/userarea/delete_record.php new file mode 100644 index 0000000..87f0b9c --- /dev/null +++ b/public/userarea/delete_record.php @@ -0,0 +1,64 @@ + false, 'message' => 'ID non valido']); + exit; +} + +// Connessione al database +try { + $db = DBHandlerSelect::getInstance(); + $pdo = $db->getConnection(); +} catch (Exception $e) { + http_response_code(500); + echo json_encode(['success' => false, 'message' => 'Errore di connessione al database: ' . $e->getMessage()]); + error_log("Errore di connessione al database: " . $e->getMessage()); + exit; +} + +// Inizia una transazione +$pdo->beginTransaction(); + +try { + // Elimina i dettagli associati dal tavolo import_data_details + $stmt = $pdo->prepare("DELETE FROM import_data_details WHERE id = ?"); + $stmt->execute([$iddatadb]); + + // Elimina il record principale dal tavolo datadb + $stmt = $pdo->prepare("DELETE FROM datadb WHERE iddatadb = ?"); + $stmt->execute([$iddatadb]); + + // Verifica se il record è stato eliminato + if ($stmt->rowCount() > 0) { + $pdo->commit(); + echo json_encode(['success' => true, 'message' => 'Record eliminato con successo']); + error_log("Record con iddatadb=$iddatadb eliminato con successo"); + } else { + $pdo->rollBack(); + http_response_code(404); + echo json_encode(['success' => false, 'message' => 'Record non trovato']); + error_log("Record con iddatadb=$iddatadb non trovato"); + } +} catch (Exception $e) { + $pdo->rollBack(); + http_response_code(500); + echo json_encode(['success' => false, 'message' => 'Errore durante la cancellazione: ' . $e->getMessage()]); + error_log("Errore durante la cancellazione del record con iddatadb=$iddatadb: " . $e->getMessage()); +} diff --git a/public/userarea/historical_trf.php b/public/userarea/historical_trf.php index 968012c..4dd1954 100644 --- a/public/userarea/historical_trf.php +++ b/public/userarea/historical_trf.php @@ -27,7 +27,11 @@ if (!in_array($status, ['i', 'P', 'l'])) { $is_readonly = in_array($status, ['P', 'l']); -// Paginazione +// Gestione modalità: lista (default per status 'i') o edit +$mode = $_GET['mode'] ?? ($status === 'i' ? 'list' : 'edit'); +$selected_ids = $_POST['selected_ids'] ?? []; + +// Paginazione (solo per mode 'list') $page = max(1, intval($_GET['page'] ?? 1)); $limit = 20; $offset = ($page - 1) * $limit; @@ -36,14 +40,8 @@ $offset = ($page - 1) * $limit; $db = DBHandlerSelect::getInstance(); $pdo = $db->getConnection(); -// Conta il numero totale di record -$stmt = $pdo->prepare("SELECT COUNT(*) FROM datadb WHERE templateid = ? AND status = ?"); -$stmt->execute([$template_id, $status]); -$total_records = $stmt->fetchColumn(); -$total_pages = ceil($total_records / $limit); - // Retrieve all mappings -$stmt = $pdo->prepare("SELECT id, excel_column, data_type, is_required, manual_default, is_manual, field_label, field_id FROM template_mapping WHERE template_id = ?"); +$stmt = $pdo->prepare("SELECT id, excel_column, data_type, is_required, manual_default, is_manual, field_label, field_id, main_field FROM template_mapping WHERE template_id = ?"); $stmt->execute([$template_id]); $allMappings = $stmt->fetchAll(PDO::FETCH_ASSOC); @@ -52,16 +50,48 @@ if (empty($allMappings)) { exit; } +// Trova il main_field +$mainFieldMapping = null; +foreach ($allMappings as $mapping) { + if ($mapping['main_field'] == 1) { + $mainFieldMapping = $mapping; + break; + } +} +if (!$mainFieldMapping) { + $mainFieldMapping = reset(array_filter($allMappings, fn($m) => !$m['is_manual'])); +} + // Retrieve data from datadb -$stmt = $pdo->prepare(" - SELECT d.*, CONCAT(u.first_name, ' ', u.last_name) AS user_name - FROM datadb d - LEFT JOIN auth_users u ON d.user_id = u.id - WHERE d.templateid = ? AND d.status = ? - LIMIT ? OFFSET ? -"); -$stmt->execute([$template_id, $status, $limit, $offset]); -$importedData = $stmt->fetchAll(PDO::FETCH_ASSOC); +if ($mode === 'edit' && !empty($selected_ids)) { + $placeholders = implode(',', array_fill(0, count($selected_ids), '?')); + $stmt = $pdo->prepare(" + SELECT d.*, CONCAT(u.first_name, ' ', u.last_name) AS user_name + FROM datadb d + LEFT JOIN auth_users u ON d.user_id = u.id + WHERE d.templateid = ? AND d.status = ? AND d.iddatadb IN ($placeholders) + "); + $params = array_merge([$template_id, $status], $selected_ids); + $stmt->execute($params); + $importedData = $stmt->fetchAll(PDO::FETCH_ASSOC); + $total_records = count($importedData); + $total_pages = 1; +} else { + $stmt = $pdo->prepare("SELECT COUNT(*) FROM datadb WHERE templateid = ? AND status = ?"); + $stmt->execute([$template_id, $status]); + $total_records = $stmt->fetchColumn(); + $total_pages = ceil($total_records / $limit); + + $stmt = $pdo->prepare(" + SELECT d.*, CONCAT(u.first_name, ' ', u.last_name) AS user_name + FROM datadb d + LEFT JOIN auth_users u ON d.user_id = u.id + WHERE d.templateid = ? AND d.status = ? + LIMIT ? OFFSET ? + "); + $stmt->execute([$template_id, $status, $limit, $offset]); + $importedData = $stmt->fetchAll(PDO::FETCH_ASSOC); +} error_log("Record caricati: " . count($importedData)); @@ -436,6 +466,39 @@ foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) { background-color: #d4edda; transition: background-color 0.5s ease; } + + .list-table { + width: 100%; + border-collapse: collapse; + } + + .list-table th, + .list-table td { + padding: 10px; + border: 1px solid #dee2e6; + text-align: left; + } + + .list-table th { + background-color: #e9ecef; + } + + .delete-btn { + background: #dc3545; + color: white; + border: none; + padding: 5px 10px; + border-radius: 4px; + cursor: pointer; + } + + .delete-btn:hover { + background: #c82333; + } + + .proceed-btn { + margin-top: 20px; + }