fixed tags and added phginx

This commit is contained in:
2026-09-21 11:22:45 +02:00
parent 9241e177ac
commit 624d94742a
5 changed files with 870 additions and 45 deletions
+57 -23
View File
@@ -4,10 +4,15 @@ include('include/headscript.php');
$conn = new mysqli($servername, $username, $password, $database);
$iduserlogin = $_SESSION['iduserlogin'];
$idhome = isset($_GET['idhome']) ? intval($_GET['idhome']) : 0;
$docpage = isset($_GET['slug']) ? $_GET['slug'] : 'legal'; // Slug principale per la pagina
$subSlug = isset($_GET['subSlug']) ? $_GET['subSlug'] : null; // Slug aggiuntivo opzionale
$docpage = isset($_GET['slug']) ? $_GET['slug'] : '';
$showOnlyRequired = isset($_GET['showOnlyRequired']) && $_GET['showOnlyRequired'] === 'true';
$sectionId = isset($_GET['sectionId']) ? $_GET['sectionId'] : null;
$sectionId = (isset($_GET['sectionId']) && $_GET['sectionId'] !== '' && $_GET['sectionId'] !== 'null') ? $_GET['sectionId'] : null;
$searchName = (isset($_GET['searchName']) && $_GET['searchName'] !== '' && $_GET['searchName'] !== 'null') ? $_GET['searchName'] : null;
// Tag: lista di ID separati da virgola
$tagIds = [];
if (isset($_GET['tags']) && $_GET['tags'] !== '' && $_GET['tags'] !== 'null') {
$tagIds = array_values(array_filter(array_map('intval', explode(',', $_GET['tags']))));
}
$queryHome = $conn->prepare("SELECT * FROM home WHERE idhome = ? AND iduser = ?");
$queryHome->bind_param('ii', $idhome, $iduserlogin);
@@ -20,27 +25,42 @@ if (!$homeData) {
die(json_encode(['error' => 'Casa non trovata o accesso non autorizzato']));
}
$queryPageId = $conn->prepare("SELECT idpages FROM pages WHERE slug = ?");
$queryPageId->bind_param('s', $docpage);
$queryPageId->execute();
$resultPageId = $queryPageId->get_result();
$pageData = $resultPageId->fetch_assoc();
// Modalità trasversale: se ci sono tag selezionati, si ignora la categoria
$crossCutting = !empty($tagIds);
if (!$pageData) {
header('HTTP/1.1 400 Bad Request');
die(json_encode(['error' => 'Pagina non valida']));
$page_id = null;
if (!$crossCutting) {
$queryPageId = $conn->prepare("SELECT idpages FROM pages WHERE slug = ?");
$queryPageId->bind_param('s', $docpage);
$queryPageId->execute();
$resultPageId = $queryPageId->get_result();
$pageData = $resultPageId->fetch_assoc();
if (!$pageData) {
header('HTTP/1.1 400 Bad Request');
die(json_encode(['error' => 'Pagina non valida']));
}
$page_id = $pageData['idpages'];
}
$page_id = $pageData['idpages'];
// Costruisci la query per i documenti
$sql = "
SELECT d.*, s.section_name AS section_name
SELECT d.*, s.section_name AS section_name,
GROUP_CONCAT(DISTINCT t.tag_name ORDER BY t.tag_name SEPARATOR ', ') AS tags
FROM documents d
LEFT JOIN sections s ON d.idsections = s.idsections
WHERE d.page_id = ?
LEFT JOIN document_tags dt ON dt.document_id = d.document_id
LEFT JOIN tags t ON t.tag_id = dt.tag_id
WHERE 1=1
";
$params = [$page_id];
$params = [];
$types = '';
if (!$crossCutting) {
$sql .= " AND d.page_id = ?";
$params[] = $page_id;
$types .= 'i';
}
if ($showOnlyRequired) {
$sql .= " AND d.is_required = 1";
@@ -49,19 +69,33 @@ if ($showOnlyRequired) {
if ($sectionId) {
$sql .= " AND md5(s.section_name) = ?";
$params[] = $sectionId;
$types .= 's';
}
// Aggiungi filtro per subSlug (assumendo che i documenti abbiano un campo slug o un riferimento)
if ($subSlug) {
$sql .= " AND d.slug = ?"; // Assumo che 'documents' abbia un campo 'slug' per il filtraggio
$params[] = $subSlug;
if ($searchName) {
$sql .= " AND d.document_name = ?";
$params[] = $searchName;
$types .= 's';
}
$sql .= " ORDER BY s.section_name, d.document_name";
// Filtro per tag (OR): il documento deve avere ALMENO UNO dei tag selezionati
if (!empty($tagIds)) {
$placeholders = implode(',', array_fill(0, count($tagIds), '?'));
$sql .= " AND d.document_id IN (
SELECT document_id FROM document_tags
WHERE tag_id IN ($placeholders)
)";
foreach ($tagIds as $tid) {
$params[] = $tid;
$types .= 'i';
}
}
$sql .= " GROUP BY d.document_id ORDER BY s.section_name, d.document_name";
$queryDocuments = $conn->prepare($sql);
$types = str_repeat('s', count($params) - 1) . 'i'; // 's' per stringhe, 'i' per page_id
$queryDocuments->bind_param($types, ...$params);
if (!empty($params)) {
$queryDocuments->bind_param($types, ...$params);
}
$queryDocuments->execute();
$resultDocuments = $queryDocuments->get_result();
+17 -19
View File
@@ -21,7 +21,7 @@ while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
}
// Imposta lo slug predefinito
$docpage = isset($_GET['docpage']) ? $_GET['docpage'] : 'legal';
$docpage = isset($_GET['docpage']) ? $_GET['docpage'] : '';
// Recupera tutti i tag disponibili
$stmt = $pdo->query("SELECT tag_id, tag_name FROM tags ORDER BY tag_name");
@@ -95,7 +95,7 @@ $allTags = $stmt->fetchAll(PDO::FETCH_ASSOC);
<div class="col-md-6 position-relative mt-3">
<label class="form-label mb-1 fw-bold">Filtra per tag:</label>
<div class="dropdown">
<button class="btn btn-outline-secondary dropdown-toggle w-100" type="button" id="tagDropdown" data-bs-toggle="dropdown" aria-expanded="false">
<button class="btn btn-outline-secondary dropdown-toggle w-100" type="button" id="tagDropdown" data-toggle="dropdown" aria-expanded="false">
Seleziona tag...
</button>
<div class="dropdown-menu w-100" aria-labelledby="tagDropdown">
@@ -408,11 +408,9 @@ $allTags = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Gestione del checkbox "Solo obbligatori"
$("#showOnlyRequired").on('change', function() {
if (currentSection) {
const sectionName = $('.section-icon.active .section-name').text();
$('.document-preloader').show();
loadDocuments(currentCategory, currentSection, sectionName);
}
const sectionName = $('.section-icon.active .section-name').text() || null;
$('.document-preloader').show();
loadDocuments(currentCategory, currentSection, sectionName);
});
// Carica le sezioni per una categoria
@@ -531,11 +529,9 @@ $allTags = $stmt->fetchAll(PDO::FETCH_ASSOC);
name: tagName
});
updateSelectedTags();
if (currentSection) {
const sectionName = $('.section-icon.active .section-name').text();
$('.document-preloader').show();
loadDocuments(currentCategory, currentSection, sectionName);
}
$('#documentSections').empty();
$('.document-preloader').show();
loadDocuments(currentCategory, null, null);
}
});
@@ -555,11 +551,9 @@ $allTags = $stmt->fetchAll(PDO::FETCH_ASSOC);
const tagId = $(this).data('tag-id');
selectedTags = selectedTags.filter(t => t.id !== tagId);
updateSelectedTags();
if (currentSection) {
const sectionName = $('.section-icon.active .section-name').text();
$('.document-preloader').show();
loadDocuments(currentCategory, currentSection, sectionName);
}
$('#documentSections').empty();
$('.document-preloader').show();
loadDocuments(currentCategory, null, null);
});
}
@@ -620,8 +614,12 @@ $allTags = $stmt->fetchAll(PDO::FETCH_ASSOC);
html += `
<div class="accordion-item">
<h2 class="accordion-header" id="heading-${md5(sectionKey)}"></h2>
<div id="collapse-${md5(sectionKey)}" class="accordion-collapse collapse" aria-labelledby="heading-${md5(sectionKey)}" data-parent="#documentSections">
<h2 class="accordion-header" id="heading-${md5(sectionKey)}">
<button class="btn btn-link text-start w-100 fw-bold text-decoration-none" type="button" data-toggle="collapse" data-target="#collapse-${md5(sectionKey)}" aria-expanded="true">
${displaySectionName}
</button>
</h2>
<div id="collapse-${md5(sectionKey)}" class="accordion-collapse collapse show" aria-labelledby="heading-${md5(sectionKey)}" data-parent="#documentSections">
<div class="accordion-body">
`;