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

135 lines
3.8 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-06-17 11:19:57 +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")).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-05-01 08:41:00 +00:00
@tags =@module_app.tags
2014-05-12 08:16:46 +00:00
@categories = @module_app.categories.enabled
2014-05-01 08:41:00 +00:00
@statuses = []
@bulletin = Bulletin.new
2014-04-03 03:18:02 +00:00
end
def create
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-05-01 08:41:00 +00:00
redirect_to "/admin/announcements"
2014-04-03 03:18:02 +00:00
end
2014-05-01 08:41:00 +00:00
def edit
@tags =@module_app.tags
2014-05-12 08:16:46 +00:00
@categories = @module_app.categories.enabled
2014-05-01 08:41:00 +00:00
@statuses = []
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-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-05-01 08:41:00 +00:00
redirect_to "/admin/announcements"
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]
bulletins = 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
bulletin_data = bulletin_params
unless bulletin_data['bulletin_files_attributes'].nil?
bulletin_data['bulletin_files_attributes'].each do |key, value|
bulletin_data['bulletin_files_attributes'][key].delete('id')
end
end
unless bulletin_data['bulletin_links_attributes'].nil?
bulletin_data['bulletin_links_attributes'].each do |key, value|
bulletin_data['bulletin_links_attributes'][key].delete('id')
end
end
bulletin = Bulletin.new(bulletin_data)
bulletin.is_preview = true
bulletin.create_user_id = current_user.id
bulletin.update_user_id = current_user.id
bulletin.save
render :text=>page_for_bulletin(bulletin)
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