From 30453d0d8a6b4a8cef1a29064b3ce754de231937 Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Thu, 13 Apr 2017 18:50:32 +0800 Subject: [PATCH] download for table --- .../admin/universal_tables_controller.rb | 19 ++++ app/models/table_column.rb | 2 +- .../admin/universal_tables/_index.html.erb | 1 + .../admin/universal_tables/index.html.erb | 70 ++++++++++++++ app/views/utable_export/export.xlsx.axlsx | 92 +++++++++++++++++++ config/locales/en.yml | 3 +- config/locales/zh_tw.yml | 3 +- config/routes.rb | 4 + lib/tasks/universal_table_tasks.rake | 19 ++++ 9 files changed, 210 insertions(+), 3 deletions(-) create mode 100644 app/views/utable_export/export.xlsx.axlsx diff --git a/app/controllers/admin/universal_tables_controller.rb b/app/controllers/admin/universal_tables_controller.rb index d270b2e..9c10f7a 100644 --- a/app/controllers/admin/universal_tables_controller.rb +++ b/app/controllers/admin/universal_tables_controller.rb @@ -32,6 +32,25 @@ class Admin::UniversalTablesController < OrbitAdminController end end + def checkforthread + running = !File.exists?("public/uploads/utable_export/#{params[:utable_id]}/#{params[:utable_title]}.xlsx") + render :json => {"status" => running}.to_json + end + + def export_data + I18n.locale = :zh_tw + table = UTable.find(params[:id]) + title = table.title.gsub(/[ "'*@#$%^&()+=;:.,?>|\\\/<~_!:,、。!?;「」〈〉【】/]/,'') + f = "public/uploads/utable_export/#{table.id}/#{title}.xlsx" + File.delete(f) if File.exists?(f) + url = request.host_with_port + Thread.new do + system("rake universal_table_tasks:prepare_download[#{table.id},#{url}] >> #{Rails.root}/log/rake.log &") + end + + render :json => {"success" => true, "title" => title}.to_json + end + def import_data_from_excel workbook = RubyXL::Parser.parse(params["import_data"].tempfile) response = {} diff --git a/app/models/table_column.rb b/app/models/table_column.rb index 7127424..6d017b7 100644 --- a/app/models/table_column.rb +++ b/app/models/table_column.rb @@ -6,7 +6,7 @@ class TableColumn field :title, localize: true field :display_in_index, type: Boolean, default: true field :type - field :date_format, default: "YYYY/MM//DD" + field :date_format, default: "YYYY/MM/DD" field :is_link_to_show, type: Boolean, default: false field :order, type: Integer field :make_categorizable, type: Boolean, default: false diff --git a/app/views/admin/universal_tables/_index.html.erb b/app/views/admin/universal_tables/_index.html.erb index 59c9687..25ca3cc 100644 --- a/app/views/admin/universal_tables/_index.html.erb +++ b/app/views/admin/universal_tables/_index.html.erb @@ -15,6 +15,7 @@ diff --git a/app/views/admin/universal_tables/index.html.erb b/app/views/admin/universal_tables/index.html.erb index d51ec89..7faaa0e 100644 --- a/app/views/admin/universal_tables/index.html.erb +++ b/app/views/admin/universal_tables/index.html.erb @@ -23,4 +23,74 @@ } return false; }) + + + + \ No newline at end of file diff --git a/app/views/utable_export/export.xlsx.axlsx b/app/views/utable_export/export.xlsx.axlsx new file mode 100644 index 0000000..4ed5d62 --- /dev/null +++ b/app/views/utable_export/export.xlsx.axlsx @@ -0,0 +1,92 @@ +# encoding: utf-8 + +wb = xlsx_package.workbook + +wb.add_worksheet(name: "Structure") do |sheet| + heading = sheet.styles.add_style(:b => true, :locked => true) + type = sheet.styles.add_style(:i => true) + + row = [] + row1 = [] + row2 = [] + + table.table_columns.asc(:order).each do |column| + case column.type + when "text" + site_in_use_locales.sort.each do |locale| + row << column.title + " - " + t(locale.to_s) + row1 << column.key + row2 << column.type + "-#{locale}" + end + when "editor" + site_in_use_locales.sort.each do |locale| + row << column.title + " - " + t(locale.to_s) + row1 << column.key + row2 << column.type + "-#{locale}" + end + when "image" + row << column.title + row1 << column.key + row2 << "Please leave this column blank. Upload the image manually." + when "date" + row << column.title + row1 << column.key + row2 << column.type + " : " + column.date_format.upcase + when "period" + row << column.title + "-From ~ To" + row1 << column.key + row2 << column.type + " : " + column.date_format.upcase + "-period_from ~ period_to" + end + end + + sheet.add_row row, :style => heading + sheet.add_row row1 + sheet.add_row row2, :style => type + + table.table_entries.asc(:created_at).each do |entry| + row = [] + table.table_columns.asc(:order).each do |col| + column = entry.column_entries.where(:table_column_id => col.id).first + case col.type + when "text" + site_in_use_locales.sort.each do |locale| + row << column.text_translations[locale.to_s] + end + when "editor" + site_in_use_locales.sort.each do |locale| + row << column.content_translations[locale.to_s] + end + when "image" + if !column.image.url.nil? + row << url + column.image.url + else + row << "" + end + when "date" + case col.date_format + when "yyyy/MM/dd hh:mm" + row << (column.date.strftime("%Y/%m/%d %H:%M") rescue "") + when "yyyy/MM/dd" + row << (column.date.strftime("%Y/%m/%d") rescue "") + when "yyyy/MM" + row << (column.date.strftime("%Y/%m/") rescue "") + when "yyyy" + row << (column.date.strftime("%Y") rescue "") + end + when "period" + case col.date_format + when "yyyy/MM/dd hh:mm" + row << (column.period_from.strftime("%Y/%m/%d %H:%M")rescue "") + " ~ " + (column.period_to.strftime("%Y/%m/%d %H:%M") rescue "") + when "yyyy/MM/dd" + row << (column.period_from.strftime("%Y/%m/%d")rescue "") + " ~ " + (column.period_to.strftime("%Y/%m/%d") rescue "") + when "yyyy/MM" + row << (column.period_from.strftime("%Y/%m")rescue "") + " ~ " + (column.period_to.strftime("%Y/%m") rescue "") + when "yyyy" + row << (column.period_from.strftime("%Y")rescue "") + " ~ " + (column.period_to.strftime("%Y") rescue "") + end + end + 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 de7d0a1..fab9c8d 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -8,4 +8,5 @@ en: total_no_of_entries: No of entries export_structure: Download table structure import_from_excel: Import From Excel - total_number_of_entries: "Total number of enteries found : %{total_number}" \ No newline at end of file + total_number_of_entries: "Total number of enteries found : %{total_number}" + export_xls: Export XLSX \ No newline at end of file diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index 78776e9..a0b687d 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -8,4 +8,5 @@ zh_tw: total_no_of_entries: No of entries export_structure: Download table Structure import_from_excel: Import From Excel - total_number_of_entries: "Total number of enteries found : %{total_number}" \ No newline at end of file + total_number_of_entries: "Total number of enteries found : %{total_number}" + export_xls: Export XLSX \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index fa0e908..dff7726 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -7,11 +7,15 @@ Rails.application.routes.draw do post "/universal_tables/add_entry", to: 'universal_tables#add_entry' patch "/universal_tables/update_entry", to: 'universal_tables#update_entry' post "/universal_tables/import_data_from_excel", to: 'universal_tables#import_data_from_excel' + get "universal_tables/checkforthread", to: "universal_tables#checkforthread" resources :universal_tables do get "new_entry" delete "delete_entry" get "edit_entry" get "export_structure" + member do + get "export_data" + end end end get "/xhr/universal_table/export", to: 'universal_tables#export_filtered' diff --git a/lib/tasks/universal_table_tasks.rake b/lib/tasks/universal_table_tasks.rake index 5adbf40..b01b0de 100644 --- a/lib/tasks/universal_table_tasks.rake +++ b/lib/tasks/universal_table_tasks.rake @@ -2,3 +2,22 @@ # task :universal_table do # # Task goes here # end +namespace :universal_table_tasks do + task :prepare_download,[:utable_id, :url] => :environment do |task,args| + id = args.utable_id + I18n.locale = :zh_tw + table = UTable.find(id) + ac = ActionController::Base.new() + url = "http://#{args.url}" + xlsx = ac.render_to_string handlers: [:axlsx], formats: [:xlsx], template: "utable_export/export", locals: {table: table, site_in_use_locales: Site.first.in_use_locales, url: url} + dirname = "public/uploads/utable_export/#{id}" + FileUtils.mkdir_p(dirname) unless File.exists?(dirname) + f = "#{dirname}/#{table.title.gsub(/[ "'*@#$%^&()+=;:.,?>|\\\/<~_!:,、。!?;「」〈〉【】/]/,'')}.xlsx" + if File.exists?(f) + File.delete(f) + end + file = File.open(f, "w") + xlsx.force_encoding("utf-8") + file.write(xlsx) + end +end