From 77097850e05920a58b5b63ac97596d5626f3571c Mon Sep 17 00:00:00 2001 From: rulingcom Date: Fri, 11 Jul 2025 22:17:00 +0800 Subject: [PATCH] added filters and file and editor search --- .../universal_tables_controller.rb | 184 +++++++++--------- .../admin/universal_tables/_column.html.erb | 7 + .../admin/universal_tables/_index.html.erb | 20 +- .../universal_tables/_table_form.html.erb | 7 + 4 files changed, 125 insertions(+), 93 deletions(-) diff --git a/app/controllers/universal_tables_controller.rb b/app/controllers/universal_tables_controller.rb index 52a5e88..dee0918 100755 --- a/app/controllers/universal_tables_controller.rb +++ b/app/controllers/universal_tables_controller.rb @@ -10,114 +10,115 @@ class UniversalTablesController < ApplicationController end end -def export_filtered - table = UTable.where(:category_id => params[:cat]).first rescue nil - page = Page.where(:page_id => params[:page_id]).first - return unless table + def export_filtered + table = UTable.where(:category_id => params[:cat]).first rescue nil + page = Page.where(:page_id => params[:page_id]).first + return unless table - host_url = Site.first.root_url - host_url = request.protocol + request.host_with_port if host_url == "http://" + host_url = Site.first.root_url + host_url = request.protocol + request.host_with_port if host_url == "http://" - @rows = [] - @tablecolumns = [] + @rows = [] + @tablecolumns = [] - # 處理 file 欄位雙欄輸出(連結與註解) - table.table_columns.where(display_in_index: true).asc(:order).each do |column| - if column.type == "file" - @tablecolumns << column - @tablecolumns << column.dup.tap { |c| c.define_singleton_method(:_file_title_column?) { true } } - else - @tablecolumns << column - end - end + # 處理 file 欄位雙欄輸出(連結與註解) + table.table_columns.where(display_in_index: true).asc(:order).each do |column| + if column.type == "file" + @tablecolumns << column + @tablecolumns << column.dup.tap { |c| c.define_singleton_method(:_file_title_column?) { true } } + else + @tablecolumns << column + end + end - entries = get_entries(params, table, page, false) + entries = get_entries(params, table, page, false) - entries.each do |te| - cols = [] + entries.each do |te| + cols = [] - @tablecolumns.each do |column| - # 跳過副註解欄(由主 file 欄處理) - if column.respond_to?(:_file_title_column?) && column._file_title_column? - next - end + @tablecolumns.each do |column| + # 跳過副註解欄(由主 file 欄處理) + if column.respond_to?(:_file_title_column?) && column._file_title_column? + next + end - ce = te.column_entries.where(table_column_id: column.id).first rescue nil + ce = te.column_entries.where(table_column_id: column.id).first rescue nil - if ce.present? - case column.type - when "text" - cols << { "text" => ce.text.to_s } + if ce.present? + case column.type + when "text" + cols << { "text" => ce.text.to_s } - when "integer" - cols << { "text" => ce.number.to_s } + when "integer" + cols << { "text" => ce.number.to_s } - when "editor" - cols << { "text" => ce.content.to_s } + when "editor" + cols << { "text" => ce.content.to_s } - when "date" - cols << { "text" => format_date(ce.date, column.date_format).to_s } + when "date" + cols << { "text" => format_date(ce.date, column.date_format).to_s } - when "period" - from = format_date(ce.period_from, column.date_format) - to = format_date(ce.period_to, column.date_format) - text = from.blank? && to.present? ? to : "#{from} ~ #{to}" - cols << { "text" => text.to_s } + when "period" + from = format_date(ce.period_from, column.date_format) + to = format_date(ce.period_to, column.date_format) + text = from.blank? && to.present? ? to : "#{from} ~ #{to}" + cols << { "text" => text.to_s } - when "image" - text = ce.image&.thumb&.url ? (host_url + ce.image.thumb.url) : "" - cols << { "text" => text } + when "image" + text = ce.image&.thumb&.url ? (host_url + ce.image.thumb.url) : "" + cols << { "text" => text } -when "file" - file_links = [] - file_titles = [] - locale = I18n.locale.to_s + when "file" + file_links = [] + file_titles = [] + locale = I18n.locale.to_s - ce.column_entry_files.desc(:sort_number).each do |entry_file| - next unless entry_file.choose_lang_display(locale) + ce.column_entry_files.desc(:sort_number).each do |entry_file| + next unless entry_file.choose_lang_display(locale) - file_links << (host_url + entry_file.get_link) + file_links << (host_url + entry_file.get_link) - title = if entry_file.respond_to?(:file_title_translations) && entry_file.file_title_translations.is_a?(Hash) - entry_file.file_title_translations[locale] - elsif entry_file.file_title.is_a?(Hash) - entry_file.file_title[locale] - else - entry_file.file_title - end + title = if entry_file.respond_to?(:file_title_translations) && entry_file.file_title_translations.is_a?(Hash) + entry_file.file_title_translations[locale] + elsif entry_file.file_title.is_a?(Hash) + entry_file.file_title[locale] + else + entry_file.file_title + end - title = entry_file.file.filename.to_s if title.blank? - file_titles << title - end + title = entry_file.file.filename.to_s if title.blank? + file_titles << title + end - cols << { "text" => file_links.join("\r\n") } - cols << { "text" => file_titles.join("\r\n") } + cols << { "text" => file_links.join("\r\n") } + cols << { "text" => file_titles.join("\r\n") } - else - cols << { "text" => "" } - end - else - if column.type == "file" - cols << { "text" => "" } - cols << { "text" => "" } - else - cols << { "text" => "" } - end - end - end + else + cols << { "text" => "" } + end + else + if column.type == "file" + cols << { "text" => "" } + cols << { "text" => "" } + else + cols << { "text" => "" } + end + end + end - @rows << { "columns" => cols } - end + @rows << { "columns" => cols } + end - excel_name = "#{table.title}.xlsx" - excel_name = 'attachment; filename="' + excel_name + '"' + excel_name = "#{table.title}.xlsx" + excel_name = 'attachment; filename="' + excel_name + '"' + + respond_to do |format| + format.xlsx { + response.headers['Content-Disposition'] = excel_name + } + end + end - respond_to do |format| - format.xlsx { - response.headers['Content-Disposition'] = excel_name - } - end -end def get_query(params) if params["column"].present? q = {params["column"] => params["q"]} @@ -146,10 +147,15 @@ end # regex = Regexp.union(regexes.map{|word| Regexp.new(".*"+word+".*", "i")}) # end regex = Regexp.new(".*"+v+".*", "i") - if column.type == "text" + case column.type + when "text" columns = column.column_entries.any_of([{"text.en" => regex}, {"text.zh_tw" => regex}]) - elsif column.type == "editor" + when "editor" columns = column.column_entries.any_of([{"content.en" => regex}, {"content.zh_tw" => regex}]) + when "file" + column_ids = column.column_entries.pluck(:id) + filtered_column_ids = ColumnEntryFile.where(:column_entry_id.in => column_ids).any_of([{ "file_title.en" => regex },{"file_title.zh_tw" => regex }]).pluck(:column_entry) + columns = ColumnEntry.where(:id.in => filtered_column_ids) end # .or(:"content.en" => regex).or(:"content.zh_tw" => regex) tmp = columns.pluck(:table_entry_id) @@ -292,7 +298,7 @@ end # csrf_input = "" csrf_input = "" have_serial_number = (page.layout != 'index1') - table_heads = table.table_columns.where(:display_in_index => true).asc(:order).collect do |tc| + table_heads = table.table_columns.where(:is_searchable => true).asc(:order).collect do |tc| field_key = tc.key field_value = params_q[field_key] field_value = nil if field_value.blank? @@ -310,7 +316,7 @@ end search = "hide" sort_url = "#" sort = "hide" - when "editor" + when "editor","file" sort_url = "#" sort = "hide" sort_class = "sort hide" diff --git a/app/views/admin/universal_tables/_column.html.erb b/app/views/admin/universal_tables/_column.html.erb index c39454b..2de8b95 100755 --- a/app/views/admin/universal_tables/_column.html.erb +++ b/app/views/admin/universal_tables/_column.html.erb @@ -81,6 +81,13 @@ <%= f.check_box :is_searchable %> Searchable + <% if column.type == "file" || column.type == "editor" %> + + + + <% end %> <% select_values = UTable::DATE_FORMATS.collect{|ft| [ft.upcase,ft]} %>