announcement-test/app/controllers/admin/announcements_controller.rb

265 lines
7.6 KiB
Ruby

# 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]
before_action :load_access_level
def initialize
super
@app_title = "announcement"
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, :last_modified]
@bulletins = Bulletin.where(:title.ne => "",:is_preview.in=>[false,nil])
.order_by(sort)
.with_categories(filters("category"))
.with_tags(filters("tag"))
.with_status(filters("status"))
@bulletins = search_data(@bulletins,[:title]).page(params[:page]).per(10)
if request.xhr?
render :partial => "index"
end
end
def feed
@table_feed_fields = ["announcement.feed_name", :tags, "announcement.rssfeed"]
@feeds = BulletinFeed.all.asc(:created_at)
end
def feedform
if params[:type] == "new"
@announcement_feed = BulletinFeed.new
render :partial => "feed_form"
else params[:type] == "edit"
@announcement_feed = BulletinFeed.find(params[:id])
render :partial => "edit_feed_form"
end
end
def createfeed
announcement_feed = BulletinFeed.new(feed_params)
announcement_feed.save
feeds = BulletinFeed.all.asc(:created_at)
render :partial => "feed", :collection => feeds
end
def updatefeed
ann_feed = BulletinFeed.find(params[:id])
ann_feed.update_attributes(feed_params)
ann_feed.save
feeds = BulletinFeed.all.asc(:created_at)
render :partial => "feed", :collection => feeds
end
def deletefeed
ann_feed = BulletinFeed.find(params[:id])
ann_feed.destroy
feeds = BulletinFeed.all.asc(:created_at)
render :partial => "feed", :collection => feeds
end
def new
@tags = @module_app.tags
@statuses = []
@bulletin = Bulletin.new
@bulletin.email_sentdate = Time.now
end
def create
if !bulletin_params['bulletin_links_attributes'].nil?
bulletin_params['bulletin_links_attributes'].each do |idx,link|
bulletin_params['bulletin_links_attributes'].delete(idx.to_s) if link['url'].blank?
end
end
bulletin = Bulletin.new(bulletin_params)
bulletin.create_user_id = current_user.id
bulletin.update_user_id = current_user.id
# if user_can_approve?
bulletin.approved = true
# end
bulletin.save
build_email(bulletin)
redirect_to params['referer_url']
end
def approve_bulletin
id = params[:id]
bulletin = Bulletin.find(id)
bulletin.approved = true
bulletin.save
redirect_to admin_announcements_path
end
def edit
if can_edit_or_delete?(@bulletin)
@tags = @module_app.tags
@categories = @module_app.categories.enabled
@statuses = []
@bulletin.email_sentdate = Time.now if @bulletin.email_sent == false
else
render_401
end
end
def update
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]
if !bulletin_params['bulletin_links_attributes'].nil?
bulletin_params['bulletin_links_attributes'].each do |idx,link|
bulletin_params['bulletin_links_attributes'].delete(idx.to_s) if link['url'].blank?
end
end
bulletin.update_attributes(bulletin_params)
bulletin.save
build_email(bulletin)
redirect_to params['referer_url']
end
def destroy
@bulletin.destroy
redirect_to "/admin/announcements"
end
def delete
if params[:ids]
Bulletin.any_in(:uid => params[:ids]).destroy_all
end
redirect_to "/admin/announcements"
end
def preview
if params['preview_type'].eql?('edit')
bulletin_data = bulletin_params
org_bulletin = Bulletin.find(params['bulletin_id'])
bulletin = org_bulletin.clone
bulletin.generate_uid
bulletin.bulletin_files = []
bulletin.bulletin_links = []
if bulletin_data['image'].blank?
bulletin.image = org_bulletin.image
end
if !bulletin_data['bulletin_files_attributes'].blank?
bulletin_data['bulletin_files_attributes'].each do |key, bulletin_file|
next if !bulletin_file['_destroy'].blank?
file = nil
if bulletin_file['id'].blank?
file = BulletinFile.new(bulletin_file)
file.bulletin_id = bulletin.id
file.save
else
org_file = BulletinFile.find(bulletin_file['id'])
file = org_file.clone
file.bulletin_id = bulletin.id
file.file = org_file.file
bulletin_file.delete('id')
bulletin_file.delete('_destroy')
file.update_attributes(bulletin_file)
end
file.save
bulletin.bulletin_files << file
end
end
if !bulletin_data['bulletin_links_attributes'].blank?
bulletin_data['bulletin_links_attributes'].each do |key, bulletin_link|
next if !bulletin_link['_destroy'].blank?
if bulletin_link['id'].blank?
link = BulletinLink.new(bulletin_link)
link.bulletin_id = bulletin.id
else
link = BulletinLink.find(bulletin_link['id']).clone
link.bulletin_id = bulletin.id
bulletin_link.delete('id')
bulletin_link.delete('_destroy')
link.update_attributes(bulletin_link)
end
link.save
bulletin.bulletin_links << link
end
end
bulletin_data.delete('bulletin_files_attributes')
bulletin_data.delete('bulletin_links_attributes')
bulletin.update_attributes(bulletin_data)
else
bulletin = Bulletin.new(bulletin_params)
end
bulletin.is_preview = true
bulletin.save
render :text=>page_for_bulletin(bulletin) + "?preview=true"
end
def destroy_preview
bulletin = Bulletin.find_by(:uid=>params['uid'])
if bulletin.is_preview
bulletin.destroy
end
render :json=>{'destroy'=>bulletin.id.to_s}
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
@bulletin = Bulletin.find(params[:id])
end
def bulletin_params
params[:bulletin][:email_sent] = params[:bulletin][:email_sent].nil? ? 0 : params[:bulletin][:email_sent]
params.require(:bulletin).permit!
end
def feed_params
params.require(:bulletin_feed).permit!
end
end