diff --git a/app/controllers/admin/e_paper_criterias_controller.rb b/app/controllers/admin/e_paper_criterias_controller.rb index dbd710b..55480e1 100644 --- a/app/controllers/admin/e_paper_criterias_controller.rb +++ b/app/controllers/admin/e_paper_criterias_controller.rb @@ -49,7 +49,7 @@ def initialize paper_criteria = PaperCriteria.find(params[:id]) page = Page.where(:module => "e_paper").first.url rescue "" papers_data = Admin::EpaperHelper.get_paper_data(paper_criteria) - 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, "siteurl" => "http://" + request.host_with_port ,"papers_data" => papers_data }, :mail_to => params[:email]) + email = Email.new(:mail_subject => paper_criteria.sending_title, :mail_lang => I18n.locale, :template => "admin/e_paper_criterias/emailer", :template_data => {"paper" => paper_criteria, "page" => "http://" + request.host_with_port + page, "siteurl" => "http://" + request.host_with_port ,"papers_data" => papers_data }, :mail_to => params[:email], :module_app_key=>'e_paper') email.deliver render :json => {"success" => true}.to_json end diff --git a/app/models/e_paper_subscriber.rb b/app/models/e_paper_subscriber.rb index 3d806d6..7b22ab9 100644 --- a/app/models/e_paper_subscriber.rb +++ b/app/models/e_paper_subscriber.rb @@ -3,9 +3,18 @@ class EPaperSubscriber include Mongoid::Document include Mongoid::Timestamps - field :email + field :email, type: String field :subscribed, type: Boolean, :default => true field :language field :last_paper_sent - + before_save do + self.fix_email + if self.class.where(:email=>self.email, :language=>self.language,:subscribed=>true).count != 0 + return false + end + end + def fix_email(save_flag=false) + self.email = self.email.to_s.gsub(/[  ]/,'').sub(/;$/, '') + self.save if save_flag + end end \ No newline at end of file diff --git a/app/views/admin/e_paper_criterias/_index.html.erb b/app/views/admin/e_paper_criterias/_index.html.erb index 8de8b73..1680f2f 100644 --- a/app/views/admin/e_paper_criterias/_index.html.erb +++ b/app/views/admin/e_paper_criterias/_index.html.erb @@ -56,7 +56,10 @@ dataType : "json", type : "get" }).done(function(){ - alert("Email sent.") + alert("Email sent."); + }).fail(function(jqXHR, textStatus, errorThrown){ + alert("Something went wrong!\nPlease contact customer service!"); + console.log(errorThrown); }) } return false; @@ -69,23 +72,27 @@ dataType : "json", type : "get", success: function(data){ - if (data.success){ - alert("Newsletter sent.") - }else{ - var window_width = window.innerWidth - var window_height = window.innerHeight - var show_html = (typeof(data.invalid_emails)=='string' ? data.invalid_emails : convert_email_to_html(data.invalid_emails)) - $("div#show_epaper_message").html(show_html) - $("div#show_epaper_message").dialog({ - autoOpen: true, - show: "blind", - model: true, - hide: "explode", - width: window_width*0.7, - height: window_height*0.6 - }) - } - } + if (data.success){ + alert("Newsletter sent.") + }else{ + var window_width = window.innerWidth + var window_height = window.innerHeight + var show_html = (typeof(data.invalid_emails)=='string' ? data.invalid_emails : convert_email_to_html(data.invalid_emails)) + $("div#show_epaper_message").html(show_html) + $("div#show_epaper_message").dialog({ + autoOpen: true, + show: "blind", + model: true, + hide: "explode", + width: window_width*0.7, + height: window_height*0.6 + }) + } + }, + error: function(error){ + alert("Something went wrong!\nPlease contact customer service!"); + console.log(error); + } }) } return false; diff --git a/config/routes.rb b/config/routes.rb index 6170c7a..c0fbd0a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,22 @@ Rails.application.routes.draw do - + if ENV['worker_num']=='0' && File.basename($0) != 'rake' && !Rails.const_defined?('Console') + Thread.new do + s = Site.first + update_flag = s.respond_to?(:tmp_flags) + need_update = !update_flag || !(s.tmp_flags.include?('ep1')) + if need_update + EPaperSubscriber.all.to_a.each do |subscriber| + subscriber.fix_email + EPaperSubscriber.where(:id=>subscriber.id).update_all(:email=>subscriber.email) + end + if update_flag + s = OrbitHelper::SharedHash['current_site']['site'] rescue Site.first + s.tmp_flags << 'ep1' + s.save + end + end + end + end locales = Site.first.in_use_locales rescue I18n.available_locales scope "(:locale)", locale: Regexp.new(locales.join("|")) do diff --git a/lib/tasks/e_paper_tasks.rake b/lib/tasks/e_paper_tasks.rake index be80b6a..963535f 100644 --- a/lib/tasks/e_paper_tasks.rake +++ b/lib/tasks/e_paper_tasks.rake @@ -26,6 +26,8 @@ namespace :epaper do end emails_invalid = emails - emails_valid paper.send_failed_emails = [] + paper.invalid_emails = emails_invalid + paper.save Thread.new do tmp = [] site = Site.first @@ -39,7 +41,7 @@ namespace :epaper do emails_valid.each_slice(mail_limit).each do |slice_emails| invalid_mails = epaper_verify_email(from_addr,slice_emails,tls) deliver_emails = slice_emails - invalid_mails - email = Email.new(:mail_subject => paper.sending_title, :mail_lang => args[:locale], :template => "admin/e_paper_criterias/emailer", :template_data => {"paper_id" => paper.id, "page" => "http://" + domain + page, "siteurl" => "http://" + domain, "papers_data" => papers_data }, :mail_to => deliver_emails) + email = Email.new(:mail_subject => paper.sending_title, :mail_lang => args[:locale], :template => "admin/e_paper_criterias/emailer", :template_data => {"paper_id" => paper.id, "page" => "http://" + domain + page, "siteurl" => "http://" + domain, "papers_data" => papers_data }, :mail_to => deliver_emails, :module_app_key=>'e_paper') begin email.save email.deliver @@ -51,7 +53,7 @@ namespace :epaper do error_flag = 0 begin invalid_mails.each do |invalid_mail| - email = Email.create(:mail_subject => paper.sending_title, :mail_lang => args[:locale], :template => "admin/e_paper_criterias/emailer", :template_data => {"paper_id" => paper.id, "page" => "http://" + domain + page, "siteurl" => "http://" + domain, "papers_data" => papers_data }, :mail_to => invalid_mail) + email = Email.create(:mail_subject => paper.sending_title, :mail_lang => args[:locale], :template => "admin/e_paper_criterias/emailer", :template_data => {"paper_id" => paper.id, "page" => "http://" + domain + page, "siteurl" => "http://" + domain, "papers_data" => papers_data }, :mail_to => invalid_mail, :module_app_key=>'e_paper') end rescue => e puts [e,e.backtrace] @@ -62,8 +64,6 @@ namespace :epaper do end paper.update_attributes(:send_failed_emails => tmp) end - paper.invalid_emails = emails_invalid - paper.save end end def epaper_verify_email(from_addr,check_emails,tls) #true if check_email is ok