first commit

This commit is contained in:
2026-01-29 21:08:01 +01:00
commit 4e4dfe66c6
28 changed files with 2509 additions and 0 deletions

View File

@@ -0,0 +1,142 @@
<?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;
}
}

View File

@@ -0,0 +1,27 @@
<?php
class DashboardController
{
public function index()
{
if (!isset($_SESSION['user_id'])) {
header('Location: /logowanie');
exit;
}
$userModel = new User();
$user = $userModel->getUserById($_SESSION['user_id']);
$calendarModel = new CalendarEvent();
$today = new DateTime('today');
$weekStart = (clone $today)->modify('monday this week');
$weekEnd = (clone $weekStart)->modify('+6 days');
$eventsThisWeek = $calendarModel->countByDateRange(
$_SESSION['user_id'],
$weekStart->format('Y-m-d'),
$weekEnd->format('Y-m-d')
);
require_once __DIR__ . '/../views/dashboard.php';
}
}

View File

@@ -0,0 +1,55 @@
<?php
class InitController
{
public function index()
{
try {
$db = Database::getInstance();
$db->initDatabase();
echo "<!DOCTYPE html>
<html lang='pl'>
<head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<title>Inicjalizacja bazy danych</title>
<link href='https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css' rel='stylesheet'>
</head>
<body class='bg-light'>
<div class='container mt-5'>
<div class='alert alert-success' role='alert'>
<h4 class='alert-heading'>Sukces!</h4>
<p>Baza danych została pomyślnie zainicjalizowana.</p>
<hr>
<p class='mb-0'>Testowy użytkownik:<br>
Login: <strong>projectpro</strong><br>
Hasło: <strong>testowehaslo</strong></p>
</div>
<a href='/logowanie' class='btn btn-primary'>Przejdź do logowania</a>
</div>
</body>
</html>";
} catch (Exception $e) {
echo "<!DOCTYPE html>
<html lang='pl'>
<head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<title>Błąd inicjalizacji</title>
<link href='https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css' rel='stylesheet'>
</head>
<body class='bg-light'>
<div class='container mt-5'>
<div class='alert alert-danger' role='alert'>
<h4 class='alert-heading'>Błąd!</h4>
<p>Nie udało się zainicjalizować bazy danych.</p>
<hr>
<p class='mb-0'>Szczegóły: " . htmlspecialchars($e->getMessage()) . "</p>
</div>
</div>
</body>
</html>";
}
}
}

View File

@@ -0,0 +1,98 @@
<?php
class LoginController
{
private $userModel;
public function __construct()
{
$this->userModel = new User();
}
public function index()
{
if (isset($_SESSION['user_id'])) {
header('Location: /panel');
exit;
}
require_once __DIR__ . '/../views/login.php';
}
public function authenticate()
{
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
header('Location: /logowanie');
exit;
}
$username = $_POST['username'] ?? '';
$password = $_POST['password'] ?? '';
$user = $this->userModel->authenticate($username, $password);
if ($user) {
// Generowanie kodu weryfikacyjnego
$code = $this->userModel->generateVerificationCode($user['id']);
// Zapisanie ID użytkownika w sesji tymczasowo
$_SESSION['pending_user_id'] = $user['id'];
$_SESSION['pending_username'] = $user['username'];
// W rzeczywistości tutaj wysłalibyśmy email
// Dla testów kod będzie wyświetlony w konsoli przeglądarki
$_SESSION['test_code'] = $code;
header('Location: /weryfikacja');
exit;
} else {
$_SESSION['error'] = 'Nieprawidłowy login lub hasło';
header('Location: /logowanie');
exit;
}
}
public function verify()
{
if (!isset($_SESSION['pending_user_id'])) {
header('Location: /logowanie');
exit;
}
require_once __DIR__ . '/../views/verify.php';
}
public function verifyCode()
{
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
header('Location: /logowanie');
exit;
}
if (!isset($_SESSION['pending_user_id'])) {
header('Location: /logowanie');
exit;
}
$code = $_POST['code'] ?? '';
$userId = $_SESSION['pending_user_id'];
if ($this->userModel->verifyCode($userId, $code)) {
// Zalogowanie użytkownika
$_SESSION['user_id'] = $userId;
$_SESSION['username'] = $_SESSION['pending_username'];
// Czyszczenie danych tymczasowych
unset($_SESSION['pending_user_id']);
unset($_SESSION['pending_username']);
unset($_SESSION['test_code']);
header('Location: /panel');
exit;
} else {
$_SESSION['error'] = 'Nieprawidłowy kod weryfikacyjny lub kod wygasł';
header('Location: /weryfikacja');
exit;
}
}
}

View File

@@ -0,0 +1,114 @@
<?php
class NotesController
{
private $notesModel;
public function __construct()
{
$this->notesModel = new Notes();
// Sprawdzenie czy użytkownik jest zalogowany
if (!isset($_SESSION['user_id'])) {
header('Location: /logowanie');
exit;
}
}
public function index()
{
$userId = $_SESSION['user_id'];
$notes = $this->notesModel->getAllByUser($userId);
$notesCount = $this->notesModel->getCount($userId);
require_once __DIR__ . '/../views/notes/index.php';
}
public function create()
{
$note = null; // Pusty formularz dla nowej notatki
require_once __DIR__ . '/../views/notes/form.php';
}
public function edit()
{
$noteId = $_GET['id'] ?? null;
if (!$noteId) {
$_SESSION['error'] = 'Nie podano ID notatki';
header('Location: /notatnik');
exit;
}
$userId = $_SESSION['user_id'];
$note = $this->notesModel->getById($noteId, $userId);
if (!$note) {
$_SESSION['error'] = 'Notatka nie została znaleziona';
header('Location: /notatnik');
exit;
}
require_once __DIR__ . '/../views/notes/form.php';
}
public function save()
{
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
header('Location: /notatnik');
exit;
}
$userId = $_SESSION['user_id'];
$noteId = $_POST['note_id'] ?? null;
$title = trim($_POST['title'] ?? '');
$content = trim($_POST['content'] ?? '');
if (empty($title)) {
$_SESSION['error'] = 'Tytuł notatki jest wymagany';
header('Location: ' . ($noteId ? "/notatka/edytuj?id=$noteId" : '/notatka/nowa'));
exit;
}
if ($noteId) {
// Aktualizacja istniejącej notatki
$result = $this->notesModel->update($noteId, $userId, $title, $content);
$_SESSION['success'] = 'Notatka została zaktualizowana';
} else {
// Tworzenie nowej notatki
$result = $this->notesModel->create($userId, $title, $content);
$_SESSION['success'] = 'Notatka została utworzona';
}
header('Location: /notatnik');
exit;
}
public function delete()
{
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
header('Location: /notatnik');
exit;
}
$noteId = $_POST['note_id'] ?? null;
if (!$noteId) {
$_SESSION['error'] = 'Nie podano ID notatki';
header('Location: /notatnik');
exit;
}
$userId = $_SESSION['user_id'];
$result = $this->notesModel->delete($noteId, $userId);
if ($result) {
$_SESSION['success'] = 'Notatka została usunięta';
} else {
$_SESSION['error'] = 'Nie udało się usunąć notatki';
}
header('Location: /notatnik');
exit;
}
}