From d111b5e13c1b5652edf6444ce57dffda23cde4c8 Mon Sep 17 00:00:00 2001 From: butterbutt Date: Wed, 16 Mar 2022 13:13:05 -0500 Subject: [PATCH] show thumbs for relationships --- ext/relationships/main.php | 31 +++++++++++++++++ ext/relationships/style.css | 50 +++++++++++++++++++++++++-- ext/relationships/theme.php | 69 +++++++++++++++++++++++++++++++++---- 3 files changed, 140 insertions(+), 10 deletions(-) diff --git a/ext/relationships/main.php b/ext/relationships/main.php index a726ed27..f3b91bc9 100644 --- a/ext/relationships/main.php +++ b/ext/relationships/main.php @@ -210,4 +210,35 @@ 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); + + $siblings = $database->get_col( + "SELECT id FROM images WHERE id!=:id AND parent_id=:pid", + ["id"=>$image_id, "pid"=>$image->parent_id] + ); + + return $siblings; + } } diff --git a/ext/relationships/style.css b/ext/relationships/style.css index cb7e5633..e98719f8 100644 --- a/ext/relationships/style.css +++ b/ext/relationships/style.css @@ -3,13 +3,57 @@ } .shm-thumb-has_child img { - border-color: lime !important; + border-color: cyan; } .shm-thumb-has_parent img { - border-color: #cc0 !important; + border-color: cyan; } .shm-thumb-has_child.shm-thumb-has_parent img { - border-color: lime #cc0 #cc0 lime !important; + border-color: magenta #cc0 #cc0 magenta !important; +} + +#PostRelationships { + margin-top: 0; +} + +#PostRelationships * { + box-sizing: border-box; +} + +#PostRelationships .shm-relationships-thumbs { + padding-top: 5px; +} + +#PostRelationships .shm-parent-thumbs, +#PostRelationships .shm-sibling-thumbs { + display: inline-block; +} + +#PostRelationships img { + display: inline-block; + height: 96px; + width: auto; + margin: 0 5px; +} + +#PostRelationships .shm-parent-thumbs img { + border: solid; + border-color: #f5ffad; +} + +#PostRelationships .shm-child-thumbs img { + border: solid; + border-color: cyan; +} + +#PostRelationships .shm-sibling-thumbs img { + border: solid; + border-color: cyan; +} + +.shm-relationships-toggle { + padding-left: 5px; + text-align: right; } diff --git a/ext/relationships/theme.php b/ext/relationships/theme.php index e37e22cb..6ca0ecef 100644 --- a/ext/relationships/theme.php +++ b/ext/relationships/theme.php @@ -7,22 +7,77 @@ 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")); + $a = "#$image->parent_id"; + $shtml = "This post belongs to a parent post ($a)"; + $thtml = "
" . $this->get_parent_thumbnail_html($image) . "
"; + if (Relationships::has_siblings($image->id)) { + $siblings = Relationships::get_siblings($image->id); + $shtml .= " and has " .count($siblings) . (count($siblings) > 1 ? " siblings" : " sibling"); + $shtml .= " ("; + foreach ($siblings as $sibling) { + $shtml .= "#$sibling" . (count($siblings) > 1 ? ", " : ")"); + } + $thtml .= "
" . $this->get_sibling_thumbnail_html($image) . "
"; + } + $shtml .= "."; + $shtml .= "« hide"; + $thtml .= "
"; + $html = $shtml . $thtml; + $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 "; + $shtml = "This post has ".(count($ids) > 1 ? "child posts" : "a child post").""; + $shtml .= " (post "; + $thtml = "
"; foreach ($ids as $id) { - $html .= "#{$id}, "; + $shtml .= "#{$id}, "; + $thtml .= $this->get_child_thumbnail_html(Image::by_id($id)); } - $html = rtrim($html, ", ").")."; + $shtml = rtrim($shtml, ", ").")."; + $shtml .= "« hide"; + $thmtl .= "
"; + + $html = $shtml . $thtml; + + $page->add_block(new Block(null, $html, "main", 5, "PostRelationships")); + } + } + + private function get_parent_thumbnail_html(Image $image): string + { + 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): string + { + $html = $this->build_thumb_html($image); + + return $html; + } - $page->add_block(new Block(null, $html, "main", 6, "ImageHasChildren")); + private function get_sibling_thumbnail_html(Image $image): string + { + global $user; + + $siblings = Relationships::get_siblings($image->id); + $html = ""; + + foreach ($siblings as $sibling) { + $sibling_image = Image::by_id($sibling); + $html .= $this->build_thumb_html($sibling_image); } + + return $html; } public function get_parent_editor_html(Image $image): string