added routine to insert templates
This commit is contained in:
parent
b39d601ec9
commit
420b0a0405
@ -1,28 +1,30 @@
|
||||
<?php include('include/headscript.php'); ?>
|
||||
<?php include('include/headscript.php');
|
||||
|
||||
// Recupera tutte le routine dal database
|
||||
$db = DBHandlerSelect::getInstance();
|
||||
$pdo = $db->getConnection();
|
||||
$stmt = $pdo->prepare("SELECT * FROM routine");
|
||||
$stmt->execute();
|
||||
$routines = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
?>
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<!-- Required meta tags -->
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<!--favicon-->
|
||||
<link rel="icon" href="assets/images/favicon-32x32.png" type="image/png" />
|
||||
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
|
||||
<?php include('cssinclude.php'); ?>
|
||||
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
|
||||
<title>Insert XLS Template <?= htmlspecialchars($titlewebsite, ENT_QUOTES, 'UTF-8'); ?></title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<!--wrapper-->
|
||||
<div class="wrapper">
|
||||
<!--sidebar wrapper -->
|
||||
<?php include('include/navbar.php'); ?>
|
||||
<!--end sidebar wrapper -->
|
||||
<!--start header -->
|
||||
<?php include('include/topbar.php'); ?>
|
||||
<!--end header -->
|
||||
<!--start page wrapper -->
|
||||
<div class="page-wrapper">
|
||||
<div class="page-content">
|
||||
<div class="card mb-4">
|
||||
@ -35,7 +37,7 @@
|
||||
<ul class="mb-0">
|
||||
<li>Template Name</li>
|
||||
<li>Row Header and Column Header: where the title of the excel starts</li>
|
||||
<li>Scheme and client</li>
|
||||
<li>Schema and client</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
@ -73,7 +75,6 @@
|
||||
|
||||
<input type="hidden" name="target_table" value="datadb">
|
||||
|
||||
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Button Size</label>
|
||||
<select name="button_size" class="form-control">
|
||||
@ -97,7 +98,7 @@
|
||||
<label class="form-label">Button Label</label>
|
||||
<input type="text" name="button_label" class="form-control" value="Click Me">
|
||||
</div>
|
||||
<!-- Aggiungi il campo per selezionare il cliente -->
|
||||
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Select Client *</label>
|
||||
<select name="client_id" id="clientSelect" class="form-control" required>
|
||||
@ -105,68 +106,33 @@
|
||||
</select>
|
||||
<span id="clientLoadingStatus" class="text-muted" style="margin-left: 10px; display: none;">Recupero clienti in corso...</span>
|
||||
</div>
|
||||
<!-- Aggiungi il campo per selezionare lo schema -->
|
||||
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Select Schema *</label>
|
||||
<select name="schema_id" id="schemaSelect" class="form-control" required>
|
||||
<option value="">Select a schema...</option>
|
||||
</select>
|
||||
<span id="schemaLoadingStatus" class="text-muted" style="margin-left: 10px; display: none;">Loading schemas...</span>
|
||||
<span id="schemaLoadingStatus" class="text-muted" style="margin-left: 10px; display: none;">Caricamento schemi in corso...</span>
|
||||
</div>
|
||||
|
||||
<!-- new section for specific client field -->
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Client-Specific Fields</label>
|
||||
|
||||
<!-- Intestazioni -->
|
||||
<div class="row fw-bold text-secondary mb-1">
|
||||
<div class="col-md-3">Field Name</div>
|
||||
<div class="col-md-2">Type</div>
|
||||
<div class="col-md-2 dropdown-values">Possible Values</div>
|
||||
<div class="col-md-1">Required</div>
|
||||
<div class="col-md-2">Export Column Name</div>
|
||||
<div class="col-md-1">Default Value</div>
|
||||
<div class="col-md-1">Actions</div>
|
||||
<label class="form-label">Select Routine</label>
|
||||
<select name="idroutine" id="routineSelect" class="form-control">
|
||||
<option value="">Select a routine...</option>
|
||||
<?php foreach ($routines as $routine): ?>
|
||||
<option value="<?php echo $routine['idroutine']; ?>">
|
||||
<?php echo htmlspecialchars($routine['name']); ?>
|
||||
</option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
<div id="routineDetails" class="mt-2" style="display: none;">
|
||||
<h6>Routine Details</h6>
|
||||
<p><strong>Name:</strong> <span id="routineName"></span></p>
|
||||
<p><strong>Description:</strong> <span id="routineDescription"></span></p>
|
||||
<p><strong>Action 1:</strong> <span id="routineAction1"></span></p>
|
||||
<p><strong>Action 2:</strong> <span id="routineAction2"></span></p>
|
||||
<p><strong>Action 3:</strong> <span id="routineAction3"></span></p>
|
||||
</div>
|
||||
|
||||
<div id="clientSpecificFields">
|
||||
<!-- Contenitore per i campi dinamici -->
|
||||
<div class="client-field-row mb-2">
|
||||
<div class="row">
|
||||
<div class="col-md-3">
|
||||
<input type="text" name="specific_fields[0][name]" class="form-control" placeholder="Field Name (e.g., SKU)">
|
||||
</div>
|
||||
<div class="col-md-2">
|
||||
<select name="specific_fields[0][type]" class="form-control" onchange="toggleDropdownValues(this)">
|
||||
<option value="text">Text</option>
|
||||
<option value="dropdown">Dropdown</option>
|
||||
<option value="date">Date</option>
|
||||
<option value="boolean">Yes/No</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="col-md-2 dropdown-values" style="visibility: hidden;">
|
||||
<input type="text" name="specific_fields[0][possible_values]" class="form-control" placeholder="Values (e.g., Red, Blue, Green)">
|
||||
</div>
|
||||
<div class="col-md-1">
|
||||
<select name="specific_fields[0][required]" class="form-control">
|
||||
<option value="1">Yes</option>
|
||||
<option value="0">No</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="col-md-2">
|
||||
<input type="text" name="specific_fields[0][export_column_name]" class="form-control" placeholder="Export Column Name (e.g., MONCLER_SKU)">
|
||||
</div>
|
||||
<div class="col-md-1">
|
||||
<input type="text" name="specific_fields[0][default_value]" class="form-control" placeholder="Default Value (optional)">
|
||||
</div>
|
||||
<div class="col-md-1">
|
||||
<button type="button" class="btn btn-danger remove-field">-</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<button type="button" class="btn btn-primary mt-2" id="addField">Add Field</button>
|
||||
</div>
|
||||
|
||||
<br>
|
||||
@ -178,125 +144,72 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<!--end page wrapper -->
|
||||
<!--start overlay-->
|
||||
<div class="overlay toggle-icon"></div>
|
||||
<!--end overlay-->
|
||||
<!--Start Back To Top Button-->
|
||||
<a href="javaScript:;" class="back-to-top"><i class='bx bxs-up-arrow-alt'></i></a>
|
||||
<!--End Back To Top Button-->
|
||||
<?php include('include/footer.php'); ?>
|
||||
</div>
|
||||
<!--end wrapper-->
|
||||
|
||||
<!-- search modal -->
|
||||
<?php //include('include/searchmodal.php');
|
||||
?>
|
||||
<!-- end search modal -->
|
||||
|
||||
<!--start switcher-->
|
||||
<?php //include('include/themeswitcher.php');
|
||||
?>
|
||||
<!--end switcher-->
|
||||
|
||||
<!-- Includi Select2 JS -->
|
||||
|
||||
<?php include('jsinclude.php'); ?>
|
||||
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
|
||||
<script>
|
||||
// Debug iniziale
|
||||
console.log("JavaScript is loaded and running!");
|
||||
|
||||
document.addEventListener("DOMContentLoaded", function() {
|
||||
console.log("DOM is loaded");
|
||||
if (typeof jQuery === 'undefined') {
|
||||
alert("Errore: jQuery non è caricato. Contatta l'amministratore.");
|
||||
return;
|
||||
}
|
||||
|
||||
const form = document.getElementById("insertTemplateForm");
|
||||
const addFieldButton = document.getElementById("addField");
|
||||
const container = document.getElementById("clientSpecificFields");
|
||||
const clientLoadingStatus = document.getElementById("clientLoadingStatus");
|
||||
const schemaLoadingStatus = document.getElementById("schemaLoadingStatus");
|
||||
const routineSelect = document.getElementById("routineSelect");
|
||||
const routineDetails = document.getElementById("routineDetails");
|
||||
const routineName = document.getElementById("routineName");
|
||||
const routineDescription = document.getElementById("routineDescription");
|
||||
const routineAction1 = document.getElementById("routineAction1");
|
||||
const routineAction2 = document.getElementById("routineAction2");
|
||||
const routineAction3 = document.getElementById("routineAction3");
|
||||
|
||||
if (!form || !addFieldButton || !container || !clientLoadingStatus || !schemaLoadingStatus) {
|
||||
console.error("One or more DOM elements not found:", {
|
||||
form,
|
||||
addFieldButton,
|
||||
container,
|
||||
clientLoadingStatus,
|
||||
schemaLoadingStatus
|
||||
});
|
||||
if (!form || !clientLoadingStatus || !schemaLoadingStatus || !routineSelect || !routineDetails) {
|
||||
alert("Errore: Uno o più elementi della pagina non sono stati trovati. Contatta l'amministratore.");
|
||||
return;
|
||||
}
|
||||
|
||||
console.log("All DOM elements found");
|
||||
|
||||
// Controllo che jQuery sia caricato
|
||||
if (typeof jQuery === 'undefined') {
|
||||
console.error("jQuery non è caricato!");
|
||||
return;
|
||||
}
|
||||
|
||||
// Inizializza Select2 sulla tendina dei clienti
|
||||
$('#clientSelect').select2({
|
||||
placeholder: "Search for a client...",
|
||||
allowClear: true
|
||||
}).on('select2:open', function() {
|
||||
console.log("Select2 initialized successfully for clientSelect");
|
||||
}).on('select2:select', function(e) {
|
||||
console.log("Client selected:", e.params.data.id, e.params.data.text);
|
||||
});
|
||||
|
||||
// Inizializza Select2 sulla tendina degli schemi
|
||||
$('#schemaSelect').select2({
|
||||
placeholder: "Search for a schema...",
|
||||
allowClear: true
|
||||
}).on('select2:open', function() {
|
||||
console.log("Select2 initialized successfully for schemaSelect");
|
||||
});
|
||||
|
||||
// Funzione per caricare i clienti
|
||||
$('#routineSelect').select2({
|
||||
placeholder: "Select a routine...",
|
||||
allowClear: true
|
||||
});
|
||||
|
||||
async function loadClients() {
|
||||
try {
|
||||
clientLoadingStatus.style.display = 'inline';
|
||||
clientLoadingStatus.textContent = 'Recupero clienti in corso...';
|
||||
|
||||
const response = await fetch("get_clienti.php", {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
});
|
||||
|
||||
const text = await response.text();
|
||||
console.log("Risposta raw (clienti):", text);
|
||||
const data = JSON.parse(text);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(data.error || `Errore HTTP: ${response.status}, Dettagli: ${JSON.stringify(data)}`);
|
||||
}
|
||||
|
||||
if (data.value && Array.isArray(data.value)) {
|
||||
const select = document.getElementById("clientSelect");
|
||||
select.innerHTML = '<option value="">Select a client...</option>';
|
||||
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);
|
||||
select.add(option);
|
||||
});
|
||||
console.log("Clienti caricati con successo.");
|
||||
clientLoadingStatus.textContent = "Clienti caricati.";
|
||||
} else {
|
||||
console.error("Nessun cliente trovato o formato dati non valido.", data);
|
||||
clientLoadingStatus.textContent = "Nessun cliente trovato.";
|
||||
Swal.fire({
|
||||
title: "Errore!",
|
||||
text: "Nessun cliente trovato o formato dati non valido.",
|
||||
icon: "error",
|
||||
confirmButtonText: "OK"
|
||||
});
|
||||
}
|
||||
const data = await response.json();
|
||||
if (!response.ok) throw new Error(data.error || `Errore HTTP: ${response.status}`);
|
||||
const select = document.getElementById("clientSelect");
|
||||
select.innerHTML = '<option value="">Select a client...</option>';
|
||||
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);
|
||||
select.add(option);
|
||||
});
|
||||
$(select).trigger('change');
|
||||
clientLoadingStatus.textContent = "Clienti caricati.";
|
||||
} catch (error) {
|
||||
console.error("Errore nel caricamento dei clienti:", error);
|
||||
clientLoadingStatus.textContent = "Errore nel caricamento.";
|
||||
Swal.fire({
|
||||
title: "Errore!",
|
||||
@ -305,175 +218,110 @@
|
||||
confirmButtonText: "OK"
|
||||
});
|
||||
} finally {
|
||||
setTimeout(() => {
|
||||
clientLoadingStatus.style.display = 'none';
|
||||
}, 2000);
|
||||
setTimeout(() => clientLoadingStatus.style.display = 'none', 2000);
|
||||
}
|
||||
}
|
||||
|
||||
// Funzione per caricare gli schemi con ritentativi
|
||||
async function loadSchemas() {
|
||||
const maxRetries = 3;
|
||||
let attempt = 0;
|
||||
|
||||
while (attempt < maxRetries) {
|
||||
try {
|
||||
schemaLoadingStatus.style.display = 'inline';
|
||||
schemaLoadingStatus.textContent = 'Caricamento schemi in corso...';
|
||||
|
||||
const response = await fetch("get_schemi.php", {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
});
|
||||
|
||||
const text = await response.text();
|
||||
console.log("Risposta raw (schemi):", text);
|
||||
const data = JSON.parse(text);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(data.error || `Errore HTTP: ${response.status}, Dettagli: ${JSON.stringify(data)}`);
|
||||
try {
|
||||
schemaLoadingStatus.style.display = 'inline';
|
||||
schemaLoadingStatus.textContent = 'Caricamento schemi in corso...';
|
||||
const response = await fetch("get_schemi.php", {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
|
||||
const select = document.getElementById("schemaSelect");
|
||||
select.innerHTML = '<option value="">Select a schema...</option>';
|
||||
data.value.forEach(schema => { // Nota: usa data.value per coerenza con il JSON restituito
|
||||
const option = new Option(`${schema.Nome} (ID: ${schema.IdSchemaCustomFields})`, schema.IdSchemaCustomFields);
|
||||
select.add(option);
|
||||
});
|
||||
|
||||
schemaLoadingStatus.textContent = "Schemi caricati.";
|
||||
break; // Esci dal ciclo se la chiamata ha successo
|
||||
} catch (error) {
|
||||
attempt++;
|
||||
console.error(`Tentativo ${attempt} fallito per schemi:`, error);
|
||||
if (attempt === maxRetries) {
|
||||
console.error("Errore finale nel caricamento degli schemi:", error);
|
||||
schemaLoadingStatus.textContent = "Errore nel caricamento.";
|
||||
Swal.fire({
|
||||
title: "Errore!",
|
||||
text: "Impossibile caricare gli schemi: " + error.message,
|
||||
icon: "error",
|
||||
confirmButtonText: "OK"
|
||||
});
|
||||
} else {
|
||||
// Ritardo prima di riprovare
|
||||
await new Promise(resolve => setTimeout(resolve, 1000 * attempt));
|
||||
}
|
||||
} finally {
|
||||
if (attempt === maxRetries || schemaLoadingStatus.textContent === "Schemi caricati.") {
|
||||
setTimeout(() => {
|
||||
schemaLoadingStatus.style.display = 'none';
|
||||
}, 2000);
|
||||
}
|
||||
}
|
||||
});
|
||||
const data = await response.json();
|
||||
if (!response.ok) throw new Error(data.error || `Errore HTTP: ${response.status}`);
|
||||
const select = document.getElementById("schemaSelect");
|
||||
select.innerHTML = '<option value="">Select a schema...</option>';
|
||||
data.value.forEach(schema => {
|
||||
const nome = schema.Nome || "Nome non disponibile";
|
||||
const id = schema.IdSchemaCustomFields || "ID non disponibile";
|
||||
const option = new Option(`${nome.trim()} (ID: ${id})`, id);
|
||||
select.add(option);
|
||||
});
|
||||
$(select).trigger('change');
|
||||
schemaLoadingStatus.textContent = "Schemi caricati.";
|
||||
} catch (error) {
|
||||
schemaLoadingStatus.textContent = "Errore nel caricamento.";
|
||||
Swal.fire({
|
||||
title: "Errore!",
|
||||
text: "Impossibile caricare gli schemi: " + error.message,
|
||||
icon: "error",
|
||||
confirmButtonText: "OK"
|
||||
});
|
||||
} finally {
|
||||
setTimeout(() => schemaLoadingStatus.style.display = 'none', 2000);
|
||||
}
|
||||
}
|
||||
|
||||
// Funzione combinata per caricare i dati in sequenza
|
||||
async function loadData() {
|
||||
try {
|
||||
await loadClients(); // Carica prima i clienti
|
||||
await loadSchemas(); // Poi carica gli schemi
|
||||
await loadClients();
|
||||
await loadSchemas();
|
||||
} catch (error) {
|
||||
console.error("Errore nel caricamento dei dati:", error);
|
||||
}
|
||||
}
|
||||
|
||||
// Avvia il caricamento dei dati
|
||||
loadData();
|
||||
|
||||
// Gestione dinamica dei campi specifici
|
||||
addFieldButton.addEventListener("click", function() {
|
||||
console.log("Add Field button clicked");
|
||||
const fieldCount = container.getElementsByClassName("client-field-row").length;
|
||||
const newField = document.createElement("div");
|
||||
newField.className = "client-field-row mb-2";
|
||||
newField.innerHTML = `
|
||||
<div class="row">
|
||||
<div class="col-md-3">
|
||||
<input type="text" name="specific_fields[${fieldCount}][name]" class="form-control" placeholder="Field Name (e.g., SKU)">
|
||||
</div>
|
||||
<div class="col-md-2">
|
||||
<select name="specific_fields[${fieldCount}][type]" class="form-control" onchange="toggleDropdownValues(this)">
|
||||
<option value="text">Text</option>
|
||||
<option value="dropdown">Dropdown</option>
|
||||
<option value="date">Date</option>
|
||||
<option value="boolean">Yes/No</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="col-md-2 dropdown-values" style="visibility: hidden;">
|
||||
<input type="text" name="specific_fields[${fieldCount}][possible_values]" class="form-control" placeholder="Values (e.g., Red, Blue, Green)">
|
||||
</div>
|
||||
<div class="col-md-1">
|
||||
<select name="specific_fields[${fieldCount}][required]" class="form-control">
|
||||
<option value="1">Yes</option>
|
||||
<option value="0">No</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="col-md-2">
|
||||
<input type="text" name="specific_fields[${fieldCount}][export_column_name]" class="form-control" placeholder="Export Column Name (e.g., MONCLER_SKU)">
|
||||
</div>
|
||||
<div class="col-md-1">
|
||||
<input type="text" name="specific_fields[${fieldCount}][default_value]" class="form-control" placeholder="Default Value (optional)">
|
||||
</div>
|
||||
<div class="col-md-1">
|
||||
<button type="button" class="btn btn-danger remove-field">-</button>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
container.appendChild(newField);
|
||||
|
||||
newField.querySelector(".remove-field").addEventListener("click", function() {
|
||||
console.log("Remove Field button clicked");
|
||||
container.removeChild(newField);
|
||||
updateFieldIndices();
|
||||
});
|
||||
});
|
||||
|
||||
window.toggleDropdownValues = function(selectElement) {
|
||||
console.log("Toggling dropdown values for:", selectElement.value);
|
||||
const row = selectElement.closest(".row");
|
||||
const dropdownValues = row.querySelector(".dropdown-values");
|
||||
if (selectElement.value === "dropdown") {
|
||||
dropdownValues.style.visibility = "visible";
|
||||
} else {
|
||||
dropdownValues.style.visibility = "hidden";
|
||||
}
|
||||
};
|
||||
|
||||
document.querySelectorAll(".remove-field").forEach(button => {
|
||||
button.addEventListener("click", function() {
|
||||
console.log("Existing remove button clicked");
|
||||
const container = document.getElementById("clientSpecificFields");
|
||||
container.removeChild(button.closest(".client-field-row"));
|
||||
updateFieldIndices();
|
||||
});
|
||||
});
|
||||
|
||||
function updateFieldIndices() {
|
||||
console.log("Updating field indices");
|
||||
const rows = container.getElementsByClassName("client-field-row");
|
||||
for (let i = 0; i < rows.length; i++) {
|
||||
const inputs = rows[i].querySelectorAll("input, select");
|
||||
inputs.forEach(input => {
|
||||
const name = input.name.replace(/\[\d+\]/, `[${i}]`);
|
||||
input.name = name;
|
||||
Swal.fire({
|
||||
title: "Errore!",
|
||||
text: "Errore nel caricamento dei dati: " + error.message,
|
||||
icon: "error",
|
||||
confirmButtonText: "OK"
|
||||
});
|
||||
}
|
||||
}
|
||||
loadData();
|
||||
|
||||
const routines = <?php echo json_encode($routines); ?>;
|
||||
|
||||
function updateRoutineDetails() {
|
||||
const selectedId = routineSelect.value;
|
||||
routineDetails.style.display = selectedId ? 'block' : 'none';
|
||||
if (selectedId) {
|
||||
const routine = routines.find(r => r.idroutine == selectedId);
|
||||
if (routine) {
|
||||
routineName.textContent = routine.name || 'N/A';
|
||||
routineDescription.textContent = routine.description || 'N/A';
|
||||
routineAction1.textContent = routine.action1 || 'N/A';
|
||||
routineAction2.textContent = routine.action2 || 'N/A';
|
||||
routineAction3.textContent = routine.action3 || 'N/A';
|
||||
} else {
|
||||
routineName.textContent = 'N/A';
|
||||
routineDescription.textContent = 'N/A';
|
||||
routineAction1.textContent = 'N/A';
|
||||
routineAction2.textContent = 'N/A';
|
||||
routineAction3.textContent = 'N/A';
|
||||
}
|
||||
} else {
|
||||
routineName.textContent = '';
|
||||
routineDescription.textContent = '';
|
||||
routineAction1.textContent = '';
|
||||
routineAction2.textContent = '';
|
||||
routineAction3.textContent = '';
|
||||
}
|
||||
}
|
||||
routineSelect.addEventListener('change', updateRoutineDetails);
|
||||
updateRoutineDetails();
|
||||
|
||||
form.addEventListener("submit", function(e) {
|
||||
e.preventDefault();
|
||||
console.log("Form submitted");
|
||||
|
||||
let formData = new FormData(this);
|
||||
|
||||
// Aggiungi il nome del cliente selezionato a FormData
|
||||
const clientSelect = document.getElementById("clientSelect");
|
||||
const clientId = clientSelect.value;
|
||||
const selectedClientOption = clientSelect.options[clientSelect.selectedIndex];
|
||||
|
||||
if (!clientId) {
|
||||
Swal.fire({
|
||||
title: "Errore!",
|
||||
text: "Per favore seleziona un cliente.",
|
||||
icon: "error",
|
||||
confirmButtonText: "OK"
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
let clientName = "";
|
||||
if (selectedClientOption) {
|
||||
const optionText = selectedClientOption.text.trim();
|
||||
@ -482,18 +330,11 @@
|
||||
}
|
||||
formData.append("client_name", clientName);
|
||||
|
||||
// Aggiungi l'ID e il nome dello schema selezionato a FormData
|
||||
const schemaSelect = document.getElementById("schemaSelect");
|
||||
const schemaId = schemaSelect.value;
|
||||
const selectedSchemaOption = schemaSelect.options[schemaSelect.selectedIndex];
|
||||
let schemaName = "";
|
||||
if (selectedSchemaOption && schemaId) {
|
||||
const optionText = selectedSchemaOption.text.trim();
|
||||
const nameMatch = optionText.match(/^(.+?)(?:\s*\(ID:\s*\d+\))?$/);
|
||||
schemaName = nameMatch ? nameMatch[1].trim() : optionText;
|
||||
formData.append("idschema", schemaId);
|
||||
formData.append("schemamaname", schemaName);
|
||||
} else {
|
||||
|
||||
if (!schemaId) {
|
||||
Swal.fire({
|
||||
title: "Errore!",
|
||||
text: "Per favore seleziona uno schema.",
|
||||
@ -503,48 +344,17 @@
|
||||
return;
|
||||
}
|
||||
|
||||
// Log per debug
|
||||
console.log("Client ID:", clientId);
|
||||
console.log("Client Name:", clientName);
|
||||
console.log("Schema ID:", schemaId);
|
||||
console.log("Schema Name:", schemaName);
|
||||
|
||||
// Genera il JSON per client_specific_fields
|
||||
let finalSpecificFields = {};
|
||||
const fieldRows = container.getElementsByClassName("client-field-row");
|
||||
for (let i = 0; i < fieldRows.length; i++) {
|
||||
const row = fieldRows[i];
|
||||
const inputs = row.querySelectorAll("input, select");
|
||||
let fieldData = {};
|
||||
|
||||
inputs.forEach(input => {
|
||||
const nameMatch = input.name.match(/specific_fields\[\d+\]\[(.*?)\]/);
|
||||
if (nameMatch) {
|
||||
const fieldName = nameMatch[1];
|
||||
fieldData[fieldName] = input.value.trim();
|
||||
}
|
||||
});
|
||||
|
||||
if (fieldData.name) {
|
||||
finalSpecificFields[fieldData.name] = {
|
||||
type: fieldData.type || "text",
|
||||
possible_values: (fieldData.possible_values && fieldData.type === "dropdown") ? fieldData.possible_values.split(",").map(v => v.trim()) : [],
|
||||
is_required: fieldData.required === "1",
|
||||
export_column_name: fieldData.export_column_name || "",
|
||||
default_value: fieldData.default_value || ""
|
||||
};
|
||||
console.log(`Field ${fieldData.name}:`, finalSpecificFields[fieldData.name]);
|
||||
}
|
||||
let schemaName = "";
|
||||
if (selectedSchemaOption) {
|
||||
const optionText = selectedSchemaOption.text.trim();
|
||||
const nameMatch = optionText.match(/^(.+?)(?:\s*\(ID:\s*\d+\))?$/);
|
||||
schemaName = nameMatch ? nameMatch[1].trim() : optionText;
|
||||
}
|
||||
formData.append("idschema", schemaId);
|
||||
formData.append("schemaname", schemaName);
|
||||
|
||||
console.log("Generated JSON for client_specific_fields:", JSON.stringify(finalSpecificFields));
|
||||
formData.append("client_specific_fields", JSON.stringify(finalSpecificFields));
|
||||
|
||||
// Debug del FormData
|
||||
console.log("FormData contents:");
|
||||
for (let pair of formData.entries()) {
|
||||
console.log(pair[0] + ': ' + pair[1]);
|
||||
}
|
||||
const routineId = routineSelect.value;
|
||||
formData.append("idroutine", routineId);
|
||||
|
||||
fetch("process_insert_template_xls.php", {
|
||||
method: "POST",
|
||||
@ -552,7 +362,6 @@
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
console.log("Fetch response:", data);
|
||||
if (data.success) {
|
||||
Swal.fire({
|
||||
title: "Successo!",
|
||||
@ -572,7 +381,6 @@
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error("Errore Fetch:", error);
|
||||
Swal.fire({
|
||||
title: "Errore!",
|
||||
text: "Si è verificato un errore imprevisto.",
|
||||
|
||||
@ -15,71 +15,53 @@ try {
|
||||
$start_column = trim($_POST['start_column']);
|
||||
$description = trim($_POST['description'] ?? '');
|
||||
$target_table = trim($_POST['target_table']);
|
||||
$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');
|
||||
$idclient = intval($_POST['client_id'] ?? 0); // Usa client_id dal form
|
||||
$clientname = trim($_POST['client_name'] ?? ''); // Usa client_name dal form
|
||||
$status = 'active'; // Default
|
||||
|
||||
// Recupera i client_specific_fields (JSON inviato dal form)
|
||||
$client_specific_fields = trim($_POST['client_specific_fields'] ?? '{}');
|
||||
// Decodifica il JSON per verificare che sia valido (opzionale, per sicurezza)
|
||||
$decoded_fields = json_decode($client_specific_fields, true);
|
||||
if (json_last_error() !== JSON_ERROR_NONE && !empty($client_specific_fields)) {
|
||||
throw new Exception("Invalid JSON format for client-specific fields.");
|
||||
}
|
||||
|
||||
// Recupera idschema e schemaname
|
||||
$idschema = intval($_POST['idschema'] ?? 0); // Nuovo campo
|
||||
$schemamaname = trim($_POST['schemamaname'] ?? ''); // Nuovo campo
|
||||
|
||||
// Controllo sui campi obbligatori
|
||||
if (empty($name) || empty($header_row) || empty($start_column) || empty($target_table) || $idschema <= 0) {
|
||||
throw new Exception("All fields marked with * are required, including schema.");
|
||||
}
|
||||
|
||||
// Validazione del idclient
|
||||
if ($idclient <= 0) {
|
||||
throw new Exception("Please select a valid client.");
|
||||
if (empty($name) || empty($header_row) || empty($start_column) || empty($target_table) || $idclient <= 0 || $idschema <= 0) {
|
||||
throw new Exception("All fields marked with * are required, including client and schema.");
|
||||
}
|
||||
|
||||
// Connessione al database
|
||||
$db = DBHandlerSelect::getInstance();
|
||||
$pdo = $db->getConnection();
|
||||
|
||||
// Inserisci nel database, aggiungendo idschema e schemaname
|
||||
$stmt = $pdo->prepare("INSERT INTO excel_templates
|
||||
(name, header_row, start_column, description, target_table, button_size, button_bg_color, button_text_color, button_label, idclient, clientname, status, client_specific_fields, schemaname, idschema, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())");
|
||||
// Inserisci il nuovo template
|
||||
$stmt = $pdo->prepare("
|
||||
INSERT INTO excel_templates
|
||||
(name, header_row, start_column, description, target_table, idclient, clientname, idschema, schemaname, idroutine,
|
||||
button_size, button_bg_color, button_text_color, button_label, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())
|
||||
");
|
||||
$stmt->execute([
|
||||
$name,
|
||||
$header_row,
|
||||
$start_column,
|
||||
$description,
|
||||
$target_table,
|
||||
$idclient,
|
||||
$clientname,
|
||||
$idschema,
|
||||
$schemaname,
|
||||
$idroutine,
|
||||
$button_size,
|
||||
$button_bg_color,
|
||||
$button_text_color,
|
||||
$button_label,
|
||||
$idclient,
|
||||
$clientname,
|
||||
$status,
|
||||
$client_specific_fields,
|
||||
$schemamaname,
|
||||
$idschema
|
||||
$button_label
|
||||
]);
|
||||
|
||||
if ($stmt->rowCount() > 0) {
|
||||
$response["success"] = true;
|
||||
$response["message"] = "Template created successfully!";
|
||||
} else {
|
||||
throw new Exception("Failed to insert template.");
|
||||
}
|
||||
$response["success"] = true;
|
||||
$response["message"] = "Template created successfully!";
|
||||
} catch (Exception $e) {
|
||||
$response["message"] = $e->getMessage();
|
||||
}
|
||||
|
||||
// Restituisce un JSON per il fetch
|
||||
echo json_encode($response);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user