From 25bd56e8ce204165739b2801ba74e2b2d79688a5 Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Tue, 27 Oct 2015 20:16:14 +0800 Subject: [PATCH] emails to managers and user if rejected --- app/controllers/admin/news_controller.rb | 27 +++++++++++++-- app/helpers/admin/news_helper.rb | 33 +++++++++++++++++++ app/views/email/new_news_email.html.erb | 3 ++ .../email/reapproval_news_email.html.erb | 3 ++ app/views/email/rejection_email.html.erb | 3 ++ 5 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 app/views/email/new_news_email.html.erb create mode 100644 app/views/email/reapproval_news_email.html.erb create mode 100644 app/views/email/rejection_email.html.erb diff --git a/app/controllers/admin/news_controller.rb b/app/controllers/admin/news_controller.rb index c5ca3fc..fe10202 100644 --- a/app/controllers/admin/news_controller.rb +++ b/app/controllers/admin/news_controller.rb @@ -62,8 +62,16 @@ class Admin::NewsController < OrbitAdminController news_bulletin.update_user_id = current_user.id if user_can_approve? news_bulletin.approved = true + news_bulletin.save + else + news_bulletin.save + Thread.new do + begin + send_notification_mail_to_managers(news_bulletin,"approval") + rescue e + end + end end - news_bulletin.save build_email(news_bulletin) redirect_to params['referer_url'] end @@ -75,12 +83,14 @@ class Admin::NewsController < OrbitAdminController news_bulletin.approved = true news_bulletin.rejected = false news_bulletin.reapproval = false + news_bulletin.save else news_bulletin.rejected = true news_bulletin.reapproval = false news_bulletin.rejection_reason = params["reason"] + news_bulletin.save + send_rejection_email(news_bulletin) end - news_bulletin.save redirect_to "/admin/news" end @@ -112,7 +122,18 @@ class Admin::NewsController < OrbitAdminController end news_bulletin.update_attributes(news_bulletin_params) - news_bulletin.reapproval = true if news_bulletin.rejected + if news_bulletin.rejected + news_bulletin.reapproval = true + news_bulletin.save + Thread.new do + begin + send_notification_mail_to_managers(news_bulletin, "reapproval") + rescue e + end + end + else + bulletin.save + end news_bulletin.save build_email(news_bulletin) redirect_to params['referer_url'] diff --git a/app/helpers/admin/news_helper.rb b/app/helpers/admin/news_helper.rb index df8da0f..6ad7e44 100644 --- a/app/helpers/admin/news_helper.rb +++ b/app/helpers/admin/news_helper.rb @@ -25,6 +25,39 @@ module Admin::NewsHelper request.protocol+(request.host_with_port+ann_page.url+'/'+news_bulletin.to_param).gsub('//','/') rescue "/" end + def send_rejection_email(news) + user = User.find(news.create_user_id) rescue nil + if !user.nil? + email = user.member_profile.email + if !email.nil? && email != "" + url = page_for_news_bulletin(news) + mail = Email.new(:mail_to => email, :mail_subject => "News rejected : #{news.title}.", :template => "email/rejection_email.html.erb", :template_data => {"url" => url, "rejector" => current_user.name, "name" => user.name, "reason" => news.rejection_reason}) + mail.deliver + end + end + end + + def send_notification_mail_to_managers(news, type) + authorizations = Authorization.where(:module_app_id => @module_app.id) + users = authorizations.collect do |auth| + auth.user + end + users.each do |user| + email = user.member_profile.email + if !email.nil? && email != "" + send_email(user.name, email, news, type) + sleep(2) + end + end + end + + def send_email(name, useremail, news, type) + url = page_for_news_bulletin(news) + template = (type == "approval" ? "email/new_news_email.html.erb" : "email/reapproval_news_email.html.erb") + email = Email.new(:mail_to => useremail, :mail_subject => "New news : #{news.title}.", :template => template, :template_data => {"url" => url, "submitter" => current_user.name, "name" => name}) + email.deliver + end + def load_access_level if current_user.is_admin? @access_level = "admin" diff --git a/app/views/email/new_news_email.html.erb b/app/views/email/new_news_email.html.erb new file mode 100644 index 0000000..2787440 --- /dev/null +++ b/app/views/email/new_news_email.html.erb @@ -0,0 +1,3 @@ +

Hello <%= @data["name"] %>,

+

<%= @data["submitter"] %> submitted a new news. + Please click here to view the news. \ No newline at end of file diff --git a/app/views/email/reapproval_news_email.html.erb b/app/views/email/reapproval_news_email.html.erb new file mode 100644 index 0000000..580d2a2 --- /dev/null +++ b/app/views/email/reapproval_news_email.html.erb @@ -0,0 +1,3 @@ +

Hello <%= @data["name"] %>,

+

<%= @data["submitter"] %> updated the news. + Please click here to view the news. \ No newline at end of file diff --git a/app/views/email/rejection_email.html.erb b/app/views/email/rejection_email.html.erb new file mode 100644 index 0000000..25bd918 --- /dev/null +++ b/app/views/email/rejection_email.html.erb @@ -0,0 +1,3 @@ +

Hello <%= @data["name"] %>,

+

<%= @data["rejector"] %> has rejected your news<%= @data["reason"].nil? || @data["reason"] == "" ? "." : ", because #{@data["reason"]}" %>

+ Please click here to view the news. \ No newline at end of file