From cca0c1218bbe9cde52d628193212998486cf13c2 Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Tue, 15 Dec 2015 18:08:07 +0800 Subject: [PATCH] excel import for books --- app/controllers/admin/books_controller.rb | 26 ++++ app/helpers/admin/personal_books_helper.rb | 87 ++++++++++++ app/views/admin/books/excel_format.xlsx.axlsx | 128 ++++++++++++++++++ app/views/admin/books/index.html.erb | 22 ++- config/routes.rb | 2 + 5 files changed, 264 insertions(+), 1 deletion(-) create mode 100644 app/views/admin/books/excel_format.xlsx.axlsx diff --git a/app/controllers/admin/books_controller.rb b/app/controllers/admin/books_controller.rb index 8c87d3b..848b15c 100644 --- a/app/controllers/admin/books_controller.rb +++ b/app/controllers/admin/books_controller.rb @@ -108,6 +108,32 @@ class Admin::BooksController < OrbitMemberController end end + def excel_format + respond_to do |format| + format.xlsx { + response.headers['Content-Disposition'] = 'attachment; filename="book_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_book(row,mp) + end + end + redirect_to admin_books_url + else + redirect_to admin_books_url(:error => "1") + end + end + def toggle_hide if params[:ids] @books = Book.any_in(_id: params[:ids]) diff --git a/app/helpers/admin/personal_books_helper.rb b/app/helpers/admin/personal_books_helper.rb index bafee92..5f7477a 100644 --- a/app/helpers/admin/personal_books_helper.rb +++ b/app/helpers/admin/personal_books_helper.rb @@ -25,4 +25,91 @@ module Admin::PersonalBooksHelper end books end + + def import_this_book(row, mp) + value = nil + book = Book.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 + book.book_title_translations = value + when 4 + value = {"en" => val} + when 5 + begin + value["zh_tw"] = val + rescue + value = {"zh_tw" => val} + end + book.extracted_chapters_translations = value + when 6 + value = {"en" => val} + when 7 + begin + value["zh_tw"] = val + rescue + value = {"zh_tw" => val} + end + book.publisher_translations = value + when 8 + value = {"en" => val} + when 9 + begin + value["zh_tw"] = val + rescue + value = {"zh_tw" => val} + end + book.editor_translations = value + when 10 + value = {"en" => val} + when 11 + begin + value["zh_tw"] = val + rescue + value = {"zh_tw" => val} + end + book.authors_translations = value + when 12 + book.year = val + when 13 + book.language = val + when 14 + bts = BookType.asc(:created_at).all.to_a + book.book_type = bts[val.to_i] if val.to_s.is_i? && val.to_i < bts.count + when 15 + book.pages = val + when 16 + bats = BookAuthorType.asc(:created_at).all.to_a + ts = val.to_s.split(",") + ts.each do |t| + book.book_author_type_ids << bats[t.to_i].id if t.to_s.is_i? && t.to_i < bats.count + end + when 17 + book.number_of_authors = val + when 18 + book.publish_date = val + when 19 + book.isbn = val + when 20 + book.url = val + when 21 + book.keywords = val + when 22 + book.note = val + end + end + book.member_profile = mp + book.save + end end \ No newline at end of file diff --git a/app/views/admin/books/excel_format.xlsx.axlsx b/app/views/admin/books/excel_format.xlsx.axlsx new file mode 100644 index 0000000..38e6f90 --- /dev/null +++ b/app/views/admin/books/excel_format.xlsx.axlsx @@ -0,0 +1,128 @@ +# encoding: utf-8 + +wb = xlsx_package.workbook + +wb.add_worksheet(name: "Books") 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_book.book_title") + " - " + t("en") + row1 << "textfield" + row2 << "" + row << t("personal_book.book_title") + " - " + t("zh_tw") + row1 << "textfield" + row2 << "" + + row << t("personal_book.extracted_chapters") + " - " + t("en") + row1 << "textfield" + row2 << "" + row << t("personal_book.extracted_chapters") + " - " + t("zh_tw") + row1 << "textfield" + row2 << "" + + row << t("personal_book.publisher") + " - " + t("en") + row1 << "textfield" + row2 << "" + row << t("personal_book.publisher") + " - " + t("zh_tw") + row1 << "textfield" + row2 << "" + + row << t("personal_book.editor") + " - " + t("en") + row1 << "textfield" + row2 << "" + row << t("personal_book.editor") + " - " + t("zh_tw") + row1 << "textfield" + row2 << "" + + row << t("personal_book.authors") + " - " + t("en") + row1 << "textarea" + row2 << "" + row << t("personal_book.authors") + " - " + t("zh_tw") + row1 << "textarea" + row2 << "" + + row << t("personal_book.year") + row1 << "number" + row2 << "Example : 2015 or 2014 or 1987" + + row << t("personal_book.language") + row1 << "select" + row2 << "en -> English, zh_tw -> Chinese" + + row << t("personal_book.book_paper_type") + row1 << "select" + t = "" + BookType.asc(:created_at).each_with_index do |jl,i| + t = t + "#{i}" + " -> " + jl.title + ", " + end + if BookType.count > 0 + t = t + " Example : 0" + else + t = "Leave this field blank" + end + row2 << t + + row << t("personal_book.pages") + row1 << "number" + row2 << "Example: 10 or 20 or 100" + + row << t("personal_book.author_type") + row1 << "checkbox" + t = "" + BookAuthorType.asc(:created_at).each_with_index do |jl,i| + t = t + "#{i}" + " -> " + jl.title + ", " + end + if BookAuthorType.count > 0 + t = t + " Example : 0, if more than one then : 0,1" + else + t = "Leave this field blank" + end + row2 << t + + row << t("personal_book.number_of_authors") + row1 << "number" + row2 << "Example: 1 or 2 or 10" + + row << t("personal_book.publish_date") + row1 << "date" + row2 << "Format: YYYY/MM/DD, Example: 2015/12/10" + + row << t("personal_book.isbn") + row1 << "textfield" + row2 << "" + + row << t("personal_book.url") + row1 << "textfield" + row2 << "http://domain.com/path" + + row << t("personal_book.keywords") + row1 << "textfield" + row2 << "Example: keyword1,keyword2" + + row << t("personal_book.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/books/index.html.erb b/app/views/admin/books/index.html.erb index a3feb70..b2274c1 100644 --- a/app/views/admin/books/index.html.erb +++ b/app/views/admin/books/index.html.erb @@ -13,6 +13,20 @@
+
+ + +
<%= link_to content_tag(:i, nil, :class => 'icon-plus icon-white') + t(:new_), new_admin_book_path, :class => 'btn btn-primary' %> <% if current_user.is_admin? %> <%= link_to content_tag(:i, nil, :class => 'icon-cog icon-white') + t('setting'), admin_book_setting_path, :class => 'btn btn-primary pull-right' %> @@ -21,4 +35,10 @@ -
\ 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 ac7e997..d69d426 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 'book_setting' => "books#setting" + get 'books/download_excel_format' => 'books#excel_format' + post 'books/import_from_excel' => 'books#import_from_excel' resources :books do collection do