dragon_fire/www/src/db_handler/post.php

218 lines
5.4 KiB
PHP
Raw Normal View History

<?php
class Post implements ArrayAccess {
public $handler;
private $content_html;
private $content_markdown;
public $data;
public $html_data;
public $raw_data;
private $_child_posts;
2024-08-24 13:09:28 +02:00
private $_parent_post;
public static function _generate_404($post_data) {
$post_data ??= [
'id' => -1,
'path' => '/404.md',
'title' => '404 Page',
'metadata' => [
'type' => '404'
]
];
return $post_data;
}
public static function _deduce_type($path) {
$ext = pathinfo($path, PATHINFO_EXTENSION);
if(preg_match("/\.(\w+)\.md$/", $path, $ext_match)) {
$ext = $ext_match[1];
}
$ext_mapping = [
'' => 'directory',
'md' => 'text/markdown',
'png' => 'image',
'jpg' => 'image',
'jpeg' => 'image'
];
return $ext_mapping[$ext] ?? '?';
}
public static function _deduce_icon($type) {
$icon_mapping = [
'' => 'question',
'text/markdown' => 'markdown',
'markdown' => 'markdown',
'blog' => 'markdown',
'blog_list' => 'rectangle-list',
'directory' => 'folder',
'gallery' => 'images',
'image' => 'image'
];
return $icon_mapping[$type] ?? 'unknown';
}
public static function _deduce_template($type) {
$template_mapping = [
'directory' => 'directory'
];
return $template_mapping[$type] ?? 'vanilla';
}
2024-08-24 13:09:28 +02:00
function __construct($post_handler, $post_data, $site_defaults) {
$this->handler = $post_handler;
$this->content_html = null;
$this->content_markdown = null;
if(!isset($post_data) or !isset($post_data['id'])) {
$post_data = $this->_generate_404($post_data);
}
2024-08-29 20:40:36 +02:00
$data = array_merge($site_defaults, $post_data);
2024-08-24 13:09:28 +02:00
if($data['path'] == '') {
$data['path'] = '/';
$data['title'] ??= 'root';
$data['basename'] ??= 'root';
}
$post_data['host'] ??= 'localhost:8081';
2024-08-24 13:09:28 +02:00
$data['url'] ??= 'http://' . $post_data['host'] . $post_data['path'];
$data['basename'] ??= basename($data['path']);
$data['title'] ??= basename($data['path']);
$data['tags'] ??= [];
$data['type'] ??= self::_deduce_type($post_data['path']);
$data['icon'] ??= self::_deduce_icon($data['type']);
$data['template'] ??= self::_deduce_template($data['type']);
if(isset($sql_meta['media_url'])) {
$data['thumb_url'] ??= $data['media_url'];
}
$data['preview_image'] ??= $data['banners'][0]['src'] ?? null;
$data['brief'] ??= $data['title'];
$this->data = $data;
}
public function __get($name) {
if($name == 'html') {
return $this->get_html();
}
if($name == 'markdown') {
return $this->get_markdown();
}
if($name == 'json') {
return $this->to_json();
}
if($name == 'child_posts') {
return $this->get_child_posts();
}
2024-08-24 13:09:28 +02:00
if($name == 'parent') {
return $this->get_parent_post();
}
if(isset($this->data[$name])) {
return $this->data[$name];
}
2024-08-29 20:40:36 +02:00
return null;
}
public function offsetGet($offset) : mixed {
return $this->__get($offset) ?? null;
}
public function offsetExists($offset) : bool {
if(isset($this->data[$offset])) {
return true;
}
return !is_null($this->offsetGet($offset));
}
public function offsetSet($offset, $value) : void {
2024-08-24 13:09:28 +02:00
$this->data[$offset] = $value;
}
public function offsetUnset($offset) : void {
2024-08-24 13:09:28 +02:00
unset($this->data[$offset]);
}
public function get_html() {
$this->content_html ??= $this->handler->render_post($this);
return $this->content_html;
}
public function get_markdown() {
$this->content_markdown ??=
$this->handler->get_markdown_for($this);
return $this->content_markdown;
}
public function get_child_posts(...$search_args) {
if(count($search_args) == 0) {
2024-08-24 13:09:28 +02:00
$this->_child_posts ??=
$this->handler->get_children_for($this);
2024-08-24 13:09:28 +02:00
return $this->_child_posts;
}
else {
return $this->handler->get_children_for($this, ...$search_args);
}
}
public function to_array($options = []) {
2024-08-29 20:40:36 +02:00
$out_data = $this->data;
if(isset($options['markdown'])) {
$out_data['markdown'] = $this->get_markdown();
}
if(isset($options['html'])) {
$out_data['html'] = $this->get_html();
}
if(isset($options['children'])) {
$children = $this->get_child_posts();
$child_arrays = [];
foreach($children AS $child) {
array_push($child_arrays, $child->to_array());
}
$out_data['children'] = $child_arrays;
}
return $out_data;
}
public function to_json($options = []) {
return json_encode($this->to_array($options));
}
public function get_parent_post() {
$parent_path = dirname($this->data['path']);
if($parent_path == '')
return null;
2024-08-24 13:09:28 +02:00
$this->_parent_post ??= $this->handler->get_post($parent_path);
2024-08-24 13:09:28 +02:00
return $this->_parent_post;
}
}
?>