nccu-news/app/controllers/admin/news_controller.rb

250 lines
8.1 KiB
Ruby
Raw Normal View History

2014-11-07 08:44:11 +00:00
# encoding: utf-8
class Admin::NewsController < OrbitAdminController
include Admin::NewsHelper
before_action ->(module_app = @app_title) { set_variables module_app }
before_action :set_news_bulletin, only: [:edit, :destroy]
before_action :load_access_level
def initialize
super
@app_title = "news"
end
def index
@tags = @module_app.tags
@categories = @module_app.categories.enabled
@filter_fields = filter_fields(@categories, @tags)
@table_fields = [:status, :category, :title, :start_date, :end_date, :image, "news.unit", :view_count]
2015-01-09 06:29:00 +00:00
if !params[:sort].blank?
if params[:sort] == 'start_date'
sort = {:postdate=>params[:order]}
elsif params[:sort] == 'end_date'
sort = {:deadline=>params[:order]}
else
sort = {params[:sort].to_sym=>params[:order]}
end
else
sort = {:postdate=>"desc"}
end
@news_bulletin = NewsBulletin.where(:is_preview.in=>[false,nil])
2014-11-07 08:44:11 +00:00
.order_by(sort)
.with_categories(filters("category"))
.with_tags(filters("tag"))
.with_status(filters("status"))
@news_bulletin = search_data(@news_bulletin,[:title]).page(params[:page]).per(10)
if request.xhr?
render :partial => "index"
end
end
def new
@tags = @module_app.tags
@statuses = []
@news_bulletin = NewsBulletin.new
@news_bulletin.email_sentdate = Time.now
2014-12-08 07:19:37 +00:00
sort = (!params[:sort].blank? ? {params[:sort].to_sym=>params[:order]} : {:id=>"asc"})
@unit = Unit.all.order_by(sort)
2014-11-07 08:44:11 +00:00
end
def create
if !news_bulletin_params['news_bulletin_links_attributes'].nil?
news_bulletin_params['news_bulletin_links_attributes'].each do |idx,link|
news_bulletin_params['news_bulletin_links_attributes'].delete(idx.to_s) if link['url'].blank?
end
end
news_bulletin = NewsBulletin.new(news_bulletin_params)
news_bulletin.create_user_id = current_user.id
news_bulletin.update_user_id = current_user.id
if user_can_approve?
news_bulletin.approved = true
end
news_bulletin.save
build_email(news_bulletin)
redirect_to params['referer_url']
end
def approve_news_bulletin
id = params[:id]
news_bulletin = NewsBulletin.find(id)
2014-11-14 09:50:18 +00:00
if params["approved"] == "true"
news_bulletin.approved = true
news_bulletin.rejected = false
2015-03-26 10:10:59 +00:00
news_bulletin.reapproval = false
2014-11-14 09:50:18 +00:00
else
news_bulletin.rejected = true
2015-03-26 10:10:59 +00:00
news_bulletin.reapproval = false
2014-11-14 09:50:18 +00:00
news_bulletin.rejection_reason = params["reason"]
end
2014-11-07 08:44:11 +00:00
news_bulletin.save
2014-11-13 08:21:59 +00:00
redirect_to "/admin/news"
2014-11-07 08:44:11 +00:00
end
def edit
if can_edit_or_delete?(@news_bulletin)
2014-12-08 07:19:37 +00:00
sort = (!params[:sort].blank? ? {params[:sort].to_sym=>params[:order]} : {:id=>"asc"})
@unit = Unit.all.order_by(sort)
2014-11-07 08:44:11 +00:00
@department = Department.where(:unit_id => @news_bulletin.unit_id)
@tags = @module_app.tags
@categories = @module_app.categories.enabled
@statuses = []
@news_bulletin.email_sentdate = Time.now if @news_bulletin.email_sent == false
@selected = nil
else
render_401
end
end
def update
uid = params[:id].split('-').last
news_bulletin = NewsBulletin.find_by(:uid=>uid)
news_bulletin_params[:tags] = news_bulletin_params[:tags].blank? ? [] : news_bulletin_params[:tags]
news_bulletin_params[:email_member_ids] = news_bulletin_params[:email_member_ids].blank? ? [] : news_bulletin_params[:email_member_ids]
if !news_bulletin_params['news_bulletin_links_attributes'].nil?
news_bulletin_params['news_bulletin_links_attributes'].each do |idx,link|
news_bulletin_params['news_bulletin_links_attributes'].delete(idx.to_s) if link['url'].blank?
end
end
news_bulletin.update_attributes(news_bulletin_params)
2015-03-26 10:10:59 +00:00
news_bulletin.reapproval = true if news_bulletin.rejected
2014-11-07 08:44:11 +00:00
news_bulletin.save
build_email(news_bulletin)
redirect_to params['referer_url']
end
def destroy
@news_bulletin.destroy
redirect_to "/admin/news"
end
def delete
if params[:ids]
NewsBulletin.any_in(:uid => params[:ids]).destroy_all
end
redirect_to "/admin/news"
end
def preview
if params['preview_type'].eql?('edit')
news_bulletin_data = news_bulletin_params
org_news_bulletin = NewsBulletin.find(params['news_bulletin_id'])
news_bulletin = org_news_bulletin.clone
news_bulletin.generate_uid
news_bulletin.news_bulletin_files = []
news_bulletin.news_bulletin_links = []
if news_bulletin_data['image'].blank?
news_bulletin.image = org_news_bulletin.image
end
if !news_bulletin_data['news_bulletin_files_attributes'].blank?
news_bulletin_data['news_bulletin_files_attributes'].each do |key, news_bulletin_file|
next if !news_bulletin_file['_destroy'].blank?
file = nil
if news_bulletin_file['id'].blank?
file = NewsBulletinFile.new(news_bulletin_file)
file.news_bulletin_id = news_bulletin.id
file.save
else
org_file = NewsBulletinFile.find(news_bulletin_file['id'])
file = org_file.clone
file.news_bulletin_id = news_bulletin.id
file.file = org_file.file
news_bulletin_file.delete('id')
news_bulletin_file.delete('_destroy')
file.update_attributes(news_bulletin_file)
end
file.save
news_bulletin.news_bulletin_files << file
end
end
if !news_bulletin_data['news_bulletin_links_attributes'].blank?
news_bulletin_data['news_bulletin_links_attributes'].each do |key, news_bulletin_link|
next if !news_bulletin_link['_destroy'].blank?
if news_bulletin_link['id'].blank?
link = NewsBulletinLink.new(news_bulletin_link)
link.news_bulletin_id = news_bulletin.id
else
link = NewsBulletinLink.find(news_bulletin_link['id']).clone
link.news_bulletin_id = news_bulletin.id
news_bulletin_link.delete('id')
news_bulletin_link.delete('_destroy')
link.update_attributes(news_bulletin_link)
end
link.save
news_bulletin.news_bulletin_links << link
end
end
news_bulletin_data.delete('news_bulletin_files_attributes')
news_bulletin_data.delete('news_bulletin_links_attributes')
news_bulletin.update_attributes(news_bulletin_data)
else
news_bulletin = NewsBulletin.new(news_bulletin_params)
end
news_bulletin.is_preview = true
news_bulletin.save
render :text=>page_for_news_bulletin(news_bulletin)
end
def destroy_preview
news_bulletin = NewsBulletin.find_by(:uid=>params['uid'])
if news_bulletin.is_preview
news_bulletin.destroy
end
render :json=>{'destroy'=>news_bulletin.id.to_s}
end
def build_email(news_bulletin)
if news_bulletin.email_sent and !news_bulletin.email_addresses.blank?
if news_bulletin.email.nil?
email = Email.new
email.save
news_bulletin.email_id = email.id
news_bulletin.save
end
is_sent = news_bulletin.email.is_sent
is_sent = !params[:resend_mail].eql?("true") if !params[:resend_mail].blank?
news_bulletin.email.update_attributes(
:create_user=>current_user,
:mail_sentdate=>news_bulletin.email_sentdate,
:module_app=>@module_app,
:mail_to=>news_bulletin.email_addresses,
:mail_subject=>news_bulletin.title,
:template=>'news/email',
:template_data=>{
"host" => request.host_with_port,
"title" => news_bulletin.title,
"url" => page_for_news_bulletin(news_bulletin)
},
:is_sent=>is_sent
)
else
news_bulletin.email.destroy if !news_bulletin.email.nil?
end
end
private
def set_news_bulletin
@news_bulletin = NewsBulletin.find(params[:id])
end
def news_bulletin_params
params[:news_bulletin][:email_sent] = params[:news_bulletin][:email_sent].nil? ? 0 : params[:news_bulletin][:email_sent]
params.require(:news_bulletin).permit!
end
end