From 1999cd0825e327e3a867b34ff94027f53c6c51ab Mon Sep 17 00:00:00 2001 From: Justin DiRose Date: Thu, 22 Aug 2019 16:58:28 -0500 Subject: [PATCH] FEATURE: Add kb search helper The naming of this is currently arbitrary and may need to be changed, but currently works off the settings `knowledge explorer categories` and `knowledge explorer tags` Remove filter when plugin disabled --- plugin.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/plugin.rb b/plugin.rb index fb63084..27b1fc1 100644 --- a/plugin.rb +++ b/plugin.rb @@ -8,3 +8,21 @@ enabled_site_setting :knowledge_explorer_enabled register_asset 'stylesheets/common/knowledge-explorer.scss' load File.expand_path('../lib/knowledge_explorer/engine.rb', __FILE__) + +after_initialize do + require_dependency 'search' + + if SiteSetting.knowledge_explorer_enabled + if Search.respond_to? :advanced_filter + Search.advanced_filter(/in:kb/) do |posts| + selected_categories = SiteSetting.knowledge_explorer_categories.split("|") + categories = Category.where('slug IN (?)', selected_categories).pluck(:id) + + selected_tags = SiteSetting.knowledge_explorer_tags.split("|") + tags = Tag.where('name IN (?)', selected_tags).pluck(:id) + + posts.where('category_id IN (?) OR topics.id IN (SELECT DISTINCT(tt.topic_id) FROM topic_tags tt WHERE tt.tag_id IN (?))', categories, tags) + end + end + end +end