From f72dec2e3389e26e43b01797a958dc03d4263d08 Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Wed, 21 Sep 2016 20:25:41 +0800 Subject: [PATCH] added total number of entries and export button for frontend --- .../universal_tables_controller.rb | 186 ++++++++++++------ .../admin/universal_tables/show.html.erb | 12 +- .../export_filtered.xlsx.axlsx | 18 ++ config/locales/en.yml | 3 +- config/locales/zh_tw.yml | 3 +- config/routes.rb | 1 + 6 files changed, 159 insertions(+), 64 deletions(-) create mode 100644 app/views/universal_tables/export_filtered.xlsx.axlsx 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 @@ - <% @table_fields.each do |f| %> - <%= thead(f) %> + <% @table_fields.each do |field| %> + <% + sort = field.to_s.include?('.') ? field.to_s.split('.')[1] : field.to_s + active = params[:sort].eql? sort + order = active ? (["asc", "desc"]-[params[:order]]).first : "asc" + arrow = (order.eql? "desc") ? "" : "" + klass = field.eql?(:title) ? "span5" : "span2" + th_data = "#{field} #{active ? arrow : ""}" + %> + <% end %> diff --git a/app/views/universal_tables/export_filtered.xlsx.axlsx b/app/views/universal_tables/export_filtered.xlsx.axlsx new file mode 100644 index 0000000..92deb06 --- /dev/null +++ b/app/views/universal_tables/export_filtered.xlsx.axlsx @@ -0,0 +1,18 @@ +# encoding: utf-8 + + +wb = xlsx_package.workbook + +wb.add_worksheet(name: "Table") do |sheet| + heading = sheet.styles.add_style(:b => true, :locked => true) + headings = @tablecolumns.collect{|tc| tc.title} + sheet.add_row headings, :style => heading + + @rows.each do |r| + row = [] + r["columns"].each do |col| + row << col["text"] + end + sheet.add_row row + end +end \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index ee97277..de7d0a1 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -7,4 +7,5 @@ en: created_time: Created Time total_no_of_entries: No of entries export_structure: Download table structure - import_from_excel: Import From Excel \ No newline at end of file + import_from_excel: Import From Excel + total_number_of_entries: "Total number of enteries found : %{total_number}" \ No newline at end of file diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index 7b7a248..78776e9 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -7,4 +7,5 @@ zh_tw: created_time: Created Time total_no_of_entries: No of entries export_structure: Download table Structure - import_from_excel: Import From Excel \ No newline at end of file + import_from_excel: Import From Excel + total_number_of_entries: "Total number of enteries found : %{total_number}" \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 6e15b1d..fa0e908 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -14,6 +14,7 @@ Rails.application.routes.draw do get "export_structure" end end + get "/xhr/universal_table/export", to: 'universal_tables#export_filtered' end end
<%= th_data.html_safe %>