144 lines
4.3 KiB
PHP
144 lines
4.3 KiB
PHP
<?php
|
|
// save_pdf_regions.php
|
|
// Salva i riquadri (etichetta + dato) di un singolo campo mappato
|
|
// nella colonna pdf_regions della tabella template_mapping.
|
|
//
|
|
// Riceve JSON:
|
|
// {
|
|
// id: <mapping id>,
|
|
// mapping_type: 'pdf' | 'manual' | 'auto' | '',
|
|
// pdf_regions: { page, label:{x,y,w,h}, value:{x,y,w,h} } | null,
|
|
// manual_default: <string|null>,
|
|
// auto_value: <string>,
|
|
// tablename: <string>
|
|
// }
|
|
//
|
|
// Coerente con save_mapping_json.php: quando mapping_type = 'pdf'
|
|
// azzera excel_column/json_node e scrive pdf_regions; per gli altri
|
|
// tipi azzera pdf_regions.
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
include('include/headscript.php');
|
|
|
|
function respond(bool $success, string $message = ''): void
|
|
{
|
|
echo json_encode(['success' => $success, 'message' => $message]);
|
|
exit;
|
|
}
|
|
|
|
$raw = file_get_contents('php://input');
|
|
$data = json_decode($raw, true);
|
|
|
|
if (!is_array($data)) {
|
|
respond(false, 'Invalid JSON payload.');
|
|
}
|
|
|
|
$mappingId = isset($data['id']) ? (int)$data['id'] : 0;
|
|
$mappingType = isset($data['mapping_type']) ? (string)$data['mapping_type'] : '';
|
|
$manualDef = $data['manual_default'] ?? null;
|
|
$autoValue = $data['auto_value'] ?? 'none';
|
|
$regions = $data['pdf_regions'] ?? null;
|
|
|
|
if ($mappingId <= 0) {
|
|
respond(false, 'Missing or invalid mapping id.');
|
|
}
|
|
|
|
// --- Validazione e normalizzazione dei riquadri ---
|
|
// Accettiamo solo se mapping_type = 'pdf'. Negli altri casi pdf_regions = NULL.
|
|
$pdfRegionsJson = null;
|
|
|
|
if ($mappingType === 'pdf') {
|
|
if (!is_array($regions)) {
|
|
respond(false, 'pdf_regions must be an object when mapping_type is pdf.');
|
|
}
|
|
|
|
// Valida un singolo rettangolo {x,y,w,h} con numeri >= 0
|
|
$validateRect = function ($rect): ?array {
|
|
if (!is_array($rect)) {
|
|
return null;
|
|
}
|
|
$out = [];
|
|
foreach (['x', 'y', 'w', 'h'] as $k) {
|
|
if (!isset($rect[$k]) || !is_numeric($rect[$k])) {
|
|
return null;
|
|
}
|
|
$out[$k] = (float)$rect[$k];
|
|
if ($out[$k] < 0) {
|
|
$out[$k] = 0;
|
|
}
|
|
}
|
|
return $out;
|
|
};
|
|
|
|
$page = isset($regions['page']) ? (int)$regions['page'] : 0;
|
|
if ($page < 1) {
|
|
respond(false, 'pdf_regions.page must be >= 1.');
|
|
}
|
|
|
|
// value è obbligatorio, label è opzionale (modalità "solo dato")
|
|
$valueRect = $validateRect($regions['value'] ?? null);
|
|
if ($valueRect === null) {
|
|
respond(false, 'pdf_regions.value rectangle is invalid.');
|
|
}
|
|
|
|
$labelRect = null;
|
|
if (isset($regions['label']) && $regions['label'] !== null) {
|
|
$labelRect = $validateRect($regions['label']);
|
|
if ($labelRect === null) {
|
|
respond(false, 'pdf_regions.label rectangle is invalid.');
|
|
}
|
|
}
|
|
|
|
$clean = [
|
|
'page' => $page,
|
|
'label' => $labelRect,
|
|
'value' => $valueRect,
|
|
];
|
|
|
|
$pdfRegionsJson = json_encode($clean, JSON_UNESCAPED_UNICODE);
|
|
}
|
|
|
|
// --- Normalizza manual/auto in base al tipo ---
|
|
$manualToSave = ($mappingType === 'manual') ? $manualDef : null;
|
|
$autoToSave = ($mappingType === 'auto') ? ($autoValue ?: 'none') : 'none';
|
|
|
|
// is_manual coerente con il tipo
|
|
$isManual = ($mappingType === 'manual') ? 1 : 0;
|
|
|
|
try {
|
|
$db = DBHandlerSelect::getInstance();
|
|
$pdo = $db->getConnection();
|
|
|
|
// Quando è PDF: azzero excel_column e json_node, scrivo pdf_regions.
|
|
// Quando NON è PDF: azzero pdf_regions.
|
|
$sql = "
|
|
UPDATE template_mapping
|
|
SET
|
|
pdf_regions = :pdf_regions,
|
|
excel_column = CASE WHEN :is_pdf = 1 THEN NULL ELSE excel_column END,
|
|
json_node = CASE WHEN :is_pdf2 = 1 THEN NULL ELSE json_node END,
|
|
is_manual = :is_manual,
|
|
manual_default = :manual_default,
|
|
auto_value = :auto_value
|
|
WHERE id = :id
|
|
";
|
|
|
|
$isPdf = ($mappingType === 'pdf') ? 1 : 0;
|
|
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->execute([
|
|
':pdf_regions' => $pdfRegionsJson,
|
|
':is_pdf' => $isPdf,
|
|
':is_pdf2' => $isPdf,
|
|
':is_manual' => $isManual,
|
|
':manual_default' => $manualToSave,
|
|
':auto_value' => $autoToSave,
|
|
':id' => $mappingId,
|
|
]);
|
|
} catch (Throwable $e) {
|
|
respond(false, 'Database update failed: ' . $e->getMessage());
|
|
}
|
|
|
|
respond(true, 'PDF regions saved.');
|