From 30824d16204a4c2a6e90a1525af96df9fc6459ec Mon Sep 17 00:00:00 2001 From: "Matt K. Fu" Date: Wed, 5 Jun 2013 18:03:09 +0800 Subject: [PATCH] add search to archive --- .../default_widget/default_widget.html.erb | 7 +--- app/views/shared/_search_header.html.erb | 6 +++ .../front_end/bulletins/index.html.erb | 7 +--- .../bulletins/search_result.html.erb | 8 +--- .../front_end/archive_files_controller.rb | 41 +++++++++++++------ .../archive/app/models/archive_file.rb | 10 +++++ .../app/models/archive_file_category.rb | 17 ++++++++ .../front_end/archive_files/index.html.erb | 3 +- ...html.erb => archive_search_block.html.erb} | 2 +- .../built_in_modules/archive/config/routes.rb | 1 + vendor/built_in_modules/archive/init.rb | 5 +++ 11 files changed, 75 insertions(+), 32 deletions(-) create mode 100644 app/views/shared/_search_header.html.erb rename vendor/built_in_modules/archive/app/views/panel/archive/widget/archive_files/{bulletins_search_block.html.erb => archive_search_block.html.erb} (79%) diff --git a/app/views/default_widget/default_widget.html.erb b/app/views/default_widget/default_widget.html.erb index 5817673c..835246bd 100644 --- a/app/views/default_widget/default_widget.html.erb +++ b/app/views/default_widget/default_widget.html.erb @@ -1,9 +1,4 @@ - <% if @data.blank? and @search %> - <%=render :partial => 'shared/search_not_found' %> - <% elsif @search %> - <%= t("search.result_get",:search_word => params[:search_query],:item_num=>@data.count) unless (@data.count == 30) %> - <%= t("search.too_many",:search_word => params[:search_query],:exceed_num=>@data.count) if (@data.count ==30) %> - <% end %> +<%= render :partial => 'shared/search_header',:locals=>{:items=>@data} %> <%= render @partial %> <% if @paginate and !@data.blank? %> diff --git a/app/views/shared/_search_header.html.erb b/app/views/shared/_search_header.html.erb new file mode 100644 index 00000000..17ca5a8f --- /dev/null +++ b/app/views/shared/_search_header.html.erb @@ -0,0 +1,6 @@ + <% if items.blank? and !params[:search_query].blank? %> + <%=render :partial => 'shared/search_not_found' %> + <% elsif !params[:search_query].blank? %> + <%= t("search.result_get",:search_word => params[:search_query],:item_num=>items.count) unless (items.count == 30) %> + <%= t("search.too_many",:search_word => params[:search_query],:exceed_num=>items.count) if (items.count ==30) %> + <% end %> \ No newline at end of file diff --git a/vendor/built_in_modules/announcement/app/views/panel/announcement/front_end/bulletins/index.html.erb b/vendor/built_in_modules/announcement/app/views/panel/announcement/front_end/bulletins/index.html.erb index 9e2ade22..2182869f 100644 --- a/vendor/built_in_modules/announcement/app/views/panel/announcement/front_end/bulletins/index.html.erb +++ b/vendor/built_in_modules/announcement/app/views/panel/announcement/front_end/bulletins/index.html.erb @@ -17,12 +17,7 @@ <%= t("search.unit_get",:unit_name => params[:name],:item_num=>@bulletins.count) %> <% end %> - <% if @bulletins.blank? and !params[:search_query].blank? %> - <%=render :partial => 'shared/search_not_found' %> - <% elsif !params[:search_query].blank? %> - <%= t("search.result_get",:search_word => params[:search_query],:item_num=>@bulletins.count) unless (@bulletins.count == 30) %> - <%= t("search.too_many",:search_word => params[:search_query],:exceed_num=>@bulletins.count) if (@bulletins.count ==30) %> - <% end %> +<%= render :partial => 'shared/search_header',:locals=>{:items=>@bulletins} %> <% @bulletins.each do |post| %> diff --git a/vendor/built_in_modules/announcement/app/views/panel/announcement/front_end/bulletins/search_result.html.erb b/vendor/built_in_modules/announcement/app/views/panel/announcement/front_end/bulletins/search_result.html.erb index ce601ea3..5f1892ef 100644 --- a/vendor/built_in_modules/announcement/app/views/panel/announcement/front_end/bulletins/search_result.html.erb +++ b/vendor/built_in_modules/announcement/app/views/panel/announcement/front_end/bulletins/search_result.html.erb @@ -17,12 +17,8 @@ <%= t("search.unit_get",:unit_name => params[:name],:item_num=>@bulletins.count) %> <% end %> - <% if @bulletins.blank? and !params[:search_query].blank? %> - <%=render :partial => 'shared/search_not_found' %> - <% elsif !params[:search_query].blank? %> - <%= t("search.result_get",:search_word => params[:search_query],:item_num=>@bulletins.count) unless (@bulletins.count == 30) %> - <%= t("search.too_many",:search_word => params[:search_query],:exceed_num=>@bulletins.count) if (@bulletins.count ==30) %> - <% end %> + + <%= render :partial => 'shared/search_header',:locals=>{:items=>@bulletins} %> <% @bulletins.each do |post| %> diff --git a/vendor/built_in_modules/archive/app/controllers/panel/archive/front_end/archive_files_controller.rb b/vendor/built_in_modules/archive/app/controllers/panel/archive/front_end/archive_files_controller.rb index 80cc6064..a0d1cc89 100644 --- a/vendor/built_in_modules/archive/app/controllers/panel/archive/front_end/archive_files_controller.rb +++ b/vendor/built_in_modules/archive/app/controllers/panel/archive/front_end/archive_files_controller.rb @@ -5,6 +5,18 @@ class Panel::Archive::FrontEnd::ArchiveFilesController < OrbitWidgetController @app_title = 'archive_files' end + def get_search_result + @search = ArchiveFileCategory.solr_search do + fulltext params[:search_query] + with(:frontend_search,true) + end + + search_result = @search.results.collect{|result| result.id} + params[:category_id] = @search.results.collect{|result| result.id} + + @archive_file_categorys = ArchiveFileCategory.any_in(_id:search_result).page( params[:page_main]).per(@page_num) + end + def index @item = Page.find(params[:page_id]) @title = @item.title @@ -16,22 +28,27 @@ class Panel::Archive::FrontEnd::ArchiveFilesController < OrbitWidgetController date_now = Time.now @archive_file_categorys = ArchiveFileCategory.all - # @archive_files = ArchiveFile.where( :is_hidden => false ).desc(:is_top).page(params[:page]).per(@page_num) - if !params[:category_id].blank? && !params[:tag_id].blank? - @archive_files = @archive_files.can_display.where(:archive_file_category_id.in => params[:category_id], :tagged_ids.in => params[:tag_id]).desc( :is_top, :created_at ).page( params[:page_main] ).per(@page_num) - - @current_category = ArchiveFileCategory.from_id(params[:category_id]) rescue nil - - elsif !params[:category_id].blank? - @archive_files = ArchiveFile.can_display.where(:archive_file_category_id.in => params[:category_id]).desc( :is_top, :created_at ).page( params[:page_main] ).per(@page_num) - @current_category = ArchiveFileCategory.from_id(params[:category_id]) rescue nil - elsif !params[:tag_id].blank? - @archive_files = ArchiveFile.can_display.where(:tagged_ids.in => params[:tag_id]).desc( :is_top, :created_at ).page( params[:page_main] ).per(@page_num) + if params[:search_query] + get_search_result + @archive_files = ArchiveFile.can_display.where(:archive_file_category_id.in => @archive_file_categorys.collect{|t| t.id}).desc( :is_top, :created_at ).page( params[:page_main] ).per(@page_num) else - @archive_files = ArchiveFile.can_display.desc( :is_top, :created_at ).page( params[:page_main] ).per(@page_num) + if !params[:category_id].blank? && !params[:tag_id].blank? + @archive_files = @archive_files.can_display.where(:archive_file_category_id.in => params[:category_id], :tagged_ids.in => params[:tag_id]).desc( :is_top, :created_at ).page( params[:page_main] ).per(@page_num) + + @current_category = ArchiveFileCategory.from_id(params[:category_id]) rescue nil + + elsif !params[:category_id].blank? + @archive_files = ArchiveFile.can_display.where(:archive_file_category_id.in => params[:category_id]).desc( :is_top, :created_at ).page( params[:page_main] ).per(@page_num) + @current_category = ArchiveFileCategory.from_id(params[:category_id]) rescue nil + elsif !params[:tag_id].blank? + @archive_files = ArchiveFile.can_display.where(:tagged_ids.in => params[:tag_id]).desc( :is_top, :created_at ).page( params[:page_main] ).per(@page_num) + else + @archive_files = ArchiveFile.can_display.desc( :is_top, :created_at ).page( params[:page_main] ).per(@page_num) + end end + get_categorys end diff --git a/vendor/built_in_modules/archive/app/models/archive_file.rb b/vendor/built_in_modules/archive/app/models/archive_file.rb index 1329125e..0b857757 100644 --- a/vendor/built_in_modules/archive/app/models/archive_file.rb +++ b/vendor/built_in_modules/archive/app/models/archive_file.rb @@ -36,6 +36,16 @@ class ArchiveFile after_save :save_archive_file_multiples + # searchable do + # text :titles do + # title_translations.to_a.collect{|t| t[1]} + # end + # boolean :frontend_search do + # !is_hidden + # end + + # end + def self.search( category_id = nil ) if category_id.to_s.size > 0 diff --git a/vendor/built_in_modules/archive/app/models/archive_file_category.rb b/vendor/built_in_modules/archive/app/models/archive_file_category.rb index 1bd00fbf..9942c4da 100644 --- a/vendor/built_in_modules/archive/app/models/archive_file_category.rb +++ b/vendor/built_in_modules/archive/app/models/archive_file_category.rb @@ -5,6 +5,7 @@ class ArchiveFileCategory include Mongoid::Timestamps include OrbitCoreLib::ObjectAuthable include OrbitCoreLib::ObjectDisable + include Sunspot::Mongo # include Mongoid::MultiParameterAttributes APP_NAME = 'Archive' ObjectAuthTitlesOptions = %W{submit_new fact_check} @@ -19,6 +20,22 @@ class ArchiveFileCategory validates :title, :at_least_one => true + searchable do + text :titles do + title_translations.to_a.collect{|t| t[1]} + end + + boolean :frontend_search do + !disable + end + + text :files do + archive_files.can_display.collect{|t| t.title_translations.to_a.collect{|t| t[1]}} + end + + end + + def pp_object title end diff --git a/vendor/built_in_modules/archive/app/views/panel/archive/front_end/archive_files/index.html.erb b/vendor/built_in_modules/archive/app/views/panel/archive/front_end/archive_files/index.html.erb index d16688df..3028774d 100644 --- a/vendor/built_in_modules/archive/app/views/panel/archive/front_end/archive_files/index.html.erb +++ b/vendor/built_in_modules/archive/app/views/panel/archive/front_end/archive_files/index.html.erb @@ -1,5 +1,6 @@ <% # encoding: utf-8 %> +<%= render :partial => 'shared/search_header',:locals=>{:items=>@archive_files} %>
@@ -14,7 +15,7 @@ <% @archive_files.each_with_index do |post,i| %> - class="even" <% end %>> + class="even" <% end %>>
<%= post.title %>
diff --git a/vendor/built_in_modules/archive/app/views/panel/archive/widget/archive_files/bulletins_search_block.html.erb b/vendor/built_in_modules/archive/app/views/panel/archive/widget/archive_files/archive_search_block.html.erb similarity index 79% rename from vendor/built_in_modules/archive/app/views/panel/archive/widget/archive_files/bulletins_search_block.html.erb rename to vendor/built_in_modules/archive/app/views/panel/archive/widget/archive_files/archive_search_block.html.erb index f49bf84a..4475e343 100644 --- a/vendor/built_in_modules/archive/app/views/panel/archive/widget/archive_files/bulletins_search_block.html.erb +++ b/vendor/built_in_modules/archive/app/views/panel/archive/widget/archive_files/archive_search_block.html.erb @@ -1,4 +1,4 @@ -<%= form_tag panel_announcement_front_end_bulletins_path, method: :get do %> +<%= form_tag panel_archive_front_end_archive_files_path, method: :get do %> <%= hidden_field_tag :category_id, params[:category_id] %> <%= hidden_field_tag :tag_id, params[:tag_id] %>

diff --git a/vendor/built_in_modules/archive/config/routes.rb b/vendor/built_in_modules/archive/config/routes.rb index 45a1b1a5..b620dbee 100644 --- a/vendor/built_in_modules/archive/config/routes.rb +++ b/vendor/built_in_modules/archive/config/routes.rb @@ -16,6 +16,7 @@ Rails.application.routes.draw do end namespace :widget do match "index" => "archive_files#index" + match "archive_search_block" => "archive_files#archive_search_block" end end end diff --git a/vendor/built_in_modules/archive/init.rb b/vendor/built_in_modules/archive/init.rb index c12fbbd3..ab3bbd16 100644 --- a/vendor/built_in_modules/archive/init.rb +++ b/vendor/built_in_modules/archive/init.rb @@ -34,6 +34,11 @@ module Archive widget_i18n "archive.widget.index" style ["1"] end + + customize_widget "archive_search_block" do + widget_i18n "announcement.widget.search" + end + # item "index","announcement.widget.index",:default_template=>true,:fields=>["title","category","postdate"] # item "bulletins_and_web_links","announcement.widget.bulletins_and_web_links" end