download for table
This commit is contained in:
parent
1256040bc7
commit
30453d0d8a
|
@ -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 = {}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
<ul class="nav nav-pills">
|
||||
<% if can_edit_or_delete?(table) %>
|
||||
<li><a href="<%= edit_admin_universal_table_path(table) %>"><%= t(:edit) %></a></li>
|
||||
<li><a href="/admin/universal_tables/<%=table.id.to_s%>/export_data?format=xlsx" data-table-id="<%= table.id.to_s %>" class="export-xls"><%= t('universal_table.export_xls') %></a></li>
|
||||
<li><a href="<%= admin_universal_table_path(table) %>" class="delete text-error" data-method="delete" data-confirm="Are you sure?"><%= t(:delete_) %></a></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
|
|
|
@ -24,3 +24,73 @@
|
|||
return false;
|
||||
})
|
||||
</script>
|
||||
<div id="downloadModal" data-backdrop="static" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="downloadModalLabel" aria-hidden="true">
|
||||
<div class="modal-header">
|
||||
<h3 id="downloadModalLabel">Download</h3>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<p id="wait-zone" style="text-align: center;">
|
||||
Please wait while we prepare your download. This may take a while.
|
||||
<br />
|
||||
<img src="/assets/spin.gif" />
|
||||
</p>
|
||||
<p id="link-zone" style="display: none; text-align: center;">
|
||||
Please click the link below to download.
|
||||
<br />
|
||||
<a href="" id="download-link" target="_blank">Download</a>
|
||||
</p>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button class="btn" id="modal-close-btn" style="display:none;" data-dismiss="modal" aria-hidden="true">Close</button>
|
||||
</div>
|
||||
</div>
|
||||
<script type="text/javascript" src="/assets/lib/process.manager.js"></script>
|
||||
<script type="text/javascript">
|
||||
|
||||
var downloadModal = $("#downloadModal"),
|
||||
checkForThread = null,
|
||||
waitZone = $("#wait-zone"),
|
||||
linkZone = $("#link-zone"),
|
||||
downloadLink = $("a#download-link"),
|
||||
modalBtn = $("#modal-close-btn"),
|
||||
processManager = new ProcessManager();
|
||||
|
||||
$(document).on("click", ".export-xls", function(){
|
||||
var link = $(this).attr("href"),
|
||||
title = null,
|
||||
id = $(this).data("table-id");
|
||||
|
||||
linkZone.hide();
|
||||
waitZone.show();
|
||||
modalBtn.hide();
|
||||
$.ajax({
|
||||
url : link,
|
||||
type : "get",
|
||||
dataType : "json"
|
||||
}).done(function(data){
|
||||
title = data.title;
|
||||
checkForThread = new Process(function(){
|
||||
$.ajax({
|
||||
url : "/admin/universal_tables/checkforthread",
|
||||
type : "get",
|
||||
data : {"utable_id" : id, "utable_title" : title},
|
||||
dataType : "json"
|
||||
}).done(function(data){
|
||||
if(!data.status){
|
||||
downloadLink.attr("href", "/uploads/utable_export/" + id + "/" + title + ".xlsx");
|
||||
waitZone.hide();
|
||||
linkZone.show();
|
||||
modalBtn.show();
|
||||
checkForThread.kill();
|
||||
}
|
||||
})
|
||||
})
|
||||
checkForThread.setTimeInterval(1000);
|
||||
checkForThread.setRepeat(Process.CONSTANTS.REPEAT_INFINITE);
|
||||
processManager.queue(checkForThread);
|
||||
})
|
||||
downloadModal.modal("show");
|
||||
return false;
|
||||
})
|
||||
|
||||
</script>
|
|
@ -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
|
|
@ -9,3 +9,4 @@ en:
|
|||
export_structure: Download table structure
|
||||
import_from_excel: Import From Excel
|
||||
total_number_of_entries: "Total number of enteries found : %{total_number}"
|
||||
export_xls: Export XLSX
|
|
@ -9,3 +9,4 @@ zh_tw:
|
|||
export_structure: Download table Structure
|
||||
import_from_excel: Import From Excel
|
||||
total_number_of_entries: "Total number of enteries found : %{total_number}"
|
||||
export_xls: Export XLSX
|
|
@ -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'
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue