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

211 lines
6.2 KiB
Ruby
Raw Normal View History

2014-05-01 08:41:00 +00:00
# encoding: utf-8
class Admin::AnnouncementsController < OrbitAdminController
2014-05-28 07:37:56 +00:00
include Admin::AnnouncementsHelper
before_action ->(module_app = @app_title) { set_variables module_app }
before_action :set_bulletin, only: [:edit, :destroy]
def initialize
super
@app_title = "announcement"
end
2014-05-01 08:41:00 +00:00
def index
@tags = @module_app.tags
2014-05-12 08:16:46 +00:00
@categories = @module_app.categories.enabled
2014-05-14 02:58:06 +00:00
@filter_fields = filter_fields(@categories, @tags)
@table_fields = [:status, :category, :title, :start_date, :end_date, :last_modified]
2014-05-02 10:21:51 +00:00
2014-08-07 10:45:21 +00:00
@bulletins = Bulletin.where(:is_preview.in=>[false,nil])
.order_by(sort)
.with_categories(filters("category"))
.with_tags(filters("tag"))
.with_status(filters("status"))
2014-08-06 11:37:43 +00:00
2014-08-07 10:45:21 +00:00
@bulletins = search_data(@bulletins,[:title]).page(params[:page]).per(10)
2014-05-02 10:21:51 +00:00
2014-05-14 02:58:06 +00:00
if request.xhr?
render :partial => "index"
2014-05-01 08:41:00 +00:00
end
end
2014-04-03 03:18:02 +00:00
def new
2014-07-31 09:12:13 +00:00
@tags = @module_app.tags
2014-05-01 08:41:00 +00:00
@statuses = []
@bulletin = Bulletin.new
2014-06-24 09:23:51 +00:00
@bulletin.email_sentdate = Time.now
2014-04-03 03:18:02 +00:00
end
def create
2014-07-21 03:01:53 +00:00
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
2014-05-01 08:41:00 +00:00
bulletin = Bulletin.new(bulletin_params)
bulletin.create_user_id = current_user.id
bulletin.update_user_id = current_user.id
bulletin.save
2014-05-28 07:37:56 +00:00
build_email(bulletin)
2014-08-05 06:37:33 +00:00
redirect_to params['referer_url']
2014-04-03 03:18:02 +00:00
end
2014-05-01 08:41:00 +00:00
def edit
2014-07-31 09:12:13 +00:00
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
2014-08-05 06:37:33 +00:00
else
render_401
end
2014-04-10 03:52:59 +00:00
end
2014-05-01 08:41:00 +00:00
def update
uid = params[:id].split('-').last
bulletin = Bulletin.find_by(:uid=>uid)
bulletin_params[:tags] = bulletin_params[:tags].blank? ? [] : bulletin_params[:tags]
2014-05-28 07:37:56 +00:00
bulletin_params[:email_member_ids] = bulletin_params[:email_member_ids].blank? ? [] : bulletin_params[:email_member_ids]
2014-07-21 03:01:53 +00:00
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
2014-05-01 08:41:00 +00:00
bulletin.update_attributes(bulletin_params)
bulletin.save
2014-05-28 07:37:56 +00:00
build_email(bulletin)
2014-08-05 06:37:33 +00:00
redirect_to params['referer_url']
2014-05-01 08:41:00 +00:00
end
2014-04-03 03:18:02 +00:00
2014-05-01 08:41:00 +00:00
def destroy
@bulletin.destroy
2014-05-01 08:41:00 +00:00
redirect_to "/admin/announcements"
end
def delete
if params[:ids]
2014-06-23 08:36:04 +00:00
Bulletin.any_in(:uid => params[:ids]).destroy_all
2014-04-03 03:18:02 +00:00
end
2014-05-01 08:41:00 +00:00
redirect_to "/admin/announcements"
end
2014-06-17 10:46:52 +00:00
def preview
2014-06-23 08:36:04 +00:00
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
2014-06-17 10:46:52 +00:00
2014-06-23 08:36:04 +00:00
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
2014-06-24 07:57:27 +00:00
org_file = BulletinFile.find(bulletin_file['id'])
file = org_file.clone
2014-06-23 08:36:04 +00:00
file.bulletin_id = bulletin.id
2014-06-24 07:57:27 +00:00
file.file = org_file.file
2014-06-23 08:36:04 +00:00
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
2014-06-17 10:46:52 +00:00
2014-06-23 08:36:04 +00:00
link.save
bulletin.bulletin_links << link
end
2014-06-17 10:46:52 +00:00
end
2014-06-23 08:36:04 +00:00
bulletin_data.delete('bulletin_files_attributes')
bulletin_data.delete('bulletin_links_attributes')
bulletin.update_attributes(bulletin_data)
else
bulletin = Bulletin.new(bulletin_params)
2014-06-17 10:46:52 +00:00
end
bulletin.is_preview = true
bulletin.save
render :text=>page_for_bulletin(bulletin)
end
2014-06-23 08:36:04 +00:00
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
2014-05-28 07:37:56 +00:00
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
2014-05-01 08:41:00 +00:00
private
def set_bulletin
@bulletin = Bulletin.find(params[:id])
2014-05-01 08:41:00 +00:00
end
def bulletin_params
2014-05-28 07:37:56 +00:00
params[:bulletin][:email_sent] = params[:bulletin][:email_sent].nil? ? 0 : params[:bulletin][:email_sent]
params.require(:bulletin).permit!
2014-05-01 08:41:00 +00:00
end
2014-04-03 03:18:02 +00:00
end