From 09408ef3bf6264051a940529e82c61dc956a4dc5 Mon Sep 17 00:00:00 2001 From: Dennis Benz Date: Fri, 1 Dec 2023 14:06:56 +0100 Subject: [PATCH] Fix same tag created by different users not filterable in videos table If another user adds an existing tag to a video, a new tag will be stored in the database since a tag references to the creator. Thus the video filter won't work because two tags of the same tag will be found with different ids. --- lib/Models/Videos.php | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/lib/Models/Videos.php b/lib/Models/Videos.php index a988b2b72..5f94b2778 100644 --- a/lib/Models/Videos.php +++ b/lib/Models/Videos.php @@ -209,12 +209,12 @@ protected static function getFilteredVideos($query, $filters) $tags = Tags::findBySQL($sq = 'tag LIKE ?', [$filter['value']]); if (!empty($tags)) { - foreach ($tags as $tag) { - $tag_ids[$tag->id] = [ - 'id' => $tag->id, - 'compare' => $filter['compare'] - ]; - } + $tag_ids[$filter['value']] = [ + 'tag_ids' => array_map(function ($tag) { + return $tag->id; + }, $tags), + 'compare' => $filter['compare'], + ]; } else { $tag_ids[] = '-1'; } @@ -269,15 +269,17 @@ protected static function getFilteredVideos($query, $filters) if (!empty($tag_ids)) { - foreach ($tag_ids as $tag) { - if ($tag['compare'] == '=') { - $sql .= ' INNER JOIN oc_video_tags AS t'. $tag['id'] .' ON (t'. $tag['id'] .'.video_id = oc_video.id ' - .' AND t'. $tag['id'] .'.tag_id = '. $tag['id'] .')'; + foreach ($tag_ids as $value => $tag_filter) { + $tags_param = ':tags' . $value; + $params[$tags_param] = $tag_filter['tag_ids']; + if ($tag_filter['compare'] == '=') { + $sql .= ' INNER JOIN oc_video_tags AS t'. $value .' ON (t'. $value .'.video_id = oc_video.id ' + .' AND t'. $value .'.tag_id IN ('. $tags_param .'))'; } else { - $sql .= ' LEFT JOIN oc_video_tags AS t'. $tag['id'] .' ON (t'. $tag['id'] .'.video_id = oc_video.id ' - .' AND t'. $tag['id'] .'.tag_id = '. $tag['id'] .')'; + $sql .= ' LEFT JOIN oc_video_tags AS t'. $value .' ON (t'. $value .'.video_id = oc_video.id ' + .' AND t'. $value .'.tag_id IN ('. $tags_param .'))'; - $where .= ' AND t'. $tag['id'] . '.tag_id IS NULL '; + $where .= ' AND t'. $value . '.tag_id IS NULL '; } } }