diff --git a/app/controllers/admin/experiences_controller.rb b/app/controllers/admin/experiences_controller.rb index 8d7c66a..32a05c9 100644 --- a/app/controllers/admin/experiences_controller.rb +++ b/app/controllers/admin/experiences_controller.rb @@ -97,6 +97,32 @@ class Admin::ExperiencesController < OrbitMemberController end end + def excel_format + respond_to do |format| + format.xlsx { + response.headers['Content-Disposition'] = 'attachment; filename="experiences_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_experience(row,mp) + end + end + redirect_to admin_experiences_url + else + redirect_to admin_experiences_url(:error => "1") + end + end + def toggle_hide if params[:ids] @experiences = Experience.any_in(_id: params[:ids]) diff --git a/app/helpers/admin/personal_experiences_helper.rb b/app/helpers/admin/personal_experiences_helper.rb index f708392..f380bd3 100644 --- a/app/helpers/admin/personal_experiences_helper.rb +++ b/app/helpers/admin/personal_experiences_helper.rb @@ -17,4 +17,61 @@ module Admin::PersonalExperiencesHelper end experiences end + + def import_this_experience(row,mp) + value = nil + exp = Experience.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 + exp.organizationt_title_translations = value + when 4 + value = {"en" => val} + when 5 + begin + value["zh_tw"] = val + rescue + value = {"zh_tw" => val} + end + exp.department_translations = value + when 6 + value = {"en" => val} + when 7 + begin + value["zh_tw"] = val + rescue + value = {"zh_tw" => val} + end + exp.job_title_translations = value + when 8 + ets = ExperienceType.asc(:created_at).all.to_a + exp.experience_type = ets[val.to_i] if val.to_s.is_i? && val.to_i < ets.count + when 9 + exp.language = val + when 10 + exp.start_date = val + when 11 + exp.end_date = val + when 12 + exp.url = val + when 13 + exp.keywords = val + when 14 + exp.note = val + end + end + exp.member_profile = mp + exp.save + end end \ No newline at end of file diff --git a/app/views/admin/experiences/excel_format.xlsx.axlsx b/app/views/admin/experiences/excel_format.xlsx.axlsx new file mode 100644 index 0000000..67c2598 --- /dev/null +++ b/app/views/admin/experiences/excel_format.xlsx.axlsx @@ -0,0 +1,90 @@ +# encoding: utf-8 + +wb = xlsx_package.workbook + +wb.add_worksheet(name: "Experience") 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_experience.organizationt_title") + " - " + t("en") + row1 << "textfield" + row2 << "" + row << t("personal_experience.organizationt_title") + " - " + t("zh_tw") + row1 << "textfield" + row2 << "" + + row << t("personal_experience.department") + " - " + t("en") + row1 << "textfield" + row2 << "" + row << t("personal_experience.department") + " - " + t("zh_tw") + row1 << "textfield" + row2 << "" + + row << t("personal_experience.job_title") + " - " + t("en") + row1 << "textfield" + row2 << "" + row << t("personal_experience.job_title") + " - " + t("zh_tw") + row1 << "textfield" + row2 << "" + + row << t("personal_experience.experience_category") + row1 << "select" + t = "" + ExperienceType.asc(:created_at).each_with_index do |jl,i| + t = t + "#{i}" + " -> " + jl.title + ", " + end + if ExperienceType.count > 0 + t = t + " Example : 0" + else + t = "Leave this field blank" + end + row2 << t + + row << t("personal_experience.language") + row1 << "select" + row2 << "en -> English, zh_tw -> Chinese" + + row << t("personal_experience.start_date") + row1 << "date" + row2 << "Format: YYYY/MM/DD, Example: 2015/12/10" + + row << t("personal_experience.end_date") + row1 << "date" + row2 << "Format: YYYY/MM/DD, Example: 2015/12/10" + + row << t("personal_experience.url") + row1 << "textfield" + row2 << "http://domain.com/path" + + row << t("personal_experience.keywords") + row1 << "textfield" + row2 << "Example: keyword1,keyword2" + + row << t("personal_experience.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/experiences/index.html.erb b/app/views/admin/experiences/index.html.erb index f945752..b2eb14f 100644 --- a/app/views/admin/experiences/index.html.erb +++ b/app/views/admin/experiences/index.html.erb @@ -15,10 +15,30 @@
+
+ + +
<%= link_to content_tag(:i, nil, :class => 'icon-plus icon-white') + t(:new_), new_admin_experience_path, :class => 'btn btn-primary' %> <%= link_to content_tag(:i, nil, :class => 'icon-cog icon-white') + t('setting'), admin_experience_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 1746c16..6764041 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -14,6 +14,8 @@ en: authors : "Authors" tags : "Tags" year : "Year" + start_date : Start Date + end_date : End Date language : "Language" isbn : "ISSN(ISBN)" vol_no : "Vol.No" diff --git a/config/routes.rb b/config/routes.rb index 720ac9c..5955424 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,6 +3,9 @@ Rails.application.routes.draw do scope "(:locale)", locale: Regexp.new(locales.join("|")) do namespace :admin do get 'experience_setting' => "experiences#setting" + get 'experiences/download_excel_format' => 'experiences#excel_format' + post 'experiences/import_from_excel' => 'experiences#import_from_excel' + resources :experiences do collection do