501 lines
27 KiB
PHP
501 lines
27 KiB
PHP
<?php include('include/headscript.php');
|
|
|
|
// Controlla se è stato passato un ID valido
|
|
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
|
|
header("Location: xlstemplates_grid.php?status=error&message=" . urlencode("Invalid ID"));
|
|
exit;
|
|
}
|
|
|
|
$id = intval($_GET['id']); // Sanifica l'ID
|
|
|
|
// Recupera il template dal database
|
|
$db = DBHandlerSelect::getInstance();
|
|
$pdo = $db->getConnection();
|
|
$stmt = $pdo->prepare("SELECT * FROM excel_templates WHERE id = ?");
|
|
$stmt->execute([$id]);
|
|
$template = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (!$template) {
|
|
header("Location: template_dashboard.php?status=error&message=" . urlencode("Template not found"));
|
|
exit;
|
|
}
|
|
|
|
// Debug del JSON
|
|
$clientSpecificFieldsJson = $template['client_specific_fields'] ?? '{}';
|
|
error_log("Raw client_specific_fields JSON: " . $clientSpecificFieldsJson);
|
|
|
|
$clientSpecificFields = json_decode($clientSpecificFieldsJson, true);
|
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
|
error_log("JSON decode error: " . json_last_error_msg());
|
|
$clientSpecificFields = [];
|
|
} else {
|
|
error_log("Decoded client_specific_fields: " . print_r($clientSpecificFields, true));
|
|
}
|
|
?>
|
|
<!doctype html>
|
|
<html lang="en">
|
|
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<link rel="icon" href="assets/images/favicon-32x32.png" type="image/png" />
|
|
<?php include('cssinclude.php'); ?>
|
|
<style>
|
|
.client-field-row .row {
|
|
margin-bottom: 0 !important;
|
|
display: flex;
|
|
align-items: center;
|
|
}
|
|
|
|
.client-field-row .col-md-1,
|
|
.client-field-row .col-md-2,
|
|
.client-field-row .col-md-3 {
|
|
padding: 0 5px;
|
|
overflow: hidden;
|
|
max-width: 100%;
|
|
flex: 0 0 auto;
|
|
}
|
|
|
|
.client-field-row input,
|
|
.client-field-row select {
|
|
width: 100%;
|
|
box-sizing: border-box;
|
|
}
|
|
</style>
|
|
<title>Edit Template <?= htmlspecialchars($titlewebsite, ENT_QUOTES, 'UTF-8'); ?></title>
|
|
</head>
|
|
|
|
<body>
|
|
<div class="wrapper">
|
|
<?php include('include/navbar.php'); ?>
|
|
<?php include('include/topbar.php'); ?>
|
|
<div class="page-wrapper">
|
|
<div class="page-content">
|
|
<div class="row row-cols-1 row-cols-md-2 row-cols-xl-4">
|
|
<div class="col">
|
|
<div class="card radius-10 border-start border-0 border-4 border-info">
|
|
<div class="card-body">
|
|
<div class="d-flex align-items-center">
|
|
<div>
|
|
<p class="mb-0 text-secondary">Total Orders</p>
|
|
<h4 class="my-1 text-info">4805</h4>
|
|
<p class="mb-0 font-13">+2.5% from last week</p>
|
|
</div>
|
|
<div class="widgets-icons-2 rounded-circle bg-gradient-blues text-white ms-auto"><i class='bx bxs-cart'></i></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="col">
|
|
<div class="card radius-10 border-start border-0 border-4 border-danger">
|
|
<div class="card-body">
|
|
<div class="d-flex align-items-center">
|
|
<div>
|
|
<p class="mb-0 text-secondary">Total Revenue</p>
|
|
<h4 class="my-1 text-danger">$84,245</h4>
|
|
<p class="mb-0 font-13">+5.4% from last week</p>
|
|
</div>
|
|
<div class="widgets-icons-2 rounded-circle bg-gradient-burning text-white ms-auto"><i class='bx bxs-wallet'></i></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="col">
|
|
<div class="card radius-10 border-start border-0 border-4 border-success">
|
|
<div class="card-body">
|
|
<div class="d-flex align-items-center">
|
|
<div>
|
|
<p class="mb-0 text-secondary">Bounce Rate</p>
|
|
<h4 class="my-1 text-success">34.6%</h4>
|
|
<p class="mb-0 font-13">-4.5% from last week</p>
|
|
</div>
|
|
<div class="widgets-icons-2 rounded-circle bg-gradient-ohhappiness text-white ms-auto"><i class='bx bxs-bar-chart-alt-2'></i></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="col">
|
|
<div class="card radius-10 border-start border-0 border-4 border-warning">
|
|
<div class="card-body">
|
|
<div class="d-flex align-items-center">
|
|
<div>
|
|
<p class="mb-0 text-secondary">Total Customers</p>
|
|
<h4 class="my-1 text-warning">8.4K</h4>
|
|
<p class="mb-0 font-13">+8.4% from last week</p>
|
|
</div>
|
|
<div class="widgets-icons-2 rounded-circle bg-gradient-orange text-white ms-auto"><i class='bx bxs-group'></i></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="card radius-10">
|
|
<div class="card-header">
|
|
<div class="d-flex align-items-center">
|
|
<div>
|
|
<h6 class="mb-0">Edit Template: <?php echo htmlspecialchars($template['name']); ?></h6>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="card-body">
|
|
<div class="col-12">
|
|
<form id="editTemplateForm" method="POST">
|
|
<input type="hidden" name="id" value="<?php echo $template['id']; ?>">
|
|
|
|
<div class="mb-3">
|
|
<label class="form-label"><?= htmlspecialchars($templatename, ENT_QUOTES, 'UTF-8'); ?> *</label>
|
|
<input type="text" name="name" class="form-control" value="<?php echo htmlspecialchars($template['name']); ?>" required>
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
<label class="form-label"><?= htmlspecialchars($rowheader, ENT_QUOTES, 'UTF-8'); ?> *</label>
|
|
<input type="number" name="header_row" class="form-control" value="<?php echo $template['header_row']; ?>" required>
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
<label class="form-label"><?= htmlspecialchars($columnheader, ENT_QUOTES, 'UTF-8'); ?>*</label>
|
|
<input type="text" name="start_column" class="form-control" value="<?php echo htmlspecialchars($template['start_column']); ?>" required>
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
<label class="form-label"><?= htmlspecialchars($desctemplate, ENT_QUOTES, 'UTF-8'); ?></label>
|
|
<textarea name="description" class="form-control"><?php echo htmlspecialchars($template['description']); ?></textarea>
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
<label class="form-label"><?= htmlspecialchars($desttable, ENT_QUOTES, 'UTF-8'); ?>*</label>
|
|
<input type="text" name="target_table" class="form-control" value="<?php echo htmlspecialchars($template['target_table']); ?>" required>
|
|
</div>
|
|
|
|
<!-- Sezione per i campi specifici del cliente -->
|
|
<div class="mb-3">
|
|
<label class="form-label">Client-Specific Fields</label>
|
|
<div id="clientSpecificFields">
|
|
<?php
|
|
$index = 0;
|
|
if (!empty($clientSpecificFields) && is_array($clientSpecificFields)) {
|
|
foreach ($clientSpecificFields as $fieldName => $fieldData) {
|
|
if (is_array($fieldData)) {
|
|
$type = $fieldData['type'] ?? 'text';
|
|
$possibleValues = implode(', ', $fieldData['possible_values'] ?? []);
|
|
$isRequired = isset($fieldData['is_required']) && $fieldData['is_required'] ? '1' : '0';
|
|
$exportColumnName = $fieldData['export_column_name'] ?? '';
|
|
$defaultValue = $fieldData['default_value'] ?? '';
|
|
error_log("Rendering field: $fieldName, type: $type, possible_values: $possibleValues, required: $isRequired, export: $exportColumnName, default: $defaultValue");
|
|
?>
|
|
<div class="client-field-row mb-2">
|
|
<div class="row align-items-center">
|
|
<div class="col-md-3">
|
|
<input type="text" name="specific_fields[<?php echo $index; ?>][name]" class="form-control" value="<?php echo htmlspecialchars($fieldName); ?>" placeholder="Field Name (e.g., SKU)">
|
|
</div>
|
|
<div class="col-md-2">
|
|
<select name="specific_fields[<?php echo $index; ?>][type]" class="form-control" onchange="toggleDropdownValues(this)">
|
|
<option value="text" <?php echo $type === 'text' ? 'selected' : ''; ?>>Text</option>
|
|
<option value="dropdown" <?php echo $type === 'dropdown' ? 'selected' : ''; ?>>Dropdown</option>
|
|
<option value="date" <?php echo $type === 'date' ? 'selected' : ''; ?>>Date</option>
|
|
<option value="boolean" <?php echo $type === 'boolean' ? 'selected' : ''; ?>>Yes/No</option>
|
|
</select>
|
|
</div>
|
|
<div class="col-md-2 dropdown-values" style="display: <?php echo $type === 'dropdown' ? 'block' : 'none'; ?>;">
|
|
<input type="text" name="specific_fields[<?php echo $index; ?>][possible_values]" class="form-control" value="<?php echo htmlspecialchars($possibleValues); ?>" placeholder="<?php echo !empty($fieldData['possible_values']) ? htmlspecialchars($possibleValues) : ''; ?>">
|
|
</div>
|
|
<div class="col-md-1">
|
|
<select name="specific_fields[<?php echo $index; ?>][required]" class="form-control">
|
|
<option value="1" <?php echo $isRequired === '1' ? 'selected' : ''; ?>>Yes</option>
|
|
<option value="0" <?php echo $isRequired === '0' ? 'selected' : ''; ?>>No</option>
|
|
</select>
|
|
</div>
|
|
<div class="col-md-2">
|
|
<input type="text" name="specific_fields[<?php echo $index; ?>][export_column_name]" class="form-control" value="<?php echo htmlspecialchars($exportColumnName); ?>" placeholder="Export Column Name (e.g., MONCLER_SKU)">
|
|
</div>
|
|
<div class="col-md-1">
|
|
<input type="text" name="specific_fields[<?php echo $index; ?>][default_value]" class="form-control" value="<?php echo htmlspecialchars($defaultValue); ?>" placeholder="Default Value (optional)">
|
|
</div>
|
|
<div class="col-md-1">
|
|
<button type="button" class="btn btn-danger remove-field">-</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php
|
|
$index++;
|
|
} else {
|
|
error_log("Invalid field data for field name: $fieldName, skipping...");
|
|
}
|
|
}
|
|
} else {
|
|
error_log("No client-specific fields found or invalid array for template ID: $id");
|
|
}
|
|
?>
|
|
</div>
|
|
<button type="button" class="btn btn-primary mt-2" id="addField">Add Field</button>
|
|
</div>
|
|
|
|
<br>
|
|
<button type="submit" class="btn btn-primary"><?= htmlspecialchars($savechanges, ENT_QUOTES, 'UTF-8'); ?></button>
|
|
<a href="templates_dashboard.php" class="btn btn-secondary">Cancel</a>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</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-->
|
|
<!-- Temporaneamente disabilitato jsinclude.php per test -->
|
|
<!-- <?php include('jsinclude.php'); ?> -->
|
|
<script>
|
|
document.addEventListener("DOMContentLoaded", function() {
|
|
const form = document.getElementById("editTemplateForm");
|
|
const addFieldButton = document.getElementById("addField");
|
|
const container = document.getElementById("clientSpecificFields");
|
|
|
|
if (!form || !addFieldButton || !container) {
|
|
console.error("One or more DOM elements not found:", {
|
|
form,
|
|
addFieldButton,
|
|
container
|
|
});
|
|
return;
|
|
}
|
|
|
|
console.log("All DOM elements found");
|
|
|
|
// Debug iniziale del DOM
|
|
const debugDom = () => {
|
|
const initialRows = container.getElementsByClassName("client-field-row");
|
|
console.log("Initial number of rows:", initialRows.length);
|
|
for (let i = 0; i < initialRows.length; i++) {
|
|
const inputs = initialRows[i].querySelectorAll("input, select");
|
|
const buttons = initialRows[i].querySelectorAll("button");
|
|
console.log(`Row ${i + 1} - Total inputs: ${inputs.length}, Total buttons: ${buttons.length}`);
|
|
inputs.forEach(input => console.log(`Input name: ${input.name}, value: ${input.value}, placeholder: ${input.placeholder}`));
|
|
buttons.forEach(button => console.log(`Button type: ${button.type}`));
|
|
}
|
|
};
|
|
|
|
debugDom();
|
|
|
|
// Pulizia del DOM da input extra
|
|
const cleanDom = () => {
|
|
const rows = container.getElementsByClassName("client-field-row");
|
|
for (let i = 0; i < rows.length; i++) {
|
|
const inputs = rows[i].querySelectorAll("input, select");
|
|
if (inputs.length > 6) { // 6 input/select attesi
|
|
console.warn(`Row ${i + 1}: Extra inputs detected, removing excess...`);
|
|
inputs.forEach((input, index) => {
|
|
if (index >= 6) {
|
|
console.log(`Removing extra input: ${input.name}`);
|
|
input.remove();
|
|
}
|
|
});
|
|
}
|
|
}
|
|
};
|
|
|
|
cleanDom();
|
|
|
|
// Osservatore del DOM per rilevare modifiche
|
|
const observer = new MutationObserver((mutations) => {
|
|
mutations.forEach(mutation => {
|
|
if (mutation.addedNodes.length) {
|
|
console.log("DOM modified: New nodes added", mutation.addedNodes);
|
|
cleanDom(); // Pulisce il DOM ogni volta che viene modificato
|
|
}
|
|
});
|
|
});
|
|
|
|
observer.observe(container, {
|
|
childList: true,
|
|
subtree: true
|
|
});
|
|
|
|
// 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 align-items-center">
|
|
<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 d-none dropdown-values" style="display: none;">
|
|
<input type="text" name="specific_fields[${fieldCount}][possible_values]" class="form-control" placeholder="">
|
|
</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();
|
|
});
|
|
});
|
|
|
|
// Funzione per mostrare/nascondere il campo dei valori possibili per i dropdown
|
|
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.display = "block";
|
|
} else {
|
|
dropdownValues.style.display = "none";
|
|
}
|
|
};
|
|
|
|
// Event listener per i pulsanti di rimozione esistenti
|
|
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();
|
|
});
|
|
});
|
|
|
|
// Funzione per aggiornare gli indici dei campi
|
|
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;
|
|
});
|
|
}
|
|
}
|
|
|
|
// Submit del form
|
|
form.addEventListener("submit", function(e) {
|
|
e.preventDefault();
|
|
console.log("Form submitted");
|
|
|
|
let formData = new FormData(this);
|
|
|
|
// Genera il JSON per client_specific_fields
|
|
let finalSpecificFields = {};
|
|
|
|
// Raccolta dei dati direttamente dal DOM
|
|
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]);
|
|
}
|
|
}
|
|
|
|
console.log("Generated JSON for client_specific_fields:", JSON.stringify(finalSpecificFields));
|
|
|
|
// Aggiungi il JSON al FormData
|
|
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]);
|
|
}
|
|
|
|
fetch("process_edit_template_xls.php", {
|
|
method: "POST",
|
|
body: formData
|
|
})
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
console.log("Fetch response:", data);
|
|
if (data.success) {
|
|
Swal.fire({
|
|
title: "Success!",
|
|
text: "Template updated successfully!",
|
|
icon: "success",
|
|
confirmButtonText: "OK"
|
|
}).then(() => {
|
|
window.location.href = "templates_dashboard.php";
|
|
});
|
|
} else {
|
|
Swal.fire({
|
|
title: "Error!",
|
|
text: data.message,
|
|
icon: "error",
|
|
confirmButtonText: "OK"
|
|
});
|
|
}
|
|
})
|
|
.catch(error => {
|
|
console.error("Fetch error:", error);
|
|
Swal.fire({
|
|
title: "Error!",
|
|
text: "An unexpected error occurred.",
|
|
icon: "error",
|
|
confirmButtonText: "OK"
|
|
});
|
|
});
|
|
});
|
|
});
|
|
</script>
|
|
</body>
|
|
|
|
</html>
|