Initial habit tracker project
This commit is contained in:
@@ -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,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user