From cea0dc4be037c3f73c7cdaeb4a085d567174e349 Mon Sep 17 00:00:00 2001 From: manson Date: Wed, 28 May 2014 15:37:56 +0800 Subject: [PATCH] Send Email --- .../admin/announcements_controller.rb | 36 +++++++++++++++++++ app/helpers/admin/announcements_helper.rb | 26 ++++++++++++-- app/models/bulletin.rb | 26 ++++++++++++-- app/views/admin/announcements/_form.html.erb | 18 +++++++--- app/views/announcements/email.html.erb | 19 ++++++++++ config/locales/en.yml | 1 + config/locales/zh_tw.yml | 1 + 7 files changed, 117 insertions(+), 10 deletions(-) create mode 100644 app/views/announcements/email.html.erb diff --git a/app/controllers/admin/announcements_controller.rb b/app/controllers/admin/announcements_controller.rb index 898deb8..4df744f 100644 --- a/app/controllers/admin/announcements_controller.rb +++ b/app/controllers/admin/announcements_controller.rb @@ -1,5 +1,6 @@ # encoding: utf-8 class Admin::AnnouncementsController < OrbitAdminController + include Admin::AnnouncementsHelper before_action ->(module_app = @app_title) { set_variables module_app } before_action :set_bulletin, only: [:edit, :destroy] @@ -33,6 +34,7 @@ class Admin::AnnouncementsController < OrbitAdminController bulletin.create_user_id = current_user.id bulletin.update_user_id = current_user.id bulletin.save + build_email(bulletin) redirect_to "/admin/announcements" end @@ -46,8 +48,10 @@ class Admin::AnnouncementsController < OrbitAdminController uid = params[:id].split('-').last bulletin = Bulletin.find_by(:uid=>uid) bulletin_params[:tags] = bulletin_params[:tags].blank? ? [] : bulletin_params[:tags] + bulletin_params[:email_member_ids] = bulletin_params[:email_member_ids].blank? ? [] : bulletin_params[:email_member_ids] bulletin.update_attributes(bulletin_params) bulletin.save + build_email(bulletin) redirect_to "/admin/announcements" end @@ -63,6 +67,37 @@ class Admin::AnnouncementsController < OrbitAdminController redirect_to "/admin/announcements" end + def build_email(bulletin) + if bulletin.email_sent and !bulletin.email_addresses.blank? + if bulletin.email.nil? + email = Email.new + email.save + bulletin.email_id = email.id + bulletin.save + end + + is_sent = bulletin.email.is_sent + is_sent = !params[:resend_mail].eql?("true") if !params[:resend_mail].blank? + + bulletin.email.update_attributes( + :create_user=>current_user, + :mail_sentdate=>bulletin.email_sentdate, + :module_app=>@module_app, + :mail_to=>bulletin.email_addresses, + :mail_subject=>bulletin.title, + :template=>'announcements/email', + :template_data=>{ + "host" => request.host_with_port, + "title" => bulletin.title, + "url" => page_for_bulletin(bulletin) + }, + :is_sent=>is_sent + ) + else + bulletin.email.destroy if !bulletin.email.nil? + end + end + private def set_bulletin @@ -70,6 +105,7 @@ class Admin::AnnouncementsController < OrbitAdminController end def bulletin_params + params[:bulletin][:email_sent] = params[:bulletin][:email_sent].nil? ? 0 : params[:bulletin][:email_sent] params.require(:bulletin).permit! end end diff --git a/app/helpers/admin/announcements_helper.rb b/app/helpers/admin/announcements_helper.rb index 1c6171e..9a46c72 100644 --- a/app/helpers/admin/announcements_helper.rb +++ b/app/helpers/admin/announcements_helper.rb @@ -1,7 +1,27 @@ module Admin::AnnouncementsHelper def page_for_bulletin(bulletin) - page = Page.where(:categories.in=>[bulletin.category.id.to_s]).first - page = Page.where(:module=>'announcement').first if page.nil? - request.protocol+(request.host_with_port+page.url+'/'+bulletin.to_param).gsub('//','/') rescue "/" + ann_page = nil + pages = Page.where(:module=>'announcement') + + pages.each do |page| + if page.categories.count ==1 + if page.categories.include?(bulletin.category.id.to_s) + ann_page = page + end + end + break if !ann_page.nil? + end + + if ann_page.nil? + pages.each do |page| + if page.categories.include?(bulletin.category.id.to_s) + ann_page = page + end + break if !ann_page.nil? + end + end + + ann_page = pages.first if ann_page.nil? + request.protocol+(request.host_with_port+ann_page.url+'/'+bulletin.to_param).gsub('//','/') rescue "/" end end diff --git a/app/models/bulletin.rb b/app/models/bulletin.rb index a077ed1..516acd2 100644 --- a/app/models/bulletin.rb +++ b/app/models/bulletin.rb @@ -17,9 +17,10 @@ class Bulletin field :postdate , :type => DateTime, :default => Time.now field :deadline , :type => DateTime + field :email_id field :email_sent, :type => Boolean, :default => false - field :email_sentdate , :type => DateTime - field :email_user_ids + field :email_sentdate , :type => DateTime, :default => Time.now+1.day + field :email_member_ids field :other_mailaddress mount_uploader :image, ImageUploader @@ -30,6 +31,8 @@ class Bulletin accepts_nested_attributes_for :bulletin_files, :allow_destroy => true accepts_nested_attributes_for :bulletin_links, :allow_destroy => true + before_destroy :destroy_email + def update_user User.find(update_user_id) rescue nil end @@ -42,4 +45,23 @@ class Bulletin self.any_of({:postdate.lt=>Time.now, :deadline.gt=>Time.now},{:postdate.lt=>Time.now, :deadline=>nil}) end + def email_members + MemberProfile.find(self.email_member_ids) rescue [] + end + + def email_addresses + addresses = self.email_members.collect{|member| member.email} rescue [] + addresses = addresses +[self.other_mailaddress] if !self.other_mailaddress.blank? + addresses.flatten + end + + def email + mail = Email.find(self.email_id) rescue nil + end + + def destroy_email + mail = Email.find(self.email_id) rescue nil + mail.destroy if !mail.nil? + end + end diff --git a/app/views/admin/announcements/_form.html.erb b/app/views/admin/announcements/_form.html.erb index 970eb5f..b5cabe5 100644 --- a/app/views/admin/announcements/_form.html.erb +++ b/app/views/admin/announcements/_form.html.erb @@ -10,7 +10,6 @@ <%= javascript_include_tag "lib/modal-preview" %> <%= javascript_include_tag "lib/file-type" %> <%= javascript_include_tag "lib/module-area" %> - <%= javascript_include_tag "member-selection" %> <% end %> @@ -133,9 +132,7 @@
-

- <%#= render partial: 'admin/member_selects/email_selection_box', locals: {field: 'bulletin[email_user_ids][]', users: @email_users} %> -

+ <%= render partial: 'admin/member_selects/email_selection_box', locals: {field: 'bulletin[email_member_ids][]', email_members: @bulletin.email_members} %>
@@ -153,11 +150,22 @@
- <%#= f.datetime_picker :email_sentdate, :no_label => true %> + <%= f.datetime_picker :email_sentdate, :no_label => true %>
+ <% if (@bulletin.email.is_sent rescue false) %> +
+
+ +
+ +
+
+
+ <% end %> + diff --git a/app/views/announcements/email.html.erb b/app/views/announcements/email.html.erb new file mode 100644 index 0000000..8483d1d --- /dev/null +++ b/app/views/announcements/email.html.erb @@ -0,0 +1,19 @@ + + + + + + +
+ + <%= t('announcement.mail_hi') %>

+ <%= t('announcement.mail_url_view') %>

+ " target="_blank"> <%= @data["title"] %>

+ + --
+ <%= t('announcement.mail_source') %> :" target="_blank"> <%= Site.first.title %>
+ <%= t('announcement.mail_time') %> <%= DateTime.now %> +
+ + + \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index 3cc6bc5..9b2c3bf 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -48,3 +48,4 @@ en: mail_source: Source mail_time: Time image_upload_size_note: The following recommendations %{image_upload_size} upload size + resend_mail: Re-send Email \ No newline at end of file diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index a8af0d1..43cd4d8 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -51,3 +51,4 @@ zh_tw: mail_source: 來源 mail_time: 時間 image_upload_size_note: 建議檔案小於%{image_upload_size} + resend_mail: 重新寄送提醒