74 lines
1.9 KiB
PHP
74 lines
1.9 KiB
PHP
<?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 {
|
|
|
|
static function add(array $data): ?int {
|
|
$db = DB();
|
|
$data['ts'] = time();
|
|
$data['html'] = markup::markdownToHtml($data['md']);
|
|
if (!$db->insert('pages', $data))
|
|
return null;
|
|
return $db->insertId();
|
|
}
|
|
|
|
static function delete(Page $page): void {
|
|
DB()->query("DELETE FROM pages WHERE short_name=?", $page->shortName);
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* @return Page[]
|
|
*/
|
|
static function getAll(): array {
|
|
$db = DB();
|
|
return array_map('Page::create_instance', $db->fetchAll($db->query("SELECT * FROM pages")));
|
|
}
|
|
|
|
} |