feat: preliminary work on revised Post system
Some checks reported warnings
/ phplint (push) Has been cancelled

This commit is contained in:
David Bailey 2024-02-12 23:20:43 +01:00
parent b8e449006b
commit 0f2761cd61
4 changed files with 58 additions and 8 deletions

View file

@ -43,6 +43,8 @@ class PostData {
$icon_mapping = [
'' => 'question',
'text/markdown' => 'markdown',
'blog' => 'markdown',
'blog_list' => 'rectangle-list',
'directory' => 'folder',
'gallery' => 'images',
'image' => 'image'
@ -52,17 +54,18 @@ class PostData {
}
function __construct($post_handler, $sql_row) {
$this->post_handler = $post_handler;
$this->handler = $post_handler;
$this->content_html = null;
$this->content_markdown = null;
$sql_meta = null;
if(!isset($sql_row) or !$sql_row['found']) {
$sql_row = $this->_generate_404($sql_row);
$sql_meta = $sql_row['post_metadata'];
}
$sql_meta = $sql_row['post_metadata'];
if(is_string($sql_meta)) {
$sql_meta = json_decode($sql_meta, true);
@ -72,7 +75,9 @@ class PostData {
$this->sql_row = $sql_row;
$this->sql_meta = $sql_meta ?? [];
$sql_settings = json_decode($sql_row['post_settings_cache'], true) ?? [];
$sql_settings = json_decode($sql_row['post_settings_cache'], true)
?? $this->handler->get_settings_for_path($sql_row['post_path']);
$data = [
'id' => $sql_row['post_id'],
@ -133,7 +138,7 @@ class PostData {
return $this->get_html();
}
if($name == 'markdown') {
return $this->sql_row['post_content'];
return $this->get_markdown();
}
if($name == 'json') {
return $this->to_json();
@ -148,12 +153,18 @@ class PostData {
return $this->content_html;
}
public function get_markdown() {
$this->content_markdown ??=
$this->handler->get_markdown_for_id($this->data['id']);
return $this->content_markdown;
}
public function to_json($with_markdown = false, $with_html = false) {
$out_data = $this->data;
if($with_markdown) {
$out_data['markdown'] = $this->sql_row['post_content'];
$out_data['markdown'] = $this->get_markdown();
}
if($with_html) {
$out_data['html'] = $this->get_html();
@ -161,6 +172,29 @@ class PostData {
return json_encode($out_data);
}
public function get_parent_post() {
$parent_path = dirname($this->data['path']);
if($parent_path == '')
return null;
$this->parent_post ??= new PostData($this->handler,
$this->handler->get_post_by_path($parent_path));
return $this->parent_post;
}
public function get_child_posts() {
if(isset($this->child_posts))
return $this->child_posts;
$child_data = $this->handler->get_subposts_by_path($this->data['path']);
$this->child_posts = array_map(function($data) {
return new PostData($this->handler, $data);
}, $child_data);
}
}
?>