diff --git a/app/controllers/panel/announcement/back_end/approvals_controller.rb b/app/controllers/panel/announcement/back_end/approvals_controller.rb
index 81ce81e..8264c25 100644
--- a/app/controllers/panel/announcement/back_end/approvals_controller.rb
+++ b/app/controllers/panel/announcement/back_end/approvals_controller.rb
@@ -1,3 +1,4 @@
+# encoding: utf-8
class Panel::Announcement::BackEnd::ApprovalsController < OrbitBackendController
before_filter :authenticate_user!
before_filter :is_admin?
@@ -6,6 +7,7 @@ class Panel::Announcement::BackEnd::ApprovalsController < OrbitBackendControlle
def preview_and_approve
+ email_group_data
@bulletin = Bulletin.find params[:bulletin_id]
end
@@ -15,6 +17,14 @@ class Panel::Announcement::BackEnd::ApprovalsController < OrbitBackendControlle
@bulletin.proc_check(params[:bulletin][:is_checked],params[:bulletin][:not_checked_reason])
@bulletin.de_pending
if @bulletin.save
+
+ if @bulletin.email_sent == true && @bulletin.is_checked == true && !@bulletin.is_rejected
+ send_email_data(@bulletin)
+
+ @bulletin.email_sent = false
+ @bulletin.save
+ end
+
notice = t('announcement.approve_bulletin_success')
else
notice = t('announcement.approve_bulletin_fail')
@@ -61,5 +71,68 @@ class Panel::Announcement::BackEnd::ApprovalsController < OrbitBackendControlle
object_auth.privilege_users = privilege_users
object_auth
end
+
+ def send_email_data(bulletin)
+
+ @site = Site.first
+ @user = User.find(bulletin.create_user_id)
+ @host = request.host_with_port
+
+ email_group_data
+
+ @group_mail = Array.new
+
+ bulletin.email_group.each do |egroup|
+
+ if @email_group_data.include?(egroup) and (egroup == 'e_0' or egroup == 'e_1' or egroup == 'e_2' or egroup == 'e_3')
+
+ @group_mail << @email_group_data[egroup]["email"]
+
+ elsif @email_group_data.include?(egroup) and egroup == 'e_4'
+
+ @group_mail << bulletin.other_mailaddress
+
+ end
+
+ end
+
+ if !@group_mail.join.blank?
+
+ @mail_content = {
+ "host" => @host,
+ "site_title" => @site.title,
+ "title" => bulletin.title,
+ "template" => 'announcement_mailer/cron_mail',
+ "url" => "http://#{@host}#{panel_announcement_front_end_bulletin_path(bulletin, :category_id => bulletin.category.id)}"
+ }
+
+ @mail_cron = {
+ :mail_from_app => 'announcement',
+ :mail_from => @user.email,
+ :mail_reply_to => @user.email,
+ :mail_subject => "#{t("announcement.mail_subject",:site_title => @site.title)}:#{bulletin.title}",
+ :mail_to => @group_mail.join(','),
+ :mail_content => @mail_content ,
+ # :mail_sentdate => bulletin.email_sentdate,
+ :mail_sentdate => DateTime.now,
+ :create_user_id => bulletin.create_user_id,
+ :update_user_id => bulletin.create_user_id
+ }
+
+ @mail_cron = MailCron.new(@mail_cron)
+
+ @mail_cron.save
+
+ MailCron.send_mail_now(@mail_cron.id)
+
+ end
+
+ end
+
+ def email_group_data
+
+ @email_group_data = Bulletin.email_group_data
+
+ end
end
diff --git a/app/controllers/panel/announcement/back_end/bulletins_controller.rb b/app/controllers/panel/announcement/back_end/bulletins_controller.rb
index de369ee..76ac4d5 100644
--- a/app/controllers/panel/announcement/back_end/bulletins_controller.rb
+++ b/app/controllers/panel/announcement/back_end/bulletins_controller.rb
@@ -1,3 +1,5 @@
+# encoding: utf-8
+
class Panel::Announcement::BackEnd::BulletinsController < OrbitBackendController
# include OrbitControllerLib::DivisionForDisable
@@ -19,6 +21,8 @@ class Panel::Announcement::BackEnd::BulletinsController < OrbitBackendController
def index
+ email_group_data
+
@tags = get_tags
@categories = get_categories_for_index
@statuses = get_statuses
@@ -47,6 +51,9 @@ class Panel::Announcement::BackEnd::BulletinsController < OrbitBackendController
# GET /bulletins/new
# GET /bulletins/new.xml
def new
+
+ email_group_data
+
if(session[:in_validate_object].blank?)
@bulletin = Bulletin.new(:postdate => DateTime.now)
else
@@ -64,6 +71,9 @@ class Panel::Announcement::BackEnd::BulletinsController < OrbitBackendController
# GET /bulletins/1/edit
def edit
+
+ email_group_data
+
@bulletin = Bulletin.find(params[:id])
@tags = get_tags
is_authorized_sub_manager = @bulletin.category.auth_sub_manager.authorized_user_ids rescue nil
@@ -82,21 +92,31 @@ class Panel::Announcement::BackEnd::BulletinsController < OrbitBackendController
# POST /bulletins
# POST /bulletins.xml
def create
+
+ email_group_data
+
@tags = get_tags
@bulletin = Bulletin.new(params[:bulletin])
@bulletin.deadline = nil if (@bulletin.deadline < @bulletin.postdate rescue nil)
@bulletin.create_user_id = current_user.id
@bulletin.update_user_id = current_user.id
- # if(is_manager? || is_admin?)
- # @bulletin.is_checked = true
- # @bulletin.is_rejected = false
- # @bulletin.de_pending
- # end
+ if(is_manager? || is_admin?)
+ @bulletin.is_checked = true
+ @bulletin.is_rejected = false
+ @bulletin.de_pending
+ end
respond_to do |format|
if @bulletin.save
+ if @bulletin.email_sent == true && @bulletin.is_checked == true && !@bulletin.is_rejected
+ send_email_data(@bulletin)
+
+ @bulletin.email_sent = false
+ @bulletin.save
+ end
+
format.html { redirect_to(panel_announcement_back_end_bulletins_url, :notice => t('announcement.create_bulletin_success')) }
format.xml { render :xml => @bulletin, :status => :created, :location => @bulletin }
# format.js
@@ -121,12 +141,18 @@ class Panel::Announcement::BackEnd::BulletinsController < OrbitBackendController
# PUT /bulletins/1.xml
def update
- @bulletin = Bulletin.find(params[:id])
- delete_out_invalid_date_from_params
- respond_to do |format|
- if @bulletin.update_attributes(params[:bulletin])
+ email_group_data
+
+ @bulletin = Bulletin.find(params[:id])
+ delete_out_invalid_date_from_params
+ respond_to do |format|
+ if @bulletin.update_attributes(params[:bulletin])
+ if(is_manager? || is_admin?)
+ @bulletin.is_checked = true
+ @bulletin.is_rejected = false
+ @bulletin.de_pending!
format.html { redirect_to(panel_announcement_back_end_bulletins_url, :notice => t('bulletin.update_bulletin_success')) }
format.js { render 'toggle_enable' }
format.xml { head :ok }
@@ -135,10 +161,27 @@ class Panel::Announcement::BackEnd::BulletinsController < OrbitBackendController
format.html { render :action => "edit" }
format.xml { render :xml => @bulletin.errors, :status => :unprocessable_entity }
end
+
+ if @bulletin.email_sent == true && @bulletin.is_checked == true && !@bulletin.is_rejected
+ send_email_data(@bulletin)
+
+ @bulletin.email_sent = false
+ @bulletin.save
+ end
+
+ format.html { redirect_to(panel_announcement_back_end_bulletins_url, :notice => t('bulletin.update_bulletin_success')) }
+ format.js { render 'toggle_enable' }
+ format.xml { head :ok }
+ else
+ @tags = get_tags
+ format.html { render :action => "edit" }
+ format.xml { render :xml => @bulletin.errors, :status => :unprocessable_entity }
end
end
+ end
+
# DELETE /bulletins/1
# DELETE /bulletins/1.xml
def destroy
@@ -164,6 +207,70 @@ class Panel::Announcement::BackEnd::BulletinsController < OrbitBackendController
protected
+ def send_email_data(bulletin)
+
+ @site = Site.first
+ @user = User.find(bulletin.create_user_id)
+ @host = request.host_with_port
+
+ email_group_data
+
+ @group_mail = Array.new
+
+ bulletin.email_group.each do |egroup|
+
+ if @email_group_data.include?(egroup) and (egroup == 'e_0' or egroup == 'e_1' or egroup == 'e_2' or egroup == 'e_3')
+
+ @group_mail << @email_group_data[egroup]["email"]
+
+ elsif @email_group_data.include?(egroup) and egroup == 'e_4'
+
+ @group_mail << bulletin.other_mailaddress
+
+ end
+
+ end
+
+ if !@group_mail.join.blank?
+
+ @mail_content = {
+ "host" => @host,
+ "site_title" => @site.title,
+ "title" => bulletin.title,
+ "template" => 'announcement_mailer/cron_mail',
+ "url" => "http://#{@host}#{panel_announcement_front_end_bulletin_path(bulletin, :category_id => bulletin.category.id)}"
+ }
+
+ @mail_cron = {
+ :mail_from_app => 'announcement',
+ :mail_from => @user.email,
+ :mail_reply_to => @user.email,
+ :mail_subject => "#{t("announcement.mail_subject")}:",
+ :mail_subject => "#{t("announcement.mail_subject",:site_title => @site.title)}:#{bulletin.title}",
+ :mail_to => @group_mail.join(','),
+ :mail_content => @mail_content ,
+ # :mail_sentdate => bulletin.email_sentdate,
+ :mail_sentdate => DateTime.now,
+ :create_user_id => bulletin.create_user_id,
+ :update_user_id => bulletin.create_user_id
+ }
+
+ @mail_cron = MailCron.new(@mail_cron)
+
+ @mail_cron.save
+
+ MailCron.send_mail_now(@mail_cron.id)
+
+ end
+
+ end
+
+ def email_group_data
+
+ @email_group_data = Bulletin.email_group_data
+
+ end
+
def delete_out_invalid_date_from_params
if((params[:bulletin]["deadline(1i)"] && params[:bulletin]["deadline(1i)"].blank?) or (params[:bulletin]["deadline(2i)"] && params[:bulletin]["deadline(2i)"].blank?) or (params[:bulletin]["deadline(3i)"] && params[:bulletin]["deadline(3i)"].blank?))
params[:bulletin].delete("deadline(1i)")
diff --git a/app/models/bulletin.rb b/app/models/bulletin.rb
index 77fa12b..7731946 100644
--- a/app/models/bulletin.rb
+++ b/app/models/bulletin.rb
@@ -25,10 +25,23 @@ class Bulletin
field :create_user_id
field :update_user_id, :class_name => "User"
+ field :is_top, :type => Boolean, :default => false
+ field :is_hot, :type => Boolean, :default => false
+ field :is_hidden, :type => Boolean, :default => false
+ field :is_checked, :type => Boolean, :default => false
+ field :is_pending, :type => Boolean, :default => true
+ field :is_rejected, :type => Boolean, :default => false
field :view_count, :type => Integer, :default => 0
+ field :not_checked_reason
+
field :public, :type => Boolean, :default => true
+ field :email_sent, :type => Boolean, :default => false
+ field :email_sentdate , :type => DateTime
+ field :email_group, :type => Array
+ field :other_mailaddress
+
mount_uploader :image, ImageUploader
has_many :bulletin_links, :autosave => true, :dependent => :destroy
@@ -109,6 +122,37 @@ class Bulletin
end
+ def proc_check(check,not_pass_info = "")
+ self.is_checked = true
+ if check =="true"
+ self.is_rejected = false
+ elsif check == "false"
+ self.is_rejected = true
+ self.not_checked_reason = not_pass_info
+ end
+ end
+
+ def de_pending
+ self.is_pending = false
+ end
+
+ def de_pending!
+ de_pending
+ self.save!
+ end
+
+ def is_checked?
+ !self.is_pending && self.is_checked && (self.is_rejected == false)
+ end
+
+ def is_pending?
+ self.is_pending
+ end
+
+ def is_rejected?
+ !self.is_pending && self.is_rejected && (self.is_rejected == true)
+ end
+
def save_bulletin_links
self.bulletin_links.each do |t|
@@ -149,9 +193,47 @@ class Bulletin
preview_object
end
+ def get_email_group_data(email_group_data)
+
+ group_mail = Array.new
+
+ self.email_group.each do |egroup|
+
+ if email_group_data.include?(egroup) and (egroup == 'e_0' or egroup == 'e_1' or egroup == 'e_2' or egroup == 'e_3')
+
+ group_mail << "#{email_group_data[egroup]["email"]}(#{email_group_data[egroup]["name"]})"
+
+ elsif email_group_data.include?(egroup) and egroup == 'e_4'
+
+ self.other_mailaddress.split(",").each do |otmail|
+
+ group_mail << "#{otmail}(#{email_group_data[egroup]["name"]})"
+
+ end
+
+ end
+
+ end
+
+ group_mail.join("
").html_safe
+
+ end
+
protected
+ def self.email_group_data
+
+ @email_group_data = {
+ 'e_0'=> {"name"=>I18n.t('announcement.email_group_data_0'), "email"=>"alluser@tea.ntue.edu.tw"},
+ 'e_1'=> {"name"=>I18n.t('announcement.email_group_data_1'), "email"=>"allstu@tea.ntue.edu.tw"},
+ 'e_2'=> {"name"=>I18n.t('announcement.email_group_data_2'), "email"=>"allgrad@tea.ntue.edu.tw"},
+ 'e_3'=> {"name"=>I18n.t('announcement.email_group_data_3'), "email"=>"allad@tea.ntue.edu.tw"},
+ 'e_4'=> {"name"=>I18n.t('announcement.email_group_data_4')}
+ }
+
+ end
+
# def clean_values
# self.bulletin_links.each do |link|
# link.delete if link.url.blank? && link.title.blank?
diff --git a/app/views/announcement_mailer/cron_mail.html.erb b/app/views/announcement_mailer/cron_mail.html.erb
new file mode 100644
index 0000000..fbf7e0b
--- /dev/null
+++ b/app/views/announcement_mailer/cron_mail.html.erb
@@ -0,0 +1,29 @@
+<% # encoding: utf-8 %>
+
+
+