From 065d2607f610df35c5d55608f1de6bd3e6df3bb6 Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Mon, 14 Dec 2015 18:25:43 +0800 Subject: [PATCH] excel upload done for diplomas --- app/controllers/admin/diplomas_controller.rb | 26 ++++++ app/helpers/admin/personal_diplomas_helper.rb | 63 ++++++++++++++ .../admin/diplomas/excel_format.xlsx.axlsx | 83 +++++++++++++++++++ app/views/admin/diplomas/index.html.erb | 22 ++++- config/locales/en.yml | 2 + config/routes.rb | 2 + 6 files changed, 197 insertions(+), 1 deletion(-) create mode 100644 app/views/admin/diplomas/excel_format.xlsx.axlsx diff --git a/app/controllers/admin/diplomas_controller.rb b/app/controllers/admin/diplomas_controller.rb index 45f7f56..53466f5 100644 --- a/app/controllers/admin/diplomas_controller.rb +++ b/app/controllers/admin/diplomas_controller.rb @@ -96,6 +96,32 @@ class Admin::DiplomasController < OrbitMemberController end end + def excel_format + respond_to do |format| + format.xlsx { + response.headers['Content-Disposition'] = 'attachment; filename="diplomas_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_diploma(row,mp) + end + end + redirect_to admin_diplomas_url + else + redirect_to admin_diplomas_url(:error => "1") + end + end + def toggle_hide if params[:ids] @diplomas = Diploma.any_in(_id: params[:ids]) diff --git a/app/helpers/admin/personal_diplomas_helper.rb b/app/helpers/admin/personal_diplomas_helper.rb index 2a8e611..ee5e4be 100644 --- a/app/helpers/admin/personal_diplomas_helper.rb +++ b/app/helpers/admin/personal_diplomas_helper.rb @@ -16,4 +16,67 @@ module Admin::PersonalDiplomasHelper end diplomas end + + def import_this_diploma(row, mp) + value = nil + diploma = Diploma.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 + begin + value["zh_tw"] = val + rescue + value = {"zh_tw" => val} + end + diploma.school_name_translations = value + when 4 + value = {"en" => val} + when 5 + begin + value["zh_tw"] = val + rescue + value = {"zh_tw" => val} + end + diploma.country_translations = value + when 6 + value = {"en" => val} + when 7 + begin + value["zh_tw"] = val + rescue + value = {"zh_tw" => val} + end + diploma.department_translations = value + when 8 + value = {"en" => val} + when 9 + begin + value["zh_tw"] = val + rescue + value = {"zh_tw" => val} + end + diploma.degree_translations = value + when 10 + diploma.start_date = val + when 11 + diploma.end_date = val + when 12 + diploma.language =val + when 13 + diploma.url = val + when 14 + diploma.keywords = val + when 15 + diploma.note = val + end + end + diploma.member_profile = mp + diploma.save + end end \ No newline at end of file diff --git a/app/views/admin/diplomas/excel_format.xlsx.axlsx b/app/views/admin/diplomas/excel_format.xlsx.axlsx new file mode 100644 index 0000000..47f5364 --- /dev/null +++ b/app/views/admin/diplomas/excel_format.xlsx.axlsx @@ -0,0 +1,83 @@ +# encoding: utf-8 + +wb = xlsx_package.workbook + +wb.add_worksheet(name: "Journal Paper") 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_diploma.school_name") + " - " + t("en") + row1 << "textfield" + row2 << "" + row << t("personal_diploma.school_name") + " - " + t("zh_tw") + row1 << "textfield" + row2 << "" + + row << t("personal_diploma.country") + " - " + t("en") + row1 << "textfield" + row2 << "" + row << t("personal_diploma.country") + " - " + t("zh_tw") + row1 << "textfield" + row2 << "" + + row << t("personal_diploma.department") + " - " + t("en") + row1 << "textfield" + row2 << "" + row << t("personal_diploma.department") + " - " + t("zh_tw") + row1 << "textfield" + row2 << "" + + row << t("personal_diploma.degree") + " - " + t("en") + row1 << "textfield" + row2 << "" + row << t("personal_diploma.degree") + " - " + t("zh_tw") + row1 << "textfield" + row2 << "" + + row << t("personal_diploma.start_date") + row1 << "date" + row2 << "Format: YYYY/MM, Example: 2013/12" + + row << t("personal_diploma.end_date") + row1 << "date" + row2 << "Format: YYYY/MM, Example: 2015/12" + + row << t("personal_diploma.language") + row1 << "select" + row2 << "en -> English, zh_tw -> Chinese" + + row << t("personal_diploma.url") + row1 << "textfield" + row2 << "http://domain.com/path" + + row << t("personal_diploma.keywords") + row1 << "textfield" + row2 << "Example: keyword1,keyword2" + + row << t("personal_diploma.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/diplomas/index.html.erb b/app/views/admin/diplomas/index.html.erb index 4375fb4..d215277 100644 --- a/app/views/admin/diplomas/index.html.erb +++ b/app/views/admin/diplomas/index.html.erb @@ -15,9 +15,29 @@
+
+ + +
<%= link_to content_tag(:i, nil, :class => 'icon-plus icon-white') + t(:new_), new_admin_diploma_path, :class => 'btn btn-primary' %>
-
\ No newline at end of file + +<% if params[:error] == "1" %> + +<% end %> \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index 69761f8..c831da5 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -20,6 +20,8 @@ en: form_to_end : "To" up_to_today : "Up to today" total_pages : "Total Pages" + start_date : Start Date + end_date : End Date keywords : "Keywords" abstract : "Abstract" publication_date : "Date of Publication" diff --git a/config/routes.rb b/config/routes.rb index 406579c..6fdafb3 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 'diploma_setting' => "diplomas#setting" + get 'diplomas/download_excel_format' => 'diplomas#excel_format' + post 'diplomas/import_from_excel' => 'diplomas#import_from_excel' resources :diplomas do collection do