diff --git a/app/controllers/admin/activities_controller.rb b/app/controllers/admin/activities_controller.rb index d02e2ab..dd502f4 100644 --- a/app/controllers/admin/activities_controller.rb +++ b/app/controllers/admin/activities_controller.rb @@ -83,6 +83,25 @@ class Admin::ActivitiesController < OrbitMemberController end end + def import_from_excel + workbook = RubyXL::Parser.parse(params["import_file"].tempfile) + sheet = workbook[0] + byebug + 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_activity(row,mp) + end + end + redirect_to admin_activities_url + else + redirect_to admin_activities_url(:error => "1") + end + end + private def set_activity diff --git a/app/helpers/admin/personal_activities_helper.rb b/app/helpers/admin/personal_activities_helper.rb index 31fc27f..cb61ecd 100644 --- a/app/helpers/admin/personal_activities_helper.rb +++ b/app/helpers/admin/personal_activities_helper.rb @@ -31,4 +31,43 @@ module Admin::PersonalActivitiesHelper end return data end + + def import_this_activity(row, mp) + value = {} + activity = Activity.new + row.cells.each_with_index do |cell,index| + next if index < 2 + val = cell.value rescue nil + case index + when 2 + value["en"] = val + when 3 + value["zh_tw"] = val + activity.activity_name_translations = value + value = {} + when 4 + value["en"] = val + when 5 + value["zh_tw"] = val + activity.activity_organizer_translations = value + value = {} + when 6 + value["en"] = val + when 7 + value["zh_tw"] = val + activity.activity_area_translations = value + value = {} + when 8 + activity.year = val + when 9 + activity.activity_start_date = val + when 10 + activity.activity_end_date = val + when 11 + activity.note = val + end + end + activity.member_profile = mp + activity.save + end end diff --git a/app/views/admin/activities/index.html.erb b/app/views/admin/activities/index.html.erb index 975c777..b2dae40 100644 --- a/app/views/admin/activities/index.html.erb +++ b/app/views/admin/activities/index.html.erb @@ -23,7 +23,12 @@ <%= link_to content_tag(:i, nil, :class => 'icon-plus icon-white') + t(:new_), new_admin_activity_path, :class => 'btn btn-primary' %> @@ -34,3 +39,10 @@ <%= content_tag :div, paginate(@activities), class: "pagination pagination-centered" %> + +<% if params[:error] == "1" %> + +<% end %> diff --git a/config/routes.rb b/config/routes.rb index 1194e92..2963af3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,7 +3,7 @@ Rails.application.routes.draw do scope "(:locale)", locale: Regexp.new(locales.join("|")) do namespace :admin do get 'activities/download_excel_format' => 'activities#download_excel_format' - #post 'activities/import_from_excel' => 'activities#import_from_excel' + post 'activities/import_from_excel' => 'activities#import_from_excel' resources :activities do collection do