diff --git a/app/controllers/universal_tables_controller.rb b/app/controllers/universal_tables_controller.rb index 7e483dd..e2f89a2 100644 --- a/app/controllers/universal_tables_controller.rb +++ b/app/controllers/universal_tables_controller.rb @@ -61,66 +61,7 @@ class UniversalTablesController < ApplicationController end tablecolumns = table.table_columns.where(:display_in_index => true).asc(:order) rows = [] - if params["column"].present? - reset = "" - keywords = params["q"] - keywords = keywords.strip.nil? ? keywords : keywords.strip - regexes = keywords.split(/\s+(?=(?:[^"]*"[^"]*")*[^"]*$)/) - regex = Regexp.union(regexes.map{|word| Regexp.new(".*"+word+".*", "i")}) - - column = table.table_columns.where(:key => params["column"]).first - if params["sort"].present? - column_to_sort = table.table_columns.where(:key => params["sortcolumn"]).first - case column_to_sort.type - when "text" - field_name = :text - when "editor" - field_name = :content - when "date" - field_name = :date - end - if params["column"] == params["sortcolumn"] - columns = column.column_entries.any_of(:"text.en" => regex).or(:"text.zh_tw" => regex).or(:"content.en" => regex).or(:"content.zh_tw" => regex).order_by(field_name => params["sort"]) - else - temp = column.column_entries.any_of(:"text.en" => regex).or(:"text.zh_tw" => regex).or(:"content.en" => regex).or(:"content.zh_tw" => regex) - columns = [] - temp.each do |c| - columns << c.table_entry.column_entries.where(:table_column_id => column_to_sort.id).first - end - sorted_columns = column_to_sort.column_entries.order_by(field_name => params["sort"]).to_a - columns = sorted_columns & columns - end - else - columns = column.column_entries.any_of(:"text.en" => regex).or(:"text.zh_tw" => regex).or(:"content.en" => regex).or(:"content.zh_tw" => regex) - end - entries = [] - columns.each do |column| - entries << column.table_entry - end - entries = entries.sort{|k,v| v["created_at"] <=> k["created_at"]} if !params["sort"].present? - entries = Kaminari.paginate_array(entries).page(params["page_no"]).per(OrbitHelper.page_data_count) - else - if params["sort"].present? - reset = "" - column_to_sort = table.table_columns.where(:key => params["sortcolumn"]).first - case column_to_sort.type - when "text" - field_name = :text - when "editor" - field_name = :content - when "date" - field_name = :date - end - columns = column_to_sort.column_entries.order_by(field_name => params["sort"]) - entries = [] - columns.each do |column| - entries << column.table_entry - end - entries = Kaminari.paginate_array(entries).page(params["page_no"]).per(OrbitHelper.page_data_count) - else - entries = table.table_entries.desc(:created_at).page(params["page_no"]).per(OrbitHelper.page_data_count) - end - end + entries = get_entries(params, table, page) total_pages = entries.total_pages entries.each do |te| cols = [] @@ -156,18 +97,143 @@ class UniversalTablesController < ApplicationController } end end + export_button = "" + if params["column"].present? + reset = "" + if !OrbitHelper.current_user.nil? + p = {} + params.each do |k,v| + p[k] = v if ["q","column","sort","sortcolumn"].include?(k) + end + export_button = "Export" + end + total_entries = t("universal_table.total_number_of_entries", :total_number => entries.total_count) + elsif params["sortcolumn"].present? + reset = "" + end { "head-columns" => table_heads, "rows" => rows, "total_pages" => total_pages, "extras" => { + "total_entries" => total_entries, "table-name" => table.title, + "export_button" => export_button, "reset" => reset, "url" => "/#{I18n.locale.to_s}#{page.url}" } } end + def export_filtered + table = UTable.where(:category_id => params[:cat]).first rescue nil + page = Page.where(:page_id => params[:page_id]).first + if !table.nil? + @rows = [] + @tablecolumns = table.table_columns.where(:display_in_index => true).asc(:order) + entries = get_entries(params, table, page, false) + entries.each do |te| + cols = [] + sort_value = "" + @tablecolumns.each do |column| + ce = te.column_entries.where(:table_column_id => column.id).first rescue nil + if !ce.nil? + text = "" + case ce.type + when "text" + text = ce.text + when "editor" + text = ce.content + when "date" + text = format_date(ce.date, column.date_format) + when "period" + text = format_date(ce.period_from, column.date_format) + " ~ " + format_date(ce.period_to, column.date_format) + text = "" if text.starts_with?(" ~") + when "image" + text = "http://#{request.host_with_port}" + ce.image.thumb.url + end + cols << {"text" => text} + else + cols << {"text" => ""} + end + end + @rows << { + "columns" => cols + } + end + excel_name = table.title + ".xlsx" + excel_name = 'attachment; filename="' + excel_name + '"' + + end + respond_to do |format| + format.xlsx { + response.headers['Content-Disposition'] = excel_name + } + 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 + regexes = keywords.split(/\s+(?=(?:[^"]*"[^"]*")*[^"]*$)/) + regex = Regexp.union(regexes.map{|word| Regexp.new(".*"+word+".*", "i")}) + + column = table.table_columns.where(:key => params["column"]).first + if params["sort"].present? + column_to_sort = table.table_columns.where(:key => params["sortcolumn"]).first + case column_to_sort.type + when "text" + field_name = :text + when "editor" + field_name = :content + when "date" + field_name = :date + end + if params["column"] == params["sortcolumn"] + columns = column.column_entries.any_of(:"text.en" => regex).or(:"text.zh_tw" => regex).or(:"content.en" => regex).or(:"content.zh_tw" => regex).order_by(field_name => params["sort"]) + else + temp = column.column_entries.any_of(:"text.en" => regex).or(:"text.zh_tw" => regex).or(:"content.en" => regex).or(:"content.zh_tw" => regex) + columns = [] + temp.each do |c| + columns << c.table_entry.column_entries.where(:table_column_id => column_to_sort.id).first + end + sorted_columns = column_to_sort.column_entries.order_by(field_name => params["sort"]).to_a + columns = sorted_columns & columns + end + else + columns = column.column_entries.any_of(:"text.en" => regex).or(:"text.zh_tw" => regex).or(:"content.en" => regex).or(:"content.zh_tw" => regex) + end + columns.each do |column| + entries << column.table_entry + end + entries = entries.sort{|k,v| v["created_at"] <=> k["created_at"]} if !params["sort"].present? + entries = Kaminari.paginate_array(entries).page(params["page_no"]).per(OrbitHelper.page_data_count) if(paginated) + else + if params["sort"].present? + column_to_sort = table.table_columns.where(:key => params["sortcolumn"]).first + case column_to_sort.type + when "text" + field_name = :text + when "editor" + field_name = :content + when "date" + field_name = :date + end + columns = column_to_sort.column_entries.order_by(field_name => params["sort"]) + columns.each do |column| + entries << column.table_entry + end + + entries = Kaminari.paginate_array(entries).page(params["page_no"]).per(OrbitHelper.page_data_count) if(paginated) + else + entries = table.table_entries.desc(:created_at).page(params["page_no"]).per(OrbitHelper.page_data_count) if(paginated) + end + end + entries + end + def show params = OrbitHelper.params entry = TableEntry.where(:uid => params[:uid]).first rescue nil diff --git a/app/views/admin/universal_tables/show.html.erb b/app/views/admin/universal_tables/show.html.erb index 19d7935..72fbfdc 100644 --- a/app/views/admin/universal_tables/show.html.erb +++ b/app/views/admin/universal_tables/show.html.erb @@ -13,8 +13,16 @@
<%= th_data.html_safe %> | <% end %>
---|