saved rows
This commit is contained in:
@@ -1301,9 +1301,21 @@ function fixedDefaultValue(array $f): string
|
|||||||
if (!inp.classList.contains('fixed-input')) return;
|
if (!inp.classList.contains('fixed-input')) return;
|
||||||
|
|
||||||
const m = inp.name.match(/rows\[\d+\]\[([^\]]+)\]/);
|
const m = inp.name.match(/rows\[\d+\]\[([^\]]+)\]/);
|
||||||
|
// Map: fixed key (logical) -> datadb real column
|
||||||
|
const fixedAliasMap = {
|
||||||
|
ClienteResponsabile: 'cliente_responsabile_id',
|
||||||
|
MoltiplicatorePrezzo: 'moltiplicatore_prezzo_id',
|
||||||
|
AnagraficaCertestObject: 'anagrafica_certest_object_id',
|
||||||
|
AnagraficaCertestService: 'anagrafica_certest_service_id',
|
||||||
|
ConsegnaRichiesta: 'consegna_richiesta'
|
||||||
|
};
|
||||||
|
|
||||||
if (m && m[1]) {
|
if (m && m[1]) {
|
||||||
formData.append(m[1], inp.value);
|
const logicalKey = m[1];
|
||||||
|
const realKey = fixedAliasMap[logicalKey] || logicalKey;
|
||||||
|
formData.append(realKey, inp.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
formData.append('iddatadb', iddatadb);
|
formData.append('iddatadb', iddatadb);
|
||||||
@@ -1386,9 +1398,21 @@ function fixedDefaultValue(array $f): string
|
|||||||
fixedInputs.forEach(inp => {
|
fixedInputs.forEach(inp => {
|
||||||
if (!inp.classList.contains('fixed-input')) return;
|
if (!inp.classList.contains('fixed-input')) return;
|
||||||
const m = inp.name.match(/rows\[\d+\]\[([^\]]+)\]/);
|
const m = inp.name.match(/rows\[\d+\]\[([^\]]+)\]/);
|
||||||
|
// Map: fixed key (logical) -> datadb real column
|
||||||
|
const fixedAliasMap = {
|
||||||
|
ClienteResponsabile: 'cliente_responsabile_id',
|
||||||
|
MoltiplicatorePrezzo: 'moltiplicatore_prezzo_id',
|
||||||
|
AnagraficaCertestObject: 'anagrafica_certest_object_id',
|
||||||
|
AnagraficaCertestService: 'anagrafica_certest_service_id',
|
||||||
|
ConsegnaRichiesta: 'consegna_richiesta'
|
||||||
|
};
|
||||||
|
|
||||||
if (m && m[1]) {
|
if (m && m[1]) {
|
||||||
formData.append(m[1], inp.value);
|
const logicalKey = m[1];
|
||||||
|
const realKey = fixedAliasMap[logicalKey] || logicalKey;
|
||||||
|
formData.append(realKey, inp.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
formData.append('iddatadb', iddatadb);
|
formData.append('iddatadb', iddatadb);
|
||||||
|
|||||||
@@ -12,11 +12,23 @@ try {
|
|||||||
|
|
||||||
$iddatadb = intval($_POST['iddatadb']);
|
$iddatadb = intval($_POST['iddatadb']);
|
||||||
$idclient = isset($_POST['idclient']) ? (is_numeric($_POST['idclient']) ? intval($_POST['idclient']) : null) : null;
|
$idclient = isset($_POST['idclient']) ? (is_numeric($_POST['idclient']) ? intval($_POST['idclient']) : null) : null;
|
||||||
|
|
||||||
$db = DBHandlerSelect::getInstance();
|
$db = DBHandlerSelect::getInstance();
|
||||||
$pdo = $db->getConnection();
|
$pdo = $db->getConnection();
|
||||||
|
|
||||||
// ---------------- FIXED FIELDS (template_fixed_mapping) ----------------
|
// ---------------- FIXED FIELDS (template_fixed_mapping) ----------------
|
||||||
|
|
||||||
|
// ALIAS: fixed_field_key "logico" -> colonna reale su datadb
|
||||||
|
// (NON tocchiamo MySQL, gestiamo solo qui la traduzione)
|
||||||
|
$fixedAliasMap = [
|
||||||
|
'ClienteResponsabile' => 'cliente_responsabile_id',
|
||||||
|
'MoltiplicatorePrezzo' => 'moltiplicatore_prezzo_id',
|
||||||
|
'AnagraficaCertestObject' => 'anagrafica_certest_object_id',
|
||||||
|
'AnagraficaCertestService' => 'anagrafica_certest_service_id',
|
||||||
|
'ClienteFornitore' => 'cliente_fornitore_id',
|
||||||
|
'ConsegnaRichiesta' => 'consegna_richiesta',
|
||||||
|
];
|
||||||
|
|
||||||
// 1) Recupera templateid dalla riga datadb (serve per sapere quali fixed_field_key sono permessi)
|
// 1) Recupera templateid dalla riga datadb (serve per sapere quali fixed_field_key sono permessi)
|
||||||
$stmtTpl = $pdo->prepare("SELECT templateid FROM datadb WHERE iddatadb = ?");
|
$stmtTpl = $pdo->prepare("SELECT templateid FROM datadb WHERE iddatadb = ?");
|
||||||
$stmtTpl->execute([$iddatadb]);
|
$stmtTpl->execute([$iddatadb]);
|
||||||
@@ -32,19 +44,20 @@ try {
|
|||||||
SELECT fixed_field_key, data_type, is_required, default_value
|
SELECT fixed_field_key, data_type, is_required, default_value
|
||||||
FROM template_fixed_mapping
|
FROM template_fixed_mapping
|
||||||
WHERE template_id = ? AND is_visible_import = 1
|
WHERE template_id = ? AND is_visible_import = 1
|
||||||
");
|
");
|
||||||
$fxStmt->execute([$templateId]);
|
$fxStmt->execute([$templateId]);
|
||||||
$fixedList = $fxStmt->fetchAll(PDO::FETCH_ASSOC);
|
$fixedList = $fxStmt->fetchAll(PDO::FETCH_ASSOC);
|
||||||
|
|
||||||
// 3) Crea whitelist: key => metadata
|
// 3) Crea whitelist LOGICA: fixed_field_key => metadata
|
||||||
$fixedWhitelist = [];
|
$fixedWhitelist = [];
|
||||||
foreach ($fixedList as $fx) {
|
foreach ($fixedList as $fx) {
|
||||||
$k = (string)$fx['fixed_field_key'];
|
$k = (string)$fx['fixed_field_key'];
|
||||||
|
|
||||||
// sicurezza: nome colonna ammesso solo se "safe" (no spazi, no caratteri strani)
|
// sicurezza: key ammessa solo se "safe"
|
||||||
if (!preg_match('/^[a-zA-Z0-9_]+$/', $k)) {
|
if (!preg_match('/^[a-zA-Z0-9_]+$/', $k)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$fixedWhitelist[$k] = [
|
$fixedWhitelist[$k] = [
|
||||||
'data_type' => (string)$fx['data_type'], // INT | DATE
|
'data_type' => (string)$fx['data_type'], // INT | DATE
|
||||||
'is_required' => (int)$fx['is_required'],
|
'is_required' => (int)$fx['is_required'],
|
||||||
@@ -52,7 +65,20 @@ try {
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 3b) Crea whitelist REALE: colonna datadb reale => metadata
|
||||||
|
$realWhitelist = [];
|
||||||
|
foreach ($fixedWhitelist as $logicalKey => $meta) {
|
||||||
|
$realCol = $fixedAliasMap[$logicalKey] ?? $logicalKey;
|
||||||
|
|
||||||
|
// sicurezza: anche la colonna reale deve essere "safe"
|
||||||
|
if (!preg_match('/^[a-zA-Z0-9_]+$/', $realCol)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$realWhitelist[$realCol] = $meta;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------- DETAILS (import_data_details) ----------------
|
||||||
$data = $_POST;
|
$data = $_POST;
|
||||||
$details = [];
|
$details = [];
|
||||||
|
|
||||||
@@ -101,7 +127,7 @@ try {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 5. Aggiorna datadb: idclient + FIXED FIELDS (whitelisted)
|
// ---------------- UPDATE datadb: idclient + FIXED FIELDS ----------------
|
||||||
$setParts = [];
|
$setParts = [];
|
||||||
$params = [];
|
$params = [];
|
||||||
|
|
||||||
@@ -111,13 +137,33 @@ try {
|
|||||||
$params[] = $idclient;
|
$params[] = $idclient;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 5b) fixed fields dal POST (solo quelli presenti nella whitelist del template)
|
// 5b) fixed fields dal POST
|
||||||
foreach ($fixedWhitelist as $col => $meta) {
|
// QUI è il punto chiave: accettiamo SOLO colonne reali (realWhitelist),
|
||||||
if (!array_key_exists($col, $_POST)) {
|
// ma se dal JS arrivassero ancora key logiche, funzionerebbe uguale
|
||||||
continue; // non inviato dal form
|
// perché sotto controlliamo anche l'alias.
|
||||||
|
foreach ($realWhitelist as $realCol => $meta) {
|
||||||
|
|
||||||
|
$postKeyToRead = null;
|
||||||
|
|
||||||
|
// Caso 1: POST contiene già la colonna reale
|
||||||
|
if (array_key_exists($realCol, $_POST)) {
|
||||||
|
$postKeyToRead = $realCol;
|
||||||
|
} else {
|
||||||
|
// Caso 2: POST contiene la key logica -> troviamo quale logica mappa su questa colonna reale
|
||||||
|
// (fallback, utile se non hai ancora aggiornato il JS)
|
||||||
|
foreach ($fixedAliasMap as $logical => $mappedReal) {
|
||||||
|
if ($mappedReal === $realCol && array_key_exists($logical, $_POST)) {
|
||||||
|
$postKeyToRead = $logical;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$val = $_POST[$col];
|
if ($postKeyToRead === null) {
|
||||||
|
continue; // non inviato
|
||||||
|
}
|
||||||
|
|
||||||
|
$val = $_POST[$postKeyToRead];
|
||||||
|
|
||||||
// Normalizzazione per tipo
|
// Normalizzazione per tipo
|
||||||
if ($meta['data_type'] === 'DATE') {
|
if ($meta['data_type'] === 'DATE') {
|
||||||
@@ -128,13 +174,14 @@ try {
|
|||||||
$val = ($val === '') ? null : (int)$val;
|
$val = ($val === '') ? null : (int)$val;
|
||||||
}
|
}
|
||||||
|
|
||||||
$setParts[] = "`$col` = ?";
|
$setParts[] = "`$realCol` = ?";
|
||||||
$params[] = $val;
|
$params[] = $val;
|
||||||
}
|
}
|
||||||
|
|
||||||
// esegui update solo se c'è qualcosa da aggiornare
|
// esegui update solo se c'è qualcosa da aggiornare
|
||||||
if (!empty($setParts)) {
|
if (!empty($setParts)) {
|
||||||
$params[] = $iddatadb;
|
$params[] = $iddatadb;
|
||||||
|
|
||||||
$sqlUpd = "UPDATE datadb SET " . implode(", ", $setParts) . " WHERE iddatadb = ?";
|
$sqlUpd = "UPDATE datadb SET " . implode(", ", $setParts) . " WHERE iddatadb = ?";
|
||||||
$updStmt = $pdo->prepare($sqlUpd);
|
$updStmt = $pdo->prepare($sqlUpd);
|
||||||
$updStmt->execute($params);
|
$updStmt->execute($params);
|
||||||
@@ -151,7 +198,6 @@ try {
|
|||||||
$response['message'] = "No changes found";
|
$response['message'] = "No changes found";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
$response['success'] = true;
|
$response['success'] = true;
|
||||||
$response['changed'] = $changed; // Debug / optional
|
$response['changed'] = $changed; // Debug / optional
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user