diff --git a/public/userarea/edit_template_xls.php b/public/userarea/edit_template_xls.php index 7915132..9677ba6 100644 --- a/public/userarea/edit_template_xls.php +++ b/public/userarea/edit_template_xls.php @@ -26,6 +26,16 @@ $stmt = $pdo->prepare("SELECT * FROM routine"); $stmt->execute(); $routines = $stmt->fetchAll(PDO::FETCH_ASSOC); +// Retrieve active API/JSON configurations +$stmt = $pdo->prepare(" + SELECT id, name, provider_code, api_type, php_class_name + FROM api_configurations + WHERE is_active = 1 + ORDER BY name ASC +"); +$stmt->execute(); +$apiConfigurations = $stmt->fetchAll(PDO::FETCH_ASSOC); + $buttonBgPalette = [ '#0d6efd' => 'Blue', '#6610f2' => 'Indigo', @@ -181,6 +191,8 @@ if (!array_key_exists($currentButtonTextColor, array_change_key_case($buttonText Choose the source used by this template @@ -195,6 +207,60 @@ if (!array_key_exists($currentButtonTextColor, array_change_key_case($buttonText +
+ + + + Use 0 for the first sheet, 1 for the second sheet, 2 for the third sheet, and so on. + +
+ + +
@@ -335,10 +401,16 @@ if (!array_key_exists($currentButtonTextColor, array_change_key_case($buttonText const routineAction3 = document.getElementById("routineAction3"); const sourceType = document.getElementById("sourceType"); + const headerRowWrapper = document.getElementById("headerRowWrapper"); const startColumnWrapper = document.getElementById("startColumnWrapper"); + const xlsSheetNumberWrapper = document.getElementById("xlsSheetNumberWrapper"); + const apiConfigWrapper = document.getElementById("apiConfigWrapper"); + const headerRow = document.getElementById("headerRow"); const startColumn = document.getElementById("startColumn"); + const xlsSheetIndex = document.getElementById("xlsSheetIndex"); + const apiConfigSelect = document.getElementById("apiConfigSelect"); const selectedClientId = ; const selectedSchemaId = ; @@ -358,27 +430,55 @@ if (!array_key_exists($currentButtonTextColor, array_change_key_case($buttonText allowClear: true }); + $('#apiConfigSelect').select2({ + placeholder: "Select an API configuration...", + allowClear: true + }); + function updateSourceFields() { const selectedSource = sourceType.value; - if (selectedSource === 'API') { - headerRowWrapper.style.opacity = '0.6'; - startColumnWrapper.style.opacity = '0.6'; + const isXls = selectedSource === 'XLS'; + const isApiOrJson = selectedSource === 'API' || selectedSource === 'JSON'; - headerRow.required = false; - startColumn.required = false; - - headerRow.disabled = true; - startColumn.disabled = true; - } else { - headerRowWrapper.style.opacity = '1'; - startColumnWrapper.style.opacity = '1'; + if (isXls) { + headerRowWrapper.style.display = 'block'; + startColumnWrapper.style.display = 'block'; + xlsSheetNumberWrapper.style.display = 'block'; headerRow.required = true; startColumn.required = true; headerRow.disabled = false; startColumn.disabled = false; + xlsSheetIndex.disabled = false; + + apiConfigWrapper.style.display = 'none'; + apiConfigSelect.required = false; + apiConfigSelect.disabled = true; + $('#apiConfigSelect').val(null).trigger('change'); + } else { + headerRowWrapper.style.display = 'none'; + startColumnWrapper.style.display = 'none'; + xlsSheetNumberWrapper.style.display = 'none'; + + headerRow.required = false; + startColumn.required = false; + + headerRow.disabled = true; + startColumn.disabled = true; + xlsSheetIndex.disabled = true; + + if (isApiOrJson) { + apiConfigWrapper.style.display = 'block'; + apiConfigSelect.required = true; + apiConfigSelect.disabled = false; + } else { + apiConfigWrapper.style.display = 'none'; + apiConfigSelect.required = false; + apiConfigSelect.disabled = true; + $('#apiConfigSelect').val(null).trigger('change'); + } } } @@ -604,6 +704,28 @@ if (!array_key_exists($currentButtonTextColor, array_change_key_case($buttonText const routineId = routineSelect.value; formData.append("idroutine", routineId); + const selectedSource = sourceType.value; + + if ((selectedSource === 'API' || selectedSource === 'JSON') && !apiConfigSelect.value) { + Swal.fire({ + title: "Error!", + text: "Please select an API/JSON configuration.", + icon: "error", + confirmButtonText: "OK" + }); + return; + } + + if (selectedSource === 'XLS' && xlsSheetIndex.value === '') { + Swal.fire({ + title: "Error!", + text: "Please enter the XLS sheet number.", + icon: "error", + confirmButtonText: "OK" + }); + return; + } + fetch("process_edit_template_xls.php", { method: "POST", body: formData diff --git a/public/userarea/insert_template_xls.php b/public/userarea/insert_template_xls.php index f3f34dd..9217b25 100644 --- a/public/userarea/insert_template_xls.php +++ b/public/userarea/insert_template_xls.php @@ -3,9 +3,20 @@ // Retrieve all routines from database $db = DBHandlerSelect::getInstance(); $pdo = $db->getConnection(); + $stmt = $pdo->prepare("SELECT * FROM routine"); $stmt->execute(); $routines = $stmt->fetchAll(PDO::FETCH_ASSOC); + +// Retrieve active API/JSON configurations +$stmt = $pdo->prepare(" + SELECT id, name, provider_code, api_type, php_class_name + FROM api_configurations + WHERE is_active = 1 + ORDER BY name ASC +"); +$stmt->execute(); +$apiConfigurations = $stmt->fetchAll(PDO::FETCH_ASSOC); ?> @@ -40,7 +51,8 @@ $routines = $stmt->fetchAll(PDO::FETCH_ASSOC);
  • Template Name
  • Source Type
  • Schema and Client
  • -
  • Row Header and Column Header only for XLS templates
  • +
  • Row Header, Column Header and Sheet Number only for XLS templates
  • +
  • API / JSON Configuration only for API / JSON templates
  • @@ -67,7 +79,8 @@ $routines = $stmt->fetchAll(PDO::FETCH_ASSOC); Choose the source used by this template @@ -82,6 +95,58 @@ $routines = $stmt->fetchAll(PDO::FETCH_ASSOC); +
    + + + + Use 0 for the first sheet, 1 for the second sheet, 2 for the third sheet, and so on. + +
    + + +
    @@ -185,10 +250,16 @@ $routines = $stmt->fetchAll(PDO::FETCH_ASSOC); const routineAction3 = document.getElementById("routineAction3"); const sourceType = document.getElementById("sourceType"); + const headerRowWrapper = document.getElementById("headerRowWrapper"); const startColumnWrapper = document.getElementById("startColumnWrapper"); + const xlsSheetNumberWrapper = document.getElementById("xlsSheetNumberWrapper"); + const apiConfigWrapper = document.getElementById("apiConfigWrapper"); + const headerRow = document.getElementById("headerRow"); const startColumn = document.getElementById("startColumn"); + const xlsSheetIndex = document.getElementById("xlsSheetIndex"); + const apiConfigSelect = document.getElementById("apiConfigSelect"); if (!form || !clientLoadingStatus || !schemaLoadingStatus || !routineSelect || !routineDetails) { alert("Errore: Uno o più elementi della pagina non sono stati trovati. Contatta l'amministratore."); @@ -210,27 +281,57 @@ $routines = $stmt->fetchAll(PDO::FETCH_ASSOC); allowClear: true }); + $('#apiConfigSelect').select2({ + placeholder: "Select an API configuration...", + allowClear: true + }); + function updateSourceFields() { const selectedSource = sourceType.value; - if (selectedSource === 'API') { - headerRowWrapper.style.opacity = '0.6'; - startColumnWrapper.style.opacity = '0.6'; + const isXls = selectedSource === 'XLS'; + const isApiJson = selectedSource === 'API'; - headerRow.required = false; - startColumn.required = false; - - headerRow.disabled = true; - startColumn.disabled = true; - } else { - headerRowWrapper.style.opacity = '1'; - startColumnWrapper.style.opacity = '1'; + if (isXls) { + headerRowWrapper.style.display = 'block'; + startColumnWrapper.style.display = 'block'; + xlsSheetNumberWrapper.style.display = 'block'; headerRow.required = true; startColumn.required = true; + xlsSheetIndex.required = true; headerRow.disabled = false; startColumn.disabled = false; + xlsSheetIndex.disabled = false; + + apiConfigWrapper.style.display = 'none'; + apiConfigSelect.required = false; + apiConfigSelect.disabled = true; + $('#apiConfigSelect').val(null).trigger('change'); + } else { + headerRowWrapper.style.display = 'none'; + startColumnWrapper.style.display = 'none'; + xlsSheetNumberWrapper.style.display = 'none'; + + headerRow.required = false; + startColumn.required = false; + xlsSheetIndex.required = false; + + headerRow.disabled = true; + startColumn.disabled = true; + xlsSheetIndex.disabled = true; + + if (isApiJson) { + apiConfigWrapper.style.display = 'block'; + apiConfigSelect.required = true; + apiConfigSelect.disabled = false; + } else { + apiConfigWrapper.style.display = 'none'; + apiConfigSelect.required = false; + apiConfigSelect.disabled = true; + $('#apiConfigSelect').val(null).trigger('change'); + } } } @@ -261,7 +362,12 @@ $routines = $stmt->fetchAll(PDO::FETCH_ASSOC); data.value.forEach(client => { const nome = client.Nominativo || "Nome non disponibile"; const id = client.IdCliente || "ID non disponibile"; - const option = new Option(`${nome.trim()} (ID: ${id})`, id); + + const codiceCliente = (client.CodiceCliente ?? client.codiceCliente ?? "").toString().trim(); + const suffix = (codiceCliente.split("_")[1] || "").trim(); + const shortCode = suffix || (codiceCliente ? codiceCliente.charAt(0) : "--"); + + const option = new Option(`${nome.trim()} - ${shortCode} (ID: ${id})`, id); select.add(option); }); @@ -388,6 +494,28 @@ $routines = $stmt->fetchAll(PDO::FETCH_ASSOC); let formData = new FormData(this); + const selectedSource = sourceType.value; + + if (selectedSource === 'XLS' && xlsSheetIndex.value === '') { + Swal.fire({ + title: "Errore!", + text: "Inserisci il numero del foglio XLS.", + icon: "error", + confirmButtonText: "OK" + }); + return; + } + + if (selectedSource === 'API' && !apiConfigSelect.value) { + Swal.fire({ + title: "Errore!", + text: "Seleziona una configurazione API / JSON.", + icon: "error", + confirmButtonText: "OK" + }); + return; + } + const clientSelect = document.getElementById("clientSelect"); const clientId = clientSelect.value; const selectedClientOption = clientSelect.options[clientSelect.selectedIndex]; diff --git a/public/userarea/process_edit_template_xls.php b/public/userarea/process_edit_template_xls.php index becdcfe..e333d9a 100644 --- a/public/userarea/process_edit_template_xls.php +++ b/public/userarea/process_edit_template_xls.php @@ -13,8 +13,21 @@ try { $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; + + $header_row = isset($_POST['header_row']) && $_POST['header_row'] !== '' + ? intval($_POST['header_row']) + : null; + $start_column = trim($_POST['start_column'] ?? ''); + + $xls_sheet_index = isset($_POST['xls_sheet_index']) && $_POST['xls_sheet_index'] !== '' + ? intval($_POST['xls_sheet_index']) + : 0; + + $api_config_id = isset($_POST['api_config_id']) && $_POST['api_config_id'] !== '' + ? intval($_POST['api_config_id']) + : null; + $description = trim($_POST['description'] ?? ''); $target_table = trim($_POST['target_table'] ?? 'datadb'); $idclient = intval($_POST['client_id'] ?? 0); @@ -27,7 +40,8 @@ try { $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)) { + // Allowed source types + if (!in_array($source_type, ['XLS', 'API', 'JSON', 'PDF'], true)) { $source_type = 'XLS'; } @@ -41,18 +55,52 @@ try { if ($header_row === null || $header_row <= 0 || $start_column === '') { throw new Exception("Header Row and Start Column are required for XLS templates."); } + + if ($xls_sheet_index < 0) { + throw new Exception("XLS Sheet Number cannot be negative."); + } + + $api_config_id = null; } - // API templates do not require XLS coordinates - if ($source_type === 'API') { + // API/JSON validation + if ($source_type === 'API' || $source_type === 'JSON') { + if (empty($api_config_id)) { + throw new Exception("API/JSON configuration is required for API or JSON templates."); + } + $header_row = null; $start_column = null; + $xls_sheet_index = null; + } + + // PDF currently does not require XLS coordinates or API configuration + if ($source_type === 'PDF') { + $header_row = null; + $start_column = null; + $xls_sheet_index = null; + $api_config_id = null; } // Database connection $db = DBHandlerSelect::getInstance(); $pdo = $db->getConnection(); + // Optional check: verify API configuration exists and is active + if ($api_config_id !== null) { + $stmt = $pdo->prepare(" + SELECT COUNT(*) + FROM api_configurations + WHERE id = ? + AND is_active = 1 + "); + $stmt->execute([$api_config_id]); + + if ((int)$stmt->fetchColumn() === 0) { + throw new Exception("Selected API/JSON configuration does not exist or is not active."); + } + } + // Update template $stmt = $pdo->prepare(" UPDATE excel_templates @@ -61,6 +109,8 @@ try { source_type = ?, header_row = ?, start_column = ?, + xls_sheet_index = ?, + api_config_id = ?, description = ?, target_table = ?, idclient = ?, @@ -81,6 +131,8 @@ try { $source_type, $header_row, $start_column, + $xls_sheet_index, + $api_config_id, $description, $target_table, $idclient, diff --git a/public/userarea/process_insert_template_xls.php b/public/userarea/process_insert_template_xls.php index ddc0a5b..e257656 100644 --- a/public/userarea/process_insert_template_xls.php +++ b/public/userarea/process_insert_template_xls.php @@ -12,22 +12,39 @@ try { // Retrieve and sanitize form data $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; + + $header_row = isset($_POST['header_row']) && $_POST['header_row'] !== '' + ? intval($_POST['header_row']) + : null; + $start_column = trim($_POST['start_column'] ?? ''); + + $xls_sheet_index = isset($_POST['xls_sheet_index']) && $_POST['xls_sheet_index'] !== '' + ? intval($_POST['xls_sheet_index']) + : 0; + + $api_config_id = isset($_POST['api_config_id']) && $_POST['api_config_id'] !== '' + ? intval($_POST['api_config_id']) + : null; + $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; + $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'); // Normalize source type - if (!in_array($source_type, ['XLS', 'API'], true)) { + // API / JSON is saved as API + if (!in_array($source_type, ['XLS', 'API', 'PDF'], true)) { $source_type = 'XLS'; } @@ -41,26 +58,62 @@ try { if ($header_row === null || $header_row <= 0 || $start_column === '') { throw new Exception("Header Row and Start Column are required for XLS templates."); } + + if ($xls_sheet_index < 0) { + throw new Exception("XLS Sheet Number cannot be negative."); + } + + $api_config_id = null; } - // API templates do not require XLS coordinates + // API / JSON validation if ($source_type === 'API') { + if (empty($api_config_id)) { + throw new Exception("API / JSON configuration is required for API / JSON templates."); + } + $header_row = null; $start_column = null; + $xls_sheet_index = null; + } + + // PDF currently does not require XLS coordinates or API configuration + if ($source_type === 'PDF') { + $header_row = null; + $start_column = null; + $xls_sheet_index = null; + $api_config_id = null; } // Database connection $db = DBHandlerSelect::getInstance(); $pdo = $db->getConnection(); + // Optional check: verify API configuration exists and is active + if ($api_config_id !== null) { + $stmt = $pdo->prepare(" + SELECT COUNT(*) + FROM api_configurations + WHERE id = ? + AND is_active = 1 + "); + $stmt->execute([$api_config_id]); + + if ((int)$stmt->fetchColumn() === 0) { + throw new Exception("Selected API / JSON configuration does not exist or is not active."); + } + } + // Insert the new template $stmt = $pdo->prepare(" - INSERT INTO excel_templates + INSERT INTO excel_templates ( name, source_type, header_row, start_column, + xls_sheet_index, + api_config_id, description, target_table, idclient, @@ -75,7 +128,13 @@ try { created_at, updated_at ) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW()) + VALUES + ( + ?, ?, ?, ?, ?, ?, + ?, ?, ?, ?, ?, ?, + ?, ?, ?, ?, ?, + NOW(), NOW() + ) "); $stmt->execute([ @@ -83,6 +142,8 @@ try { $source_type, $header_row, $start_column, + $xls_sheet_index, + $api_config_id, $description, $target_table, $idclient,