93 lines
2.9 KiB
PHP
93 lines
2.9 KiB
PHP
<?php
|
|
include('include/headscript.php');
|
|
|
|
// Connessione al database
|
|
$conn = new mysqli($servername, $username, $password, $database);
|
|
|
|
// Recupera l'id utente loggato
|
|
$iduserlogin = $_SESSION['iduserlogin'];
|
|
|
|
// Recupera l'id della casa dall'URL
|
|
$idhome = isset($_GET['idhome']) ? intval($_GET['idhome']) : 0;
|
|
|
|
// Recupera lo slug dalla richiesta
|
|
$docpage = isset($_GET['slug']) ? $_GET['slug'] : 'legal';
|
|
|
|
// Recupera il parametro showOnlyRequired
|
|
$showOnlyRequired = isset($_GET['showOnlyRequired']) && $_GET['showOnlyRequired'] === 'true';
|
|
|
|
// Recupera i dettagli della casa (per verifica accesso)
|
|
$queryHome = $conn->prepare("SELECT * FROM home WHERE idhome = ? AND iduser = ?");
|
|
$queryHome->bind_param('ii', $idhome, $iduserlogin);
|
|
$queryHome->execute();
|
|
$resultHome = $queryHome->get_result();
|
|
$homeData = $resultHome->fetch_assoc();
|
|
|
|
if (!$homeData) {
|
|
header('HTTP/1.1 400 Bad Request');
|
|
die(json_encode(['error' => 'Casa non trovata o accesso non autorizzato']));
|
|
}
|
|
|
|
// Recupera il `page_id` corrispondente allo slug
|
|
$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'];
|
|
|
|
// Recupera i documenti associati al `page_id`, con le sezioni
|
|
$sql = "
|
|
SELECT d.*, s.section_name AS section_name
|
|
FROM documents d
|
|
LEFT JOIN sections s ON d.idsections = s.idsections
|
|
WHERE d.page_id = ?
|
|
";
|
|
if ($showOnlyRequired) {
|
|
$sql .= " AND d.is_required = 1";
|
|
}
|
|
$sql .= " ORDER BY s.section_name, d.document_name";
|
|
|
|
$queryDocuments = $conn->prepare($sql);
|
|
$queryDocuments->bind_param('i', $page_id);
|
|
$queryDocuments->execute();
|
|
$resultDocuments = $queryDocuments->get_result();
|
|
|
|
$documents = [];
|
|
while ($row = $resultDocuments->fetch_assoc()) {
|
|
$sectionName = $row['section_name'] ?: 'Senza sezione';
|
|
$documents[$sectionName][] = $row;
|
|
}
|
|
|
|
// Recupera i documenti già caricati per questa casa
|
|
$queryLoadedDocuments = $conn->prepare("SELECT * FROM doc_storage WHERE idhome = ?");
|
|
$queryLoadedDocuments->bind_param('i', $idhome);
|
|
$queryLoadedDocuments->execute();
|
|
$resultLoadedDocuments = $queryLoadedDocuments->get_result();
|
|
|
|
$loadedDocuments = [];
|
|
while ($row = $resultLoadedDocuments->fetch_assoc()) {
|
|
$loadedDocuments[$row['document_id']][] = $row;
|
|
}
|
|
|
|
// Prepara la risposta JSON
|
|
$response = [
|
|
'homeName' => $homeData['name'] ?? '',
|
|
'homeAddress' => ($homeData['address'] ?? '') . ', ' . ($homeData['city'] ?? '') . ' ' . ($homeData['zip'] ?? ''),
|
|
'documents' => $documents,
|
|
'loadedDocuments' => $loadedDocuments
|
|
];
|
|
|
|
// Imposta l'header per indicare JSON
|
|
header('Content-Type: application/json');
|
|
|
|
// Evita output extra
|
|
echo json_encode($response, JSON_PRETTY_PRINT);
|
|
exit;
|