From 0754f781e666268afe465edadc9b25e53e04748d Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Tue, 15 Dec 2015 16:10:46 +0800 Subject: [PATCH] import done for project --- app/controllers/admin/projects_controller.rb | 26 +++++ app/helpers/admin/personal_projects_helper.rb | 70 ++++++++++++ .../admin/projects/excel_format.xlsx.axlsx | 104 ++++++++++++++++++ app/views/admin/projects/index.html.erb | 22 +++- config/routes.rb | 2 + 5 files changed, 223 insertions(+), 1 deletion(-) create mode 100644 app/views/admin/projects/excel_format.xlsx.axlsx diff --git a/app/controllers/admin/projects_controller.rb b/app/controllers/admin/projects_controller.rb index a352357..b40e217 100644 --- a/app/controllers/admin/projects_controller.rb +++ b/app/controllers/admin/projects_controller.rb @@ -96,6 +96,32 @@ class Admin::ProjectsController < OrbitMemberController end end + def excel_format + respond_to do |format| + format.xlsx { + response.headers['Content-Disposition'] = 'attachment; filename="projects_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_project(row,mp) + end + end + redirect_to admin_projects_url + else + redirect_to admin_projects_url(:error => "1") + end + end + def toggle_hide if params[:ids] @projects = Project.any_in(_id: params[:ids]) diff --git a/app/helpers/admin/personal_projects_helper.rb b/app/helpers/admin/personal_projects_helper.rb index 9b0453c..ea2f8d6 100644 --- a/app/helpers/admin/personal_projects_helper.rb +++ b/app/helpers/admin/personal_projects_helper.rb @@ -26,4 +26,74 @@ module Admin::PersonalProjectsHelper end projects end + + def import_this_project(row, mp) + value = nil + project = Project.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 + project.project_title_translations = value + when 4 + value = {"en" => val} + when 5 + begin + value["zh_tw"] = val + rescue + value = {"zh_tw" => val} + end + project.job_title_translations = value + when 6 + value = {"en" => val} + when 7 + begin + value["zh_tw"] = val + rescue + value = {"zh_tw" => val} + end + project.participator_translations = value + when 8 + value = {"en" => val} + when 9 + begin + value["zh_tw"] = val + rescue + value = {"zh_tw" => val} + end + project.unit_translations = value + when 10 + project.year = val + when 11 + project.language = val + when 12 + pts = ProjectType.asc(:created_at).all.to_a + project.project_type = pts[val.to_i] if val.to_s.is_i? && val.to_i < pts.count + when 13 + project.period_start_date = val + when 14 + project.period_end_date = val + when 15 + project.url = val + when 16 + project.keywords = val + when 17 + project.abstract = val + when 18 + project.note = val + end + end + project.member_profile = mp + project.save + end end \ No newline at end of file diff --git a/app/views/admin/projects/excel_format.xlsx.axlsx b/app/views/admin/projects/excel_format.xlsx.axlsx new file mode 100644 index 0000000..d104e3d --- /dev/null +++ b/app/views/admin/projects/excel_format.xlsx.axlsx @@ -0,0 +1,104 @@ +# encoding: utf-8 + +wb = xlsx_package.workbook + +wb.add_worksheet(name: "Project") 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_project.project_title") + " - " + t("en") + row1 << "textfield" + row2 << "" + row << t("personal_project.project_title") + " - " + t("zh_tw") + row1 << "textfield" + row2 << "" + + row << t("personal_project.job_title") + " - " + t("en") + row1 << "textfield" + row2 << "" + row << t("personal_project.job_title") + " - " + t("zh_tw") + row1 << "textfield" + row2 << "" + + row << t("personal_project.participator") + " - " + t("en") + row1 << "textfield" + row2 << "" + row << t("personal_project.participator") + " - " + t("zh_tw") + row1 << "textfield" + row2 << "" + + row << t("personal_project.unit") + " - " + t("en") + row1 << "textfield" + row2 << "" + row << t("personal_project.unit") + " - " + t("zh_tw") + row1 << "textfield" + row2 << "" + + row << t("personal_project.year") + row1 << "number" + row2 << "Example : 2015 or 2014 or 1987" + + row << t("personal_project.language") + row1 << "select" + row2 << "en -> English, zh_tw -> Chinese" + + row << t("personal_project.project_category") + row1 << "select" + t = "" + ProjectType.asc(:created_at).each_with_index do |jl,i| + t = t + "#{i}" + " -> " + jl.title + ", " + end + if ProjectType.count > 0 + t = t + " Example : 0" + else + t = "Leave this field blank" + end + row2 << t + + row << t("personal_project.start_date") + row1 << "date" + row2 << "Format: YYYY/MM, Example: 2014/12" + + row << t("personal_project.end_date") + row1 << "date" + row2 << "Format: YYYY/MM, Example: 2015/12" + + row << t("personal_project.url") + row1 << "textfield" + row2 << "http://domain.com/path" + + row << t("personal_project.keywords") + row1 << "textfield" + row2 << "Example: keyword1,keyword2" + + row << t("personal_project.abstract") + row1 << "textarea" + row2 << "" + + row << t("personal_project.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/projects/index.html.erb b/app/views/admin/projects/index.html.erb index 04e23ca..e67d1c8 100644 --- a/app/views/admin/projects/index.html.erb +++ b/app/views/admin/projects/index.html.erb @@ -16,10 +16,30 @@
+
+ + +
<%= link_to content_tag(:i, nil, :class => 'icon-plus icon-white') + t(:new_), new_admin_project_path, :class => 'btn btn-primary' %> <%= link_to content_tag(:i, nil, :class => 'icon-cog icon-white') + t('setting'), admin_project_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 8518a6a..96a9368 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 'project_setting' => "projects#setting" + get 'projects/download_excel_format' => 'projects#excel_format' + post 'projects/import_from_excel' => 'projects#import_from_excel' resources :projects do collection do