From 87d691de04a14c3a85c41ffaafe63f8309392a78 Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Tue, 15 Dec 2015 19:33:41 +0800 Subject: [PATCH] conference paper import from excel --- .../admin/writing_conferences_controller.rb | 26 +++ .../admin/personal_conferences_helper.rb | 102 ++++++++++++ .../excel_format.xlsx.axlsx | 156 ++++++++++++++++++ .../admin/writing_conferences/index.html.erb | 22 ++- config/routes.rb | 2 + 5 files changed, 307 insertions(+), 1 deletion(-) create mode 100644 app/views/admin/writing_conferences/excel_format.xlsx.axlsx diff --git a/app/controllers/admin/writing_conferences_controller.rb b/app/controllers/admin/writing_conferences_controller.rb index 9d3d1d4..a48161b 100644 --- a/app/controllers/admin/writing_conferences_controller.rb +++ b/app/controllers/admin/writing_conferences_controller.rb @@ -97,6 +97,32 @@ class Admin::WritingConferencesController < OrbitMemberController end end + def excel_format + respond_to do |format| + format.xlsx { + response.headers['Content-Disposition'] = 'attachment; filename="conferences_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_writing_conference(row,mp) + end + end + redirect_to admin_writing_conferences_url + else + redirect_to admin_writing_conferences_url(:error => "1") + end + end + def toggle_hide if params[:ids] @writing_conferences = WritingConference.any_in(_id: params[:ids]) diff --git a/app/helpers/admin/personal_conferences_helper.rb b/app/helpers/admin/personal_conferences_helper.rb index 2a7eb3d..d14d126 100644 --- a/app/helpers/admin/personal_conferences_helper.rb +++ b/app/helpers/admin/personal_conferences_helper.rb @@ -27,4 +27,106 @@ module Admin::PersonalConferencesHelper end conferences end + + def import_this_writing_conference(row, mp) + value = nil + conference = WritingConference.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 + conference.paper_title_translations = value + when 4 + value = {"en" => val} + when 5 + begin + value["zh_tw"] = val + rescue + value = {"zh_tw" => val} + end + conference.conference_title_translations = value + when 6 + value = {"en" => val} + when 7 + begin + value["zh_tw"] = val + rescue + value = {"zh_tw" => val} + end + conference.location_translations = value + when 8 + value = {"en" => val} + when 9 + begin + value["zh_tw"] = val + rescue + value = {"zh_tw" => val} + end + conference.sponsor_translations = value + when 10 + value = {"en" => val} + when 11 + begin + value["zh_tw"] = val + rescue + value = {"zh_tw" => val} + end + conference.authors_translations = value + when 12 + conference.year = val + when 13 + conference.language = val + when 14 + conference.period_start_date = val + when 15 + conference.period_end_date = val + when 16 + cpls = ConferencePaperLevel.asc(:created_at).all.to_a + ts = val.to_s.split(",") + ts.each do |t| + conference.conference_paper_level_ids << cpls[t.to_i].id if t.to_s.is_i? && t.to_i < cpls.count + end + when 17 + cpls = ConferencePaperType.asc(:created_at).all.to_a + ts = val.to_s.split(",") + ts.each do |t| + conference.conference_paper_type_ids << cpls[t.to_i].id if t.to_s.is_i? && t.to_i < cpls.count + end + when 18 + cpls = ConferenceAuthorType.asc(:created_at).all.to_a + ts = val.to_s.split(",") + ts.each do |t| + conference.conference_author_type_ids << cpls[t.to_i].id if t.to_s.is_i? && t.to_i < cpls.count + end + when 19 + conference.number_of_authors = val + when 20 + conference.isbn = val + when 21 + conference.publication_date = val + when 22 + conference.isi_number = val + when 23 + conference.url = val + when 24 + conference.keywords = val + when 25 + conference.abstract = val + when 26 + conference.note = val + end + end + conference.member_profile = mp + conference.save + end end \ No newline at end of file diff --git a/app/views/admin/writing_conferences/excel_format.xlsx.axlsx b/app/views/admin/writing_conferences/excel_format.xlsx.axlsx new file mode 100644 index 0000000..8d62886 --- /dev/null +++ b/app/views/admin/writing_conferences/excel_format.xlsx.axlsx @@ -0,0 +1,156 @@ +# 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_conference.paper_title") + " - " + t("en") + row1 << "textfield" + row2 << "" + row << t("personal_conference.paper_title") + " - " + t("zh_tw") + row1 << "textfield" + row2 << "" + + row << t("personal_conference.conference_title") + " - " + t("en") + row1 << "textfield" + row2 << "" + row << t("personal_conference.conference_title") + " - " + t("zh_tw") + row1 << "textfield" + row2 << "" + + row << t("personal_conference.location") + " - " + t("en") + row1 << "textfield" + row2 << "" + row << t("personal_conference.location") + " - " + t("zh_tw") + row1 << "textfield" + row2 << "" + + row << t("personal_conference.sponsor") + " - " + t("en") + row1 << "textfield" + row2 << "" + row << t("personal_conference.sponsor") + " - " + t("zh_tw") + row1 << "textfield" + row2 << "" + + row << t("personal_conference.authors") + " - " + t("en") + row1 << "textarea" + row2 << "" + row << t("personal_conference.authors") + " - " + t("zh_tw") + row1 << "textarea" + row2 << "" + + row << t("personal_conference.year") + row1 << "number" + row2 << "Example : 2015 or 2014 or 1987" + + row << t("personal_conference.language") + row1 << "select" + row2 << "en -> English, zh_tw -> Chinese" + + + row << t("personal_conference.period_start_date") + row1 << "date" + row2 << "Format: YYYY/MM/DD, Example: 2015/12/10" + + row << t("personal_conference.period_end_date") + row1 << "date" + row2 << "Format: YYYY/MM/DD, Example: 2015/12/10" + + row << t("personal_conference.paper_level") + row1 << "checkbox" + t = "" + ConferencePaperLevel.asc(:created_at).each_with_index do |jl,i| + t = t + "#{i}" + " -> " + jl.title + ", " + end + if ConferencePaperLevel.count > 0 + t = t + " Example : 0, if more than one then : 0,1" + else + t = "Leave this field blank" + end + row2 << t + + row << t("personal_conference.paper_type") + row1 << "checkbox" + t = "" + ConferencePaperType.asc(:created_at).each_with_index do |jl,i| + t = t + "#{i}" + " -> " + jl.title + ", " + end + if ConferencePaperType.count > 0 + t = t + " Example : 0, if more than one then : 0,1" + else + t = "Leave this field blank" + end + row2 << t + + row << t("personal_conference.author_type") + row1 << "checkbox" + t = "" + ConferenceAuthorType.asc(:created_at).each_with_index do |jl,i| + t = t + "#{i}" + " -> " + jl.title + ", " + end + if ConferenceAuthorType.count > 0 + t = t + " Example : 0, if more than one then : 0,1" + else + t = "Leave this field blank" + end + row2 << t + + row << t("personal_conference.number_of_authors") + row1 << "number" + row2 << "Example: 1 or 2 or 10" + + row << t("personal_conference.isbn") + row1 << "textfield" + row2 << "" + + row << t("personal_conference.publication_date") + row1 << "date" + row2 << "Format: YYYY/MM/DD, Example: 2015/12/10" + + row << t("personal_conference.isi_number") + row1 << "textfield" + row2 << "" + + row << t("personal_conference.url") + row1 << "textfield" + row2 << "http://domain.com/path" + + row << t("personal_conference.keywords") + row1 << "textfield" + row2 << "Example: keyword1,keyword2" + + row << t("personal_conference.abstract") + row1 << "textarea" + row2 << "" + + row << t("personal_conference.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/writing_conferences/index.html.erb b/app/views/admin/writing_conferences/index.html.erb index 65278f0..75820b5 100644 --- a/app/views/admin/writing_conferences/index.html.erb +++ b/app/views/admin/writing_conferences/index.html.erb @@ -13,10 +13,30 @@
+
+ + +
<%= link_to content_tag(:i, nil, :class => 'icon-plus icon-white') + t(:new_), new_admin_writing_conference_path, :class => 'btn btn-primary' %> <%= link_to content_tag(:i, nil, :class => 'icon-cog icon-white') + t('setting'), admin_writing_conference_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 647f0be..afec906 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 'writing_conference_setting' => "writing_conferences#setting" + get 'writing_conferences/download_excel_format' => 'writing_conferences#excel_format' + post 'writing_conferences/import_from_excel' => 'writing_conferences#import_from_excel' resources :writing_conferences do collection do