trf_certest/public/userarea/process_edit_template_xls.php
2026-03-28 10:33:28 +01:00

105 lines
3.2 KiB
PHP

<?php
header('Content-Type: application/json');
require_once 'class/db-functions.php';
$response = ["success" => false, "message" => ""];
try {
if ($_SERVER["REQUEST_METHOD"] !== "POST") {
throw new Exception("Invalid request method.");
}
// Retrieve and sanitize form data
$id = intval($_POST['id'] ?? 0);
$name = trim($_POST['name'] ?? '');
$source_type = strtoupper(trim($_POST['source_type'] ?? 'XLS'));
$header_row = isset($_POST['header_row']) && $_POST['header_row'] !== '' ? intval($_POST['header_row']) : null;
$start_column = trim($_POST['start_column'] ?? '');
$description = trim($_POST['description'] ?? '');
$target_table = trim($_POST['target_table'] ?? 'datadb');
$idclient = intval($_POST['client_id'] ?? 0);
$clientname = trim($_POST['client_name'] ?? '');
$idschema = intval($_POST['idschema'] ?? 0);
$schemaname = trim($_POST['schemaname'] ?? '');
$idroutine = isset($_POST['idroutine']) && $_POST['idroutine'] !== '' ? intval($_POST['idroutine']) : null;
$button_size = trim($_POST['button_size'] ?? 'medium');
$button_bg_color = trim($_POST['button_bg_color'] ?? '#007bff');
$button_text_color = trim($_POST['button_text_color'] ?? '#ffffff');
$button_label = trim($_POST['button_label'] ?? 'Click Me');
if (!in_array($source_type, ['XLS', 'API'], true)) {
$source_type = 'XLS';
}
// Required fields validation
if ($id <= 0 || $name === '' || $target_table === '' || $idclient <= 0 || $idschema <= 0) {
throw new Exception("All fields marked with * are required, including client and schema.");
}
// XLS-only validation
if ($source_type === 'XLS') {
if ($header_row === null || $header_row <= 0 || $start_column === '') {
throw new Exception("Header Row and Start Column are required for XLS templates.");
}
}
// API templates do not require XLS coordinates
if ($source_type === 'API') {
$header_row = null;
$start_column = null;
}
// Database connection
$db = DBHandlerSelect::getInstance();
$pdo = $db->getConnection();
// Update template
$stmt = $pdo->prepare("
UPDATE excel_templates
SET
name = ?,
source_type = ?,
header_row = ?,
start_column = ?,
description = ?,
target_table = ?,
idclient = ?,
clientname = ?,
schemaname = ?,
idschema = ?,
idroutine = ?,
button_size = ?,
button_bg_color = ?,
button_text_color = ?,
button_label = ?,
updated_at = NOW()
WHERE id = ?
");
$stmt->execute([
$name,
$source_type,
$header_row,
$start_column,
$description,
$target_table,
$idclient,
$clientname,
$schemaname,
$idschema,
$idroutine,
$button_size,
$button_bg_color,
$button_text_color,
$button_label,
$id
]);
$response["success"] = true;
$response["message"] = "Template updated successfully!";
} catch (Exception $e) {
$response["message"] = $e->getMessage();
}
echo json_encode($response);