first test for importing data for universal table

This commit is contained in:
Harry Bomrah 2015-12-17 21:58:04 +08:00
parent 316d211b99
commit 99006e2507
8 changed files with 182 additions and 6 deletions

View File

@ -1,6 +1,6 @@
class Admin::UniversalTablesController < OrbitAdminController class Admin::UniversalTablesController < OrbitAdminController
def index def index
@table_fields = ["universal_table.table_name","universal_table.created_time","universal_table.total_no_of_entries"] @table_fields = ["universal_table.table_name","universal_table.created_time","universal_table.total_no_of_entries", "universal_table.import_from_excel"]
@tables = UTable.where(:title.ne => "") @tables = UTable.where(:title.ne => "")
.order_by(sort) .order_by(sort)
@ -22,6 +22,95 @@ class Admin::UniversalTablesController < OrbitAdminController
end end
end end
def export_structure
uid = params[:universal_table_id].split("-").last
@table = UTable.where(:uid => uid).first rescue nil
respond_to do |format|
format.xlsx {
response.headers['Content-Disposition'] = "attachment; filename=#{@table.title.downcase.underscore}.xlsx"
}
end
end
def import_data_from_excel
workbook = RubyXL::Parser.parse(params["import_data"].tempfile)
response = {}
current_locale = I18n.locale
table = UTable.find(params["universal_table_id"]) rescue nil
if !table.nil?
sheet = workbook[0]
if sheet.count <= 503
columns = sheet[1].cells.collect{|c|
table.table_columns.where(:key => c.value).first rescue nil
}
languages = sheet[2].cells.collect{|c|
c.value.split("-").last rescue nil
}
sheet.each_with_index do |row, i|
next if i < 3
te = TableEntry.new
te.u_table = table
skip = 0
row.cells.each_with_index do |cell,index|
if skip > 1
skip = skip - 1
next
end
skip = 0
val = cell.value rescue nil
tc = columns[index]
if !tc.nil?
ce = ColumnEntry.new
case tc.type
when "text"
v = {}
@site_in_use_locales.each_with_index do |locale,x|
v[locale.to_s] = row.cells[index + x].value rescue nil
skip = skip + 1
end
ce.text_translations = v
when "editor"
v = {}
@site_in_use_locales.each_with_index do |locale,x|
v[locale.to_s] = row.cells[index + x].value rescue nil
skip = skip + 1
end
ce.content_translations = v
when "date"
ce.date = val
when "period"
if tc.date_format == "yyyy" && !val.nil?
val = val.to_s + "/01/01"
end
skip = 2
ce.period_from = val
val = row.cells[index + 1].value rescue nil
if tc.date_format == "yyyy" && !val.nil?
val = val.to_s + "/01/01"
end
ce.period_to = val
end
ce.table_column_id = tc.id
ce.save
te.column_entries << ce
end
end
te.save
end
response["success"] = true
response["count"] = table.table_entries.count
response["id"] = table.id.to_s
else
response["success"] = false
response["msg"] = "More than 500 entries. Please split the entries in different files."
end
else
response["success"] = false
response["msg"] = "Table not found."
end
render :json => response.to_json
end
def new_entry def new_entry
uid = params[:universal_table_id].split("-").last uid = params[:universal_table_id].split("-").last
@table = UTable.where(:uid => uid).first rescue nil @table = UTable.where(:uid => uid).first rescue nil

View File

@ -8,7 +8,7 @@
</thead> </thead>
<tbody> <tbody>
<% @tables.each do |table| %> <% @tables.each do |table| %>
<tr> <tr id="table_<%= table.id.to_s %>">
<td> <td>
<a href="<%= admin_universal_table_path(table) %>"><%= table.title %></a> <a href="<%= admin_universal_table_path(table) %>"><%= table.title %></a>
<div class="quick-edit"> <div class="quick-edit">
@ -26,6 +26,15 @@
<td> <td>
<%= table.table_entries.count %> <%= table.table_entries.count %>
</td> </td>
<td>
<form action="/admin/universal_tables/import_data_from_excel" method="post" enctype="multipart/form-data" class="import_from_excel_form">
<%= hidden_field_tag :authenticity_token, form_authenticity_token %>
<input type="file" name="import_data" />
<button class="btn btn-primary btn-small"><i class="icons-upload"></i></button>
<input type="hidden" name="universal_table_id" value="<%= table.id.to_s %>" />
<a href="<%= admin_universal_table_export_structure_path(table, :format => "xlsx") %>"><%= t("universal_table.export_structure") %></a>
</form>
</td>
</tr> </tr>
<% end %> <% end %>
</tbody> </tbody>

View File

@ -0,0 +1,49 @@
# 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.each do |locale|
row << column.title + " - " + t(locale.to_s)
row1 << column.key
row2 << column.type + "-#{locale}"
end
when "editor"
@site_in_use_locales.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"
row1 << column.key
row2 << column.type + " : " + column.date_format.upcase + "-period_from"
row << column.title + "-To"
row1 << column.key
row2 << column.type + " : " + column.date_format.upcase + "-period_to"
end
end
sheet.add_row row, :style => heading
sheet.add_row row1
sheet.add_row row2, :style => type
end

View File

@ -1,3 +1,26 @@
<% content_for :page_specific_javascript do %>
<%= javascript_include_tag "lib/jquery.form" %>
<% end %>
<div id="index_table"> <div id="index_table">
<%= render 'index'%> <%= render 'index'%>
</div> </div>
<script type="text/javascript">
$("form.import_from_excel_form").on("submit",function(){
var form = this;
if($(this).find("input[type=file]").val() != ""){
$(this).ajaxSubmit({
dataType : "json",
success : function(data){
if(data.success){
alert("Import successfull.")
$("tr#table_" + data.id + " td:eq(2)").text(data.count);
}else{
alert(data.msg);
}
form.reset();
}
})
}
return false;
})
</script>

View File

@ -47,7 +47,7 @@
<div class="quick-edit"> <div class="quick-edit">
<ul class="nav nav-pills"> <ul class="nav nav-pills">
<li><a href="<%= admin_universal_table_edit_entry_path(entry) %>"><%= t(:edit) %></a></li> <li><a href="<%= admin_universal_table_edit_entry_path(entry) %>"><%= t(:edit) %></a></li>
<li><a href="<%= admin_universal_table_delete_entry_path(entry) %>" class="delete text-error" data-method="delete" data-confirm="Are you sure?"><%= t(:delete_) %></a></li> <li><a href="<%= admin_universal_table_delete_entry_path(entry.id) %>" class="delete text-error" data-method="delete" data-confirm="Are you sure?"><%= t(:delete_) %></a></li>
</ul> </ul>
</div> </div>
<% end %> <% end %>

View File

@ -6,3 +6,5 @@ en:
table_name: Table Name table_name: Table Name
created_time: Created Time created_time: Created Time
total_no_of_entries: No of entries total_no_of_entries: No of entries
export_structure: Download table structure
import_from_excel: Import From Excel

View File

@ -6,3 +6,5 @@ zh_tw:
table_name: Table Name table_name: Table Name
created_time: Created Time created_time: Created Time
total_no_of_entries: No of entries total_no_of_entries: No of entries
export_structure: Download table Structure
import_from_excel: Import From Excel

View File

@ -6,10 +6,12 @@ Rails.application.routes.draw do
namespace :admin do namespace :admin do
post "/universal_tables/add_entry", to: 'universal_tables#add_entry' post "/universal_tables/add_entry", to: 'universal_tables#add_entry'
patch "/universal_tables/update_entry", to: 'universal_tables#update_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'
resources :universal_tables do resources :universal_tables do
get "new_entry" get "new_entry"
delete "delete_entry" delete "delete_entry"
get "edit_entry" get "edit_entry"
get "export_structure"
end end
end end
end end