From 6181a5a75daff29943ca9849ad2d881f6379dfed Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Mon, 14 Dec 2015 15:18:27 +0800 Subject: [PATCH] added import from xlsx file --- app/controllers/admin/honors_controller.rb | 26 ++++++ app/helpers/admin/personal_honors_helper.rb | 40 +++++++++ .../admin/honors/excel_format.xlsx.axlsx | 81 +++++++++++++++++++ app/views/admin/honors/index.html.erb | 22 ++++- config/routes.rb | 2 + 5 files changed, 170 insertions(+), 1 deletion(-) create mode 100644 app/views/admin/honors/excel_format.xlsx.axlsx diff --git a/app/controllers/admin/honors_controller.rb b/app/controllers/admin/honors_controller.rb index ed2df5f..d6da61c 100644 --- a/app/controllers/admin/honors_controller.rb +++ b/app/controllers/admin/honors_controller.rb @@ -110,6 +110,32 @@ class Admin::HonorsController < OrbitMemberController render json: {"success"=>true} end + def excel_format + respond_to do |format| + format.xlsx { + response.headers['Content-Disposition'] = 'attachment; filename="honor_format.xlsx"' + } + end + end + + def import_from_excel + workbook = RubyXL::Parser.parse(params["import_file"].tempfile) + sheet = workbook[0] + if sheet.count <= 503 + sheet.each_with_index do |row, i| + next if i < 3 + user = User.where(:user_name => row.cells[0].value).first rescue nil + if !user.nil? + mp = user.member_profile + import_this_honor(row,mp) + end + end + redirect_to admin_honors_url + else + redirect_to admin_honors_url(:error => "1") + end + end + def setting end diff --git a/app/helpers/admin/personal_honors_helper.rb b/app/helpers/admin/personal_honors_helper.rb index 2a41f74..c266b96 100644 --- a/app/helpers/admin/personal_honors_helper.rb +++ b/app/helpers/admin/personal_honors_helper.rb @@ -16,4 +16,44 @@ module Admin::PersonalHonorsHelper end honors end + + def import_this_honor(row,mp) + value = nil + honor = Honor.new + row.cells.each_with_index do |cell,index| + next if index < 2 + next if cell.nil? + val = cell.value + next if val.nil? || val == "" + case index + when 2 + value = {"en" => val} + when 3 + value["zh_tw"] = val rescue value = {"zh_tw" => val} + honor.award_name_translations = value + when 4 + value = {"en" => val} + when 5 + value["zh_tw"] = val rescue value = {"zh_tw" => val} + honor.awarding_unit_translations = value + when 6 + honor.year = val + when 7 + honor.language = val + when 8 + hts = HonorType.asc(:created_at).all.to_a + honor.honor_type = hts[val.to_i] if val.to_s.is_i? && val.to_i < hts.count + when 9 + honor.url = val + when 10 + honor.keywords = val + when 11 + # honor.abstract = val + when 12 + honor.note = val + end + end + honor.member_profile = mp + honor.save + end end \ No newline at end of file diff --git a/app/views/admin/honors/excel_format.xlsx.axlsx b/app/views/admin/honors/excel_format.xlsx.axlsx new file mode 100644 index 0000000..a1264c1 --- /dev/null +++ b/app/views/admin/honors/excel_format.xlsx.axlsx @@ -0,0 +1,81 @@ +# encoding: utf-8 +wb = xlsx_package.workbook + +wb.add_worksheet(name: "Honor") do |sheet| + heading = sheet.styles.add_style(:b => true, :locked => true) + example = sheet.styles.add_style(:i => true) + + row = ["user_id"] + row1 = [""] + row2 = [""] + + row << "name" + row1 << "" + row2 << "" + + row << t("personal_honor.award_name") + " - " + t("en") + row1 << "textfield" + row2 << "" + + row << t("personal_honor.award_name") + " - " + t("zh_tw") + row1 << "textfield" + row2 << "" + + row << t("personal_honor.awarding_unit") + " - " + t("en") + row1 << "textfield" + row2 << "" + + row << t("personal_honor.awarding_unit") + " - " + t("zh_tw") + row1 << "textfield" + row2 << "" + + row << t("personal_honor.year") + row1 << "number" + row2 << "Example : 2015 or 2014 or 1987" + + row << t("personal_honor.language") + row1 << "select" + row2 << "en -> English, zh_tw -> Chinese" + + row << t("personal_honor.honor_category") + row1 << "select" + t = "" + HonorType.asc(:created_at).each_with_index do |jl,i| + t = t + "#{i}" + " -> " + jl.title + ", " + end + if HonorType.count > 0 + t = t + " Example : 0" + else + t = "Leave this field blank" + end + row2 << t + + row << t("personal_honor.url") + row1 << "textfield" + row2 << "http://domain.com/path" + + + row << t("personal_honor.keywords") + row1 << "textfield" + row2 << "Example: keyword1,keyword2" + + row << t("personal_honor.abstract") + row1 << "textarea" + row2 << "" + + row << t("personal_honor.note") + row1 << "textarea" + row2 << "" + + sheet.add_row row, :style => heading + sheet.add_row row1 + sheet.add_row row2, :style => example + + User.where(:user_name.ne => "rulingcom").each do |user| + + r = [user.user_name] + r << user.name + + sheet.add_row r + end +end \ No newline at end of file diff --git a/app/views/admin/honors/index.html.erb b/app/views/admin/honors/index.html.erb index 97c6f50..d7fa2fb 100644 --- a/app/views/admin/honors/index.html.erb +++ b/app/views/admin/honors/index.html.erb @@ -14,10 +14,30 @@
+
+ + +
<%= link_to content_tag(:i, nil, :class => 'icon-plus icon-white') + t(:new_), new_admin_honor_path, :class => 'btn btn-primary' %> <%= link_to content_tag(:i, nil, :class => 'icon-cog icon-white') + t('setting'), admin_honor_setting_path, :class => 'btn btn-primary pull-right' %>
-
\ No newline at end of file + +<% if params[:error] == "1" %> + +<% end %> \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 1c6905f..6233fca 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,6 +3,8 @@ Rails.application.routes.draw do scope "(:locale)", locale: Regexp.new(locales.join("|")) do namespace :admin do get 'honor_setting' => "honors#setting" + get 'honors/download_excel_format' => 'honors#excel_format' + post 'honors/import_from_excel' => 'honors#import_from_excel' resources :honors do collection do