Files
pagedev.pl/controllers/CalendarController.php
2026-01-29 21:08:01 +01:00

143 lines
4.4 KiB
PHP

<?php
class CalendarController
{
private $calendarModel;
public function __construct()
{
if (!isset($_SESSION['user_id'])) {
header('Location: /logowanie');
exit;
}
$this->calendarModel = new CalendarEvent();
}
public function index()
{
$userId = $_SESSION['user_id'];
$month = $_GET['month'] ?? date('Y-m');
$monthDate = DateTime::createFromFormat('Y-m', $month);
if (!$monthDate) {
$monthDate = new DateTime('first day of this month');
$month = $monthDate->format('Y-m');
}
$firstDay = (clone $monthDate)->modify('first day of this month');
$daysInMonth = (int)$firstDay->format('t');
$startWeekday = (int)$firstDay->format('N');
$prevMonth = (clone $monthDate)->modify('-1 month')->format('Y-m');
$nextMonth = (clone $monthDate)->modify('+1 month')->format('Y-m');
$monthLabel = $monthDate->format('F Y');
$selectedDate = $_GET['date'] ?? date('Y-m-d');
$events = $this->calendarModel->getByMonth($userId, $month);
$eventsByDate = [];
foreach ($events as $event) {
$eventsByDate[$event['event_date']][] = $event;
}
$eventsForSelected = $this->calendarModel->getByDate($userId, $selectedDate);
require_once __DIR__ . '/../views/calendar/index.php';
}
public function create()
{
$event = null;
$defaultDate = $_GET['date'] ?? date('Y-m-d');
$returnMonth = $_GET['month'] ?? date('Y-m');
require_once __DIR__ . '/../views/calendar/form.php';
}
public function edit()
{
$eventId = $_GET['id'] ?? null;
if (!$eventId) {
$_SESSION['error'] = 'Nie podano ID wydarzenia';
header('Location: /kalendarz');
exit;
}
$userId = $_SESSION['user_id'];
$event = $this->calendarModel->getById($eventId, $userId);
if (!$event) {
$_SESSION['error'] = 'Wydarzenie nie zostało znalezione';
header('Location: /kalendarz');
exit;
}
$defaultDate = $event['event_date'];
$returnMonth = $_GET['month'] ?? date('Y-m', strtotime($event['event_date']));
require_once __DIR__ . '/../views/calendar/form.php';
}
public function save()
{
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
header('Location: /kalendarz');
exit;
}
$userId = $_SESSION['user_id'];
$eventId = $_POST['event_id'] ?? null;
$title = trim($_POST['title'] ?? '');
$content = trim($_POST['content'] ?? '');
$eventDate = $_POST['event_date'] ?? date('Y-m-d');
$returnMonth = $_POST['return_month'] ?? date('Y-m');
if ($title === '') {
$_SESSION['error'] = 'Tytuł wydarzenia jest wymagany';
$redirect = $eventId ? "/wydarzenie/edytuj?id=$eventId&month=$returnMonth" : "/wydarzenie/nowe?date=$eventDate&month=$returnMonth";
header('Location: ' . $redirect);
exit;
}
if ($eventId) {
$this->calendarModel->update($eventId, $userId, $title, $content, $eventDate);
$_SESSION['success'] = 'Wydarzenie zostało zaktualizowane';
} else {
$this->calendarModel->create($userId, $title, $content, $eventDate);
$_SESSION['success'] = 'Wydarzenie zostało utworzone';
}
header('Location: /kalendarz?month=' . $returnMonth . '&date=' . $eventDate);
exit;
}
public function delete()
{
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
header('Location: /kalendarz');
exit;
}
$eventId = $_POST['event_id'] ?? null;
$returnMonth = $_POST['return_month'] ?? date('Y-m');
$returnDate = $_POST['return_date'] ?? date('Y-m-d');
if (!$eventId) {
$_SESSION['error'] = 'Nie podano ID wydarzenia';
header('Location: /kalendarz');
exit;
}
$userId = $_SESSION['user_id'];
$result = $this->calendarModel->delete($eventId, $userId);
if ($result) {
$_SESSION['success'] = 'Wydarzenie zostało usunięte';
} else {
$_SESSION['error'] = 'Nie udało się usunąć wydarzenia';
}
header('Location: /kalendarz?month=' . $returnMonth . '&date=' . $returnDate);
exit;
}
}