From 5e0c6dd4272d116a6241298f63a14165dc26a8c6 Mon Sep 17 00:00:00 2001 From: Joe Date: Wed, 14 Feb 2024 22:30:41 -0600 Subject: [PATCH] add togglable thumbnails for post relationships --- ext/relationships/info.php | 2 +- ext/relationships/main.php | 48 ++++++++++++++++----- ext/relationships/script.js | 35 ++++++++++++++++ ext/relationships/theme.php | 83 +++++++++++++++++++++++++++++++------ 4 files changed, 145 insertions(+), 23 deletions(-) create mode 100644 ext/relationships/script.js diff --git a/ext/relationships/info.php b/ext/relationships/info.php index 84f9dd36..618eae30 100644 --- a/ext/relationships/info.php +++ b/ext/relationships/info.php @@ -10,7 +10,7 @@ class RelationshipsInfo extends ExtensionInfo public string $key = self::KEY; public string $name = "Post Relationships"; - public array $authors = ["Angus Johnston" => "admin@codeanimu.net"]; + public array $authors = ["Angus Johnston" => "admin@codeanimu.net", 'joe' => 'joe@thisisjoes.site']; public string $license = self::LICENSE_GPLV2; public ExtensionCategory $category = ExtensionCategory::METADATA; public string $description = "Allow posts to have relationships (parent/child)."; diff --git a/ext/relationships/main.php b/ext/relationships/main.php index 97103449..603b7c49 100644 --- a/ext/relationships/main.php +++ b/ext/relationships/main.php @@ -172,18 +172,14 @@ class Relationships extends Extension /** * @return Image[] */ - public static function get_children(Image $image, int $omit = null): array + public static function get_children(int $image_id): array { global $database; - $results = $database->get_all_iterable("SELECT * FROM images WHERE parent_id = :pid ", ["pid" => $image->id]); - $output = []; - foreach ($results as $result) { - if ($result["id"] == $omit) { - continue; - } - $output[] = new Image($result); - } - return $output; + $child_ids = $database->get_col("SELECT id FROM images WHERE parent_id = :pid ", ["pid" => $image_id]); + + $children = Search::get_images($child_ids); + + return $children; } private function remove_parent(int $imageID): void @@ -217,4 +213,36 @@ class Relationships extends Extension ["has_children" => $children > 0, "pid" => $parent_id] ); } + + public static function has_siblings(int $image_id): bool + { + global $database; + + $image = Image::by_id($image_id); + + $count = $database->get_one( + "SELECT COUNT(*) FROM images WHERE id!=:id AND parent_id=:pid", + ["id" => $image_id, "pid" => $image['parent_id']] + ); + + if ($count > 0) { + return true; + } + return false; + } + + public static function get_siblings(int $image_id): array + { + global $database; + + $image = Image::by_id($image_id); + + $sibling_ids = $database->get_col( + "SELECT id FROM images WHERE id!=:id AND parent_id=:pid", + ["id" => $image_id, "pid" => $image['parent_id']] + ); + $siblings = Search::get_images($sibling_ids); + + return $siblings; + } } diff --git a/ext/relationships/script.js b/ext/relationships/script.js new file mode 100644 index 00000000..88cefea4 --- /dev/null +++ b/ext/relationships/script.js @@ -0,0 +1,35 @@ +document.addEventListener('DOMContentLoaded', () => { + $(".shm-relationships-parent-toggle").click(function() { + $(".shm-relationships-parent-thumbs").slideToggle("fast", function() { + if($(".shm-relationships-parent-thumbs").is(":hidden")) { + $(".shm-relationships-parent-toggle").text("show »"); + Cookies.set("ui-relationships-parent-hidden", 'true'); + } + else { + $(".shm-relationships-parent-toggle").text("« hide"); + Cookies.set("ui-relationships-parent-hidden", 'false'); + } + }); + }); + if(Cookies.get("ui-relationships-parent-hidden") === 'true') { + $(".shm-relationships-parent-thumbs").hide(); + $(".shm-relationships-parent-toggle").text("show »"); + } + + $(".shm-relationships-child-toggle").click(function() { + $(".shm-relationships-child-thumbs").slideToggle("fast", function() { + if($(".shm-relationships-child-thumbs").is(":hidden")) { + $(".shm-relationships-child-toggle").text("show »"); + Cookies.set("ui-relationships-child-hidden", 'true'); + } + else { + $(".shm-relationships-child-toggle").text("« hide"); + Cookies.set("ui-relationships-child-hidden", 'false'); + } + }); + }); + if(Cookies.get("ui-relationships-child-hidden") === 'true') { + $(".shm-relationships-child-thumbs").hide(); + $(".shm-relationships-child-toggle").text("show »"); + } +}); diff --git a/ext/relationships/theme.php b/ext/relationships/theme.php index 10ed598e..fe968f7b 100644 --- a/ext/relationships/theme.php +++ b/ext/relationships/theme.php @@ -14,22 +14,48 @@ class RelationshipsTheme extends Themelet { global $page, $database; - if ($image['parent_id'] !== null) { - $a = "parent post"; - $page->add_block(new Block(null, "This post belongs to a $a.", "main", 5, "ImageHasParent")); + $parent = Search::get_images([$image['parent_id']]); + if (!empty($parent)) { + $parent_id = $image['parent_id']; + $a = "#$parent_id"; + $parent_summary_html = "This post belongs to a parent post ($a)"; + $parent_thumb_html = "
" . $this->get_parent_thumbnail_html($image) . "
"; + if (Relationships::has_siblings($image->id)) { + $visible_siblings = Relationships::get_siblings($image->id); + if (!empty($visible_siblings)) { + $parent_summary_html .= " and has " .count($visible_siblings) . (count($visible_siblings) > 1 ? " siblings" : " sibling"); + $parent_summary_html .= " ("; + foreach ($visible_siblings as $sibling) { + $parent_summary_html .= "id) . "'>#$sibling->id" . (count($visible_siblings) > 1 ? ", " : ""); + } + $parent_summary_html = trim($parent_summary_html, ', '); + $parent_summary_html .= ")"; + $parent_thumb_html .= "
" . $this->get_sibling_thumbnail_html($image) . "
"; + } + } + $parent_summary_html .= "."; + $parent_summary_html .= "« hide"; + $parent_thumb_html .= "
"; + $html = $parent_summary_html . $parent_thumb_html; + $page->add_block(new Block(null, $html, "main", 5, "PostRelationships")); } if (bool_escape($image['has_children'])) { - $ids = $database->get_col("SELECT id FROM images WHERE parent_id = :iid", ["iid" => $image->id]); - - $html = "This post has ".(count($ids) > 1 ? "child posts" : "a child post").""; - $html .= " (post "; - foreach ($ids as $id) { - $html .= "#{$id}, "; + $visible_children = Relationships::get_children($image->id); + if (!empty($visible_children)) { + $child_summary_html = "This post has ".(count($visible_children) > 1 ? "child posts" : "a child post").""; + $child_summary_html .= " (post "; + $child_thumb_html = "
"; + foreach ($visible_children as $child) { + $child_summary_html .= "id)."'>#{$child->id}, "; + $child_thumb_html .= $this->get_child_thumbnail_html(Image::by_id($child->id)); + } + $child_summary_html = rtrim($child_summary_html, ", ").")."; + $child_summary_html .= "« hide"; + $child_thumb_html .= "
"; + $html = $child_summary_html . $child_thumb_html; + $page->add_block(new Block(null, $html, "main", 5, "PostRelationships")); } - $html = rtrim($html, ", ").")."; - - $page->add_block(new Block(null, $html, "main", 6, "ImageHasChildren")); } } @@ -70,4 +96,37 @@ class RelationshipsTheme extends Themelet '; } + + private function get_parent_thumbnail_html(Image $image): HTMLElement + { + global $user; + + $parent_id = $image['parent_id']; + $parent_image = Image::by_id($parent_id); + + $html = $this->build_thumb_html($parent_image); + + return $html; + } + + private function get_child_thumbnail_html(Image $image): HTMLElement + { + $html = $this->build_thumb_html($image); + + return $html; + } + + private function get_sibling_thumbnail_html(Image $image): string + { + global $user; + + $siblings = Relationships::get_siblings($image->id); + $html = ""; + + foreach ($siblings as $sibling) { + $html .= $this->build_thumb_html($sibling); + } + + return $html; + } }