first commit
This commit is contained in:
126
app/controllers/NoteController.php
Normal file
126
app/controllers/NoteController.php
Normal file
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . '/../models/Note.php';
|
||||
|
||||
class NoteController
|
||||
{
|
||||
private function requireAuth(): void
|
||||
{
|
||||
if (!isset($_SESSION['logged_in']) || !$_SESSION['logged_in']) {
|
||||
header('Location: /logowanie');
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
public function index(): void
|
||||
{
|
||||
$this->requireAuth();
|
||||
|
||||
$userId = $_SESSION['user_id'];
|
||||
$userLogin = $_SESSION['user_login'];
|
||||
$notes = Note::getAllByUser($userId);
|
||||
|
||||
$success = $_SESSION['success'] ?? null;
|
||||
$error = $_SESSION['error'] ?? null;
|
||||
unset($_SESSION['success'], $_SESSION['error']);
|
||||
|
||||
require __DIR__ . '/../views/notes/index.php';
|
||||
}
|
||||
|
||||
public function create(): void
|
||||
{
|
||||
$this->requireAuth();
|
||||
|
||||
$userLogin = $_SESSION['user_login'];
|
||||
$note = null;
|
||||
$isEdit = false;
|
||||
|
||||
require __DIR__ . '/../views/notes/form.php';
|
||||
}
|
||||
|
||||
public function store(): void
|
||||
{
|
||||
$this->requireAuth();
|
||||
|
||||
$userId = $_SESSION['user_id'];
|
||||
$title = trim($_POST['title'] ?? '');
|
||||
$content = trim($_POST['content'] ?? '');
|
||||
$color = $_POST['color'] ?? 'primary';
|
||||
|
||||
if (empty($title)) {
|
||||
$_SESSION['error'] = 'Tytuł notatki jest wymagany.';
|
||||
header('Location: /notatnik/nowa');
|
||||
exit;
|
||||
}
|
||||
|
||||
Note::create($userId, $title, $content, $color);
|
||||
$_SESSION['success'] = 'Notatka została dodana.';
|
||||
header('Location: /notatnik');
|
||||
exit;
|
||||
}
|
||||
|
||||
public function edit(int $id): void
|
||||
{
|
||||
$this->requireAuth();
|
||||
|
||||
$userId = $_SESSION['user_id'];
|
||||
$userLogin = $_SESSION['user_login'];
|
||||
$note = Note::getById($id, $userId);
|
||||
|
||||
if (!$note) {
|
||||
$_SESSION['error'] = 'Notatka nie została znaleziona.';
|
||||
header('Location: /notatnik');
|
||||
exit;
|
||||
}
|
||||
|
||||
$isEdit = true;
|
||||
require __DIR__ . '/../views/notes/form.php';
|
||||
}
|
||||
|
||||
public function update(int $id): void
|
||||
{
|
||||
$this->requireAuth();
|
||||
|
||||
$userId = $_SESSION['user_id'];
|
||||
$title = trim($_POST['title'] ?? '');
|
||||
$content = trim($_POST['content'] ?? '');
|
||||
$color = $_POST['color'] ?? 'primary';
|
||||
|
||||
if (empty($title)) {
|
||||
$_SESSION['error'] = 'Tytuł notatki jest wymagany.';
|
||||
header("Location: /notatnik/edytuj/$id");
|
||||
exit;
|
||||
}
|
||||
|
||||
$note = Note::getById($id, $userId);
|
||||
if (!$note) {
|
||||
$_SESSION['error'] = 'Notatka nie została znaleziona.';
|
||||
header('Location: /notatnik');
|
||||
exit;
|
||||
}
|
||||
|
||||
Note::update($id, $userId, $title, $content, $color);
|
||||
$_SESSION['success'] = 'Notatka została zaktualizowana.';
|
||||
header('Location: /notatnik');
|
||||
exit;
|
||||
}
|
||||
|
||||
public function delete(int $id): void
|
||||
{
|
||||
$this->requireAuth();
|
||||
|
||||
$userId = $_SESSION['user_id'];
|
||||
$note = Note::getById($id, $userId);
|
||||
|
||||
if (!$note) {
|
||||
$_SESSION['error'] = 'Notatka nie została znaleziona.';
|
||||
header('Location: /notatnik');
|
||||
exit;
|
||||
}
|
||||
|
||||
Note::delete($id, $userId);
|
||||
$_SESSION['success'] = 'Notatka została usunięta.';
|
||||
header('Location: /notatnik');
|
||||
exit;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user