From 8ec4b19c2854b8a74139ed1877eeec26006d1774 Mon Sep 17 00:00:00 2001 From: bohung Date: Thu, 4 Mar 2021 17:21:51 +0800 Subject: [PATCH] Add search feature. --- .../personal_plugin_fields_controller.rb | 3 +- .../personal_plugin_templates_controller.rb | 101 +++++++++++++++++- template_generator/config/locales/en.yml | 3 + template_generator/config/locales/zh_tw.yml | 3 + .../index_search1.html.erb | 52 +++++++++ .../personal_plugin_template/info.json | 8 ++ 6 files changed, 165 insertions(+), 5 deletions(-) create mode 100644 template_generator/modules/personal_plugin_template/index_search1.html.erb diff --git a/app/controllers/admin/personal_plugin_fields_controller.rb b/app/controllers/admin/personal_plugin_fields_controller.rb index 05a688b..52cd83e 100644 --- a/app/controllers/admin/personal_plugin_fields_controller.rb +++ b/app/controllers/admin/personal_plugin_fields_controller.rb @@ -297,7 +297,8 @@ class Admin::PersonalPluginFieldsController < OrbitAdminController "analysis_field_input_fields" => analysis_field_input_fields, "before_save_codes" => before_save_codes, "time_fields_text" => time_fields.to_s, - "iterate_step_text" => iterate_step_text + "iterate_step_text" => iterate_step_text, + "plugin_template_related_members" => plugin_template_related_members.to_s } max_length = @match_pattern.keys.map{|k| k.length}.max @match_pattern = @match_pattern.sort_by{|k,v| (v.class != Array) ? (max_length - k.length) : -k.length} diff --git a/template_generator/app/controllers/personal_plugin_templates_controller.rb b/template_generator/app/controllers/personal_plugin_templates_controller.rb index abb4f6e..1b4003a 100644 --- a/template_generator/app/controllers/personal_plugin_templates_controller.rb +++ b/template_generator/app/controllers/personal_plugin_templates_controller.rb @@ -1,9 +1,14 @@ class PersonalPluginTemplatesController < ApplicationController def index - plugin_templates = PluginTemplate.where(:is_hidden=>false).order_by(:year=>'desc').page(OrbitHelper.params[:page_no]).per(OrbitHelper.page_data_count) + params = OrbitHelper.params + plugin_templates = PluginTemplate.where(:is_hidden=>false).order_by(plugin_template_sort_hash) + fields_to_show = col_name_to_show_in_index_page + if params[:keywords].present? + plugin_templates = filter_keywords(plugin_templates,params[:selectbox],params[:keywords]) + end + plugin_templates = plugin_templates.page(params[:page_no]).per(OrbitHelper.page_data_count) plugin_templates_list = plugin_templates.collect do |plugin_template| member = plugin_template.member_profile - fields_to_show = col_name_to_show_in_index_page fields_hash = fields_to_show.map{|field| [field, get_display_field(plugin_template,field)]}.to_h @@ -14,12 +19,34 @@ class PersonalPluginTemplatesController < ApplicationController end extras = extra_translate_title - + choice_show = [] + fields_to_show.each do |fs| + choice_show << t("personal_plugin_template.#{fs}") + end + choice_value = fields_to_show + choice_value.unshift('default') + choice_select = choice_value.map { |iter| iter == params[:selectbox] ? 'selected' : '' } + choice_select = choice_select.map { |value| { 'choice_select' => value } } + choice_value = choice_value.map { |value| { 'choice_value' => value } } + choice_default = t('personal_plugin_template.extend_translate.select_class') + choice_show.unshift(choice_default) + choice_show = choice_show.map { |value| { 'choice_show' => value } } + choice = choice_value.zip(choice_show, choice_select) + choice = choice.map { |value| value.inject :merge } + select_text = t('personal_plugin_template.extend_translate.search_class') + search_text = t('personal_plugin_template.extend_translate.word_to_search') + + extras = extras.merge({ 'url' => '/' + I18n.locale.to_s + params[:url], + 'select_text' => select_text, + 'search_text' => search_text, + 'search_value' => params[:keywords].to_s.gsub(/\"/,'') + }) extras["widget-title"] = I18n.t("module_name.personal_plugin_template") { "plugin_templates" => plugin_templates_list, "extras" => extras, - "total_pages" => plugin_templates.total_pages + "total_pages" => plugin_templates.total_pages, + 'choice' => choice } end @@ -34,4 +61,70 @@ class PersonalPluginTemplatesController < ApplicationController display_field_code return value end + def filter_keywords(plugin_templates,select_field,keywords) + member_fields = plugin_template_related_members + file_fields = plugin_template_related_files_text + link_fields = plugin_template_related_links_text + if select_field == "default" + plugin_templates = plugin_templates.where(:slug_title=>/#{gsub_invalid_character(keywords)}/) + elsif select_field == "member_profile" + ms = MemberProfile.all.select{|m| m.name.include?(keywords)} + plugin_templates = plugin_templates.where(:member_profile_id.in=>ms.map{|m| m.id}) + elsif member_fields.include?(select_field) + ms = MemberProfile.all.select{|m| m.name.include?(keywords)} + m_ids = ms.map{|m| m.id.to_s } + tmp_plugin_templates = plugin_templates.select{|p| (p.send("#{select_field.singularize}_ids") & m_ids).count != 0} + plugin_templates = plugin_templates.where(:id.in=>tmp_plugin_templates.map{|p| p.id}) + elsif select_field.split(".").count > 1 + relate_name = select_field.split(".").first + field_name = select_field.split(".").last + relate = relate_name.classify.constantize + relate_ids = relate.where(field_name.to_sym=>/#{gsub_invalid_character(keywords)}/).pluck(:id) + plugin_templates = plugin_templates.where("#{relate_name.singularize}_id".to_sym.in=>relate_ids) + elsif (PluginTemplate.fields[select_field].options[:type] == Date rescue false) + keywords = keywords.split(/[\/\-]/) + if keywords.count > 1 + Date.parse(keywords.join("/")) + else + start_time = Date.parse(keywords[0] + "/1/1") + end_time = Date.parse(keywords[0] + "/12/31") + plugin_templates = plugin_templates.where(select_field.to_sym.gte=>start_time,select_field.to_sym.lte=>end_time) + end + elsif (PluginTemplate.fields[select_field].options[:type] == DateTime rescue false) + keywords = keywords.split(/[\/\-]/) + if keywords.count > 1 + DateTime.parse(keywords.join("/")) + elsif keywords[0].include?(":") + tmp_plugin_templates = plugin_templates.select{|p| (p.send(select_field).strftime('%Y/%m/%d %H:%M').include?(keywords[0]) rescue false)} + plugin_templates = plugin_templates.where(:id.in=>tmp_plugin_templates.map{|p| p.id}) + else + start_time = DateTime.parse(keywords[0] + "/1/1 00:00") + end_time = DateTime.parse(keywords[0] + "/12/31 23:59") + plugin_templates = plugin_templates.where(select_field.to_sym.gte=>start_time,select_field.to_sym.lte=>end_time) + end + elsif (PluginTemplate.fields[select_field].options[:type] == Integer rescue false) + tmp_plugin_templates = plugin_templates.select{|p| p.send(select_field).to_s.include?(keywords)} + plugin_templates = plugin_templates.where(:id.in=>tmp_plugin_templates.map{|p| p.id}) + elsif file_fields.include?(select_field) + file_field = select_field.classify.constantize + ids1 = file_field.where(:file=>/#{gsub_invalid_character(keywords)}/).pluck(:id) + ids2 = file_field.where(:title=>/#{gsub_invalid_character(keywords)}/).pluck(:id) + ids = ids1 + ids2 + tmp_plugin_templates = plugin_templates.select{|p| (p.send("#{select_field}_ids") & ids).count != 0} + plugin_templates = plugin_templates.where(:id.in=>tmp_plugin_templates.map{|p| p.id}) + elsif link_fields.include?(select_field) + link_field = select_field.classify.constantize + ids1 = link_field.where(:title=>/#{gsub_invalid_character(keywords)}/).pluck(:id) + ids2 = link_field.where(:url=>/#{gsub_invalid_character(keywords)}/).pluck(:id) + ids = ids1 + ids2 + tmp_plugin_templates = plugin_templates.select{|p| (p.send("#{select_field}_ids") & ids).count != 0} + plugin_templates = plugin_templates.where(:id.in=>tmp_plugin_templates.map{|p| p.id}) + else + plugin_templates = plugin_templates.where(select_field.to_sym=>/#{gsub_invalid_character(keywords)}/) + end + return plugin_templates + end + def gsub_invalid_character(text) + text.to_s.gsub(/(\/|\*|\\|\]|\[|\(|\)|\.|\+|\?|\!)/){|ff| "\\"+ff} + end end diff --git a/template_generator/config/locales/en.yml b/template_generator/config/locales/en.yml index e7db082..1d46adf 100644 --- a/template_generator/config/locales/en.yml +++ b/template_generator/config/locales/en.yml @@ -16,5 +16,8 @@ en: start_year_month: Start year/month end_year_month: End year/month total_number: Total number + select_class: "——select class——" + search_class: "search class:" + word_to_search: "word to search:" graph_by: "Graph By" col_name_translate_yaml \ No newline at end of file diff --git a/template_generator/config/locales/zh_tw.yml b/template_generator/config/locales/zh_tw.yml index 9fc705e..b02335e 100644 --- a/template_generator/config/locales/zh_tw.yml +++ b/template_generator/config/locales/zh_tw.yml @@ -16,5 +16,8 @@ zh_tw: start_year_month: 開始年月 end_year_month: 結束年月 total_number: 總數量 + select_class: "——選取分類——" + search_class: "搜尋類別:" + word_to_search: "關鍵字搜尋:" graph_by: "Graph By" col_name_translate_yaml \ No newline at end of file diff --git a/template_generator/modules/personal_plugin_template/index_search1.html.erb b/template_generator/modules/personal_plugin_template/index_search1.html.erb new file mode 100644 index 0000000..dfa6ceb --- /dev/null +++ b/template_generator/modules/personal_plugin_template/index_search1.html.erb @@ -0,0 +1,52 @@ + +

{{widget-title}}

+
+
+ + {{select_text}} + + {{search_text}} + + + Clear +
+
+ + + + + parse_again_start + + parse_again_end + + + + + parse_again_start + + parse_again_end + + +

{{widget-title}}

{{th-col_name_to_show_in_index_page_arr}}
{{col_name_to_show_in_index_page_arr}}
+{{pagination_goes_here}} + + \ No newline at end of file diff --git a/template_generator/modules/personal_plugin_template/info.json b/template_generator/modules/personal_plugin_template/info.json index cda3d13..bdb0c2c 100644 --- a/template_generator/modules/personal_plugin_template/info.json +++ b/template_generator/modules/personal_plugin_template/info.json @@ -7,6 +7,14 @@ "en" : "1. List" }, "thumbnail" : "thumb.png" + }, + { + "filename" : "index_search1", + "name" : { + "zh_tw" : "2. 列表(含搜尋)", + "en" : "2. List which includes search" + }, + "thumbnail" : "thumb.png" } ] } \ No newline at end of file