add admin :import_from_excel
This commit is contained in:
parent
c3cc50d293
commit
66ca7c0068
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -23,7 +23,12 @@
|
|||
<span class="caret"></span>
|
||||
</button>
|
||||
<div class="dropdown-menu upload-box">
|
||||
<a class="" href="/admin/activities/download_excel_format.xlsx">Download excel format</a>
|
||||
<form action="/admin/activities/import_from_excel" method="post" enctype="multipart/form-data">
|
||||
<%= hidden_field_tag :authenticity_token, form_authenticity_token %>
|
||||
<input type="file" name="import_file" >
|
||||
<button class="btn btn-primary" type="submit"><%= t(:submit) %></button>
|
||||
<a class="" href="/admin/activities/download_excel_format.xlsx">Download excel format</a>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
<%= 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" %>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<% if params[:error] == "1" %>
|
||||
<script type="text/javascript">
|
||||
alert("File cannot be imported. File has more than 500 entries. Please seperate the entries in different files.");
|
||||
window.location.href = "<%= admin_activities_url %>"
|
||||
</script>
|
||||
<% end %>
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue