diff --git a/app/controllers/admin/e_paper_criterias_controller.rb b/app/controllers/admin/e_paper_criterias_controller.rb
index db3ddaa..bd2a150 100644
--- a/app/controllers/admin/e_paper_criterias_controller.rb
+++ b/app/controllers/admin/e_paper_criterias_controller.rb
@@ -1,3 +1,4 @@
+require 'rake'
class Admin::EPaperCriteriasController < OrbitAdminController
def initialize
@@ -46,10 +47,17 @@ def initialize
def send_test_email
paper_criteria = PaperCriteria.find(params[:id])
- page = Page.where(:module_app => "e_paper").first.url rescue ""
- email = Email.new(:mail_subject => paper_criteria.title, :mail_lang => :zh_tw, :template => "admin/e_paper_criterias/emailer", :template_data => {"paper" => paper_criteria, "page" => "http://" + request.host_with_port + page}, :mail_to => params[:email])
+ page = Page.where(:module => "e_paper").first.url rescue ""
+ email = Email.new(:mail_subject => paper_criteria.sending_title, :mail_lang => :zh_tw, :template => "admin/e_paper_criterias/emailer", :template_data => {"paper" => paper_criteria, "page" => "http://" + request.host_with_port + page}, :mail_to => params[:email])
email.deliver
-
+ render :json => {"success" => true}.to_json
+ end
+
+ def send_newsletter
+ Orbit::Application.load_tasks
+ rake = Rake::Task['epaper:send_email'].execute(OpenStruct.new({:paper_criteria_id => params[:id], :domain => request.host_with_port}))
+ # rake = Rake::Task['epaper:send_email'].invoke([params[:id], request.host_with_port])
+ render :json => {"success" => true}.to_json
end
private
diff --git a/app/models/paper_criteria.rb b/app/models/paper_criteria.rb
index ad983ee..0f030de 100644
--- a/app/models/paper_criteria.rb
+++ b/app/models/paper_criteria.rb
@@ -10,13 +10,21 @@ class PaperCriteria
field :sending_time, type: DateTime
field :sending_title, localize: true
field :sending_notes
- field :language_to_send, type: Array, :default => []
- field :receivers, type: Array, :default => []
+ field :language_to_send, type: Array, :default => ["en","zh_tw"]
+ field :receivers, type: Array, :default => ["subscriber"]
field :other_emails
def epaper_topics
EPaperTopic.where(:period.gte => self.start_date, :period.lte => self.end_date).can_display
end
+
+ def other_emails_ids
+ if !self.other_emails.nil?
+ return self.other_emails.split(",")
+ else
+ return []
+ end
+ end
end
\ No newline at end of file
diff --git a/app/views/admin/e_paper_criterias/_form.html.erb b/app/views/admin/e_paper_criterias/_form.html.erb
index ddc4aa4..10ed171 100644
--- a/app/views/admin/e_paper_criterias/_form.html.erb
+++ b/app/views/admin/e_paper_criterias/_form.html.erb
@@ -27,13 +27,13 @@
-
+
@@ -77,7 +77,7 @@
<% end %>
-
+
diff --git a/app/views/admin/e_paper_criterias/_index.html.erb b/app/views/admin/e_paper_criterias/_index.html.erb
index fb984cf..514e631 100644
--- a/app/views/admin/e_paper_criterias/_index.html.erb
+++ b/app/views/admin/e_paper_criterias/_index.html.erb
@@ -15,6 +15,7 @@
@@ -48,4 +49,17 @@
}
return false;
})
+ $(".send-newsletter").on("click",function(){
+ var el = $(this);
+ if(confirm("Are you sure?")){
+ $.ajax({
+ url : el.attr("href"),
+ dataType : "json",
+ type : "get"
+ }).done(function(){
+ alert("Newsletter sent.")
+ })
+ }
+ return false;
+ })
\ No newline at end of file
diff --git a/app/views/admin/e_paper_criterias/emailer.html.erb b/app/views/admin/e_paper_criterias/emailer.html.erb
index b7d38b3..6c45408 100644
--- a/app/views/admin/e_paper_criterias/emailer.html.erb
+++ b/app/views/admin/e_paper_criterias/emailer.html.erb
@@ -150,19 +150,18 @@
<%= t('e_paper.intro') %>
-
<%= t('e_paper.title') %>:<%= paper.title %>
+
<%= t('e_paper.date') %>:<%= paper.created_at.strftime("%Y-%m-%d") %>
-
<%= t('e_paper.period') %>:<%= paper.period.strftime("%Y-%m-%d") %>
<%= t('e_paper.description') %><%= paper.description %>
<% paper.epaper_topics.desc(:created_at).each do |topic| %>
<% end %>
diff --git a/config/locales/en.yml b/config/locales/en.yml
index e38ecd9..d7fc454 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -34,3 +34,4 @@ en:
website_member: Website Members
other_email: Other Email
receivers: Receivers
+ send_newsletter: Send Newsletter
diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml
index f2fb419..39959e0 100644
--- a/config/locales/zh_tw.yml
+++ b/config/locales/zh_tw.yml
@@ -34,4 +34,5 @@ zh_tw:
subscriber: 訂閱者
website_member: 網站會員
other_email: 其他Email
- receivers: 收件人
\ No newline at end of file
+ receivers: 收件人
+ send_newsletter: 發送時事通訊
\ No newline at end of file
diff --git a/config/routes.rb b/config/routes.rb
index ebd0325..ef59198 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -7,6 +7,7 @@ Rails.application.routes.draw do
resources :e_paper_criterias do
member do
get "send_test_email"
+ get "send_newsletter"
end
end
resources :e_paper_subscribers
diff --git a/lib/tasks/e_paper_tasks.rake b/lib/tasks/e_paper_tasks.rake
index 04f5e79..d36fcf2 100644
--- a/lib/tasks/e_paper_tasks.rake
+++ b/lib/tasks/e_paper_tasks.rake
@@ -1,4 +1,44 @@
# desc "Explaining what the task does"
-# task :e_paper do
-# # Task goes here
-# end
+namespace :epaper do
+ task :send_email, [:paper_criteria_id, :domain] => :environment do |task,args|
+ paper = PaperCriteria.find(args[:paper_criteria_id]) rescue nil
+ page = Page.where(:module => "e_paper").first.url rescue ""
+ domain = args[:domain]
+ if !paper.nil?
+ subscribers = EPaperSubscriber.all
+ emails = []
+ mail_limit = 25
+ subscribers.each do |subscriber|
+ emails << subscriber.email if paper.language_to_send.include? subscriber.language
+ subscriber.last_paper_sent = paper.id.to_s
+ subscriber.save
+ if emails.count == mail_limit
+ puts "Sending #{mail_limit} mails"
+ email = Email.new(:mail_subject => paper.sending_title, :mail_lang => :zh_tw, :template => "admin/e_paper_criterias/emailer", :template_data => {"paper" => paper, "page" => "http://" + domain + page}, :mail_to => emails)
+ email.deliver
+ emails = []
+ sleep 0.5
+ end
+ end
+ other_emails = paper.other_emails_ids
+ if !other_emails.blank?
+ other_emails.each do |email|
+ emails << email
+ if emails.count == mail_limit
+ puts "Sending #{mail_limit} mails"
+ email = Email.new(:mail_subject => paper.sending_title, :mail_lang => :zh_tw, :template => "admin/e_paper_criterias/emailer", :template_data => {"paper" => paper, "page" => "http://" + domain + page}, :mail_to => emails)
+ email.deliver
+ emails = []
+ sleep 0.5
+ end
+ end
+ end
+ if !emails.empty?
+ puts "Sending last emails"
+ email = Email.new(:mail_subject => paper.sending_title, :mail_lang => :zh_tw, :template => "admin/e_paper_criterias/emailer", :template_data => {"paper" => paper, "page" => "http://" + domain + page}, :mail_to => emails)
+ email.deliver
+ end
+ paper.save
+ end
+ end
+end