diff --git a/app/controllers/admin/announcements_controller.rb b/app/controllers/admin/announcements_controller.rb index 737677e..ad55ed9 100644 --- a/app/controllers/admin/announcements_controller.rb +++ b/app/controllers/admin/announcements_controller.rb @@ -1,4 +1,5 @@ # encoding: utf-8 +require 'rubyXL' class Admin::AnnouncementsController < OrbitAdminController include Admin::AnnouncementsHelper before_action ->(module_app = @app_title) { set_variables module_app } @@ -43,6 +44,36 @@ class Admin::AnnouncementsController < OrbitAdminController end end + def import + end + + def excel_format + respond_to do |format| + format.xlsx { + response.headers['Content-Disposition'] = 'attachment; filename="announcement_import_format.xlsx"' + } + end + end + + + def importanns + workbook = RubyXL::Parser.parse(params["import_file"].tempfile) + categories = @module_app.categories.asc(:created_at).to_a + tags = @module_app.tags.asc(:created_at).to_a + sheet = workbook[0] + if sheet.count <= 503 + sheet.each_with_index do |row, i| + next if i < 3 + v = row.cells.first.value + next if v == "" || v.nil? + import_this_announcement(row, categories, tags) + end + redirect_to admin_announcements_path + else + redirect_to admin_announcements_path(:error => "1") + end + end + def createsettings setting = AnnouncementSetting.new(settings_params) setting.save diff --git a/app/helpers/admin/announcements_helper.rb b/app/helpers/admin/announcements_helper.rb index 26cfa8e..68772cc 100644 --- a/app/helpers/admin/announcements_helper.rb +++ b/app/helpers/admin/announcements_helper.rb @@ -25,6 +25,71 @@ module Admin::AnnouncementsHelper request.protocol+(request.host_with_port+ann_page.url+'/'+bulletin.to_param).gsub('//','/') rescue "/" end + def import_this_announcement(row,categories,tags) + value = {} + anns = Bulletin.new + row.cells.each_with_index do |cell,index| + next if cell.nil? + val = cell.value + next if val.nil? || val == "" + case index + when 0 + anns.category = categories[val.to_i] + when 1 + new_tags = [] + if (val.include?(",") rescue false) + ts = val.split(",") + ts.each do |t| + new_tags << tags[t.to_i] + end + else + new_tags << tags[val.to_i] + end + anns.tags=new_tags + when 2 + anns.postdate = val + when 3 + anns.deadline = val + when 4 + anns.is_top = (val.to_i == 1 ? true : false) + when 5 + anns.is_hot = (val.to_i == 1 ? true : false) + when 6 + anns.is_hidden = (val.to_i == 1 ? true : false) + when 7 + anns.remote_image_url = val + when 8 + value["en"] = val + when 9 + value["zh_tw"] = val + anns.image_description_translations = value + value = {} + when 10 + value["en"] = val + when 11 + value["zh_tw"] = val + anns.title_translations = value + value = {} + when 12 + value["en"] = val + when 13 + value["zh_tw"] = val + anns.subtitle_translations = value + value = {} + when 14 + value["en"] = val + when 15 + value["zh_tw"] = val + anns.text_translations = value + value = {} + end + end + anns.create_user_id = current_user.id.to_s + anns.update_user_id = current_user.id.to_s + anns.approved = true + anns.save + end + def load_access_level if (current_user.is_admin? rescue false) @access_level = "admin" diff --git a/app/views/admin/announcements/excel_format.xlsx.axlsx b/app/views/admin/announcements/excel_format.xlsx.axlsx new file mode 100644 index 0000000..1ecceaf --- /dev/null +++ b/app/views/admin/announcements/excel_format.xlsx.axlsx @@ -0,0 +1,95 @@ +# encoding: utf-8 + +wb = xlsx_package.workbook + +wb.add_worksheet(name: "Annoucement") do |sheet| + + heading = sheet.styles.add_style(:b => true, :locked => true) + example = sheet.styles.add_style(:i => true) + row = [] + row1 = [] + row2 = [] + + row << t("category") + row1 << "select" + t = "" + @module_app.categories.asc(:created_at).each_with_index do |cat,i| + t = t + "#{i}" + " -> " + cat.title + ", " + end + if @module_app.categories.count > 0 + t = t + " Example : 0" + else + t = "Leave this field blank" + end + row2 << t + + row << t("tags") + row1 << "select" + t = "" + @module_app.tags.asc(:created_at).each_with_index do |tag,i| + t = t + "#{i}" + " -> " + tag.name + ", " + end + if @module_app.tags.count > 0 + t = t + " Example : 0,1,2" + else + t = "Leave this field blank" + end + row2 << t + + row << t("start_date") + row1 << "date" + row2 << "Format: YYYY/MM/DD, Example: 2015/12/10" + + row << t("end_date") + row1 << "date" + row2 << "Format: YYYY/MM/DD, Example: 2015/12/12" + + row << t("top") + row1 << "boolean" + row2 << "0 for false, 1 for true" + + row << t("hot") + row1 << "boolean" + row2 << "0 for false, 1 for true" + + row << t("hide") + row1 << "boolean" + row2 << "0 for false, 1 for true " + + row << t("image") + row1 << "url" + row2 << "http://www.example.com/images/example.png" + + row << t("image") + " " + t("description") + " - " + t("en") + row1 << "textfield" + row2 << "" + row << t("image") + " " + t("description") + " - " + t("zh_tw") + row1 << "textfield" + row2 << "" + + row << t("title") + " - " + t("en") + row1 << "textfield" + row2 << "" + row << t("title") + " - " + t("zh_tw") + row1 << "textfield" + row2 << "" + + row << t("subtitle") + " - " + t("en") + row1 << "textarea" + row2 << "" + row << t("subtitle") + " - " + t("zh_tw") + row1 << "textarea" + row2 << "" + + row << t("content") + " - " + t("en") + row1 << "editor" + row2 << "" + row << t("content") + " - " + t("zh_tw") + row1 << "editor" + row2 << "" + + sheet.add_row row, :style => heading + sheet.add_row row1 + sheet.add_row row2, :style => example + +end \ No newline at end of file diff --git a/app/views/admin/announcements/import.html.erb b/app/views/admin/announcements/import.html.erb new file mode 100644 index 0000000..27e34c8 --- /dev/null +++ b/app/views/admin/announcements/import.html.erb @@ -0,0 +1,30 @@ +
\ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index 40e9764..8a9879e 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -18,6 +18,7 @@ en: approve: Approve all_articles: All Articles settings: Settings + import: Import announcement: Announcement approval_setting: Approval Setting approve_bulletin_fail: Approval Fail diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index e2de266..967ac30 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -15,6 +15,7 @@ zh_tw: approve: 通過 feed_name: Feed 標題 settings: Settings + import: Import rssfeed: Rss Feed Link feed_list: 訂閱清單 all_articles: 文章列表 diff --git a/config/routes.rb b/config/routes.rb index 985ff12..507ab7c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,6 +6,8 @@ Rails.application.routes.draw do namespace :admin do post 'announcement/preview', to: 'announcements#preview' post 'announcement/createfeed', to: 'announcements#createfeed' + post 'announcement/importanns', to: 'announcements#importanns' + get 'announcement/excel_format', to: 'announcements#excel_format' patch 'announcement/updatefeed', to: 'announcements#updatefeed' delete 'announcement/deletefeed', to: 'announcements#deletefeed' get 'announcement/destroy_preview/:slug_title-:uid', to: 'announcements#destroy_preview' @@ -13,6 +15,7 @@ Rails.application.routes.draw do get 'announcement/feed', to: 'announcements#feed' get 'announcements/feedform', to: 'announcements#feedform' get 'announcement/settings', to: 'announcements#settings' + get 'announcement/import', to: 'announcements#import' post 'announcement/createsettings', to: 'announcements#createsettings' patch 'announcement/updatesettings', to: 'announcements#updatesettings' resources :announcements diff --git a/lib/announcement/engine.rb b/lib/announcement/engine.rb index 5dd7f50..5a55f1b 100644 --- a/lib/announcement/engine.rb +++ b/lib/announcement/engine.rb @@ -49,6 +49,11 @@ module Announcement :priority=>5, :active_for_action=>{'admin/announcements'=>'feed'}, :available_for => 'managers' + context_link 'announcement.import', + :link_path=>"admin_announcement_import_path" , + :priority=>6, + :active_for_action=>{'admin/announcements'=>'import'}, + :available_for => 'managers' context_link 'announcement.settings', :link_path=>"admin_announcement_settings_path" , :priority=>6,