eremail/app/controllers/admin/er_emails_controller.rb

224 lines
5.3 KiB
Ruby
Raw Normal View History

2014-07-16 08:32:50 +00:00
# encoding: utf-8
class Admin::ErEmailsController < OrbitAdminController
include Admin::ErEmailsHelper
before_action ->(module_app = @app_title) { set_variables module_app }
before_action :set_email_er, only: [:edit, :show, :send_email, :test_send_email, :destroy]
def initialize
super
@app_title = "er_email"
end
def index
@categories = @module_app.categories.enabled
@table_fields = [:status, :category, 'er_email.mail_subject', 'er_email.created_user']
@email_ers = EmailEr.where(:is_preview.in=>[false,nil]).order_by(sort).with_categories(filters("category")).page(params[:page]).per(10)
if request.xhr?
render :partial => "index"
end
end
def show
end
def new
@categories = @module_app.categories.enabled
@email_er = EmailEr.new
@email_er.mail_sentdate = Time.now
end
def create
@email_er = EmailEr.new(email_er_params)
@email_er.create_user_id = current_user.id
@email_er.update_user_id = current_user.id
# @email_er.save
# build_email(@email_er)
respond_to do |format|
if @email_er.save
# build_email(@email_er)
format.html { redirect_to "/admin/er_emails" }
else
format.html { render :action => "new" }
end
end
end
def edit
@categories = @module_app.categories.enabled
# @email_er.mail_sentdate = Time.now
end
def update
uid = params[:id].split('-').last
@email_er = EmailEr.find_by(:uid=>uid)
@email_er.email_confirm = false
@email_er.update_user_id = current_user.id
email_er_params[:email_member_ids] = email_er_params[:email_member_ids].blank? ? [] : email_er_params[:email_member_ids]
# email_er.update_attributes(email_er_params)
# email_er.save
# build_email(email_er)
respond_to do |format|
if @email_er.update_attributes(email_er_params)
@email_er.save
# build_email(@email_er)
format.html { redirect_to "/admin/er_emails" }
else
format.html { render :action => "edit" }
end
end
end
def destroy
@email_er.destroy
redirect_to "/admin/er_emails"
end
def delete
if params[:ids]
EmailEr.any_in(:uid => params[:ids]).destroy_all
end
redirect_to "/admin/er_emails"
end
def preview
if params['preview_type'].eql?('edit')
email_er_data = email_er_params
org_email_er = EmailEr.find(params['email_er_id'])
email_er = org_email_er.clone
email_er.generate_uid
email_er.email_er_files = []
if !email_er_data['email_er_files_attributes'].blank?
email_er_data['email_er_files_attributes'].each do |key, email_er_file|
next if !email_er_file['_destroy'].blank?
file = nil
if email_er_file['id'].blank?
file = EmailErFile.new(email_er_file)
file.email_er_id = email_er.id
file.save
else
org_file = EmailErFile.find(email_er_file['id'])
file = org_file.clone
file.email_er_id = email_er.id
file.file = org_file.file
email_er_file.delete('id')
email_er_file.delete('_destroy')
file.update_attributes(email_er_file)
end
file.save
email_er.email_er_files << file
end
end
email_er_data.delete('email_er_files_attributes')
email_er.update_attributes(email_er_data)
else
email_er = EmailEr.new(email_er_params)
end
email_er.is_preview = true
email_er.save
render :text=>page_for_email_er(email_er)
end
def destroy_preview
email_er = EmailEr.find(params[:id])
if email_er.is_preview
email_er.destroy
end
render :json=>{'destroy'=>email_er.id.to_s}
end
def build_email(email_er , test_send = false)
email = Email.new
email.save
email_er.email_id = email.id
email_er.email_confirm = true if !test_send
email_er.save
if !test_send
@group_mail = email_er.email_addresses
@mail_sentdate = email_er.mail_sentdate
else
@user = MemberProfile.find(User.find(email_er.create_user_id).member_profile_id)
@group_mail = [@user.email]
@mail_sentdate = DateTime.now
end
email_er.email.update_attributes(
:create_user=>current_user,
:mail_sentdate=>@mail_sentdate,
:module_app=>@module_app,
:mail_to=>@group_mail,
:mail_content => email_er.mail_content ,
:mail_subject=>email_er.mail_subject
)
if !email_er.email_er_files.blank?
email.email_files.destroy_all
email_er.email_er_files.each do | mrfile |
@mcfile = email.email_files.build
@mcfile.file = mrfile.file
@mcfile.title = mrfile.title
end
email.save
end
OrbitMailer.set_mail(email_er.email).deliver if test_send
end
def send_email
if params[:id]
build_email(@email_er)
end
redirect_to "/admin/er_emails"
end
def test_send_email
if params[:id]
build_email(@email_er, true)
end
redirect_to "/admin/er_emails"
end
private
def set_email_er
@email_er = EmailEr.find(params[:id])
end
def email_er_params
# params[:email_er][:email_sent] = params[:email_er][:email_sent].nil? ? 0 : params[:email_er][:email_sent]
params.require(:email_er).permit!
end
end