Add protected reminder management
This commit is contained in:
@@ -0,0 +1,187 @@
|
||||
<?php
|
||||
|
||||
class ReminderController
|
||||
{
|
||||
public function create(): void
|
||||
{
|
||||
require_login();
|
||||
|
||||
$habit = Habit::findForUser($this->queryId('habit_id'), current_user_id());
|
||||
|
||||
if (!$habit) {
|
||||
flash('error', 'Der Habit fuer die Erinnerung wurde nicht gefunden.');
|
||||
redirect('dashboard');
|
||||
}
|
||||
|
||||
View::render('reminders/form', [
|
||||
'habit' => $habit,
|
||||
'reminder' => ['habit_id' => $habit['id'], 'is_active' => 1],
|
||||
'errors' => [],
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(): void
|
||||
{
|
||||
require_login();
|
||||
verify_csrf_token();
|
||||
|
||||
$habit = Habit::findForUser($this->postId('habit_id'), current_user_id());
|
||||
|
||||
if (!$habit) {
|
||||
flash('error', 'Der Habit fuer die Erinnerung wurde nicht gefunden.');
|
||||
redirect('dashboard');
|
||||
}
|
||||
|
||||
$data = $this->validatedData();
|
||||
|
||||
if (!empty($data['errors'])) {
|
||||
View::render('reminders/form', [
|
||||
'habit' => $habit,
|
||||
'reminder' => array_merge($_POST, $data),
|
||||
'errors' => $data['errors'],
|
||||
]);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Reminder::createForHabit((int) $habit['id'], current_user_id(), $data)) {
|
||||
flash('error', 'Die Erinnerung konnte nicht gespeichert werden.');
|
||||
redirect('dashboard');
|
||||
}
|
||||
|
||||
flash('success', 'Erinnerung wurde erstellt.');
|
||||
redirect('dashboard');
|
||||
}
|
||||
|
||||
public function edit(): void
|
||||
{
|
||||
require_login();
|
||||
|
||||
$reminder = Reminder::findForUser($this->queryId('id'), current_user_id());
|
||||
|
||||
if (!$reminder) {
|
||||
flash('error', 'Erinnerung wurde nicht gefunden.');
|
||||
redirect('dashboard');
|
||||
}
|
||||
|
||||
$habit = Habit::findForUser((int) $reminder['habit_id'], current_user_id());
|
||||
|
||||
View::render('reminders/form', [
|
||||
'habit' => $habit,
|
||||
'reminder' => $reminder,
|
||||
'errors' => [],
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(): void
|
||||
{
|
||||
require_login();
|
||||
verify_csrf_token();
|
||||
|
||||
$reminder = Reminder::findForUser($this->postId('id'), current_user_id());
|
||||
|
||||
if (!$reminder) {
|
||||
flash('error', 'Erinnerung wurde nicht gefunden.');
|
||||
redirect('dashboard');
|
||||
}
|
||||
|
||||
$habit = Habit::findForUser((int) $reminder['habit_id'], current_user_id());
|
||||
$data = $this->validatedData();
|
||||
|
||||
if (!empty($data['errors'])) {
|
||||
View::render('reminders/form', [
|
||||
'habit' => $habit,
|
||||
'reminder' => array_merge($reminder, $data),
|
||||
'errors' => $data['errors'],
|
||||
]);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Reminder::update((int) $reminder['id'], current_user_id(), $data)) {
|
||||
flash('error', 'Die Erinnerung konnte nicht gespeichert werden.');
|
||||
redirect('dashboard');
|
||||
}
|
||||
|
||||
flash('success', 'Erinnerung wurde gespeichert.');
|
||||
redirect('dashboard');
|
||||
}
|
||||
|
||||
public function delete(): void
|
||||
{
|
||||
require_login();
|
||||
verify_csrf_token();
|
||||
|
||||
$reminder = Reminder::findForUser($this->postId('id'), current_user_id());
|
||||
|
||||
if (!$reminder || !Reminder::delete((int) $reminder['id'], current_user_id())) {
|
||||
flash('error', 'Die Erinnerung konnte nicht geloescht werden.');
|
||||
redirect('dashboard');
|
||||
}
|
||||
|
||||
flash('success', 'Erinnerung wurde geloescht.');
|
||||
redirect('dashboard');
|
||||
}
|
||||
|
||||
public function toggle(): void
|
||||
{
|
||||
require_login();
|
||||
verify_csrf_token();
|
||||
|
||||
$reminder = Reminder::findForUser($this->postId('id'), current_user_id());
|
||||
|
||||
if (!$reminder || !Reminder::toggle((int) $reminder['id'], current_user_id())) {
|
||||
flash('error', 'Der Status der Erinnerung konnte nicht geaendert werden.');
|
||||
redirect('dashboard');
|
||||
}
|
||||
|
||||
flash('success', $reminder['is_active'] ? 'Erinnerung pausiert.' : 'Erinnerung aktiviert.');
|
||||
redirect('dashboard');
|
||||
}
|
||||
|
||||
private function validatedData(): array
|
||||
{
|
||||
$time = $this->postString('reminder_time');
|
||||
$weekdayValue = $this->postString('weekday');
|
||||
$activeValue = $this->postString('is_active');
|
||||
$errors = [];
|
||||
|
||||
if (!preg_match('/^(?:[01]\\d|2[0-3]):[0-5]\\d$/', $time)) {
|
||||
$errors['reminder_time'] = 'Bitte gib eine Uhrzeit im Format HH:MM ein.';
|
||||
}
|
||||
|
||||
if ($weekdayValue !== '' && !preg_match('/^[1-7]$/', $weekdayValue)) {
|
||||
$errors['weekday'] = 'Bitte waehle einen gueltigen Wochentag aus.';
|
||||
}
|
||||
|
||||
if (!in_array($activeValue, ['0', '1'], true)) {
|
||||
$errors['is_active'] = 'Bitte waehle einen gueltigen Status aus.';
|
||||
}
|
||||
|
||||
return [
|
||||
'reminder_time' => $time,
|
||||
'weekday' => $weekdayValue === '' ? null : (int) $weekdayValue,
|
||||
'is_active' => $activeValue === '0' ? 0 : 1,
|
||||
'errors' => $errors,
|
||||
];
|
||||
}
|
||||
|
||||
private function queryId(string $key): int
|
||||
{
|
||||
$value = $_GET[$key] ?? '';
|
||||
|
||||
return is_string($value) && preg_match('/^[1-9]\\d*$/', $value) ? (int) $value : 0;
|
||||
}
|
||||
|
||||
private function postId(string $key): int
|
||||
{
|
||||
$value = $_POST[$key] ?? '';
|
||||
|
||||
return is_string($value) && preg_match('/^[1-9]\\d*$/', $value) ? (int) $value : 0;
|
||||
}
|
||||
|
||||
private function postString(string $key): string
|
||||
{
|
||||
$value = $_POST[$key] ?? '';
|
||||
|
||||
return is_string($value) ? trim($value) : '';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user