add togglable thumbnails for post relationships

This commit is contained in:
Joe 2024-02-14 22:30:41 -06:00 committed by Shish
parent f033efd456
commit 5e0c6dd427
4 changed files with 145 additions and 23 deletions

View file

@ -10,7 +10,7 @@ class RelationshipsInfo extends ExtensionInfo
public string $key = self::KEY; public string $key = self::KEY;
public string $name = "Post Relationships"; 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 string $license = self::LICENSE_GPLV2;
public ExtensionCategory $category = ExtensionCategory::METADATA; public ExtensionCategory $category = ExtensionCategory::METADATA;
public string $description = "Allow posts to have relationships (parent/child)."; public string $description = "Allow posts to have relationships (parent/child).";

View file

@ -172,18 +172,14 @@ class Relationships extends Extension
/** /**
* @return Image[] * @return Image[]
*/ */
public static function get_children(Image $image, int $omit = null): array public static function get_children(int $image_id): array
{ {
global $database; global $database;
$results = $database->get_all_iterable("SELECT * FROM images WHERE parent_id = :pid ", ["pid" => $image->id]); $child_ids = $database->get_col("SELECT id FROM images WHERE parent_id = :pid ", ["pid" => $image_id]);
$output = [];
foreach ($results as $result) { $children = Search::get_images($child_ids);
if ($result["id"] == $omit) {
continue; return $children;
}
$output[] = new Image($result);
}
return $output;
} }
private function remove_parent(int $imageID): void private function remove_parent(int $imageID): void
@ -217,4 +213,36 @@ class Relationships extends Extension
["has_children" => $children > 0, "pid" => $parent_id] ["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;
}
} }

View file

@ -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 »");
}
});

View file

@ -14,22 +14,48 @@ class RelationshipsTheme extends Themelet
{ {
global $page, $database; global $page, $database;
if ($image['parent_id'] !== null) { $parent = Search::get_images([$image['parent_id']]);
$a = "<a href='".make_link("post/view/".$image['parent_id'])."'>parent post</a>"; if (!empty($parent)) {
$page->add_block(new Block(null, "This post belongs to a $a.", "main", 5, "ImageHasParent")); $parent_id = $image['parent_id'];
$a = "<a href='".make_link("post/view/".$parent_id)."'>#$parent_id</a>";
$parent_summary_html = "<span>This post belongs to a parent post ($a)";
$parent_thumb_html = "<div class='shm-relationships-parent-thumbs'><div class='shm-parent-thumbs'>" . $this->get_parent_thumbnail_html($image) . "</div>";
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 .= "<a href='" . make_link('post/view/'.$sibling->id) . "'>#$sibling->id</a>" . (count($visible_siblings) > 1 ? ", " : "");
}
$parent_summary_html = trim($parent_summary_html, ', ');
$parent_summary_html .= ")";
$parent_thumb_html .= "<div class='shm-sibling-thumbs'>" . $this->get_sibling_thumbnail_html($image) . "</div>";
}
}
$parent_summary_html .= ".</span>";
$parent_summary_html .= "<a href='#' id='relationships-parent-toggle' class='shm-relationships-parent-toggle'>« hide</a>";
$parent_thumb_html .= "</div>";
$html = $parent_summary_html . $parent_thumb_html;
$page->add_block(new Block(null, $html, "main", 5, "PostRelationships"));
} }
if (bool_escape($image['has_children'])) { if (bool_escape($image['has_children'])) {
$ids = $database->get_col("SELECT id FROM images WHERE parent_id = :iid", ["iid" => $image->id]); $visible_children = Relationships::get_children($image->id);
if (!empty($visible_children)) {
$html = "This post has <a href='".search_link(['parent='.$image->id])."'>".(count($ids) > 1 ? "child posts" : "a child post")."</a>"; $child_summary_html = "<span>This post has <a href='".make_link('post/list/parent='.$image->id.'/1')."'>".(count($visible_children) > 1 ? "child posts" : "a child post")."</a>";
$html .= " (post "; $child_summary_html .= " (post ";
foreach ($ids as $id) { $child_thumb_html = "<div class='shm-relationships-child-thumbs'><div class='shm-child-thumbs'>";
$html .= "#<a href='".make_link('post/view/'.$id)."'>{$id}</a>, "; foreach ($visible_children as $child) {
$child_summary_html .= "<a href='".make_link('post/view/'.$child->id)."'>#{$child->id}</a>, ";
$child_thumb_html .= $this->get_child_thumbnail_html(Image::by_id($child->id));
}
$child_summary_html = rtrim($child_summary_html, ", ").").";
$child_summary_html .= "</span><a href='#' id='relationships-child-toggle' class='shm-relationships-child-toggle'>« hide</a>";
$child_thumb_html .= "</div></div>";
$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
</div> </div>
'; ';
} }
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;
}
} }