[tag_history] add diff colour-coding to tag changes

This commit is contained in:
discomrade 2024-06-16 14:10:44 +00:00 committed by Shish
parent 8c2b6ade4b
commit f50795029a
3 changed files with 40 additions and 2 deletions

View file

@ -301,6 +301,22 @@ class TagHistory extends Extension
", ["offset" => ($page_id - 1) * 100]);
}
/**
* @return array<string, mixed>|null
*/
public function get_previous_tags(int $image_id, int $id): ?array
{
global $database;
$row = $database->get_row("
SELECT tags
FROM tag_histories
WHERE image_id = :image_id AND id < :id
ORDER BY id DESC
LIMIT 1
", ["image_id" => $image_id, "id" => $id]);
return ($row ? $row : null);
}
/**
* This function attempts to revert all changes by a given IP within an (optional) timeframe.
*/

View file

@ -0,0 +1,2 @@
.added-tag{background:lightgreen;}
.deleted-tag{background:pink;text-decoration:line-through;}

View file

@ -132,10 +132,30 @@ class TagHistoryTheme extends Themelet
: null;
$setter = A(["href" => make_link("user/" . url_escape($name))], $name);
$th = new TagHistory();
$pt = $th->get_previous_tags($image_id, $current_id);
if ($pt) {
$previous_tags = Tag::explode($pt["tags"]);
}
$current_tags = Tag::explode($current_tags);
if ($pt) {
$tags = array_unique(array_merge($current_tags, $previous_tags));
sort($tags);
} else {
$tags = $current_tags;
}
$taglinks = SPAN();
foreach ($current_tags as $tag) {
$taglinks->appendChild(A(["href" => search_link([$tag])], $tag));
foreach ($tags as $tag) {
$class = "";
if ($pt) {
if (!in_array($tag, $previous_tags)) {
$class = "added-tag";
}
if (!in_array($tag, $current_tags)) {
$class = "deleted-tag";
}
}
$taglinks->appendChild(A(["href" => search_link([$tag]), "class" => $class], $tag));
$taglinks->appendChild(" ");
}