From 026fef47af7bda1c07f839d40f7c71bcd9cf831e Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Wed, 20 Jul 2016 20:35:23 +0800 Subject: [PATCH] rejection now included with email systems. --- .../admin/announcements_controller.rb | 21 ++- app/helpers/admin/announcements_helper.rb | 34 +++++ app/models/bulletin.rb | 3 + .../announcements/_approval_modal.html.erb | 122 ++++++++++++++++++ app/views/admin/announcements/_index.html.erb | 35 ++--- app/views/admin/announcements/index.html.erb | 17 ++- .../email/new_announcement_email.html.erb | 3 + .../reapproval_announcement_email.html.erb | 3 + app/views/email/rejection_email.html.erb | 3 + config/routes.rb | 2 +- 10 files changed, 223 insertions(+), 20 deletions(-) create mode 100644 app/views/admin/announcements/_approval_modal.html.erb create mode 100644 app/views/email/new_announcement_email.html.erb create mode 100644 app/views/email/reapproval_announcement_email.html.erb create mode 100644 app/views/email/rejection_email.html.erb diff --git a/app/controllers/admin/announcements_controller.rb b/app/controllers/admin/announcements_controller.rb index b775af6..8222198 100644 --- a/app/controllers/admin/announcements_controller.rb +++ b/app/controllers/admin/announcements_controller.rb @@ -149,6 +149,8 @@ class Admin::AnnouncementsController < OrbitAdminController if AnnouncementSetting.is_pro? if user_can_approve? bulletin.approved = true + else + send_notification_mail_to_managers(bulletin,"approval") end else bulletin.approved = true @@ -162,7 +164,16 @@ class Admin::AnnouncementsController < OrbitAdminController def approve_bulletin id = params[:id] bulletin = Bulletin.find(id) - bulletin.approved = true + if params["approved"] == "true" + bulletin.approved = true + bulletin.rejected = false + bulletin.reapproval = false + else + bulletin.rejected = true + bulletin.reapproval = false + bulletin.rejection_reason = params["reason"] + send_rejection_email(bulletin) rescue "" + end bulletin.save redirect_to admin_announcements_path end @@ -197,7 +208,13 @@ class Admin::AnnouncementsController < OrbitAdminController end bulletin.update_attributes(bps) - bulletin.save + if bulletin.rejected + bulletin.reapproval = true + bulletin.save + send_notification_mail_to_managers(bulletin,"reapproval") + else + bulletin.save + end build_email(bulletin) redirect_to params['referer_url'] end diff --git a/app/helpers/admin/announcements_helper.rb b/app/helpers/admin/announcements_helper.rb index e0feaaa..128fb3a 100644 --- a/app/helpers/admin/announcements_helper.rb +++ b/app/helpers/admin/announcements_helper.rb @@ -119,6 +119,40 @@ module Admin::AnnouncementsHelper anns.save end + def send_rejection_email(announcement) + user = User.find(announcement.create_user_id) rescue nil + if !user.nil? + email = user.member_profile.email + if !email.nil? && email != "" + url = page_for_bulletin(announcement) + mail = Email.new(:mail_to => email, :mail_subject => "Announcement rejected : #{announcement.title}.", :template => "email/rejection_email.html.erb", :template_data => {"url" => url, "rejector" => current_user.name, "name" => user.name, "reason" => announcement.rejection_reason}) + mail.deliver rescue nil + end + end + end + + def send_notification_mail_to_managers(announcement, type) + authorizations = Authorization.where(:module_app_id => @module_app.id) + users = authorizations.collect do |auth| + auth.user + end + users.delete(nil) + users.each do |user| + email = user.member_profile.email + if !email.nil? && email != "" + send_email(user.name, email, announcement, type) + sleep(1) + end + end + end + + def send_email(name, useremail, announcement, type) + url = page_for_bulletin(announcement) + template = (type == "approval" ? "email/new_announcement_email.html.erb" : "email/reapproval_announcement_email.html.erb") + email = Email.new(:mail_to => useremail, :mail_subject => "New announcement : #{announcement.title}.", :template => template, :template_data => {"url" => url, "submitter" => current_user.name, "name" => name}) + email.deliver rescue nil + end + def download_tmp_xml(url) xml = File.join(Rails.root, "tmp", "ann_cc_ntu.xml") open(xml, 'wb') do |fo| diff --git a/app/models/bulletin.rb b/app/models/bulletin.rb index cbabf3f..4f21b4c 100644 --- a/app/models/bulletin.rb +++ b/app/models/bulletin.rb @@ -21,6 +21,9 @@ class Bulletin field :approved, :type => Boolean, :default => false field :is_preview, :type => Boolean, :default => false field :expirable_created_at, type: DateTime + field :rejected, :type => Boolean, :default => false + field :reapproval, :type => Boolean, :default => false + field :rejection_reason field :email_id field :email_sent, :type => Boolean, :default => false diff --git a/app/views/admin/announcements/_approval_modal.html.erb b/app/views/admin/announcements/_approval_modal.html.erb new file mode 100644 index 0000000..2e49a8b --- /dev/null +++ b/app/views/admin/announcements/_approval_modal.html.erb @@ -0,0 +1,122 @@ + + \ No newline at end of file diff --git a/app/views/admin/announcements/_index.html.erb b/app/views/admin/announcements/_index.html.erb index 635b12b..48e7d37 100644 --- a/app/views/admin/announcements/_index.html.erb +++ b/app/views/admin/announcements/_index.html.erb @@ -23,21 +23,24 @@ <% end %> - <% if b.expired? %> - <%= b.title %> <%= t(:expired) %> - <% elsif !b.approved? %> - <% if (b.category.disable rescue false)%> - <%= b.title %> - <% else %> - <%= b.title %> - <% end %> - <%= t(:pending) %> + <% if b.expired? || (b.category.disable rescue false)%> + <%= b.title %> <% else %> - <% if (b.category.disable rescue false) %> - <%= b.title %> - <% else %> - <%= b.title %> - <% end %> + <%= b.title %> + <% end %> + + <% if b.expired? %> + <%= t(:expired) %> + <% end %> + + <% if b.reapproval %> + <%= t("announcement.reapproval") + " " + t(:pending) %> + <% end %> + <% if b.rejected %> + <%= t(:rejected) %> : <%= b.rejection_reason rescue "" %> + <% end %> + <% if !b.approved? && !b.rejected %> + <%= t(:pending) %> <% end %>
diff --git a/app/views/admin/announcements/index.html.erb b/app/views/admin/announcements/index.html.erb index 1ad9a26..6ae0abc 100644 --- a/app/views/admin/announcements/index.html.erb +++ b/app/views/admin/announcements/index.html.erb @@ -3,4 +3,19 @@ <%= render 'index'%> -<%= render 'layouts/delete_modal', delete_options: @delete_options %> \ No newline at end of file +<%= render 'layouts/delete_modal', delete_options: @delete_options %> + +<% if AnnouncementSetting.is_pro? && (@access_level == "admin" || @access_level == "manager") %> + <%= render :partial=> "approval_modal" %> + + +<% end %> \ No newline at end of file diff --git a/app/views/email/new_announcement_email.html.erb b/app/views/email/new_announcement_email.html.erb new file mode 100644 index 0000000..e8e32c8 --- /dev/null +++ b/app/views/email/new_announcement_email.html.erb @@ -0,0 +1,3 @@ +

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

+

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

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

+

<%= @data["submitter"] %> updated the announcement. + Please click here to view the annoucement. \ 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..8bc9867 --- /dev/null +++ b/app/views/email/rejection_email.html.erb @@ -0,0 +1,3 @@ +

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

+

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

+ Please click here to view the annoucement. \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 19ccf4b..dcf340f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -12,7 +12,7 @@ Rails.application.routes.draw do patch 'announcement/updatefeed', to: 'announcements#updatefeed' delete 'announcement/deletefeed', to: 'announcements#deletefeed' get 'announcement/destroy_preview/:slug_title-:uid', to: 'announcements#destroy_preview' - get 'announcement/approve_bulletin', to: 'announcements#approve_bulletin' + post 'announcement/approve_bulletin', to: 'announcements#approve_bulletin' get 'announcement/feed', to: 'announcements#feed' get 'announcements/feedform', to: 'announcements#feedform' get 'announcement/settings', to: 'announcements#settings'