From b4011a8ada9c3cd1081f10eb6d58ffa9b915588c Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Fri, 11 Dec 2015 19:06:33 +0800 Subject: [PATCH] journal paper import from excel done. --- .../admin/journal_papers_controller.rb | 27 ++++ app/helpers/admin/journal_papers_helper.rb | 91 +++++++++++ .../journal_papers/excel_format.xlsx.axlsx | 148 ++++++++++++++++++ app/views/admin/journal_papers/index.html.erb | 26 ++- config/locales/en.yml | 3 + config/routes.rb | 2 + 6 files changed, 294 insertions(+), 3 deletions(-) create mode 100644 app/views/admin/journal_papers/excel_format.xlsx.axlsx diff --git a/app/controllers/admin/journal_papers_controller.rb b/app/controllers/admin/journal_papers_controller.rb index 9e552f0..5a45371 100644 --- a/app/controllers/admin/journal_papers_controller.rb +++ b/app/controllers/admin/journal_papers_controller.rb @@ -1,3 +1,4 @@ +require 'rubyXL' class Admin::JournalPapersController < OrbitMemberController include Admin::JournalPapersHelper layout "member_plugin" @@ -97,6 +98,32 @@ class Admin::JournalPapersController < OrbitMemberController end end + def excel_format + respond_to do |format| + format.xlsx { + response.headers['Content-Disposition'] = 'attachment; filename="journal_paper_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_journal_paper(row,mp) + end + end + redirect_to admin_journal_papers_url + else + redirect_to admin_journal_papers_url(:error => "1") + end + end + def toggle_hide if params[:ids] @journal_papers = JournalPaper.any_in(_id: params[:ids]) diff --git a/app/helpers/admin/journal_papers_helper.rb b/app/helpers/admin/journal_papers_helper.rb index 3607785..fcb80e6 100644 --- a/app/helpers/admin/journal_papers_helper.rb +++ b/app/helpers/admin/journal_papers_helper.rb @@ -33,4 +33,95 @@ module Admin::JournalPapersHelper end journals end + def import_this_journal_paper(row,mp) + value = nil + jp = JournalPaper.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 + jp.paper_title_translations = value + when 4 + value = {"en" => val} + when 5 + begin + value["zh_tw"] = val + rescue + value = {"zh_tw" => val} + end + jp.journal_title_translations = value + when 6 + value = {"en" => val} + when 7 + begin + value["zh_tw"] = val + rescue + value = {"zh_tw" => val} + end + jp.authors_translations = value + when 8 + jp.year = val + when 9 + jp.language = val + when 10 + jp.vol_no = val + when 11 + jp.issue_no = val + when 12 + jp.total_pages = val + when 13 + jls = JournalLevel.all.to_a + ts = val.to_s.split(",") + ts.each do |t| + jp.journal_level_ids << jls[t.to_i].id + end + when 14 + jpts = JournalPaperType.all.to_a + jp.journal_paper_type = jpts[val.to_i] + when 15 + jpas = JournalPaperAuthorType.all.to_a + ts = val.to_s.split(",") + ts.each do |t| + jp.journal_paper_author_type_ids << jpas[t.to_i].id + end + when 16 + jp.number_of_authors = val + when 17 + jp.isbn = val + when 18 + jp.publication_date = val + when 19 + jp.url = val + when 20 + jp.keywords = val + when 21 + jp.abstract = val + when 22 + jp.note = val + when 23 + jp.associated_project_translations = {"en" => val, "zh_tw" => val} + end + end + jp.member_profile = mp + jp.save + end end + + + + + + + + + diff --git a/app/views/admin/journal_papers/excel_format.xlsx.axlsx b/app/views/admin/journal_papers/excel_format.xlsx.axlsx new file mode 100644 index 0000000..922538a --- /dev/null +++ b/app/views/admin/journal_papers/excel_format.xlsx.axlsx @@ -0,0 +1,148 @@ +# 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_journal.paper_title") + " - " + t("en") + row1 << "textfield" + row2 << "" + row << t("personal_journal.paper_title") + " - " + t("zh_tw") + row1 << "textfield" + row2 << "" + + row << t("personal_journal.journal_title") + " - " + t("en") + row1 << "textfield" + row2 << "" + row << t("personal_journal.journal_title") + " - " + t("zh_tw") + row1 << "textfield" + row2 << "" + + row << t("personal_journal.authors") + " - " + t("en") + row1 << "textarea" + row2 << "" + row << t("personal_journal.authors") + " - " + t("zh_tw") + row1 << "textarea" + row2 << "" + + row << t("personal_journal.year") + row1 << "number" + row2 << "Example : 2015 or 2014 or 1987" + + row << t("personal_journal.language") + row1 << "select" + row2 << "en -> English, zh_tw -> Chinese" + + row << t("personal_journal.vol_no") + row1 << "textfield" + row2 << "" + + row << t("personal_journal.issue_no") + row1 << "textfield" + row2 << "" + + row << t("personal_journal.total_pages") + row1 << "number" + row2 << "Example: 10 or 20 or 100" + + row << t("personal_journal.level_type") + row1 << "checkbox" + t = "" + keys = [] + JournalLevel.each_with_index do |jl,i| + t = t + "#{i}" + " -> " + jl.title + ", " + end + if JournalLevel.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_journal.paper_type") + row1 << "select" + t = "" + keys = [] + JournalPaperType.each_with_index do |jl,i| + t = t + "#{i}" + " -> " + jl.title + ", " + end + if JournalPaperType.count > 0 + t = t + " Example : 0" + else + t = "Leave this field blank" + end + row2 << t + + row << t("personal_journal.author_type") + row1 << "checkbox" + t = "" + keys = [] + JournalPaperAuthorType.each_with_index do |jl,i| + t = t + "#{i}" + " -> " + jl.title + ", " + end + if JournalPaperAuthorType.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_journal.number_of_authors") + row1 << "number" + row2 << "Example: 1 or 2 or 10" + + row << t("personal_journal.isbn") + row1 << "textfield" + row2 << "" + + row << t("personal_journal.publication_date") + row1 << "date" + row2 << "Format: YYYY/MM/DD, Example: 2015/12/10" + + row << t("personal_journal.url") + row1 << "textfield" + row2 << "http://domain.com/path" + + row << t("personal_journal.keywords") + row1 << "textfield" + row2 << "Example: keyword1,keyword2" + + row << t("personal_journal.abstract") + row1 << "textarea" + row2 << "" + + row << t("personal_journal.note") + row1 << "textarea" + row2 << "" + + row << t("personal_journal.associated_project") + row1 << "textfield" + 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/journal_papers/index.html.erb b/app/views/admin/journal_papers/index.html.erb index 71a8015..24eeed8 100644 --- a/app/views/admin/journal_papers/index.html.erb +++ b/app/views/admin/journal_papers/index.html.erb @@ -13,10 +13,30 @@
- <%= link_to content_tag(:i, nil, :class => 'icon-plus icon-white') + t(:new_), new_admin_journal_paper_path, :class => 'btn btn-primary' %> - <%= link_to content_tag(:i, nil, :class => 'icon-cog icon-white') + t('setting'), admin_writing_journal_setting_path, :class => 'btn btn-primary pull-right' %> +
+ + +
+ <%= link_to content_tag(:i, nil, :class => 'icon-plus icon-white') + t(:new_), new_admin_journal_paper_path, :class => 'btn btn-primary' %> + <%= link_to content_tag(:i, nil, :class => 'icon-cog icon-white') + t('setting'), admin_writing_journal_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/locales/en.yml b/config/locales/en.yml index 32b9128..206ea14 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -4,6 +4,9 @@ en: journal_paper: Journal Paper personal_journal: paper_title : "Paper Title" + associated_project: Associated Project + paper_type : "Paper Type" + number_of_authors: "Number of Authors" journal_title : "Journal Title" authors : "Authors" author: "Author" diff --git a/config/routes.rb b/config/routes.rb index b32056e..cfd2b9b 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_journal_setting' => "journal_papers#setting" + get 'journal_papers/download_excel_format' => 'journal_papers#excel_format' + post 'journal_papers/import_from_excel' => 'journal_papers#import_from_excel' resources :journal_papers do collection do