diff --git a/src/convert-from-v3.php b/src/convert-from-v3.php
index d499de7..b6af213 100644
--- a/src/convert-from-v3.php
+++ b/src/convert-from-v3.php
@@ -1,3 +1,50 @@
new Note($note->asOf, $note->notes), $req->notes ?? []);
+ $history = array_map(fn(stdClass $hist) =>
+ new History(
+ asOf: $hist->asOf,
+ action: RequestAction::from($hist->status),
+ text: property_exists($hist, 'text') ? $hist->text : null),
+ $req->history);
+ $recurParts = explode(' ', $req->recurrence);
+ $recurPeriod = RecurrencePeriod::from(end($recurParts));
+ $recur = match ($recurPeriod) {
+ RecurrencePeriod::Immediate => new Recurrence(RecurrencePeriod::Immediate),
+ default => new Recurrence($recurPeriod, (int)$recurParts[0])
+ };
+ $v4Req = new Request(
+ id: $req->id,
+ enteredOn: $req->enteredOn,
+ userId: $req->userId,
+ snoozedUntil: property_exists($req, 'snoozedUntil') ? $req->snoozedUntil : null,
+ showAfter: property_exists($req, 'showAfter') ? $req->showAfter : null,
+ recurrence: $recur,
+ history: $history,
+ notes: $notes);
+ Document::insert(Table::REQUEST, $v4Req);
+}
+
+echo PHP_EOL . 'done' . PHP_EOL;
diff --git a/src/lib/Auth.php b/src/lib/Auth.php
index 7763beb..d667aad 100644
--- a/src/lib/Auth.php
+++ b/src/lib/Auth.php
@@ -22,6 +22,16 @@ class Auth
return self::$auth0;
}
+ /**
+ * Get the logged on user information
+ *
+ * @return array|null The user information (null if no user is logged on)
+ */
+ public static function user(): ?array
+ {
+ return self::client()->getUser();
+ }
+
/**
* Initiate a log on with Auth0
*
@@ -50,4 +60,20 @@ class Auth
header('Location: ' . self::client()->logout($_ENV['AUTH0_BASE_URL']));
exit;
}
+
+ /**
+ * Require a user be logged on
+ *
+ * @param bool $redirect Whether to redirect to log on if there is not a user logged on
+ * @return void If it returns, there is a user logged on; if not, we will be redirected to log on
+ * @throws ConfigurationException If the Auth0 client is not configured correctly
+ */
+ public static function requireUser(bool $redirect = true): void
+ {
+ if (is_null(self::user())) {
+ if ($redirect) self::logOn();
+ http_response_code(403);
+ die('Not Authorized');
+ }
+ }
}
diff --git a/src/lib/History.php b/src/lib/History.php
index b89cd95..067aaf7 100644
--- a/src/lib/History.php
+++ b/src/lib/History.php
@@ -2,10 +2,12 @@
namespace MyPrayerJournal;
+use JsonSerializable;
+
/**
* A record of an action taken on a request
*/
-class History
+class History implements JsonSerializable
{
/**
* @param string $asOf The date/time this entry was made
@@ -13,4 +15,11 @@ class History
* @param string|null $text The text for this history entry (optional)
*/
public function __construct(public string $asOf, public RequestAction $action, public ?string $text = null) { }
+
+ public function jsonSerialize(): mixed
+ {
+ $values = ['asOf' => $this->asOf, 'action' => $this->action->value];
+ if (!is_null($this->text)) $values['text'] = $this->text;
+ return $values;
+ }
}
diff --git a/src/lib/Layout.php b/src/lib/Layout.php
index e5fdf96..a10a0b2 100644
--- a/src/lib/Layout.php
+++ b/src/lib/Layout.php
@@ -114,4 +114,15 @@ class Layout
+
+
+
+
=$text?>
'btn btn-primary']); ?>
+
+
$this->period->value];
+ if (!is_null($this->interval)) $values['interval'] = $this->interval;
+ return $values;
+ }
}
diff --git a/src/lib/RecurrencePeriod.php b/src/lib/RecurrencePeriod.php
index 800c53b..70b76e7 100644
--- a/src/lib/RecurrencePeriod.php
+++ b/src/lib/RecurrencePeriod.php
@@ -5,17 +5,17 @@ namespace MyPrayerJournal;
/**
* The type of recurrence a request can have
*/
-enum RecurrencePeriod
+enum RecurrencePeriod: string
{
/** Requests, once prayed, are available again immediately */
- case Immediate;
+ case Immediate = 'Immediate';
/** Requests, once prayed, appear again in a number of hours */
- case Hours;
+ case Hours = 'Hours';
/** Requests, once prayed, appear again in a number of days */
- case Days;
+ case Days = 'Days';
/** Requests, once prayed, appear again in a number of weeks */
- case Weeks;
+ case Weeks = 'Weeks';
}
diff --git a/src/lib/Request.php b/src/lib/Request.php
index 58fe2ef..b72d09e 100644
--- a/src/lib/Request.php
+++ b/src/lib/Request.php
@@ -4,12 +4,13 @@ namespace MyPrayerJournal;
use BitBadger\PDODocument\{DocumentException, Find};
use Exception;
+use JsonSerializable;
use Visus\Cuid2\Cuid2;
/**
* A prayer request
*/
-class Request
+class Request implements JsonSerializable
{
/**
* @param string $id The ID for the request
@@ -18,8 +19,8 @@ class Request
* @param string|null $snoozedUntil The date/time the snooze expires for this request (null = not snoozed)
* @param string|null $showAfter The date/time the current recurrence period is over (null = immediate)
* @param Recurrence $recurrence The recurrence for this request
- * @param array|History[] $history The history of this request
- * @param array|Note[] $notes Notes regarding this request
+ * @param History[] $history The history of this request
+ * @param Note[] $notes Notes regarding this request
* @throws Exception If the ID generation fails
*/
public function __construct(public string $id = '', public string $enteredOn = '', public string $userId = '',
@@ -44,4 +45,19 @@ class Request
$req = Find::byId(Table::REQUEST, $id, self::class);
return ($req && $req->userId == $_SESSION['user_id']) ? $req : false;
}
+
+ public function jsonSerialize(): mixed
+ {
+ $values = [
+ 'id' => $this->id,
+ 'enteredOn' => $this->enteredOn,
+ 'userId' => $this->userId,
+ 'recurrence' => $this->recurrence,
+ 'history' => $this->history,
+ 'notes' => $this->notes
+ ];
+ if (!is_null($this->snoozedUntil)) $values['snoozedUntil'] = $this->snoozedUntil;
+ if (!is_null($this->showAfter)) $values['showAfter'] = $this->showAfter;
+ return $values;
+ }
}
diff --git a/src/lib/RequestAction.php b/src/lib/RequestAction.php
index ff59cc7..41756de 100644
--- a/src/lib/RequestAction.php
+++ b/src/lib/RequestAction.php
@@ -5,17 +5,17 @@ namespace MyPrayerJournal;
/**
* An action taken on a prayer request
*/
-enum RequestAction
+enum RequestAction: string
{
/** The request was created */
- case Created;
+ case Created = 'Created';
/** The request was marked as having been prayed for */
- case Prayed;
+ case Prayed = 'Prayed';
/** The request was updated */
- case Updated;
+ case Updated = 'Updated';
/** The request was marked as answered */
- case Answered;
+ case Answered = 'Answered';
}
diff --git a/src/public/components/journal-items.php b/src/public/components/journal-items.php
new file mode 100644
index 0000000..227e36b
--- /dev/null
+++ b/src/public/components/journal-items.php
@@ -0,0 +1,64 @@
+>'userId' = :userId AND data->>'$.history[0].action' <> 'Answered'",
+ [':userId' => Auth::user()['sub']], new DocumentMapper(Request::class));
+
+if ($reqs->hasItems()) { ?>
+ ';
+ foreach ($reqs->items() as /** @var Request $req */ $req) {
+ $withText = array_filter($req->history, fn($hist) => isset($hist->text));
+ $text = $withText[array_key_first($withText)]->text; ?>
+
+
+
+
+
=htmlentities($text);?>
+
+
+
+
+
+
+ =$name?>=$name == 'Your' ? '' : '’s'?> Prayer Journal
+ 'btn btn-primary']); ?>
+
+ Loading your prayer journal…
+
+
+exchange($_ENV['AUTH0_BASE_URL'] . '/user/log-on/success');
// TODO: get the possible redirect URL
-header('Location: /');
+header('Location: /journal');
exit();
diff --git a/src/start.php b/src/start.php
index d3276b3..7029ff1 100644
--- a/src/start.php
+++ b/src/start.php
@@ -1,7 +1,8 @@
user['sub'];
}
+Configuration::$pdoDSN = 'sqlite:' . implode(DIRECTORY_SEPARATOR, [__DIR__, 'data', 'mpj.db']);
+Configuration::$mode = Mode::SQLite;
+Definition::ensureTable(Table::REQUEST);
+
/**
* Is this an htmx request?
*
@@ -33,6 +38,16 @@ function page_link(string $href, string $text, array $attrs = []): void
echo ">$text";
}
+/**
+ * Generate a material icon
+ *
+ * @param string $name The name of the material icon
+ * @return string The material icon wrapped in a `span` tag
+ */
+function icon(string $name): string {
+ return "$name";
+}
+
function page_head(string $title): void
{
Layout::htmlHead($title);
@@ -42,6 +57,11 @@ function page_head(string $title): void
echo '';
}
+function bare_head(): void
+{
+ echo '';
+}
+
function page_foot(): void
{
echo '';
@@ -51,3 +71,8 @@ function page_foot(): void
}
echo '';
}
+
+function bare_foot(): void
+{
+ echo '