diff --git a/app/controllers/admin/announcements_controller.rb b/app/controllers/admin/announcements_controller.rb index 3afef4c..21a9b5f 100644 --- a/app/controllers/admin/announcements_controller.rb +++ b/app/controllers/admin/announcements_controller.rb @@ -52,6 +52,15 @@ class Admin::AnnouncementsController < OrbitAdminController end end + def export_excel + @announcements = Bulletin.all.desc(:created_at) + respond_to do |format| + format.xlsx { + response.headers['Content-Disposition'] = 'attachment; filename="announcement_export.xlsx"' + } + end + end + def import_from_xml download_tmp_xml params["import_xml"] import_from_tmp_xml File.read(File.join(Rails.root, "tmp", "ann_cc_ntu.xml")) diff --git a/app/views/admin/announcements/export_excel.xlsx.axlsx b/app/views/admin/announcements/export_excel.xlsx.axlsx new file mode 100644 index 0000000..a64c5e9 --- /dev/null +++ b/app/views/admin/announcements/export_excel.xlsx.axlsx @@ -0,0 +1,176 @@ +# 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 = "" + categories = @module_app.categories.asc(:created_at) + categories.each_with_index do |cat,i| + t = t + "#{i}" + " -> " + cat.title + ", " + end + if categories.count > 0 + t = t + " Example : 0" + else + t = "Leave this field blank" + end + row2 << t + + row << t("tags") + row1 << "select" + t = "" + tags = @module_app.tags.asc(:created_at) + tags.each_with_index do |tag,i| + t = t + "#{i}" + " -> " + tag.name + ", " + end + if 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 << "" + + row << t("link") + row1 << "textfield" + row2 << "Seperate with ';'. Example: http://rulingcom.com; http://google.com" + + row << t("link") + " " + t("url_alt") + " - " + t("en") + row1 << "textfield" + row2 << "Seperate with ';' with respective to the links in the link columns. Example : Rulingcom official site; Google search engine" + row << t("link") + " " + t("url_alt") + " - " + t("zh_tw") + row1 << "textfield" + row2 << "Seperate with ';' with respective to the links in the link columns. Example : Rulingcom official site; Google search engine" + + row << t("file_") + row1 << "textfield" + row2 << "Seperate with ';'. Example: http://www.example.com/images/example.png; http://www.example.com/images/example2.png" + + row << t("file_") + " " + t("description") + " - " + t("en") + row1 << "textfield" + row2 << "Seperate with ';' with respective to the links in the link columns. Example : Great view; Nice potrait" + row << t("file_") + " " + t("description") + " - " + t("zh_tw") + row1 << "textfield" + row2 << "Seperate with ';' with respective to the links in the link columns. Example : Great view; Nice potrait" + + row << t("file_") + " " + t("alternative") + " - " + t("en") + row1 << "textfield" + row2 << "Seperate with ';' with respective to the links in the link columns. Example : example1; example2" + row << t("file_") + " " + t("alternative") + " - " + t("zh_tw") + row1 << "textfield" + row2 << "Seperate with ';' with respective to the links in the link columns. Example : example1; example2" + + + sheet.add_row row, :style => heading + sheet.add_row row1 + sheet.add_row row2, :style => example + + @announcements.each do |anns| + row = [] + row << categories.to_a.index(anns.category) + t = [] + anns.tags.each do |tag| + t << tags.to_a.index(tag) + end + row << t.join(",") + row << (anns.postdate.strftime("%Y/%m/%d") rescue "") + row << (anns.deadline.strftime("%Y/%m/%d") rescue "") + row << (anns.is_top? ? 1 : 0) + row << (anns.is_hot? ? 1 : 0) + row << (anns.is_hidden? ? 1 : 0) + row << ("http://" + request.host_with_port + anns.image.url rescue "") + row << anns.image_description_translations["en"] + row << anns.image_description_translations["zh_tw"] + row << anns.title_translations["en"] + row << anns.title_translations["zh_tw"] + row << anns.subtitle_translations["en"] + row << anns.subtitle_translations["zh_tw"] + row << anns.text_translations["en"] + row << anns.text_translations["zh_tw"] + + links = anns.bulletin_links.asc(:created_at) + t = links.collect{|l|l.url} + row << t.join(";") + t = links.collect{|l|l.title_translations["en"]} + row << t.join(";") + t = links.collect{|l|l.title_translations["zh_tw"]} + row << t.join(";") + + files = anns.bulletin_files.asc(:created_at) + t = files.collect{|f|"http://" + request.host_with_port + f.file.url} + row << t.join(";") + t = files.collect{|l|l.description_translations["en"]} + row << t.join(";") + t = files.collect{|l|l.description_translations["zh_tw"]} + row << t.join(";") + t = files.collect{|l|l.title_translations["en"]} + row << t.join(";") + t = files.collect{|l|l.title_translations["zh_tw"]} + row << t.join(";") + sheet.add_row row + end + + + + + +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 index 3a09775..6bb67af 100644 --- a/app/views/admin/announcements/import.html.erb +++ b/app/views/admin/announcements/import.html.erb @@ -2,6 +2,12 @@ <% end %>
+

Export to Excel

+
+ +

Import from Excel

<%= hidden_field_tag :authenticity_token, form_authenticity_token %>
diff --git a/config/routes.rb b/config/routes.rb index dcf340f..6219823 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -9,6 +9,7 @@ Rails.application.routes.draw do post 'announcement/importanns', to: 'announcements#importanns' post 'announcement/import_from_xml', to: 'announcements#import_from_xml' get 'announcement/excel_format', to: 'announcements#excel_format' + get 'announcement/export_excel', to: 'announcements#export_excel' patch 'announcement/updatefeed', to: 'announcements#updatefeed' delete 'announcement/deletefeed', to: 'announcements#deletefeed' get 'announcement/destroy_preview/:slug_title-:uid', to: 'announcements#destroy_preview'