diff --git a/src/MyPrayerJournal/Handlers.fs b/src/MyPrayerJournal/Handlers.fs index b6b3792..4d0d9f5 100644 --- a/src/MyPrayerJournal/Handlers.fs +++ b/src/MyPrayerJournal/Handlers.fs @@ -528,7 +528,7 @@ let routes = [ GET_HEAD [ route "journal-items" Components.journalItems // done routef "request/%s/add-notes" Components.addNotes // done - routef "request/%s/item" Components.requestItem // not used + routef "request/%s/item" Components.requestItem routef "request/%s/notes" Components.notes // done routef "request/%s/snooze" Components.snooze // done ] diff --git a/src/lib/UI.php b/src/lib/UI.php index 73302f8..ed34392 100644 --- a/src/lib/UI.php +++ b/src/lib/UI.php @@ -168,6 +168,44 @@ class UI return $dtFrom > $dtTo ? "$value ago" : "in $value"; } + public static function requestItem(Request $req): void + { + $btnClass = "btn btn-light mx-2"; + $restoreBtn = fn(string $id, string $link, string $title) => + ''; ?> +
id", self::icon('description'), + ['class' => $btnClass, 'title' => 'View Full Request']); + if (!$req->isAnswered()) { + self::pageLink("/request/edit?id=$req->id", self::icon('edit'), + ['class' => $btnClass, 'title' => 'Edit Request']); + } + if ($req->isSnoozed()) { + echo $restoreBtn($req->id, 'cancel-snooze', 'Cancel Snooze'); + } elseif ($req->isPending()) { + echo $restoreBtn($req->id, 'show', 'Show Now'); + } + echo '

' . $req->currentText(); + if ($req->isSnoozed() || $req->isPending() || $req->isAnswered()) { ?> +
+ isSnoozed(): + echo 'Snooze expires '; self::relativeDate($req->snoozedUntil); + break; + case $req->isPending(): + echo 'Request appears next '; self::relativeDate($req->showAfter); + break; + default: + echo 'Answered '; self::relativeDate($req->history[0]->asOf); + } ?> + +

- ''; ?> -
items() as /** @var Request $req */ $req) { ?> -
id", self::icon('description'), - ['class' => $btnClass, 'title' => 'View Full Request']); - if (!$req->isAnswered()) { - self::pageLink("/request/edit?id=$req->id", self::icon('edit'), - ['class' => $btnClass, 'title' => 'Edit Request']); - } - if ($req->isSnoozed()) { - echo $restoreBtn($req->id, 'cancel-snooze', 'Cancel Snooze'); - } elseif ($req->isPending()) { - echo $restoreBtn($req->id, 'show', 'Show Now'); - } - echo '

' . $req->currentText(); - if ($req->isSnoozed() || $req->isPending() || $req->isAnswered()) { ?> -
- isSnoozed(): - echo 'Snooze expires '; self::relativeDate($req->snoozedUntil); - break; - case $req->isPending(): - echo 'Request appears next '; self::relativeDate($req->showAfter); - break; - default: - echo 'Answered '; self::relativeDate($req->history[0]->asOf); - } ?> - -

-
'; + foreach ($reqs->items() as $req) self::requestItem($req); + echo ''; } } diff --git a/src/public/components/request/add-note.php b/src/public/components/request/add-note.php index 4ce73da..5783966 100644 --- a/src/public/components/request/add-note.php +++ b/src/public/components/request/add-note.php @@ -3,12 +3,8 @@ use MyPrayerJournal\{Auth, Layout, Request}; require '../../../start.php'; -if ($_SERVER['REQUEST_METHOD'] <> 'GET') not_found(); -Auth::requireUser(false); - -$req = Request::byId($_GET['id']); -if (!$req) not_found(); +$req = validate_request($_GET['id'], ['GET'], false); Layout::bareHead(); ?>
diff --git a/src/public/components/request/notes.php b/src/public/components/request/notes.php index 41b3810..766fc08 100644 --- a/src/public/components/request/notes.php +++ b/src/public/components/request/notes.php @@ -1,14 +1,10 @@ 'GET') not_found(); -Auth::requireUser(false); - -$req = Request::byId($_GET['id']); -if (!$req) not_found(); +$req = validate_request($_GET['id'], ['GET'], false); Layout::bareHead();?>

Prior Notes for This Request 'GET') not_found(); -Auth::requireUser(false); - -$req = Request::byId($_GET['id']); -if (!$req) not_found(); +$req = validate_request($_GET['id'], ['GET'], false); Layout::bareHead(); ?> diff --git a/src/public/request/cancel-snooze.php b/src/public/request/cancel-snooze.php new file mode 100644 index 0000000..3cef812 --- /dev/null +++ b/src/public/request/cancel-snooze.php @@ -0,0 +1,15 @@ +id, ['snoozedUntil']); + +// TODO: message +Layout::bareHead(); +UI::requestItem($req); +Layout::bareFoot(); diff --git a/src/public/request/full.php b/src/public/request/full.php index 6071df2..7b74dfa 100644 --- a/src/public/request/full.php +++ b/src/public/request/full.php @@ -1,14 +1,10 @@ 'GET') not_found(); -Auth::requireUser(); - -$req = Request::byId($_GET['id']); -if (!$req) not_found(); +$req = validate_request($_GET['id'], ['GET']); $answered = $req->isAnswered() ? new DateTimeImmutable($req->history[0]->asOf) : null; $prayed = sizeof(array_filter($req->history, fn(History $hist) => $hist->action == RequestAction::Prayed)); diff --git a/src/public/request/note.php b/src/public/request/note.php index 4493f2d..d918bee 100644 --- a/src/public/request/note.php +++ b/src/public/request/note.php @@ -1,15 +1,11 @@ 'POST') not_found(); -Auth::requireUser(false); - -$req = Request::byId($_GET['id']); -if (!$req) not_found(); +$req = validate_request($_GET['id'], ['POST'], false); array_unshift($req->notes, new Note((new DateTimeImmutable('now'))->format('c'), $_POST['notes'])); Patch::byId(Table::REQUEST, $req->id, ['notes' => $req->notes]); diff --git a/src/public/request/prayed.php b/src/public/request/prayed.php index 18aca8e..baefb30 100644 --- a/src/public/request/prayed.php +++ b/src/public/request/prayed.php @@ -1,16 +1,11 @@ 'PATCH') not_found(); - -Auth::requireUser(false); - -$req = Request::byId($_GET['id']); -if (!$req) not_found(); +$req = validate_request($_GET['id'], ['PATCH'], false); $now = new DateTimeImmutable('now'); array_unshift($req->history, new History($now->format('c'), RequestAction::Prayed)); diff --git a/src/public/request/snooze.php b/src/public/request/snooze.php index 6b1257b..62eb805 100644 --- a/src/public/request/snooze.php +++ b/src/public/request/snooze.php @@ -1,15 +1,11 @@ 'PATCH') not_found(); -Auth::requireUser(false); - -$req = Request::byId($_GET['id']); -if (!$req) not_found(); +$req = validate_request($_GET['id'], ['PATCH'], false); $until = (new DateTimeImmutable($_PATCH['until'] . 'T00:00:00', new DateTimeZone($_REQUEST['time_zone']))) ->setTimezone(new DateTimeZone('Etc/UTC')); diff --git a/src/start.php b/src/start.php index a775f75..1a1e141 100644 --- a/src/start.php +++ b/src/start.php @@ -1,8 +1,9 @@ $_SERVER['REQUEST_METHOD'] == $it)) == 0) not_found(); + + Auth::requireUser($redirect); + + $req = Request::byId($id); + if (!$req) not_found(); + + return $req; +}