prepare('SELECT * FROM home WHERE idhome = ? LIMIT 1'); $homeStmt->execute([$idhome]); $home = $homeStmt->fetch(); if (!$home) { json_error(404, 'Home not found'); } // Category page by slug. $pageStmt = $pdo->prepare('SELECT idpages FROM pages WHERE slug = ? LIMIT 1'); $pageStmt->execute([$slug]); $pageId = $pageStmt->fetchColumn(); if ($pageId === false) { json_error(404, 'Page not found'); } // Requirements with section. $sql = 'SELECT d.*, s.section_name FROM documents d LEFT JOIN sections s ON s.idsections = d.idsections WHERE d.page_id = ?'; $params = [$pageId]; if (query('only_required') === 'true' || query('only_required') === '1') { $sql .= ' AND d.is_required = 1'; } if (($sectionId = (int) query('section_id', 0)) > 0) { $sql .= ' AND d.idsections = ?'; $params[] = $sectionId; } $sql .= ' ORDER BY s.section_name, d.document_name'; $docStmt = $pdo->prepare($sql); $docStmt->execute($params); $documents = $docStmt->fetchAll(); // Uploaded files for this home, grouped by document_id. $filesStmt = $pdo->prepare('SELECT * FROM doc_storage WHERE idhome = ?'); $filesStmt->execute([$idhome]); $filesByDoc = []; foreach ($filesStmt->fetchAll() as $f) { $filesByDoc[(int) $f['document_id']][] = present_file($f); } $result = array_map(function ($d) use ($filesByDoc) { $id = (int) $d['document_id']; return [ 'document_id' => $id, 'document_name' => $d['document_name'], 'page_id' => isset($d['page_id']) ? (int) $d['page_id'] : null, 'idsections' => isset($d['idsections']) ? (int) $d['idsections'] : null, 'section_name' => $d['section_name'] ?? null, 'max_documents' => (int) ($d['max_documents'] ?? 0), 'is_required' => (bool) ($d['is_required'] ?? 0), 'notes' => $d['notes'] ?? null, 'files' => $filesByDoc[$id] ?? [], ]; }, $documents); http_response_code(200); echo json_encode([ 'home' => present_home($home, (int) $home['iduser'] === (int) $user['id'], home_counts($pdo, [$idhome])[$idhome] ?? null), 'data' => $result, ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);