Files
casadoc/public/userportal/api/documents.php
T
2026-07-27 21:04:07 +03:00

98 lines
3.3 KiB
PHP

<?php
require __DIR__ . '/_bootstrap.php';
/**
* @OA\Get(
* path="/documents.php",
* tags={"Documents"},
* summary="Home document requirements with uploaded files",
* security={{"bearerAuth":{}}},
* @OA\Parameter(name="idhome", in="query", required=true, @OA\Schema(type="integer")),
* @OA\Parameter(name="slug", in="query", @OA\Schema(type="string", default="legal")),
* @OA\Parameter(name="section_id", in="query", @OA\Schema(type="integer")),
* @OA\Parameter(name="only_required", in="query", @OA\Schema(type="boolean")),
* @OA\Response(response=200, description="OK", @OA\JsonContent(
* @OA\Property(property="home", ref="#/components/schemas/Home"),
* @OA\Property(property="data", type="array",
* @OA\Items(ref="#/components/schemas/DocumentRequirement"))
* )),
* @OA\Response(response=403, ref="#/components/responses/Forbidden")
* )
*/
require_method('GET');
$user = require_auth($pdo);
$idhome = (int) query('idhome', 0);
$slug = (string) query('slug', 'legal');
if ($idhome <= 0) {
json_error(422, 'idhome is required');
}
if (!user_can_access_home($pdo, $user, $idhome)) {
json_error(403, 'No access to this home');
}
$homeStmt = $pdo->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);