diff --git a/app/controllers/universal_tables_controller.rb b/app/controllers/universal_tables_controller.rb index 98242cf..5b953be 100644 --- a/app/controllers/universal_tables_controller.rb +++ b/app/controllers/universal_tables_controller.rb @@ -7,18 +7,24 @@ class UniversalTablesController < ApplicationController if !table.nil? reset = "hide" csrf_value = (0...46).map { ('a'..'z').to_a[rand(26)] }.join - params_column = params["column"].to_s.gsub("\"",'') - params_q = params["q"].to_s.gsub("\"",'') + params_q = params["q"].to_h.each{|_, v| v.gsub!("\"", '')} rescue {} params_no = params["page_no"].to_s.gsub("\"",'') + if params_q.present? + query_string = '&' + params_q.map{|k, v| ["q[#{k}]",v]}.to_h.to_query + else + query_string = '' + end + query_string += "&page_no=#{params_no}" if params_no.present? + csrf_input = "" table_heads = table.table_columns.where(:display_in_index => true).asc(:order).collect do |tc| + field_key = tc.key + field_value = params_q[field_key] + field_value = nil if field_value.blank? search = "" sort_class = "sort" sort = "" - form_field = "" - query_string = "" - query_string = "&column=#{params_column}&q=#{params_q}" if params["column"].present? - query_string = query_string + "&page_no=#{params_no}" if params["page_no"].present? - sort_url = "/#{I18n.locale.to_s}#{page.url}?sortcolumn=#{tc.key}&sort=asc#{query_string}" + form_field = "#{csrf_input}" + sort_url = "/#{I18n.locale.to_s}#{page.url}?sortcolumn=#{field_key}&sort=asc#{query_string}" title_class = "" case tc.type when "date" @@ -35,26 +41,36 @@ class UniversalTablesController < ApplicationController when "text" if tc.make_categorizable select_values = tc.column_entries.distinct("text.#{I18n.locale.to_s}") - form_field = "" select_values.each do |sv| - form_field = form_field + "" + if field_value && sv == field_value + selected = " selected" + else + selected = "" + end + form_field += "" end - form_field = form_field + "" + form_field += "" end when "integer" if tc.make_categorizable select_values = tc.column_entries.distinct(:number) - form_field = "" select_values.each do |sv| - form_field = form_field + "" + if field_value && sv == field_value + selected = " selected" + else + selected = "" + end + form_field += "" end - form_field = form_field + "" + form_field += "" end end - if params["sortcolumn"] == tc.key + if params["sortcolumn"] == field_key sort_class = params["sort"] == "asc" ? "sort-desc" : "sort-asc" - sort_url = "/#{I18n.locale.to_s}#{page.url}?sortcolumn=#{tc.key}&sort=#{params["sort"] == "asc" ? "desc" : "asc"}#{query_string}" + sort_url = "/#{I18n.locale.to_s}#{page.url}?sortcolumn=#{field_key}&sort=#{params["sort"] == "asc" ? "desc" : "asc"}#{query_string}" end title_class = title_class + "no-sort" if sort == "sort" @@ -63,7 +79,7 @@ class UniversalTablesController < ApplicationController { "title" => tc.title, "type" => tc.type, - "key" => tc.key, + "key" => field_key, "search" => search, "form-field" => form_field, "sort" => sort, @@ -98,7 +114,7 @@ class UniversalTablesController < ApplicationController end end export_button = "" - if params["column"].present? + if get_query(params).present? reset = "" if !OrbitHelper.current_user.nil? p = {} @@ -186,28 +202,54 @@ class UniversalTablesController < ApplicationController end end + def get_query(params) + if params["column"].present? + q = {params["column"] => params["q"]} + else + q = params["q"] + end + if q.present? + q.map{|k,v| [k, v.to_s.strip] if v.present?}.compact.to_h + else + nil + end + end + def get_entries(params, table, page, paginated=true) entries = [] - if params["column"].present? - keywords = params["q"] - keywords = keywords.strip.nil? ? keywords : keywords.strip - column = table.table_columns.where(:key => params["column"]).first - if column.make_categorizable - regex = Regexp.new(".*"+keywords+".*", "i") + q = get_query(params) + if q.present? + table_entry_ids = nil + q.each do |k, v| + column = table.table_columns.where(:key => k).first + next if column.nil? + if column.make_categorizable + regex = Regexp.new(".*"+v+".*", "i") + else + regexes = v.split(/\s+(?=(?:[^"]*"[^"]*")*[^"]*$)/) + regex = Regexp.union(regexes.map{|word| Regexp.new(".*"+word+".*", "i")}) + end + if column.type == "text" + columns = column.column_entries.any_of([{"text.en" => regex}, {"text.zh_tw" => regex}]) + elsif column.type == "editor" + columns = column.column_entries.any_of([{"content.en" => regex}, {"content.zh_tw" => regex}]) + end + # .or(:"content.en" => regex).or(:"content.zh_tw" => regex) + tmp = columns.pluck(:table_entry_id) + if table_entry_ids.nil? + table_entry_ids = tmp + else + table_entry_ids = table_entry_ids & tmp + end + end + if table_entry_ids.nil? + entries = TableEntry.where(:u_table_id=>table.id) else - regexes = keywords.split(/\s+(?=(?:[^"]*"[^"]*")*[^"]*$)/) - regex = Regexp.union(regexes.map{|word| Regexp.new(".*"+word+".*", "i")}) + entries = TableEntry.where(:id.in=> table_entry_ids) end - if column.type == "text" - columns = column.column_entries.any_of([{"text.en" => regex}, {"text.zh_tw" => regex}]) - elsif column.type == "editor" - columns = column.column_entries.any_of([{"content.en" => regex}, {"content.zh_tw" => regex}]) - end - # .or(:"content.en" => regex).or(:"content.zh_tw" => regex) + entries = TableEntry.sorted(entries: entries, params: params, table: table, paginated: paginated) if paginated - entries = TableEntry.where(:u_table_id=>table.id).sorting(params: params,table: table,column_entries: columns,page_num: params["page_no"],per: OrbitHelper.page_data_count) - else - entries = TableEntry.where(:u_table_id=>table.id).sorting(params: params,table: table,column_entries: columns,paginated: false) + entries = entries.page(params["page_no"]).per(OrbitHelper.page_data_count) end else if paginated diff --git a/app/models/table_entry.rb b/app/models/table_entry.rb index f298471..d72c106 100644 --- a/app/models/table_entry.rb +++ b/app/models/table_entry.rb @@ -23,13 +23,13 @@ class TableEntry UTable.find(criteria.selector['u_table_id']) end - def self.sorting(params: nil,table: nil,field: nil,direction: nil,page_num: nil,per: nil,column_entries: nil,paginated: true) - page_num = 1 if page_num.blank? - page_num = page_num.to_i + def self.get_sort_field(params: nil, table: nil) + field = nil + direction = nil if table.nil? - table = u_table + table = self.u_table end - if field.nil? || direction.nil? + if params if !params[:sortcolumn].blank? field = params[:sortcolumn] direction = params[:sort] @@ -38,7 +38,7 @@ class TableEntry direction = params[:order].blank? ? 'desc' : params[:order] end if field.nil? - field,direction = table.default_ordered + field, direction = table.default_ordered else field = field.to_s if !(field=='created_at' || field == 'sort_number') @@ -46,6 +46,39 @@ class TableEntry end end end + [table, field, direction] + end + + def self.sorted(entries: nil, params: nil, table: nil, field: nil, direction: nil, paginated: true) + if field.nil? || direction.nil? + table, field, direction = self.get_sort_field(params: params, table: table) + end + if entries.nil? + entries = table.table_entries + end + if (field=='created_at' || field == 'sort_number') + values = entries.order_by({field => direction}) + else + column_to_sort = field + if entries.selector.present? + column_entries = ColumnEntry.where(:table_column_id=>column_to_sort.id,:table_entry_id.in => entries.pluck(:id)).order_by(column_to_sort.sort_hash(direction)) + else + column_entries = ColumnEntry.where(:table_column_id=>column_to_sort.id).order_by(column_to_sort.sort_hash(direction)) + end + values = column_entries.map{|v| v.table_entry} + if paginated + values = Kaminari.paginate_array(values) + end + end + values + end + + def self.sorting(params: nil,table: nil,field: nil,direction: nil,page_num: nil,per: nil,column_entries: nil,paginated: true) + page_num = 1 if page_num.blank? + page_num = page_num.to_i + if field.nil? || direction.nil? + table, field, direction = self.get_sort_field(params: params, table: table) + end if (field=='created_at' || field == 'sort_number') if column_entries.nil? diff --git a/modules/universal_table/index.html.erb b/modules/universal_table/index.html.erb index 8515dcf..a90f105 100644 --- a/modules/universal_table/index.html.erb +++ b/modules/universal_table/index.html.erb @@ -48,6 +48,7 @@ width: 120px; } +

{{table-name}}

@@ -64,13 +65,10 @@ @@ -82,6 +80,7 @@
+
{{total_entries}}
{{export_button}}
{{pagination_goes_here}} diff --git a/modules/universal_table/index2.html.erb b/modules/universal_table/index2.html.erb index 21be44b..5aa43eb 100644 --- a/modules/universal_table/index2.html.erb +++ b/modules/universal_table/index2.html.erb @@ -66,6 +66,7 @@ width: 120px; } +

{{table-name}}

@@ -82,13 +83,10 @@ @@ -100,6 +98,7 @@
+
{{total_entries}}
{{export_button}}
{{pagination_goes_here}}