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

95 lines
3.2 KiB
PHP

<?php
require __DIR__ . '/_bootstrap.php';
/**
* @OA\Post(
* path="/document-file-update.php",
* tags={"Documents"},
* summary="Update an uploaded file's metadata",
* description="Partial update: only the fields present in the body are changed.",
* security={{"bearerAuth":{}}},
* @OA\RequestBody(required=true, @OA\JsonContent(
* required={"id"},
* @OA\Property(property="id", type="integer", description="doc_storage.id"),
* @OA\Property(property="expiry_date", type="string", format="date", nullable=true),
* @OA\Property(property="note", type="string", nullable=true),
* @OA\Property(property="title", type="string", nullable=true,
* description="display name; null resets it to the requirement name"),
* @OA\Property(property="document_id", type="integer", description="move file to another requirement")
* )),
* @OA\Response(response=200, description="Updated", @OA\JsonContent(
* @OA\Property(property="data", ref="#/components/schemas/UploadedFile"))),
* @OA\Response(response=403, ref="#/components/responses/Forbidden"),
* @OA\Response(response=404, ref="#/components/responses/NotFound"),
* @OA\Response(response=422, ref="#/components/responses/ValidationError")
* )
*/
require_method('POST');
$user = require_auth($pdo);
$in = body();
$id = (int) ($in['id'] ?? 0);
if ($id <= 0) {
json_error(422, 'id is required');
}
$stmt = $pdo->prepare('SELECT * FROM doc_storage WHERE id = ? LIMIT 1');
$stmt->execute([$id]);
$file = $stmt->fetch();
if (!$file) {
json_error(404, 'File not found');
}
$allowed = false;
if (!empty($file['idhome'])) {
$allowed = user_owns_home($pdo, $user, (int) $file['idhome']);
} elseif (!empty($file['owner_id'])) {
$allowed = user_owns_owner($pdo, $user, (int) $file['owner_id']);
}
if (!$allowed) {
json_error(403, 'No access to this file');
}
$data = [];
if (array_key_exists('expiry_date', $in)) {
$expiry = $in['expiry_date'] !== '' ? $in['expiry_date'] : null;
$data['expirydate'] = $expiry;
$data['expirystatus'] = $expiry ? 1 : 0;
}
if (array_key_exists('note', $in)) {
$data['note'] = $in['note'] !== '' ? $in['note'] : null;
}
// null or "" resets the display name back to the requirement's name.
if (array_key_exists('title', $in)) {
$data['title'] = ($in['title'] !== null && $in['title'] !== '') ? $in['title'] : null;
}
if (array_key_exists('document_id', $in)) {
$documentId = (int) $in['document_id'];
if ($documentId <= 0) {
json_error(422, 'Validation failed', ['document_id' => ['Must be a positive integer']]);
}
$exists = $pdo->prepare('SELECT 1 FROM documents WHERE document_id = ? LIMIT 1');
$exists->execute([$documentId]);
if (!$exists->fetchColumn()) {
json_error(422, 'Validation failed', ['document_id' => ['Unknown document']]);
}
$data['document_id'] = $documentId;
}
if (!$data) {
json_error(422, 'Nothing to update');
}
$set = implode(', ', array_map(fn ($c) => "$c = ?", array_keys($data)));
$pdo->prepare("UPDATE doc_storage SET $set WHERE id = ?")
->execute([...array_values($data), $id]);
$row = $pdo->prepare('SELECT * FROM doc_storage WHERE id = ? LIMIT 1');
$row->execute([$id]);
json_data(present_file($row->fetch()));