diff --git a/app/controllers/admin/patents_controller.rb b/app/controllers/admin/patents_controller.rb index 6f6bd63..6ad2cff 100644 --- a/app/controllers/admin/patents_controller.rb +++ b/app/controllers/admin/patents_controller.rb @@ -97,6 +97,33 @@ class Admin::PatentsController < OrbitMemberController end end + def excel_format + respond_to do |format| + format.xlsx { + response.headers['Content-Disposition'] = 'attachment; filename="patents_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_patent(row,mp) + end + end + redirect_to admin_patents_url + else + redirect_to admin_patents_url(:error => "1") + end + end + + def toggle_hide if params[:ids] @patents = Patent.any_in(_id: params[:ids]) diff --git a/app/helpers/admin/personal_patents_helper.rb b/app/helpers/admin/personal_patents_helper.rb index 6c165f6..0a48b69 100644 --- a/app/helpers/admin/personal_patents_helper.rb +++ b/app/helpers/admin/personal_patents_helper.rb @@ -25,4 +25,66 @@ module Admin::PersonalPatentsHelper end patents end + + def import_this_patent(row,mp) + value = nil + patent = Patent.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 + patent.patent_title_translations = value + when 4 + value = {"en" => val} + when 5 + begin + value["zh_tw"] = val + rescue + value = {"zh_tw" => val} + end + patent.patent_country_translations = value + when 6 + value = {"en" => val} + when 7 + begin + value["zh_tw"] = val + rescue + value = {"zh_tw" => val} + end + patent.authors_translations = value + when 8 + patent.year = val + when 9 + patent.language = val + when 10 + pts = PatentType.asc(:created_at).all.to_a + ts = val.to_s.split(",") + ts.each do |t| + patent.patent_type_ids << pts[t.to_i].id if t.to_s.is_i? && t.to_i < pts.count + end + when 11 + patent.patent_no = val + when 12 + patent.publish_date = val + when 13 + patent.url = val + when 14 + patent.keywords = val + when 15 + patent.note = val + end + end + patent.member_profile = mp + patent.save + end end \ No newline at end of file diff --git a/app/views/admin/patents/excel_format.xlsx.axlsx b/app/views/admin/patents/excel_format.xlsx.axlsx new file mode 100644 index 0000000..bfed97d --- /dev/null +++ b/app/views/admin/patents/excel_format.xlsx.axlsx @@ -0,0 +1,96 @@ +# encoding: utf-8 + +wb = xlsx_package.workbook + +wb.add_worksheet(name: "Patents") 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_patent.patent_title") + " - " + t("en") + row1 << "textfield" + row2 << "" + row << t("personal_patent.patent_title") + " - " + t("zh_tw") + row1 << "textfield" + row2 << "" + + row << t("personal_patent.patent_country") + " - " + t("en") + row1 << "textfield" + row2 << "" + row << t("personal_patent.patent_country") + " - " + t("zh_tw") + row1 << "textfield" + row2 << "" + + row << t("personal_patent.authors") + " - " + t("en") + row1 << "textfield" + row2 << "" + row << t("personal_patent.authors") + " - " + t("zh_tw") + row1 << "textfield" + row2 << "" + + + row << t("personal_patent.year") + row1 << "number" + row2 << "Example : 2015 or 2014 or 1987" + + row << t("personal_patent.language") + row1 << "select" + row2 << "en -> English, zh_tw -> Chinese" + + row << t("personal_patent.patent_category") + row1 << "checkbox" + t = "" + PatentType.asc(:created_at).each_with_index do |jl,i| + t = t + "#{i}" + " -> " + jl.title + ", " + end + if PatentType.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_patent.issue_no") + row1 << "textfield" + row2 << "" + + row << t("personal_patent.publication_date") + row1 << "date" + row2 << "Format: YYYY/MM, Example: 2015/12" + + row << t("personal_patent.url") + row1 << "textfield" + row2 << "http://domain.com/path" + + row << t("personal_patent.keywords") + row1 << "textfield" + row2 << "Example: keyword1,keyword2" + + row << t("personal_patent.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/patents/index.html.erb b/app/views/admin/patents/index.html.erb index aa1890a..bc6d465 100644 --- a/app/views/admin/patents/index.html.erb +++ b/app/views/admin/patents/index.html.erb @@ -15,10 +15,30 @@
+
+ + +
<%= link_to content_tag(:i, nil, :class => 'icon-plus icon-white') + t(:new_), new_admin_patent_path, :class => 'btn btn-primary' %> <%= link_to content_tag(:i, nil, :class => 'icon-cog icon-white') + t('setting'), admin_patent_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 31e510c..5ac5d3f 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 'patent_setting' => "patents#setting" + get 'patents/download_excel_format' => 'patents#excel_format' + post 'patents/import_from_excel' => 'patents#import_from_excel' resources :patents do collection do