diff --git a/app/controllers/admin/universal_tables_controller.rb b/app/controllers/admin/universal_tables_controller.rb index b35bb3e..be18f84 100644 --- a/app/controllers/admin/universal_tables_controller.rb +++ b/app/controllers/admin/universal_tables_controller.rb @@ -1,6 +1,6 @@ class Admin::UniversalTablesController < OrbitAdminController 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 => "") .order_by(sort) @@ -22,6 +22,95 @@ class Admin::UniversalTablesController < OrbitAdminController 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 uid = params[:universal_table_id].split("-").last @table = UTable.where(:uid => uid).first rescue nil diff --git a/app/views/admin/universal_tables/_index.html.erb b/app/views/admin/universal_tables/_index.html.erb index 218f916..59c9687 100644 --- a/app/views/admin/universal_tables/_index.html.erb +++ b/app/views/admin/universal_tables/_index.html.erb @@ -8,7 +8,7 @@ <% @tables.each do |table| %> - + <%= table.title %>
@@ -26,6 +26,15 @@ <%= table.table_entries.count %> + +
+ <%= hidden_field_tag :authenticity_token, form_authenticity_token %> + + + + "><%= t("universal_table.export_structure") %> +
+ <% end %> diff --git a/app/views/admin/universal_tables/export_structure.xlsx.axlsx b/app/views/admin/universal_tables/export_structure.xlsx.axlsx new file mode 100644 index 0000000..99f8323 --- /dev/null +++ b/app/views/admin/universal_tables/export_structure.xlsx.axlsx @@ -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 \ No newline at end of file diff --git a/app/views/admin/universal_tables/index.html.erb b/app/views/admin/universal_tables/index.html.erb index f1bdaea..d51ec89 100644 --- a/app/views/admin/universal_tables/index.html.erb +++ b/app/views/admin/universal_tables/index.html.erb @@ -1,3 +1,26 @@ +<% content_for :page_specific_javascript do %> + <%= javascript_include_tag "lib/jquery.form" %> +<% end %>
<%= render 'index'%> -
\ No newline at end of file +
+ \ No newline at end of file diff --git a/app/views/admin/universal_tables/show.html.erb b/app/views/admin/universal_tables/show.html.erb index e05d5da..19d7935 100644 --- a/app/views/admin/universal_tables/show.html.erb +++ b/app/views/admin/universal_tables/show.html.erb @@ -47,7 +47,7 @@
<% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index ef75e77..ee97277 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -5,4 +5,6 @@ en: new_table: Create New Table table_name: Table Name created_time: Created Time - total_no_of_entries: No of entries \ No newline at end of file + total_no_of_entries: No of entries + export_structure: Download table structure + import_from_excel: Import From Excel \ No newline at end of file diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index 4dff978..7b7a248 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -5,4 +5,6 @@ zh_tw: new_table: Create New Table table_name: Table Name created_time: Created Time - total_no_of_entries: No of entries \ No newline at end of file + total_no_of_entries: No of entries + export_structure: Download table Structure + import_from_excel: Import From Excel \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 1f51a9f..6e15b1d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,10 +6,12 @@ Rails.application.routes.draw do namespace :admin 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' resources :universal_tables do get "new_entry" delete "delete_entry" get "edit_entry" + get "export_structure" end end end