254 lines
8.0 KiB
Ruby
254 lines
8.0 KiB
Ruby
# encoding: utf-8
|
||
class Panel::Announcement::BackEnd::BulletinsController < OrbitBackendController
|
||
# include OrbitControllerLib::DivisionForDisable
|
||
|
||
before_filter :clean_values, :only => [:create, :update]
|
||
|
||
open_for_visitor :only => [:index, :show, :get_sorted_and_filtered_bulletins]
|
||
|
||
before_filter :only => [ :new, :create, :edit, :update ] do |controller|
|
||
@categories = get_categories_for_form
|
||
end
|
||
|
||
def preview
|
||
params['bulletin']['image'] = nil
|
||
bulletin = Bulletin.new params
|
||
@preview_obj = bulletin.to_preview
|
||
@preview_obj.save
|
||
render '/shared/preview/preview.html.erb',:layout=>false
|
||
end
|
||
|
||
|
||
def index
|
||
@tags = get_tags
|
||
@categories = get_categories_for_index
|
||
@statuses = get_statuses
|
||
category_ids = @categories.collect{|t| t.id}
|
||
|
||
@bulletins = get_sorted_and_filtered("bulletin", :category_id.in => category_ids)
|
||
respond_to do |format|
|
||
format.html # index.html.erb
|
||
format.js { }
|
||
format.xml { render :xml => @bulletins }
|
||
end
|
||
end
|
||
|
||
# GET /bulletins/1
|
||
# GET /bulletins/1.xml
|
||
def show
|
||
@bulletin = Bulletin.find(params[:id])
|
||
@tags = get_tags
|
||
|
||
respond_to do |format|
|
||
format.html # show.html.erb
|
||
format.xml { render :xml => @bulletin }
|
||
end
|
||
end
|
||
|
||
# GET /bulletins/new
|
||
# GET /bulletins/new.xml
|
||
def new
|
||
if(session[:in_validate_object].blank?)
|
||
@bulletin = Bulletin.new(:postdate => DateTime.now, :email_sentdate => DateTime.now)
|
||
else
|
||
@bulletin = session[:in_validate_object]
|
||
session[:in_validate_object] = {}
|
||
end
|
||
|
||
@link_url = panel_announcement_back_end_bulletins_path
|
||
@tags = get_tags
|
||
respond_to do |format|
|
||
format.html # new.html.erb
|
||
format.xml { render :xml => @bulletin }
|
||
end
|
||
end
|
||
|
||
# GET /bulletins/1/edit
|
||
def edit
|
||
@bulletin = Bulletin.find(params[:id])
|
||
@email_users = @bulletin.get_email_users
|
||
@tags = get_tags
|
||
is_authorized_sub_manager = @bulletin.category.auth_sub_manager.authorized_user_ids rescue nil
|
||
|
||
if !(is_manager? || is_admin? || is_authorized_sub_manager.include?(current_user.id))
|
||
redirect_to :action => :index
|
||
else
|
||
# @summary_variable = @bulletin.summary_variable
|
||
|
||
@link_url = panel_announcement_back_end_bulletin_path(@bulletin)
|
||
|
||
@tags = get_tags
|
||
end
|
||
end
|
||
|
||
# POST /bulletins
|
||
# POST /bulletins.xml
|
||
def create
|
||
@tags = get_tags
|
||
|
||
params[:bulletin][:email_user_ids] = params[:bulletin][:email_user_ids].uniq
|
||
params[:bulletin][:email_user_ids].delete('')
|
||
|
||
@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
|
||
|
||
respond_to do |format|
|
||
if @bulletin.save
|
||
|
||
if @bulletin.email_sent == true && (is_manager? || is_admin?)
|
||
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
|
||
format.js {
|
||
@info = {"success"=>"true","redirect_url"=>panel_announcement_back_end_bulletins_url}
|
||
flash[:notice] = t('bulletin.create_bulletin_success')
|
||
render "/shared/preview/after_create.js.erb"
|
||
}
|
||
else
|
||
format.html { render :action => "new" }
|
||
format.xml { render :xml => @bulletin.errors, :status => :unprocessable_entity }
|
||
format.js {
|
||
@info = {"success"=>"false","redirect_url"=>new_panel_announcement_back_end_bulletin_url(:bulletin => @bulletin)}
|
||
session[:in_validate_object] = @bulletin
|
||
render "/shared/preview/after_create.js.erb"
|
||
}
|
||
end
|
||
end
|
||
end
|
||
|
||
# PUT /bulletins/1
|
||
# PUT /bulletins/1.xml
|
||
|
||
def update
|
||
@bulletin = Bulletin.find(params[:id])
|
||
|
||
params[:bulletin][:tag_ids] = (params[:bulletin][:tag_ids] ? params[:bulletin][:tag_ids] : [])
|
||
|
||
params[:bulletin][:email_user_ids] = params[:bulletin][:email_user_ids].uniq
|
||
params[:bulletin][:email_user_ids].delete('')
|
||
|
||
delete_out_invalid_date_from_params
|
||
respond_to do |format|
|
||
if @bulletin.update_attributes(params[:bulletin])
|
||
|
||
if @bulletin.email_sent == true && (is_manager? || is_admin?)
|
||
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
|
||
@bulletin = Bulletin.find(params[:id])
|
||
# @bulletin.bulletin_files.destroy
|
||
@bulletin.destroy
|
||
|
||
respond_to do |format|
|
||
|
||
format.html { redirect_to(panel_announcement_back_end_bulletins_url) }
|
||
# format.xml { head :ok }
|
||
format.js
|
||
end
|
||
end
|
||
|
||
def delete
|
||
if params[:ids]
|
||
bulletins = Bulletin.any_in(:_id => params[:ids]).destroy_all
|
||
end
|
||
redirect_to panel_announcement_back_end_bulletins_url(:filter => params[:filter], :direction => params[:direction], :sort => params[:sort], :sort_options => params[:sort_options])
|
||
end
|
||
|
||
|
||
protected
|
||
|
||
def send_email_data(bulletin)
|
||
|
||
@site = Site.first
|
||
@user = User.find(bulletin.create_user_id)
|
||
@host = request.host_with_port
|
||
|
||
@group_mail = MailCron.get_send_group_mail( bulletin.email_user_ids , bulletin.other_mailaddress )
|
||
|
||
if !@group_mail.join.blank?
|
||
|
||
@mail_content = {
|
||
"host" => @host,
|
||
"lang" => I18n.locale,
|
||
"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,
|
||
:create_user_id => bulletin.create_user_id,
|
||
:update_user_id => bulletin.create_user_id
|
||
}
|
||
|
||
@mail_cron = MailCron.new(@mail_cron)
|
||
|
||
@mail_cron.save
|
||
|
||
end
|
||
|
||
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)")
|
||
params[:bulletin].delete("deadline(2i)")
|
||
params[:bulletin].delete("deadline(3i)")
|
||
params[:bulletin].delete("deadline(4i)")
|
||
params[:bulletin].delete("deadline(5i)")
|
||
@bulletin.deadline = nil
|
||
end rescue nil
|
||
end
|
||
|
||
def clean_values
|
||
if params[:bulletin]
|
||
params[:bulletin][:bulletin_links_attributes].each_with_index do |link, index|
|
||
params[:bulletin][:bulletin_links_attributes].delete(index.to_s) if link[1]['url'].blank? and link[1]['title_translations'].blank?
|
||
end rescue nil
|
||
params[:bulletin][:bulletin_files_attributes].each_with_index do |link, index|
|
||
params[:bulletin][:bulletin_files_attributes].delete(index.to_s) if link[1]['file'].blank? and link[1]['title_translations'].blank?
|
||
end rescue nil
|
||
end
|
||
end
|
||
end
|