diff --git a/app/controllers/admin/labs_controller.rb b/app/controllers/admin/labs_controller.rb index f7bdd99..d5d2e9e 100644 --- a/app/controllers/admin/labs_controller.rb +++ b/app/controllers/admin/labs_controller.rb @@ -96,6 +96,33 @@ class Admin::LabsController < OrbitMemberController end end + def excel_format + respond_to do |format| + format.xlsx { + response.headers['Content-Disposition'] = 'attachment; filename="labs_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_lab(row,mp) + end + end + redirect_to admin_labs_url + else + redirect_to admin_labs_url(:error => "1") + end + end + + def toggle_hide if params[:ids] @labs = Lab.any_in(_id: params[:ids]) diff --git a/app/helpers/admin/personal_labs_helper.rb b/app/helpers/admin/personal_labs_helper.rb index a4fd25f..9b31f8e 100644 --- a/app/helpers/admin/personal_labs_helper.rb +++ b/app/helpers/admin/personal_labs_helper.rb @@ -24,4 +24,70 @@ module Admin::PersonalLabsHelper end labs end + + def import_this_lab(row, mp) + value = nil + lab = Lab.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 + lab.lab_title_translations = value + when 4 + value = {"en" => val} + when 5 + begin + value["zh_tw"] = val + rescue + value = {"zh_tw" => val} + end + lab.location_translations = value + when 6 + value = {"en" => val} + when 7 + begin + value["zh_tw"] = val + rescue + value = {"zh_tw" => val} + end + lab.participating_professor_translations = value + when 8 + value = {"en" => val} + when 9 + begin + value["zh_tw"] = val + rescue + value = {"zh_tw" => val} + end + lab.participating_student_translations = value + when 10 + lab.year = val + when 11 + lab.extension_no = val + when 12 + lab.research_direction = val + when 13 + lab.facility = val + when 14 + lab.url = val + when 15 + lab.keywords = val + when 16 + lab.note = val + end + end + lab.member_profile = mp + lab.save + end + end \ No newline at end of file diff --git a/app/models/lab.rb b/app/models/lab.rb index 8af1121..b4fa245 100644 --- a/app/models/lab.rb +++ b/app/models/lab.rb @@ -49,12 +49,23 @@ class Lab "location" ] - pd_title = fields_to_show.collect do |t| - { - "plugin_data_title" => I18n.t("personal_lab.#{t}") - } + fields_to_remove = [] + + pd_title = [] + + fields_to_show.each do |t| + if (self.fields[t].type.to_s == "String" rescue false) + fields_to_remove << t if (datas.where(t.to_sym.ne => nil).or(t.to_sym.ne => "").count == 0 rescue false) + else + fields_to_remove << t if (datas.where(t.to_sym.ne => "").count == 0 rescue false) + end + pd_title << { + "plugin_data_title" => I18n.t("personal_lab.#{t}") + } if !fields_to_remove.include?(t) end + fields_to_show = fields_to_show - fields_to_remove + plugin_datas = datas.sort_for_frontend.collect do |p| pd_data = [] diff --git a/app/views/admin/labs/excel_format.xlsx.axlsx b/app/views/admin/labs/excel_format.xlsx.axlsx new file mode 100644 index 0000000..4ad7dac --- /dev/null +++ b/app/views/admin/labs/excel_format.xlsx.axlsx @@ -0,0 +1,86 @@ +# encoding: utf-8 + +wb = xlsx_package.workbook + +wb.add_worksheet(name: "Lab") 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_lab.lab_title") + " - " + t("en") + row1 << "textfield" + row2 << "" + row << t("personal_lab.lab_title") + " - " + t("zh_tw") + row1 << "textfield" + row2 << "" + + row << t("personal_lab.location") + " - " + t("en") + row1 << "textfield" + row2 << "" + row << t("personal_lab.location") + " - " + t("zh_tw") + row1 << "textfield" + row2 << "" + + row << t("personal_lab.participating_professor") + " - " + t("en") + row1 << "textfield" + row2 << "" + row << t("personal_lab.participating_professor") + " - " + t("zh_tw") + row1 << "textfield" + row2 << "" + + row << t("personal_lab.participating_student") + " - " + t("en") + row1 << "textfield" + row2 << "" + row << t("personal_lab.participating_student") + " - " + t("zh_tw") + row1 << "textfield" + row2 << "" + + row << t("personal_lab.year") + row1 << "number" + row2 << "Example : 2015 or 2014 or 1987" + + row << t("personal_lab.extension_no") + row1 << "textfield" + row2 << "" + + row << t("personal_lab.research_direction") + row1 << "textfield" + row2 << "" + + row << t("personal_lab.facility") + row1 << "textfield" + row2 << "" + + row << t("personal_lab.url") + row1 << "textfield" + row2 << "http://domain.com/path" + + row << t("personal_lab.keywords") + row1 << "textfield" + row2 << "Example: keyword1,keyword2" + + row << t("personal_lab.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/labs/index.html.erb b/app/views/admin/labs/index.html.erb index 38ffe03..1b05cf0 100644 --- a/app/views/admin/labs/index.html.erb +++ b/app/views/admin/labs/index.html.erb @@ -14,9 +14,29 @@
+
+ + +
<%= link_to content_tag(:i, nil, :class => 'icon-plus icon-white') + t(:new_), new_admin_lab_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/routes.rb b/config/routes.rb index 2810046..69d78e2 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 'lab_setting' => "labs#setting" + get 'labs/download_excel_format' => 'labs#excel_format' + post 'labs/import_from_excel' => 'labs#import_from_excel' resources :labs do collection do