4in1_ws_web/lib/Page.php

48 lines
1.3 KiB
PHP

<?php
class Page extends model {
const DB_TABLE = 'pages';
public int $id;
public int $parentId;
public string $title;
public string $md;
public string $html;
public int $ts;
public int $updateTs;
public bool $visible;
public bool $renderTitle;
public string $shortName;
public function edit(array $fields) {
$fields['update_ts'] = time();
if ($fields['md'] != $this->md || $fields['render_title'] != $this->renderTitle || $fields['title'] != $this->title) {
$md = $fields['md'];
if ($fields['render_title'])
$md = '# '.$fields['title']."\n\n".$md;
$fields['html'] = markup::markdownToHtml($md);
}
parent::edit($fields);
}
public function isUpdated(): bool {
return $this->updateTs && $this->updateTs != $this->ts;
}
public function getHtml(bool $is_retina, string $user_theme): string {
return markup::htmlImagesFix($this->html, $is_retina, $user_theme);
}
public function getUrl(): string {
return "/{$this->shortName}/";
}
public function updateHtml(): void {
$html = markup::markdownToHtml($this->md);
$this->html = $html;
DB()->query("UPDATE pages SET html=? WHERE short_name=?", $html, $this->shortName);
}
}