first commit
This commit is contained in:
179
app/controllers/EventController.php
Normal file
179
app/controllers/EventController.php
Normal file
@@ -0,0 +1,179 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . '/../models/Event.php';
|
||||
|
||||
class EventController
|
||||
{
|
||||
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'];
|
||||
|
||||
// Pobierz rok i miesiąc z parametrów lub użyj bieżących
|
||||
$year = isset($_GET['rok']) ? (int) $_GET['rok'] : (int) date('Y');
|
||||
$month = isset($_GET['miesiac']) ? (int) $_GET['miesiac'] : (int) date('m');
|
||||
|
||||
// Walidacja
|
||||
if ($month < 1) {
|
||||
$month = 12;
|
||||
$year--;
|
||||
} elseif ($month > 12) {
|
||||
$month = 1;
|
||||
$year++;
|
||||
}
|
||||
|
||||
$events = Event::getByMonth($userId, $year, $month);
|
||||
|
||||
// Grupowanie wydarzeń po dacie
|
||||
$eventsByDate = [];
|
||||
foreach ($events as $event) {
|
||||
$eventsByDate[$event['event_date']][] = $event;
|
||||
}
|
||||
|
||||
$success = $_SESSION['success'] ?? null;
|
||||
$error = $_SESSION['error'] ?? null;
|
||||
unset($_SESSION['success'], $_SESSION['error']);
|
||||
|
||||
require __DIR__ . '/../views/calendar/index.php';
|
||||
}
|
||||
|
||||
public function dayEvents(string $date): void
|
||||
{
|
||||
$this->requireAuth();
|
||||
|
||||
$userId = $_SESSION['user_id'];
|
||||
$userLogin = $_SESSION['user_login'];
|
||||
|
||||
// Walidacja daty
|
||||
if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $date)) {
|
||||
$_SESSION['error'] = 'Nieprawidłowy format daty.';
|
||||
header('Location: /kalendarz');
|
||||
exit;
|
||||
}
|
||||
|
||||
$events = Event::getByDate($userId, $date);
|
||||
|
||||
$success = $_SESSION['success'] ?? null;
|
||||
$error = $_SESSION['error'] ?? null;
|
||||
unset($_SESSION['success'], $_SESSION['error']);
|
||||
|
||||
require __DIR__ . '/../views/calendar/day.php';
|
||||
}
|
||||
|
||||
public function create(string $date = null): void
|
||||
{
|
||||
$this->requireAuth();
|
||||
|
||||
$userLogin = $_SESSION['user_login'];
|
||||
$event = null;
|
||||
$isEdit = false;
|
||||
$selectedDate = $date ?? date('Y-m-d');
|
||||
|
||||
require __DIR__ . '/../views/calendar/form.php';
|
||||
}
|
||||
|
||||
public function store(): void
|
||||
{
|
||||
$this->requireAuth();
|
||||
|
||||
$userId = $_SESSION['user_id'];
|
||||
$title = trim($_POST['title'] ?? '');
|
||||
$content = trim($_POST['content'] ?? '');
|
||||
$eventDate = $_POST['event_date'] ?? '';
|
||||
|
||||
if (empty($title)) {
|
||||
$_SESSION['error'] = 'Tytuł wydarzenia jest wymagany.';
|
||||
header('Location: /kalendarz/nowe');
|
||||
exit;
|
||||
}
|
||||
|
||||
if (empty($eventDate)) {
|
||||
$_SESSION['error'] = 'Data wydarzenia jest wymagana.';
|
||||
header('Location: /kalendarz/nowe');
|
||||
exit;
|
||||
}
|
||||
|
||||
Event::create($userId, $title, $content, $eventDate);
|
||||
$_SESSION['success'] = 'Wydarzenie zostało dodane.';
|
||||
header('Location: /kalendarz/dzien/' . $eventDate);
|
||||
exit;
|
||||
}
|
||||
|
||||
public function edit(int $id): void
|
||||
{
|
||||
$this->requireAuth();
|
||||
|
||||
$userId = $_SESSION['user_id'];
|
||||
$userLogin = $_SESSION['user_login'];
|
||||
$event = Event::getById($id, $userId);
|
||||
|
||||
if (!$event) {
|
||||
$_SESSION['error'] = 'Wydarzenie nie zostało znalezione.';
|
||||
header('Location: /kalendarz');
|
||||
exit;
|
||||
}
|
||||
|
||||
$isEdit = true;
|
||||
$selectedDate = $event['event_date'];
|
||||
|
||||
require __DIR__ . '/../views/calendar/form.php';
|
||||
}
|
||||
|
||||
public function update(int $id): void
|
||||
{
|
||||
$this->requireAuth();
|
||||
|
||||
$userId = $_SESSION['user_id'];
|
||||
$title = trim($_POST['title'] ?? '');
|
||||
$content = trim($_POST['content'] ?? '');
|
||||
$eventDate = $_POST['event_date'] ?? '';
|
||||
|
||||
if (empty($title)) {
|
||||
$_SESSION['error'] = 'Tytuł wydarzenia jest wymagany.';
|
||||
header("Location: /kalendarz/edytuj/$id");
|
||||
exit;
|
||||
}
|
||||
|
||||
$event = Event::getById($id, $userId);
|
||||
if (!$event) {
|
||||
$_SESSION['error'] = 'Wydarzenie nie zostało znalezione.';
|
||||
header('Location: /kalendarz');
|
||||
exit;
|
||||
}
|
||||
|
||||
Event::update($id, $userId, $title, $content, $eventDate);
|
||||
$_SESSION['success'] = 'Wydarzenie zostało zaktualizowane.';
|
||||
header('Location: /kalendarz/dzien/' . $eventDate);
|
||||
exit;
|
||||
}
|
||||
|
||||
public function delete(int $id): void
|
||||
{
|
||||
$this->requireAuth();
|
||||
|
||||
$userId = $_SESSION['user_id'];
|
||||
$event = Event::getById($id, $userId);
|
||||
|
||||
if (!$event) {
|
||||
$_SESSION['error'] = 'Wydarzenie nie zostało znalezione.';
|
||||
header('Location: /kalendarz');
|
||||
exit;
|
||||
}
|
||||
|
||||
$eventDate = $event['event_date'];
|
||||
Event::delete($id, $userId);
|
||||
$_SESSION['success'] = 'Wydarzenie zostało usunięte.';
|
||||
header('Location: /kalendarz/dzien/' . $eventDate);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user