103 lines
2.8 KiB
PHP
103 lines
2.8 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
ini_set('display_errors', 1);
|
|
ini_set('display_startup_errors', 1);
|
|
error_reporting(E_ALL);
|
|
|
|
require_once(__DIR__ . '/class/db-functions.php');
|
|
|
|
$db = DBHandlerSelect::getInstance();
|
|
$pdo = $db->getConnection();
|
|
|
|
$data = json_decode(file_get_contents("php://input"), true);
|
|
|
|
if (!$data || !isset($data['id'])) {
|
|
echo json_encode(["success" => false, "message" => "Invalid or missing ID"]);
|
|
exit;
|
|
}
|
|
|
|
$mappingId = $data['id'];
|
|
$mappingType = $data['mapping_type'] ?? '';
|
|
$excelColumn = $data['excel_column'] ?? null;
|
|
$manualDefault = $data['manual_default'] ?? null;
|
|
|
|
$autoValue = $data['auto_value'] ?? 'none';
|
|
$tablename = $data['tablename'] ?? '';
|
|
|
|
try {
|
|
// Normalize mapping type
|
|
$allowedTypes = ['', 'xls', 'manual', 'auto'];
|
|
if (!in_array($mappingType, $allowedTypes, true)) {
|
|
echo json_encode(["success" => false, "message" => "Invalid mapping_type"]);
|
|
exit;
|
|
}
|
|
|
|
// Normalize auto_value
|
|
$allowedAuto = ['none', 'import_date', 'import_time', 'export_date', 'export_time'];
|
|
if (!in_array($autoValue, $allowedAuto, true)) {
|
|
$autoValue = 'none';
|
|
}
|
|
|
|
// Decide what to persist based on mapping_type
|
|
$isManual = 0;
|
|
$excelToSave = null;
|
|
$manualToSave = null;
|
|
$autoToSave = 'none';
|
|
|
|
if ($mappingType === 'xls') {
|
|
$isManual = 0;
|
|
$excelToSave = $excelColumn ?: null;
|
|
$manualToSave = null;
|
|
$autoToSave = 'none';
|
|
} elseif ($mappingType === 'manual') {
|
|
$isManual = 1;
|
|
$excelToSave = null;
|
|
$manualToSave = $manualDefault;
|
|
$autoToSave = 'none';
|
|
} elseif ($mappingType === 'auto') {
|
|
$isManual = 0;
|
|
$excelToSave = null;
|
|
$manualToSave = null;
|
|
$autoToSave = $autoValue ?: 'none';
|
|
} else {
|
|
// reset
|
|
$isManual = 0;
|
|
$excelToSave = null;
|
|
$manualToSave = null;
|
|
$autoToSave = 'none';
|
|
}
|
|
|
|
$stmt = $pdo->prepare("
|
|
UPDATE template_mapping
|
|
SET
|
|
is_manual = ?,
|
|
excel_column = ?,
|
|
manual_default = ?,
|
|
auto_value = ?
|
|
WHERE id = ?
|
|
");
|
|
|
|
$result = $stmt->execute([$isManual, $excelToSave, $manualToSave, $autoToSave, $mappingId]);
|
|
|
|
if (!$result) {
|
|
echo json_encode(["success" => false, "message" => "Database update failed"]);
|
|
exit;
|
|
}
|
|
|
|
echo json_encode([
|
|
"success" => true,
|
|
"message" => "Mapping updated successfully",
|
|
"saved" => [
|
|
"id" => (int)$mappingId,
|
|
"mapping_type" => $mappingType,
|
|
"is_manual" => $isManual,
|
|
"excel_column" => $excelToSave,
|
|
"manual_default" => $manualToSave,
|
|
"auto_value" => $autoToSave
|
|
]
|
|
]);
|
|
} catch (Exception $e) {
|
|
echo json_encode(["success" => false, "message" => "Error: " . $e->getMessage()]);
|
|
}
|
|
exit;
|