aboutsummaryrefslogtreecommitdiff
path: root/lib/pages.php
diff options
context:
space:
mode:
Diffstat (limited to 'lib/pages.php')
-rw-r--r--lib/pages.php58
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")));
}