added note detail view

This commit is contained in:
Felix Ivo 2025-06-16 15:11:33 +02:00
parent a4d6aeea18
commit b06536baf6
4 changed files with 70 additions and 1 deletions

View File

@ -28,4 +28,12 @@ class NotesController
]);
}
public function showNoteDetails()
{
$noteId = $_GET['id'];
$note = $this->notesModel->getNoteById($noteId);
$this->view->setVars([
"note" => $note
]);
}
}

View File

@ -43,4 +43,22 @@ class NotesModel extends Database
return false;
}
}
function getNoteById($noteId) {
$pdo = $this->linkDB();
if (!$pdo) return null;
try {
if ($_SESSION['role'] === 'admin') { // Admin can fetch any note
$stmt = $pdo->prepare("SELECT n.*, u.username as owner_username FROM notes n JOIN users u ON n.user_id = u.id WHERE n.id = ?");
$stmt->execute([$noteId]);
} else { // Regular user can only fetch their own notes
$stmt = $pdo->prepare("SELECT * FROM notes WHERE id = ? AND user_id = ?");
$stmt->execute([$noteId, $_SESSION['user_id']]);
}
return $stmt->fetch();
} catch (PDOException $e) {
error_log("Get Note Error: " . $e->getMessage());
return null;
}
}
}

View File

@ -0,0 +1,43 @@
<?php include dirname(__DIR__).'/header.phtml'; ?>
<?php
$parsedown = new Parsedown();
$parsedown->setSafeMode(true);
?>
<div class="container">
<?php if (isset($note) && $note): ?>
<div class="note-details">
<div class="note-header">
<h2><?php echo htmlspecialchars($note['title'] ?? ''); ?></h2>
<div class="note-meta">
<?php if (($isAdmin ?? false) && isset($note['owner_username'])): ?>
<span class="note-owner">Owner: <?php echo htmlspecialchars($note['owner_username']); ?></span>
<?php endif; ?>
<span class="note-date">
Last updated: <?php echo isset($note['updated_at']) ? date("d.m.Y H:i", strtotime($note['updated_at'])) : 'N/A'; ?>
</span>
</div>
</div>
<div class="note-content">
<?php echo $parsedown->text($note['content'] ?? ''); ?>
</div>
<div class="note-actions">
<a href="?controller=NotesController&page=showNotes" class="button">Back to Notes</a>
<?php if (isset($note['id'])): ?>
<a href="?controller=NotesController&page=editNote&note_id=<?php echo (int)$note['id']; ?>" class="button">Edit Note</a>
<?php endif; ?>
</div>
</div>
<?php else: ?>
<div class="error-message">
<h2>Note Not Found</h2>
<p><?php echo htmlspecialchars($error ?? 'The requested note could not be found.'); ?></p>
<a href="?controller=NotesController&page=showNotes" class="button">Back to Notes</a>
</div>
<?php endif; ?>
</div>
<?php include dirname(__DIR__).'/footer.phtml'; ?>

View File

@ -38,7 +38,7 @@
<?php foreach ($notes as $note): ?>
<tr>
<td><?php echo sanitize($note['id']); ?></td>
<td><a href="index.php?page=view_note&id=<?php echo $note['id']; ?>"><?php echo sanitize($note['title']); ?></a></td>
<td><a href="?controller=Notes&do=showNoteDetails&id=<?php echo $note['id']; ?>"><?php echo sanitize($note['title']); ?></a></td>
<?php if (isAdmin()): ?>
<td><?php echo sanitize($note['owner_username']); ?></td>
<?php endif; ?>