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.
This commit is contained in:
parent
2c514a8ab6
commit
24cda34681
@ -1,5 +1,5 @@
|
||||
<?php
|
||||
require_once dirname(__DIR__, 2) . '/vendor/autoload.php'; // Torna al livello di public
|
||||
require_once dirname(__DIR__, 2) . '/vendor/autoload.php';
|
||||
require_once dirname(__FILE__) . '/class/VisualLimsApiClient.class.php';
|
||||
|
||||
header('Content-Type: application/json');
|
||||
@ -9,20 +9,30 @@ error_reporting(E_ALL);
|
||||
try {
|
||||
$api = VisualLimsApiClient::getInstance();
|
||||
|
||||
// ID del campo custom passato da GET oppure default
|
||||
$customFieldId = isset($_GET['field_id']) && is_numeric($_GET['field_id']) ? intval($_GET['field_id']) : 156;
|
||||
// მივიღოთ მრავლობითი field_ids
|
||||
$fieldIds = [];
|
||||
if (isset($_GET['field_ids'])) {
|
||||
$fieldIds = array_filter(array_map('intval', explode(',', $_GET['field_ids'])));
|
||||
}
|
||||
|
||||
// Endpoint con $expand per ottenere i valori
|
||||
$endpoint = "CustomField($customFieldId)?\$expand=CustomFieldsValues";
|
||||
// თუ არ გადმოგვცეს -> ერთი 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([
|
||||
|
||||
@ -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) {
|
||||
<?php include('include/topbar.php'); ?>
|
||||
<div class="page-wrapper">
|
||||
<div class="page-content">
|
||||
<?php //include('top_stat_widget.php');
|
||||
<?php //include('top_stat_widget.php');
|
||||
?>
|
||||
<div class="mb-3 text">
|
||||
<a href="historical_trf.php?id=<?= $template_id ?>&status=i" class="btn btn-warning me-2">Imported (i)</a>
|
||||
@ -448,6 +450,9 @@ foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
|
||||
<div class="d-flex align-items-center">
|
||||
<div>
|
||||
<h6 class="mb-0">Modifica Dati Importati</h6>
|
||||
<div id="unsavedChanges" style="display:none; color: red; font-weight: bold; margin:10px 0;">
|
||||
⚠️ Unsaved changes detected! Please save before leaving this page.
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@ -731,6 +736,38 @@ foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
|
||||
<script src="photos.js"></script>
|
||||
<script src="parts.js"></script>
|
||||
<script src="tracking.js"></script>
|
||||
<script>
|
||||
document.addEventListener("DOMContentLoaded", function() {
|
||||
const inputs = document.querySelectorAll(".cell-input, .dropdown-select, .carrier-select, .awb-input");
|
||||
const unsavedDiv = document.getElementById("unsavedChanges");
|
||||
let hasChanges = false;
|
||||
|
||||
// როცა მნიშვნელობა შეიცვლება
|
||||
inputs.forEach(el => {
|
||||
el.addEventListener("change", () => {
|
||||
hasChanges = true;
|
||||
unsavedDiv.style.display = "block";
|
||||
});
|
||||
});
|
||||
|
||||
// როცა save ღილაკს დააჭერს
|
||||
document.querySelectorAll(".save-btn").forEach(btn => {
|
||||
btn.addEventListener("click", () => {
|
||||
hasChanges = false;
|
||||
unsavedDiv.style.display = "none";
|
||||
});
|
||||
});
|
||||
|
||||
// სურვილისამებრ: გაფრთხილება გვერდიდან გასვლისას
|
||||
window.addEventListener("beforeunload", function (e) {
|
||||
if (hasChanges) {
|
||||
e.preventDefault();
|
||||
e.returnValue = "";
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
||||
<script>
|
||||
document.addEventListener("DOMContentLoaded", function() {
|
||||
const inputs = document.querySelectorAll('.cell-input');
|
||||
@ -757,6 +794,7 @@ foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
|
||||
formData.append(name, input.value);
|
||||
});
|
||||
formData.append('iddatadb', iddatadb);
|
||||
formData.append('mapping', JSON.stringify(<?= json_encode($allMappings) ?>));
|
||||
|
||||
fetch('save_edited_row.php', {
|
||||
method: 'POST',
|
||||
@ -772,6 +810,7 @@ foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
|
||||
cell.classList.add('flash-success');
|
||||
});
|
||||
setTimeout(() => cells.forEach(cell => cell.classList.remove('flash-success')), 500);
|
||||
alert('Salvataggio avvenuto con successo!');
|
||||
} else {
|
||||
alert('Errore durante il salvataggio: ' + data.message);
|
||||
}
|
||||
@ -858,23 +897,32 @@ foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
|
||||
if (dropdowns.length === 0) return;
|
||||
|
||||
// Recupera i dati solo per i field_id univoci
|
||||
const uniqueFieldIds = [...new Set(Array.from(dropdowns).map(d => d.getAttribute('data-field-id')))].filter(fieldId => fieldId);
|
||||
for (const fieldId of uniqueFieldIds) {
|
||||
if (!dropdownData[fieldId]) {
|
||||
try {
|
||||
const response = await fetch(`get_customfield_values.php?field_id=${fieldId}`);
|
||||
const data = await response.json();
|
||||
if (data.error) {
|
||||
console.error('Errore per field_id', fieldId, ':', data.error);
|
||||
continue;
|
||||
const uniqueFieldIds = [
|
||||
...new Set(Array.from(dropdowns).map(d => d.getAttribute('data-field-id')))
|
||||
].filter(fieldId => fieldId);
|
||||
|
||||
const missingFieldIds = uniqueFieldIds.filter(fieldId => !dropdownData[fieldId]);
|
||||
|
||||
if (missingFieldIds.length > 0) {
|
||||
try {
|
||||
const response = await fetch(
|
||||
`get_customfield_values.php?field_ids=${missingFieldIds.join(",")}`
|
||||
);
|
||||
const data = await response.json();
|
||||
|
||||
if (data.error) {
|
||||
console.error("Errore fetch multiplo:", data.error);
|
||||
} else {
|
||||
for (const fieldId of Object.keys(data)) {
|
||||
dropdownData[fieldId] = data[fieldId] || [];
|
||||
}
|
||||
dropdownData[fieldId] = data.CustomFieldsValues || [];
|
||||
} catch (error) {
|
||||
console.error('Errore nel fetch per field_id', fieldId, ':', error);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Errore generale nel fetch multiplo:", error);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Popola tutti i dropdown con i dati recuperati
|
||||
dropdowns.forEach(dropdown => {
|
||||
const fieldId = dropdown.getAttribute('data-field-id');
|
||||
@ -1066,4 +1114,4 @@ foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
|
||||
</script> -->
|
||||
</body>
|
||||
|
||||
</html>
|
||||
</html>
|
||||
|
||||
157
public/userarea/import_insert.php
Normal file
157
public/userarea/import_insert.php
Normal file
@ -0,0 +1,157 @@
|
||||
<?php
|
||||
ini_set('display_errors', 1);
|
||||
ini_set('display_startup_errors', 1);
|
||||
error_reporting(E_ALL);
|
||||
ini_set('log_errors', 1);
|
||||
ini_set('error_log', __DIR__ . '/import_debug.log');
|
||||
if (!file_exists(__DIR__ . '/import_debug.log')) {
|
||||
file_put_contents(__DIR__ . '/import_debug.log', "Inizio importazione alle " . date('Y-m-d H:i:s') . "\n", FILE_APPEND);
|
||||
}
|
||||
|
||||
// Log iniziale
|
||||
error_log("Inizio importazione alle " . date('Y-m-d H:i:s'));
|
||||
|
||||
include('include/headscript.php');
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST' || !isset($_POST['template_id']) || !isset($_POST['selected_rows']) || !isset($_POST['filename'])) {
|
||||
header("Location: xlstemplates_grid.php?status=error&message=" . urlencode("Richiesta non valida"));
|
||||
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']);
|
||||
|
||||
$_SESSION['template_id'] = $template_id;
|
||||
$_SESSION['selected_rows'] = $selected_rows;
|
||||
$_SESSION['columns'] = $columns;
|
||||
$_SESSION['rows'] = $rows;
|
||||
$_SESSION['filename'] = $newFilename;
|
||||
|
||||
error_log("Received Data - Template ID: $template_id, Selected Rows: " . json_encode($selected_rows));
|
||||
error_log("Columns: " . json_encode($columns));
|
||||
error_log("Rows: " . json_encode($rows));
|
||||
|
||||
$user_id = $iduserlogin ?? 1; // Default a 1 se non definito
|
||||
|
||||
$db = DBHandlerSelect::getInstance();
|
||||
$pdo = $db->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,
|
||||
];
|
||||
|
||||
?>
|
||||
<form id="redirectForm" action="import_edit2.php" method="post">
|
||||
<input type="hidden" name="template_id" value="<?= htmlspecialchars($template_id) ?>">
|
||||
<input type="hidden" name="filename" value="<?= htmlspecialchars($newFilename) ?>">
|
||||
|
||||
<?php foreach ($selected_rows as $row): ?>
|
||||
<input type="hidden" name="selected_rows[]" value="<?= htmlspecialchars($row) ?>">
|
||||
<?php endforeach; ?>
|
||||
|
||||
<?php foreach ($insertedIds as $id): ?>
|
||||
<input type="hidden" name="inserted_ids[]" value="<?= htmlspecialchars($id) ?>">
|
||||
<?php endforeach; ?>
|
||||
|
||||
<input type="hidden" name="columns" value='<?= json_encode($columns) ?>'>
|
||||
<input type="hidden" name="rows" value='<?= json_encode($rows) ?>'>
|
||||
</form>
|
||||
<script>
|
||||
document.getElementById('redirectForm').submit();
|
||||
</script>
|
||||
<?php
|
||||
|
||||
exit;
|
||||
|
||||
@ -164,12 +164,12 @@ error_log("Loaded template: " . print_r($template, true));
|
||||
<!--end wrapper-->
|
||||
|
||||
<!-- search modal -->
|
||||
<?php //include('include/searchmodal.php');
|
||||
<?php //include('include/searchmodal.php');
|
||||
?>
|
||||
<!-- end search modal -->
|
||||
|
||||
<!--start switcher-->
|
||||
<?php //include('include/themeswitcher.php');
|
||||
<?php //include('include/themeswitcher.php');
|
||||
?>
|
||||
<!--end switcher-->
|
||||
<?php include('jsinclude.php'); ?>
|
||||
@ -205,7 +205,7 @@ error_log("Loaded template: " . print_r($template, true));
|
||||
errorContainer.style.display = 'block';
|
||||
} else {
|
||||
let html = `
|
||||
<form id="selectRowsForm" action="import_edit2.php" method="POST">
|
||||
<form id="selectRowsForm" action="import_insert.php" method="POST">
|
||||
<input type="hidden" name="template_id" value="${data.template_id}">
|
||||
<input type="hidden" name="columns" value='${JSON.stringify(data.columns)}'>
|
||||
<input type="hidden" name="rows" value='${JSON.stringify(data.rows)}'>
|
||||
@ -326,4 +326,4 @@ error_log("Loaded template: " . print_r($template, true));
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
</html>
|
||||
|
||||
@ -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());
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user