From 4735b107d76b94c18be3d7deba7a052d5aa0e476 Mon Sep 17 00:00:00 2001 From: bohung Date: Mon, 23 May 2022 22:20:09 +0800 Subject: [PATCH] Add export and import and batch delete subscribers feature. --- .../admin/e_paper_subscribers_controller.rb | 83 +++++ app/controllers/e_papers_controller.rb | 2 + .../admin/e_paper_subscribers/_index.html.erb | 21 +- .../_modal_select.html.erb | 202 +++++++++++ .../batch_delete_subscribers.html.erb | 339 ++++++++++++++++++ .../download_excel_format.axlsx | 25 ++ .../export_excel.xlsx.axlsx | 39 ++ .../admin/e_paper_subscribers/index.html.erb | 50 +++ config/locales/en.yml | 9 + config/locales/zh_tw.yml | 9 + config/routes.rb | 6 + lib/e_paper/engine.rb | 7 +- 12 files changed, 790 insertions(+), 2 deletions(-) create mode 100644 app/views/admin/e_paper_subscribers/_modal_select.html.erb create mode 100644 app/views/admin/e_paper_subscribers/batch_delete_subscribers.html.erb create mode 100644 app/views/admin/e_paper_subscribers/download_excel_format.axlsx create mode 100644 app/views/admin/e_paper_subscribers/export_excel.xlsx.axlsx diff --git a/app/controllers/admin/e_paper_subscribers_controller.rb b/app/controllers/admin/e_paper_subscribers_controller.rb index 2e064d0..9fe5271 100644 --- a/app/controllers/admin/e_paper_subscribers_controller.rb +++ b/app/controllers/admin/e_paper_subscribers_controller.rb @@ -25,4 +25,87 @@ class Admin::EPaperSubscribersController < OrbitAdminController end redirect_to admin_e_paper_subscribers_path end + + def export_excel + @epaper_subscribers = EPaperSubscriber.where(:email.nin=>[nil,""]).desc(:created_at) + @subscribers = @epaper_subscribers.where(:subscribed.ne=>false) + @unsubscribers = @epaper_subscribers.where(:subscribed=>false) + respond_to do |format| + format.xlsx { + response.headers['Content-Disposition'] = 'attachment; filename="'+Site.first.title+'-'+I18n.t('e_paper.e_paper')+'-'+I18n.t('e_paper.subscriber')+'.xlsx"' + } + end + end + def get_subscribers_modal + @epaper_subscribers = EPaperSubscriber.where(:email.nin=>[nil,""]).desc(:created_at) + @subscribers = @epaper_subscribers.where(:subscribed.ne=>false) + @unsubscribers = @epaper_subscribers.where(:subscribed=>false) + render :partial => 'modal_select', :layout => false + end + def import_from_excel + workbook = RubyXL::Parser.parse(params["import_file"].tempfile) + subscribe_sheet = workbook['Subscribe'] + unsubscribe_sheet = workbook['Unsubscribe'] + subscribe_sheet.each_with_index do |row, i| + next if i < 1 + c0 = row.cells[0] + c1 = row.cells[1] + if c0 + email = c0.value + if email.present? + subscriber = EPaperSubscriber.where(:email=>email).first + if subscriber.nil? + subscriber = EPaperSubscriber.new(:email=>email) + end + language = c1.value + if language.blank? + language = I18n.locale.to_s + end + subscriber.subscribed = true + subscriber.language = language + subscriber.save + end + end + end + unsubscribe_sheet.each_with_index do |row, i| + next if i < 1 + c0 = row.cells[0] + c1 = row.cells[1] + if c0 + email = c0.value + if email.present? + subscriber = EPaperSubscriber.where(:email=>email).first + if subscriber.nil? + subscriber = EPaperSubscriber.new(:email=>email) + end + language = c1.value + if language.blank? + language = I18n.locale.to_s + end + subscriber.subscribed = false + subscriber.language = language + subscriber.save + end + end + end + redirect_to admin_e_paper_subscribers_path + end + + def download_excel_format + @subscribers = [] + @unsubscribers = [] + respond_to do |format| + format.xlsx { + response.headers['Content-Disposition'] = 'attachment; filename="'+Site.first.title+'-'+I18n.t('e_paper.e_paper')+'-'+I18n.t('e_paper.subscriber')+'excel_format.xlsx"' + } + end + end + + def delete_subscribers + subscriber_ids = params['subscriber_ids'] + if subscriber_ids + EPaperSubscriber.where(:id.in=>subscriber_ids).destroy + end + redirect_to admin_e_paper_subscribers_path + end end \ No newline at end of file diff --git a/app/controllers/e_papers_controller.rb b/app/controllers/e_papers_controller.rb index f9b93c8..323de31 100644 --- a/app/controllers/e_papers_controller.rb +++ b/app/controllers/e_papers_controller.rb @@ -269,6 +269,8 @@ class EPapersController < ApplicationController subscriber.subscribed = false subscriber.save data = {"success" => true, "msg" => "Successfully Unsubscribed!!!"} + else + data = {"success" => false, "msg" => "You are not a subscriber!!!"} end render :json => data.to_json end diff --git a/app/views/admin/e_paper_subscribers/_index.html.erb b/app/views/admin/e_paper_subscribers/_index.html.erb index a95fc00..a238e5c 100644 --- a/app/views/admin/e_paper_subscribers/_index.html.erb +++ b/app/views/admin/e_paper_subscribers/_index.html.erb @@ -28,5 +28,24 @@
- <%= content_tag :div, paginate(@subscribers), class: "pagination pagination-centered" %> + <%= content_tag(:div, paginate(@subscribers), class: "pagination pagination-centered") %> +
+ <%= + content_tag(:div, link_to(t("e_paper.export"), admin_e_paper_subscribers_export_excel_path + '.xlsx', :class=>"btn btn-success"), class: "pull-right") + %> +
+ + +
+
\ No newline at end of file diff --git a/app/views/admin/e_paper_subscribers/_modal_select.html.erb b/app/views/admin/e_paper_subscribers/_modal_select.html.erb new file mode 100644 index 0000000..8158fd3 --- /dev/null +++ b/app/views/admin/e_paper_subscribers/_modal_select.html.erb @@ -0,0 +1,202 @@ + + + + + + \ No newline at end of file diff --git a/app/views/admin/e_paper_subscribers/batch_delete_subscribers.html.erb b/app/views/admin/e_paper_subscribers/batch_delete_subscribers.html.erb new file mode 100644 index 0000000..93c3b67 --- /dev/null +++ b/app/views/admin/e_paper_subscribers/batch_delete_subscribers.html.erb @@ -0,0 +1,339 @@ +<%= stylesheet_link_tag "member_select" %> +<%= stylesheet_link_tag "lib/checkbox-card" %> +
+
+
+
+

<%= t('e_paper.batch_delete_subscribers') %>

+
+
    +
    + <%= form_tag admin_e_paper_subscribers_delete_subscribers_path , :id=>'members_form' do %> + +

    <%= t("e_paper.delete_subscribers_hint") %>

    + + <% end %> +
    +
+
+
+
+ + + +
+
+ + + + + +
+
+<% if @thread %> + +<% end %> +
+ + + + + \ No newline at end of file diff --git a/app/views/admin/e_paper_subscribers/download_excel_format.axlsx b/app/views/admin/e_paper_subscribers/download_excel_format.axlsx new file mode 100644 index 0000000..bc7ef8e --- /dev/null +++ b/app/views/admin/e_paper_subscribers/download_excel_format.axlsx @@ -0,0 +1,25 @@ +# encoding: utf-8 + +wb = xlsx_package.workbook + +wb.add_worksheet(name: "Subscribe") do |sheet| + + heading = sheet.styles.add_style(:b => true, :locked => true) + body = sheet.styles.add_style(:font_name => '微軟正黑體') + row = [] + row << t('email') + row << t('language') + " , ex: #{I18n.available_locales.map{|l| l.to_s}.join(', ')}" + + sheet.add_row row, :style => heading +end + +wb.add_worksheet(name: "Unsubscribe") do |sheet| + + heading = sheet.styles.add_style(:b => true, :locked => true) + body = sheet.styles.add_style(:font_name => '微軟正黑體') + row = [] + row << t('email') + row << t('language') + " , ex: #{I18n.available_locales.map{|l| l.to_s}.join(', ')}" + + sheet.add_row row, :style => heading +end \ No newline at end of file diff --git a/app/views/admin/e_paper_subscribers/export_excel.xlsx.axlsx b/app/views/admin/e_paper_subscribers/export_excel.xlsx.axlsx new file mode 100644 index 0000000..c5ee895 --- /dev/null +++ b/app/views/admin/e_paper_subscribers/export_excel.xlsx.axlsx @@ -0,0 +1,39 @@ +# encoding: utf-8 + +wb = xlsx_package.workbook + +wb.add_worksheet(name: "Subscribe") do |sheet| + + heading = sheet.styles.add_style(:b => true, :locked => true) + body = sheet.styles.add_style(:font_name => '微軟正黑體') + row = [] + row << t('email') + row << t('language') + + sheet.add_row row, :style => heading + + @subscribers.each do |subscriber| + row = [] + row << subscriber.email + row << subscriber.language + sheet.add_row row, :style => body, :types => [:string,:string] + end +end + +wb.add_worksheet(name: "Unsubscribe") do |sheet| + + heading = sheet.styles.add_style(:b => true, :locked => true) + body = sheet.styles.add_style(:font_name => '微軟正黑體') + row = [] + row << t('email') + row << t('language') + + sheet.add_row row, :style => heading + + @unsubscribers.each do |subscriber| + row = [] + row << subscriber.email + row << subscriber.language + sheet.add_row row, :style => body, :types => [:string,:string] + end +end \ No newline at end of file diff --git a/app/views/admin/e_paper_subscribers/index.html.erb b/app/views/admin/e_paper_subscribers/index.html.erb index 80f6fa7..0fb0afd 100644 --- a/app/views/admin/e_paper_subscribers/index.html.erb +++ b/app/views/admin/e_paper_subscribers/index.html.erb @@ -1,3 +1,53 @@ + <%= render_filter @filter_fields, "index_table" %>
<%= render 'index'%> diff --git a/config/locales/en.yml b/config/locales/en.yml index 9c9fef0..7d26b3b 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1,7 +1,16 @@ en: module_name: e_paper: "ePaper" + restful_actions: + batch_delete_subscribers: "Batch Delete Subscribers" e_paper: + delete_subscribers_hint2: "Delete Subscribers below forever." + delete_subscribers_hint1: "Do you realy want to delete Subscribers you added forever?" + delete_subscribers_hint: "Click 『Add』 button to select Subscribers" + delete_subscribers: "Delete Subscribers" + batch_delete_subscribers: "Batch Delete Subscribers" + import: "Import" + export: "Export" get_invalid_emails: get invalid email get_failed_emails: get sended-failed emails e_paper: "ePaper" diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index 1c39424..b93b329 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -1,7 +1,16 @@ zh_tw: module_name: e_paper: 電子報 + restful_actions: + batch_delete_subscribers: "批次刪除訂閱者" e_paper: + delete_subscribers_hint2: "永久刪除以下訂閱者" + delete_subscribers_hint1: "您確定真的要永久刪除所選的訂閱者嗎?" + delete_subscribers_hint: "點選『新增』按鈕選擇訂閱者" + delete_subscribers: "刪除訂閱者" + batch_delete_subscribers: "批次刪除訂閱者" + import: "匯入" + export: "匯出" get_invalid_emails: 顯示格式錯誤的email get_failed_emails: 顯示寄送失敗的email e_paper: "電子報" diff --git a/config/routes.rb b/config/routes.rb index 17006f7..6170c7a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -12,6 +12,12 @@ Rails.application.routes.draw do get "get_invalid_emails" end end + get 'e_paper_subscribers/export_excel', to: 'e_paper_subscribers#export_excel' + get 'e_paper_subscribers/download_excel_format', to: 'e_paper_subscribers#download_excel_format' + post 'e_paper_subscribers/import_from_excel', to: 'e_paper_subscribers#import_from_excel' + get 'e_paper_subscribers/get_subscribers_modal', to: 'e_paper_subscribers#get_subscribers_modal' + get 'e_paper_subscribers/batch_delete_subscribers', to: 'e_paper_subscribers#batch_delete_subscribers' + post 'e_paper_subscribers/delete_subscribers', to: 'e_paper_subscribers#delete_subscribers' resources :e_paper_subscribers resources :e_papers do member do diff --git a/lib/e_paper/engine.rb b/lib/e_paper/engine.rb index 651be4e..352127f 100644 --- a/lib/e_paper/engine.rb +++ b/lib/e_paper/engine.rb @@ -65,9 +65,14 @@ module EPaper :priority=>6, :active_for_action=>{'admin/e_paper_subscribers'=>'index'}, :available_for => 'managers' + context_link 'e_paper.batch_delete_subscribers', + :link_path=>"admin_e_paper_subscribers_batch_delete_subscribers_path" , + :priority=>7, + :active_for_action=>{'admin/e_paper_subscribers'=>'batch_delete_subscribers'}, + :available_for => 'managers' context_link 'e_paper.instructions', :link_path=>"instructions_admin_e_papers_path" , - :priority=>7, + :priority=>8, :active_for_action=>{'admin/e_paper'=>'instructions'}, :available_for => 'users' end