diff options
author | Evgeny Zinoviev <me@ch1p.io> | 2024-01-31 06:11:00 +0300 |
---|---|---|
committer | Evgeny Zinoviev <me@ch1p.io> | 2024-01-31 20:45:40 +0300 |
commit | c0dc531ebefd8912819f3b6c8bda1fed3c7e750c (patch) | |
tree | 2c75aa9df182260aef09faf4befd81a4c2b9c5e2 /lib/pages.php | |
parent | 48d688cdf7f9eae1bf11b8a6f0e5b98687c604cb (diff) |
make it simple, but not simpler
Diffstat (limited to 'lib/pages.php')
-rw-r--r-- | lib/pages.php | 58 |
1 files changed, 50 insertions, 8 deletions
diff --git a/lib/pages.php b/lib/pages.php index 281ee52..8524cf3 100644 --- a/lib/pages.php +++ b/lib/pages.php @@ -1,9 +1,51 @@ <?php +class Page extends model { + + const DB_TABLE = 'pages'; + const DB_KEY = 'short_name'; + + public string $title; + public string $md; + public string $html; + public int $ts; + public int $updateTs; + public bool $visible; + public string $shortName; + + function edit(array $fields) { + $fields['update_ts'] = time(); + if ($fields['md'] != $this->md) + $fields['html'] = markup::markdownToHtml($fields['md']); + parent::edit($fields); + } + + function isUpdated(): bool { + return $this->updateTs && $this->updateTs != $this->ts; + } + + function getHtml(bool $is_retina, string $user_theme): string { + $html = $this->html; + $html = markup::htmlImagesFix($html, $is_retina, $user_theme); + return $html; + } + + function getUrl(): string { + return "/{$this->shortName}/"; + } + + function updateHtml(): void { + $html = markup::markdownToHtml($this->md); + $this->html = $html; + DB()->query("UPDATE pages SET html=? WHERE short_name=?", $html, $this->shortName); + } + +} + class pages { - public static function add(array $data): ?int { - $db = getDb(); + static function add(array $data): ?int { + $db = DB(); $data['ts'] = time(); $data['html'] = markup::markdownToHtml($data['md']); if (!$db->insert('pages', $data)) @@ -11,12 +53,12 @@ class pages { return $db->insertId(); } - public static function delete(Page $page): void { - getDb()->query("DELETE FROM pages WHERE short_name=?", $page->shortName); + static function delete(Page $page): void { + DB()->query("DELETE FROM pages WHERE short_name=?", $page->shortName); } - public static function getPageByName(string $short_name): ?Page { - $db = getDb(); + static function getByName(string $short_name): ?Page { + $db = DB(); $q = $db->query("SELECT * FROM pages WHERE short_name=?", $short_name); return $db->numRows($q) ? new Page($db->fetch($q)) : null; } @@ -24,8 +66,8 @@ class pages { /** * @return Page[] */ - public static function getAll(): array { - $db = getDb(); + static function getAll(): array { + $db = DB(); return array_map('Page::create_instance', $db->fetchAll($db->query("SELECT * FROM pages"))); } |