update shared homes and documents section
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
<?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 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
|
||||
$queryDocuments = $conn->prepare("
|
||||
SELECT d.*, s.section_name AS section_name
|
||||
FROM documents d
|
||||
LEFT JOIN sections s ON d.idsections = s.idsections
|
||||
WHERE d.page_id = ?
|
||||
ORDER BY s.section_name, d.document_name
|
||||
");
|
||||
$queryDocuments->bind_param('i', $page_id);
|
||||
$queryDocuments->execute();
|
||||
$resultDocuments = $queryDocuments->get_result();
|
||||
|
||||
$documents = [];
|
||||
while ($row = $resultDocuments->fetch_assoc()) {
|
||||
$documents[$row['section_name']][] = $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;
|
||||
Reference in New Issue
Block a user