myPrayerJournal/src/start.php

79 lines
1.9 KiB
PHP
Raw Normal View History

2024-06-21 03:06:31 +00:00
<?php declare(strict_types=1);
2024-06-22 03:22:56 +00:00
use BitBadger\PDODocument\{Configuration, Definition, Mode};
2024-06-21 23:01:12 +00:00
use Dotenv\Dotenv;
2024-06-22 03:22:56 +00:00
use MyPrayerJournal\{Auth, Layout, Table};
2024-06-21 23:01:12 +00:00
require __DIR__ . '/vendor/autoload.php';
/** The version of this application */
const MPJ_VERSION = '4.0.0-alpha1';
(Dotenv::createImmutable(__DIR__))->load();
session_start();
$auth0_session = Auth::client()->getCredentials();
if (!is_null($auth0_session)) {
$_SESSION['user_id'] = $auth0_session->user['sub'];
}
2024-06-22 03:22:56 +00:00
Configuration::$pdoDSN = 'sqlite:' . implode(DIRECTORY_SEPARATOR, [__DIR__, 'data', 'mpj.db']);
Configuration::$mode = Mode::SQLite;
Definition::ensureTable(Table::REQUEST);
2024-06-21 23:01:12 +00:00
/**
* Is this an htmx request?
*
* @return bool True if this is an htmx request, false if not
*/
function is_htmx(): bool
{
return key_exists('HTTP_HX_REQUEST', $_SERVER) && !key_exists('HTTP_HX_HISTORY_RESTORE_REQUEST', $_SERVER);
2024-06-21 03:06:31 +00:00
}
function page_link(string $href, string $text, array $attrs = []): void
2024-06-21 23:01:12 +00:00
{
echo '<a href="' . $href . '" hx-get="' . $href . '" hx-target="#top" hx-swap=innerHTML hx-push-url=true';
foreach ($attrs as $key => $value) echo " $key=\"" . htmlspecialchars($value) . "\"";
echo ">$text</a>";
}
2024-06-22 03:22:56 +00:00
/**
* 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 "<span class=material-icons>$name</span>";
}
2024-06-21 23:01:12 +00:00
function page_head(string $title): void
{
Layout::htmlHead($title);
echo '<body>';
if (!is_htmx()) echo '<section id=top aria-label="Top navigation">';
Layout::navBar();
echo '<main role=main>';
2024-06-21 03:06:31 +00:00
}
2024-06-22 03:22:56 +00:00
function bare_head(): void
{
echo '<!DOCTYPE html><html lang=en><head><title></title></head><body>';
}
2024-06-21 23:01:12 +00:00
function page_foot(): void
2024-06-21 03:06:31 +00:00
{
2024-06-21 23:01:12 +00:00
echo '</main>';
if (!is_htmx()) {
echo '</section>';
Layout::htmlFoot();
}
echo '</body></html>';
}
2024-06-22 03:22:56 +00:00
function bare_foot(): void
{
echo '</body></html>';
}