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
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

View File

@ -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

View File

@ -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

View File

@ -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 %>
<!-- Input Area -->
@ -133,9 +132,7 @@
</label>
<div class="content-box">
<p>
<%#= render partial: 'admin/member_selects/email_selection_box', locals: {field: 'bulletin[email_user_ids][]', users: @email_users} %>
</p>
<%= render partial: 'admin/member_selects/email_selection_box', locals: {field: 'bulletin[email_member_ids][]', email_members: @bulletin.email_members} %>
</div>
</div>
</div>
@ -153,11 +150,22 @@
<div class="control-group">
<label class="control-label muted"><%= t("announcement.email_sentdate") %></label>
<div class="controls">
<%#= f.datetime_picker :email_sentdate, :no_label => true %>
<%= f.datetime_picker :email_sentdate, :no_label => true %>
</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>

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_time: Time
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_time: 時間
image_upload_size_note: 建議檔案小於%{image_upload_size}
resend_mail: 重新寄送提醒