epaper/app/controllers/admin/e_paper_subscribers_control...

169 lines
6.5 KiB
Ruby

class Admin::EPaperSubscribersController < OrbitAdminController
def initialize
super
@app_title = "e_paper"
end
def index
@table_fields = [t('email'), t('status'), t('language')]
@filter_fields = filter_fields([], [])
@filter_fields.delete(:status)
@filter_fields.delete(:category)
@filter_fields.delete(:tags)
@subscribers = EPaperSubscriber.order_by(sort)
@subscribers = search_data(@subscribers,[:email]).page(params[:page]).per(10)
@thread = (params[:thread_id] ? Multithread.find(params[:thread_id]) : nil rescue nil)
if @thread && @thread.status[:status] == 'finish'
@thread = nil
end
render :partial => "index" if request.xhr?
end
def destroy
subscriber = EPaperSubscriber.find(params[:id]) rescue nil
if !subscriber.nil?
subscriber.destroy
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
thread = Multithread.where(:key=>'import_epaper_subscribers').first
if thread.nil?
thread = Multithread.create(:key=>'import_epaper_subscribers',:status=>{:status=>'Processing'})
else
thread.update(:status=>{:status=>'Processing'})
end
Thread.new do
workbook = RubyXL::Parser.parse(params["import_file"].tempfile)
subscribe_sheet = workbook['Subscribe']
unsubscribe_sheet = workbook['Unsubscribe']
all_count = (subscribe_sheet ? (subscribe_sheet.count - 1) : 0) + (unsubscribe_sheet ? (unsubscribe_sheet.count - 1) : 0)
puts_every_count = [all_count * 3 / 100, 1].max
current_count = 0
finish_percent = 0
thread.update(:status=>{:status=>'Importing','all_count'=>all_count,'current_count'=>current_count,'finish_percent'=>finish_percent})
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
current_count += 1
if current_count % puts_every_count == 0
finish_percent = (current_count * 100.0 / all_count).round(1)
thread.update(:status=>{:status=>'Importing','all_count'=>all_count,'current_count'=>current_count,'finish_percent'=>finish_percent})
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
current_count += 1
if current_count % puts_every_count == 0
finish_percent = (current_count * 100.0 / all_count).round(1)
thread.update(:status=>{:status=>'Importing','all_count'=>all_count,'current_count'=>current_count,'finish_percent'=>finish_percent})
end
end
finish_percent = 100
thread.update(:status=>{:status=>'finish','all_count'=>all_count,'current_count'=>current_count,'finish_percent'=>finish_percent})
end
redirect_to admin_e_paper_subscribers_path(thread_id: thread.id)
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 batch_delete_subscribers
@thread = (params[:thread_id] ? Multithread.find(params[:thread_id]) : nil rescue nil)
if @thread && @thread.status[:status] == 'finish'
@thread = nil
end
end
def delete_subscribers
subscriber_ids = params['subscriber_ids']
thread = Multithread.where(:key=>'delete_epaper_subscribers').first
if thread.nil?
thread = Multithread.create(:key=>'delete_epaper_subscriber',:status=>{:status=>'Processing'})
else
thread.update(:status=>{:status=>'Processing'})
end
if subscriber_ids
all_count = subscriber_ids.count
puts_every_count = [all_count * 3 / 100, 1].max
current_count = 0
finish_percent = 0
thread.update(:status=>{:status=>'Deleting','all_count'=>all_count,'current_count'=>current_count,'finish_percent'=>finish_percent})
Thread.new do
EPaperSubscriber.where(:id.in=>subscriber_ids).to_a.each do |s|
s.destroy
current_count += 1
if current_count % puts_every_count == 0
finish_percent = (current_count * 100.0 / all_count).round(1)
thread.update(:status=>{:status=>'Deleting','all_count'=>all_count,'current_count'=>current_count,'finish_percent'=>finish_percent})
end
end
finish_percent = 100
thread.update(:status=>{:status=>'finish','all_count'=>all_count,'current_count'=>current_count,'finish_percent'=>finish_percent})
end
else
thread.update(:status=>{:status=>'finish'})
end
redirect_to admin_e_paper_subscribers_batch_delete_subscribers_path(thread_id: thread.id)
end
end