Send Email

This commit is contained in:
manson 2014-05-28 15:37:56 +08:00
parent 0b43ae8303
commit cea0dc4be0
7 changed files with 117 additions and 10 deletions

View File

@ -1,5 +1,6 @@
# encoding: utf-8 # encoding: utf-8
class Admin::AnnouncementsController < OrbitAdminController class Admin::AnnouncementsController < OrbitAdminController
include Admin::AnnouncementsHelper
before_action ->(module_app = @app_title) { set_variables module_app } before_action ->(module_app = @app_title) { set_variables module_app }
before_action :set_bulletin, only: [:edit, :destroy] before_action :set_bulletin, only: [:edit, :destroy]
@ -33,6 +34,7 @@ class Admin::AnnouncementsController < OrbitAdminController
bulletin.create_user_id = current_user.id bulletin.create_user_id = current_user.id
bulletin.update_user_id = current_user.id bulletin.update_user_id = current_user.id
bulletin.save bulletin.save
build_email(bulletin)
redirect_to "/admin/announcements" redirect_to "/admin/announcements"
end end
@ -46,8 +48,10 @@ class Admin::AnnouncementsController < OrbitAdminController
uid = params[:id].split('-').last uid = params[:id].split('-').last
bulletin = Bulletin.find_by(:uid=>uid) bulletin = Bulletin.find_by(:uid=>uid)
bulletin_params[:tags] = bulletin_params[:tags].blank? ? [] : bulletin_params[:tags] 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.update_attributes(bulletin_params)
bulletin.save bulletin.save
build_email(bulletin)
redirect_to "/admin/announcements" redirect_to "/admin/announcements"
end end
@ -63,6 +67,37 @@ class Admin::AnnouncementsController < OrbitAdminController
redirect_to "/admin/announcements" redirect_to "/admin/announcements"
end 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 private
def set_bulletin def set_bulletin
@ -70,6 +105,7 @@ class Admin::AnnouncementsController < OrbitAdminController
end end
def bulletin_params def bulletin_params
params[:bulletin][:email_sent] = params[:bulletin][:email_sent].nil? ? 0 : params[:bulletin][:email_sent]
params.require(:bulletin).permit! params.require(:bulletin).permit!
end end
end end

View File

@ -1,7 +1,27 @@
module Admin::AnnouncementsHelper module Admin::AnnouncementsHelper
def page_for_bulletin(bulletin) def page_for_bulletin(bulletin)
page = Page.where(:categories.in=>[bulletin.category.id.to_s]).first ann_page = nil
page = Page.where(:module=>'announcement').first if page.nil? pages = Page.where(:module=>'announcement')
request.protocol+(request.host_with_port+page.url+'/'+bulletin.to_param).gsub('//','/') rescue "/"
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
end end

View File

@ -17,9 +17,10 @@ class Bulletin
field :postdate , :type => DateTime, :default => Time.now field :postdate , :type => DateTime, :default => Time.now
field :deadline , :type => DateTime field :deadline , :type => DateTime
field :email_id
field :email_sent, :type => Boolean, :default => false field :email_sent, :type => Boolean, :default => false
field :email_sentdate , :type => DateTime field :email_sentdate , :type => DateTime, :default => Time.now+1.day
field :email_user_ids field :email_member_ids
field :other_mailaddress field :other_mailaddress
mount_uploader :image, ImageUploader mount_uploader :image, ImageUploader
@ -30,6 +31,8 @@ class Bulletin
accepts_nested_attributes_for :bulletin_files, :allow_destroy => true accepts_nested_attributes_for :bulletin_files, :allow_destroy => true
accepts_nested_attributes_for :bulletin_links, :allow_destroy => true accepts_nested_attributes_for :bulletin_links, :allow_destroy => true
before_destroy :destroy_email
def update_user def update_user
User.find(update_user_id) rescue nil User.find(update_user_id) rescue nil
end end
@ -42,4 +45,23 @@ class Bulletin
self.any_of({:postdate.lt=>Time.now, :deadline.gt=>Time.now},{:postdate.lt=>Time.now, :deadline=>nil}) self.any_of({:postdate.lt=>Time.now, :deadline.gt=>Time.now},{:postdate.lt=>Time.now, :deadline=>nil})
end 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 end

View File

@ -10,7 +10,6 @@
<%= javascript_include_tag "lib/modal-preview" %> <%= javascript_include_tag "lib/modal-preview" %>
<%= javascript_include_tag "lib/file-type" %> <%= javascript_include_tag "lib/file-type" %>
<%= javascript_include_tag "lib/module-area" %> <%= javascript_include_tag "lib/module-area" %>
<%= javascript_include_tag "member-selection" %>
<% end %> <% end %>
<!-- Input Area --> <!-- Input Area -->
@ -133,9 +132,7 @@
</label> </label>
<div class="content-box"> <div class="content-box">
<p> <%= render partial: 'admin/member_selects/email_selection_box', locals: {field: 'bulletin[email_member_ids][]', email_members: @bulletin.email_members} %>
<%#= render partial: 'admin/member_selects/email_selection_box', locals: {field: 'bulletin[email_user_ids][]', users: @email_users} %>
</p>
</div> </div>
</div> </div>
</div> </div>
@ -153,11 +150,22 @@
<div class="control-group"> <div class="control-group">
<label class="control-label muted"><%= t("announcement.email_sentdate") %></label> <label class="control-label muted"><%= t("announcement.email_sentdate") %></label>
<div class="controls"> <div class="controls">
<%#= f.datetime_picker :email_sentdate, :no_label => true %> <%= f.datetime_picker :email_sentdate, :no_label => true %>
</div> </div>
</div> </div>
</div> </div>
<% if (@bulletin.email.is_sent rescue false) %>
<div class="content-box">
<div class="control-group">
<label class="control-label muted"><%= t("announcement.resend_mail") %></label>
<div class="controls">
<input type="checkbox" name="resend_mail" value="true">
</div>
</div>
</div>
<% end %>
</div> </div>
</div> </div>

View File

@ -0,0 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
</head>
<body bgcolor="#FFFFFF">
<div style="text-ident:20px"></div>
<%= t('announcement.mail_hi') %> <br /><br />
<%= t('announcement.mail_url_view') %> <br /><br />
<a href="<%= @data["url"] %>" target="_blank"> <%= @data["title"] %> </a> <br /><br />
<span style="color:#555">--<br />
<%= t('announcement.mail_source') %> <a href="http://<%= @data["host"] %>" target="_blank"> <%= Site.first.title %> </a><br />
<%= t('announcement.mail_time') %> <%= DateTime.now %>
</span>
</body>
</html>

View File

@ -48,3 +48,4 @@ en:
mail_source: Source mail_source: Source
mail_time: Time mail_time: Time
image_upload_size_note: The following recommendations %{image_upload_size} upload size image_upload_size_note: The following recommendations %{image_upload_size} upload size
resend_mail: Re-send Email

View File

@ -51,3 +51,4 @@ zh_tw:
mail_source: 來源 mail_source: 來源
mail_time: 時間 mail_time: 時間
image_upload_size_note: 建議檔案小於%{image_upload_size} image_upload_size_note: 建議檔案小於%{image_upload_size}
resend_mail: 重新寄送提醒