From 24cda34681995941aaa7455b3a29cb05747b4023 Mon Sep 17 00:00:00 2001 From: kapsona777 Date: Thu, 21 Aug 2025 20:39:31 +0400 Subject: [PATCH] added feature to update inserted information, fixed bug that was inserting new rows after each refresh of the page and optimized get_customfield_values , it was sending many requests and optimized to 1. --- public/userarea/get_customfield_values.php | 32 ++-- public/userarea/import_edit2.php | 212 +++++++++++++-------- public/userarea/import_insert.php | 157 +++++++++++++++ public/userarea/import_xls2.php | 8 +- public/userarea/save_edited_row.php | 66 +++++-- 5 files changed, 363 insertions(+), 112 deletions(-) create mode 100644 public/userarea/import_insert.php diff --git a/public/userarea/get_customfield_values.php b/public/userarea/get_customfield_values.php index 6001e74..5770073 100644 --- a/public/userarea/get_customfield_values.php +++ b/public/userarea/get_customfield_values.php @@ -1,5 +1,5 @@ ერთი default + if (empty($fieldIds)) { + $fieldIds = [156]; + } - // Recupera i dati dal server - $data = $api->get($endpoint); + $results = []; - // Salva la risposta in un file per debug - file_put_contents(__DIR__ . '/customfield_values_response.json', json_encode($data)); + foreach ($fieldIds as $customFieldId) { + $endpoint = "CustomField($customFieldId)?\$expand=CustomFieldsValues"; + $data = $api->get($endpoint); - // Output JSON al client - echo json_encode($data); + $results[$customFieldId] = $data['CustomFieldsValues'] ?? []; + } + + // Debug ფაილი + file_put_contents(__DIR__ . '/customfield_values_response.json', json_encode($results)); + + echo json_encode($results); } catch (Exception $e) { http_response_code(500); echo json_encode([ diff --git a/public/userarea/import_edit2.php b/public/userarea/import_edit2.php index d5d7336..e0bdfcc 100644 --- a/public/userarea/import_edit2.php +++ b/public/userarea/import_edit2.php @@ -19,11 +19,11 @@ if ($_SERVER['REQUEST_METHOD'] !== 'POST' || !isset($_POST['template_id']) || !i exit; } -$template_id = intval($_POST['template_id']); -$selected_rows = $_POST['selected_rows']; -$columns = json_decode($_POST['columns'], true); // Header dell'XLS -$rows = json_decode($_POST['rows'], true); // Dati dell'XLS -$newFilename = htmlspecialchars($_POST['filename']); +$template_id = intval($_POST['template_id']) ?? $_SESSION['template_id']; +$selected_rows = $_POST['selected_rows'] ?? $_SESSION['selected_rows']; +$columns = json_decode($_POST['columns'], true) ?? $_SESSION['columns']; // Header dell'XLS +$rows = json_decode($_POST['rows'], true) ?? $_SESSION['rows']; // Dati dell'XLS +$newFilename = htmlspecialchars($_POST['filename']) ?? $_SESSION['filename']; // Log dei dati ricevuti error_log("Received Data - Template ID: $template_id, Selected Rows: " . json_encode($selected_rows)); @@ -58,70 +58,72 @@ foreach ($allMappings as $mapping) { } } -// Inserisci le righe selezionate in datadb (solo campi generici con templateid) -$insertedIds = []; -foreach ($selected_rows as $rowIndex) { - $row = $rows[$rowIndex]; - $values = [ - $template_id, // templateid - $importReferenceCode, // importreferencecode - $newFilename, // filename_import - 'i', // status - $user_id, // user_id - null, // limscode - date('Y-m-d') // importdate - ]; - $sql = "INSERT INTO datadb (templateid, importreferencecode, filename_import, status, user_id, limscode, importdate) VALUES (?, ?, ?, ?, ?, ?, ?)"; - $stmt = $pdo->prepare($sql); - $stmt->execute($values); +//// Inserisci le righe selezionate in datadb (solo campi generici con templateid) +//$insertedIds = []; +//foreach ($selected_rows as $rowIndex) { +// $row = $rows[$rowIndex]; +// $values = [ +// $template_id, // templateid +// $importReferenceCode, // importreferencecode +// $newFilename, // filename_import +// 'i', // status +// $user_id, // user_id +// null, // limscode +// date('Y-m-d') // importdate +// ]; +// $sql = "INSERT INTO datadb (templateid, importreferencecode, filename_import, status, user_id, limscode, importdate) VALUES (?, ?, ?, ?, ?, ?, ?)"; +// $stmt = $pdo->prepare($sql); +// $stmt->execute($values); +// +// $iddatadb = $pdo->lastInsertId(); +// $insertedIds[] = $iddatadb; +// +// // Inserisci tutti i campi (automatici e manuali) in import_data_details +// foreach ($allMappings as $mapping) { +// $fieldValue = null; +// if (!$mapping['is_manual']) { // Campi automatici dall'XLS +// $excelColumn = trim($mapping['excel_column']); +// $excelColumnIndex = array_search($excelColumn, array_map('trim', $columns)); +// if ($excelColumnIndex !== false && isset($row[$excelColumnIndex]) && $row[$excelColumnIndex] !== '') { +// $fieldValue = $row[$excelColumnIndex]; +// error_log("Found Excel column '$excelColumn' at index $excelColumnIndex, value: " . var_export($fieldValue, true)); +// } else { +// $fieldValue = $mapping['manual_default'] ?? ''; +// error_log("Excel column '$excelColumn' not found or empty, using default: " . var_export($fieldValue, true)); +// } +// switch ($mapping['data_type']) { +// case 'INT': +// $fieldValue = is_numeric($fieldValue) ? (int)$fieldValue : ($mapping['manual_default'] ?? 0); +// break; +// case 'DATE': +// $fieldValue = !empty($fieldValue) ? date('Y-m-d', strtotime($fieldValue)) : ($mapping['manual_default'] === 'today' ? date('Y-m-d') : ($mapping['manual_default'] ?? '')); +// break; +// case 'CHAR': +// $fieldValue = !empty($fieldValue) ? substr((string)$fieldValue, 0, 1) : ($mapping['manual_default'] ?? ''); +// break; +// case 'Testo': +// case 'VARCHAR': +// default: +// $fieldValue = !empty($fieldValue) ? htmlspecialchars((string)$fieldValue) : ($mapping['manual_default'] ?? ''); +// break; +// } +// } else { // Campi manuali +// $fieldValue = $mapping['manual_default'] ?? ''; +// if ($mapping['data_type'] === 'DATE' && $mapping['manual_default'] === 'today') { +// $fieldValue = date('Y-m-d'); +// } +// } +// if ($mapping['is_required'] && (is_null($fieldValue) || $fieldValue === '')) { +// error_log("Required field missing for mapping ID: " . $mapping['id'] . ", field: " . $mapping['field_label']); +// } +// error_log("Inserting into import_data_details - Mapping ID: " . $mapping['id'] . ", Field Value: " . var_export($fieldValue, true) . ", Is Manual: " . $mapping['is_manual'] . ", Excel Column: " . ($mapping['excel_column'] ?? 'N/A') . ", Manual Default: " . ($mapping['manual_default'] ?? 'N/A')); +// $stmt = $pdo->prepare("INSERT INTO import_data_details (id, mapping_id, field_value) VALUES (?, ?, ?)"); +// $stmt->execute([$iddatadb, $mapping['id'], $fieldValue]); +// error_log("Inserted into import_data_details for ID $iddatadb, Mapping ID: " . $mapping['id'] . ", Field Value: " . var_export($fieldValue, true)); +// } +//} - $iddatadb = $pdo->lastInsertId(); - $insertedIds[] = $iddatadb; - - // Inserisci tutti i campi (automatici e manuali) in import_data_details - foreach ($allMappings as $mapping) { - $fieldValue = null; - if (!$mapping['is_manual']) { // Campi automatici dall'XLS - $excelColumn = trim($mapping['excel_column']); - $excelColumnIndex = array_search($excelColumn, array_map('trim', $columns)); - if ($excelColumnIndex !== false && isset($row[$excelColumnIndex]) && $row[$excelColumnIndex] !== '') { - $fieldValue = $row[$excelColumnIndex]; - error_log("Found Excel column '$excelColumn' at index $excelColumnIndex, value: " . var_export($fieldValue, true)); - } else { - $fieldValue = $mapping['manual_default'] ?? ''; - error_log("Excel column '$excelColumn' not found or empty, using default: " . var_export($fieldValue, true)); - } - switch ($mapping['data_type']) { - case 'INT': - $fieldValue = is_numeric($fieldValue) ? (int)$fieldValue : ($mapping['manual_default'] ?? 0); - break; - case 'DATE': - $fieldValue = !empty($fieldValue) ? date('Y-m-d', strtotime($fieldValue)) : ($mapping['manual_default'] === 'today' ? date('Y-m-d') : ($mapping['manual_default'] ?? '')); - break; - case 'CHAR': - $fieldValue = !empty($fieldValue) ? substr((string)$fieldValue, 0, 1) : ($mapping['manual_default'] ?? ''); - break; - case 'Testo': - case 'VARCHAR': - default: - $fieldValue = !empty($fieldValue) ? htmlspecialchars((string)$fieldValue) : ($mapping['manual_default'] ?? ''); - break; - } - } else { // Campi manuali - $fieldValue = $mapping['manual_default'] ?? ''; - if ($mapping['data_type'] === 'DATE' && $mapping['manual_default'] === 'today') { - $fieldValue = date('Y-m-d'); - } - } - if ($mapping['is_required'] && (is_null($fieldValue) || $fieldValue === '')) { - error_log("Required field missing for mapping ID: " . $mapping['id'] . ", field: " . $mapping['field_label']); - } - error_log("Inserting into import_data_details - Mapping ID: " . $mapping['id'] . ", Field Value: " . var_export($fieldValue, true) . ", Is Manual: " . $mapping['is_manual'] . ", Excel Column: " . ($mapping['excel_column'] ?? 'N/A') . ", Manual Default: " . ($mapping['manual_default'] ?? 'N/A')); - $stmt = $pdo->prepare("INSERT INTO import_data_details (id, mapping_id, field_value) VALUES (?, ?, ?)"); - $stmt->execute([$iddatadb, $mapping['id'], $fieldValue]); - error_log("Inserted into import_data_details for ID $iddatadb, Mapping ID: " . $mapping['id'] . ", Field Value: " . var_export($fieldValue, true)); - } -} +$insertedIds = $_POST['inserted_ids'] ?? $_SESSION['inserted_ids']; // Recupera i dati appena inseriti con i nomi degli utenti $stmt = $pdo->prepare(" @@ -436,7 +438,7 @@ foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
-
Imported (i) @@ -448,6 +450,9 @@ foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
Modifica Dati Importati
+
@@ -731,6 +736,38 @@ foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) { + + --> - \ No newline at end of file + diff --git a/public/userarea/import_insert.php b/public/userarea/import_insert.php new file mode 100644 index 0000000..7a8d012 --- /dev/null +++ b/public/userarea/import_insert.php @@ -0,0 +1,157 @@ +getConnection(); + +// Genera un UUID univoco per importreferencecode +$importReferenceCode = date('YmdHis') . '-' . uniqid(); + +// Recupera tutti i mapping dal template +$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); + +if (empty($allMappings)) { + header("Location: import_xls.php?id=$template_id&status=error&message=" . urlencode("Nessun mapping trovato per il template")); + exit; +} + +// Trova il campo main_field +$mainFieldMapping = null; +foreach ($allMappings as $mapping) { + if ($mapping['main_field'] == 1) { + $mainFieldMapping = $mapping; + break; + } +} + +// Inserisci le righe selezionate in datadb (solo campi generici con templateid) +$insertedIds = []; +foreach ($selected_rows as $rowIndex) { + $row = $rows[$rowIndex]; + $values = [ + $template_id, // templateid + $importReferenceCode, // importreferencecode + $newFilename, // filename_import + 'i', // status + $user_id, // user_id + null, // limscode + date('Y-m-d') // importdate + ]; + $sql = "INSERT INTO datadb (templateid, importreferencecode, filename_import, status, user_id, limscode, importdate) VALUES (?, ?, ?, ?, ?, ?, ?)"; + $stmt = $pdo->prepare($sql); + $stmt->execute($values); + + $iddatadb = $pdo->lastInsertId(); + $insertedIds[] = $iddatadb; + + // Inserisci tutti i campi (automatici e manuali) in import_data_details + foreach ($allMappings as $mapping) { + $fieldValue = null; + if (!$mapping['is_manual']) { // Campi automatici dall'XLS + $excelColumn = trim($mapping['excel_column']); + $excelColumnIndex = array_search($excelColumn, array_map('trim', $columns)); + if ($excelColumnIndex !== false && isset($row[$excelColumnIndex]) && $row[$excelColumnIndex] !== '') { + $fieldValue = $row[$excelColumnIndex]; + error_log("Found Excel column '$excelColumn' at index $excelColumnIndex, value: " . var_export($fieldValue, true)); + } else { + $fieldValue = $mapping['manual_default'] ?? ''; + error_log("Excel column '$excelColumn' not found or empty, using default: " . var_export($fieldValue, true)); + } + switch ($mapping['data_type']) { + case 'INT': + $fieldValue = is_numeric($fieldValue) ? (int)$fieldValue : ($mapping['manual_default'] ?? 0); + break; + case 'DATE': + $fieldValue = !empty($fieldValue) ? date('Y-m-d', strtotime($fieldValue)) : ($mapping['manual_default'] === 'today' ? date('Y-m-d') : ($mapping['manual_default'] ?? '')); + break; + case 'CHAR': + $fieldValue = !empty($fieldValue) ? substr((string)$fieldValue, 0, 1) : ($mapping['manual_default'] ?? ''); + break; + case 'Testo': + case 'VARCHAR': + default: + $fieldValue = !empty($fieldValue) ? htmlspecialchars((string)$fieldValue) : ($mapping['manual_default'] ?? ''); + break; + } + } else { // Campi manuali + $fieldValue = $mapping['manual_default'] ?? ''; + if ($mapping['data_type'] === 'DATE' && $mapping['manual_default'] === 'today') { + $fieldValue = date('Y-m-d'); + } + } + if ($mapping['is_required'] && (is_null($fieldValue) || $fieldValue === '')) { + error_log("Required field missing for mapping ID: " . $mapping['id'] . ", field: " . $mapping['field_label']); + } + error_log("Inserting into import_data_details - Mapping ID: " . $mapping['id'] . ", Field Value: " . var_export($fieldValue, true) . ", Is Manual: " . $mapping['is_manual'] . ", Excel Column: " . ($mapping['excel_column'] ?? 'N/A') . ", Manual Default: " . ($mapping['manual_default'] ?? 'N/A')); + $stmt = $pdo->prepare("INSERT INTO import_data_details (id, mapping_id, field_value) VALUES (?, ?, ?)"); + $stmt->execute([$iddatadb, $mapping['id'], $fieldValue]); + error_log("Inserted into import_data_details for ID $iddatadb, Mapping ID: " . $mapping['id'] . ", Field Value: " . var_export($fieldValue, true)); + } +} + +$_SESSION['inserted_ids'] = $insertedIds; + +$params = [ + 'template_id' => $template_id, + 'filename' => $newFilename, +]; + +?> +
+ + + + + + + + + + + + + +
+ + - - @@ -205,7 +205,7 @@ error_log("Loaded template: " . print_r($template, true)); errorContainer.style.display = 'block'; } else { let html = ` -
+ @@ -326,4 +326,4 @@ error_log("Loaded template: " . print_r($template, true)); - \ No newline at end of file + diff --git a/public/userarea/save_edited_row.php b/public/userarea/save_edited_row.php index e395bed..fa8df22 100644 --- a/public/userarea/save_edited_row.php +++ b/public/userarea/save_edited_row.php @@ -14,28 +14,64 @@ try { $db = DBHandlerSelect::getInstance(); $pdo = $db->getConnection(); - // Prepara i dati da aggiornare - $updates = []; - $values = []; - foreach ($_POST as $key => $value) { - if ($key !== 'iddatadb') { - $updates[] = "$key = ?"; - $values[] = htmlspecialchars($value); + $data = $_POST; + $details = []; + + // 1. POST-დან ამოვიღოთ მხოლოდ details + foreach ($data as $key => $value) { + if (preg_match('/^details(\d+)field_value$/', $key, $matches)) { + $id = $matches[1]; + $details[$id] = $value; } } - $values[] = $iddatadb; - if (empty($updates)) { - throw new Exception('Nessun dato da aggiornare'); + // 2. DB-დან წამოვიღოთ არსებული მნიშვნელობები + $stmt = $pdo->prepare("SELECT mapping_id, field_value FROM import_data_details WHERE id = ?"); + $stmt->execute([$iddatadb]); + + $currentValues = []; + while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { + $currentValues[$row['mapping_id']] = $row['field_value']; } - $sql = "UPDATE datadb SET " . implode(', ', $updates) . " WHERE iddatadb = ?"; - $stmt = $pdo->prepare($sql); - $stmt->execute($values); + // 3. შევადაროთ POST-ს და DB-ს + $changed = []; + foreach ($details as $id => $newValue) { + $oldValue = $currentValues[$id] ?? null; + if ($oldValue !== $newValue) { + $changed[$id] = [ + 'old' => $oldValue, + 'new' => $newValue + ]; + } + } + + // 4. თუ არის ცვლილებები → UPDATE + if (!empty($changed)) { + $updateStmt = $pdo->prepare(" + UPDATE import_data_details + SET field_value = :newValue + WHERE id = :iddatadb AND mapping_id = :mappingId + "); + + foreach ($changed as $mappingId => $values) { + $updateStmt->execute([ + ':newValue' => $values['new'], + ':iddatadb' => $iddatadb, + ':mappingId' => $mappingId + ]); + } + + $response['success'] = true; + $response['message'] = "Updated successfully"; + $response['changed'] = $changed; // Debug / optional + } else { + $response['success'] = true; + $response['message'] = "No changes found"; + } - $response['success'] = true; - $response['message'] = 'Riga aggiornata con successo'; } catch (Exception $e) { + $response['success'] = false; $response['message'] = $e->getMessage(); error_log("Errore in save_edited_row.php: " . $e->getMessage()); }