Initial habit tracker project

This commit is contained in:
2026-07-10 16:18:31 +02:00
commit 23416ee382
20 changed files with 1157 additions and 0 deletions
+75
View File
@@ -0,0 +1,75 @@
<?php
class AuthController
{
public function showLogin(): void
{
View::render('auth/login');
}
public function login(): void
{
verify_csrf_token();
$email = trim($_POST['email'] ?? '');
$password = $_POST['password'] ?? '';
if (!filter_var($email, FILTER_VALIDATE_EMAIL) || $password === '') {
flash('error', 'Bitte gib eine gueltige E-Mail und dein Passwort ein.');
redirect('login');
}
$user = User::findByEmail($email);
if (!$user || !password_verify($password, $user['password_hash'])) {
flash('error', 'E-Mail oder Passwort ist nicht korrekt.');
redirect('login');
}
session_regenerate_id(true);
$_SESSION['user_id'] = (int) $user['id'];
$_SESSION['user_name'] = $user['name'];
redirect('dashboard');
}
public function showRegister(): void
{
View::render('auth/register');
}
public function register(): void
{
verify_csrf_token();
$name = trim($_POST['name'] ?? '');
$email = trim($_POST['email'] ?? '');
$password = $_POST['password'] ?? '';
if ($name === '' || !filter_var($email, FILTER_VALIDATE_EMAIL) || strlen($password) < 8) {
flash('error', 'Bitte Name, gueltige E-Mail und ein Passwort mit mindestens 8 Zeichen eingeben.');
redirect('register');
}
if (User::findByEmail($email)) {
flash('error', 'Diese E-Mail ist bereits registriert.');
redirect('register');
}
User::create($name, $email, $password);
flash('success', 'Registrierung erfolgreich. Du kannst dich jetzt anmelden.');
redirect('login');
}
public function logout(): void
{
verify_csrf_token();
$_SESSION = [];
if (ini_get('session.use_cookies')) {
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000, $params['path'], $params['domain'], $params['secure'], $params['httponly']);
}
session_destroy();
redirect('login');
}
}
+137
View File
@@ -0,0 +1,137 @@
<?php
class HabitController
{
public function dashboard(): void
{
require_login();
$filters = [
'category_id' => $_GET['category_id'] ?? '',
'status' => $_GET['status'] ?? '',
];
View::render('habits/dashboard', [
'habits' => Habit::allForUser(current_user_id(), $filters),
'categories' => Category::all(),
'filters' => $filters,
]);
}
public function create(): void
{
require_login();
View::render('habits/form', [
'habit' => null,
'categories' => Category::all(),
'errors' => [],
]);
}
public function store(): void
{
require_login();
verify_csrf_token();
$data = $this->validatedData();
if (!empty($data['errors'])) {
View::render('habits/form', [
'habit' => $_POST,
'categories' => Category::all(),
'errors' => $data['errors'],
]);
return;
}
Habit::create(current_user_id(), $data);
flash('success', 'Habit wurde erstellt.');
redirect('dashboard');
}
public function edit(): void
{
require_login();
$habit = Habit::findForUser((int) ($_GET['id'] ?? 0), current_user_id());
if (!$habit) {
flash('error', 'Habit wurde nicht gefunden.');
redirect('dashboard');
}
View::render('habits/form', [
'habit' => $habit,
'categories' => Category::all(),
'errors' => [],
]);
}
public function update(): void
{
require_login();
verify_csrf_token();
$id = (int) ($_POST['id'] ?? 0);
$data = $this->validatedData();
if (!empty($data['errors'])) {
View::render('habits/form', [
'habit' => array_merge($_POST, ['id' => $id]),
'categories' => Category::all(),
'errors' => $data['errors'],
]);
return;
}
Habit::update($id, current_user_id(), $data);
flash('success', 'Habit wurde gespeichert.');
redirect('dashboard');
}
public function delete(): void
{
require_login();
verify_csrf_token();
Habit::delete((int) ($_POST['id'] ?? 0), current_user_id());
flash('success', 'Habit wurde geloescht.');
redirect('dashboard');
}
public function toggle(): void
{
require_login();
verify_csrf_token();
Habit::toggleToday((int) ($_POST['id'] ?? 0), current_user_id());
redirect('dashboard');
}
private function validatedData(): array
{
$title = trim($_POST['title'] ?? '');
$description = trim($_POST['description'] ?? '');
$categoryId = $_POST['category_id'] ?? '';
$targetPerWeek = (int) ($_POST['target_per_week'] ?? 0);
$errors = [];
if ($title === '') {
$errors['title'] = 'Bitte gib einen Namen fuer den Habit ein.';
}
if ($targetPerWeek < 1 || $targetPerWeek > 7) {
$errors['target_per_week'] = 'Bitte waehle einen Wert zwischen 1 und 7.';
}
return [
'title' => $title,
'description' => $description,
'category_id' => $categoryId,
'target_per_week' => $targetPerWeek,
'errors' => $errors,
];
}
}