seminar/app/controllers/seminars_controller.rb

434 lines
13 KiB
Ruby
Raw Normal View History

2016-06-29 06:57:45 +00:00
class SeminarsController < ApplicationController
helper MemberHelper
include ActionView::Context #vary important (only add this can access @@session from view)
2016-06-29 06:57:45 +00:00
include Admin::SeminarsHelper
# include SimpleCaptcha::ControllerHelpers
2016-06-29 06:57:45 +00:00
def index
time_now = Time.now
2020-01-22 13:52:32 +00:00
seminars = SeminarMain.all.desc(:seminar_start_date).filter_by_categories
2016-06-29 06:57:45 +00:00
{
"seminars" => seminars,
'time_now' => time_now,
"total_pages" => seminars.total_pages
}
end
def show_privacy
params = OrbitHelper.params
seminar = SeminarMain.where(uid: params[:uid]).first
2016-06-29 06:57:45 +00:00
seminar_agreement = SeminarAgreement.first
{
'seminar' => seminar,
"seminar_agreement" => seminar_agreement
}
end
def show_data
time_now = Time.now
params = OrbitHelper.params
seminar = SeminarMain.find_by(uid: params[:uid])
if !seminar.registration_status.blank? && seminar.signup_start_date <= time_now && ( seminar.signup_end_date.nil? || seminar.signup_end_date+1 >= time_now )
2016-06-29 06:57:45 +00:00
sign_up = ('<a href="'+ OrbitHelper.url_to_show(seminar.to_param) + '" target="_blank">' + t('seminar.signup') + '</a>').html_safe
elsif seminar.registration_status.blank?
sign_up = t('seminar.sign_up_not_open')
elsif seminar.signup_start_date > time_now
sign_up = t('seminar.sign_up_not_yet')
else
sign_up = t('seminar.sign_up_overdue')
end
if !seminar.registration_status.blank? && seminar.registration_status.include?('C') && seminar.contribute_start_date <= time_now && ( seminar.contribute_end_date.nil? || seminar.contribute_end_date+1 >= time_now )
if !@@session[:seminar_signup_id].blank?
if @@session[:seminar_main_id] == seminar.id
2016-06-29 06:57:45 +00:00
contribute_action = ('<a href="'+ OrbitHelper.url_to_show(seminar.to_param) + '?method=con_upload">' + t('seminar_signup.uploads') + '</a>').html_safe
else
contribute_action = ('<a href="'+ OrbitHelper.url_to_show(seminar.to_param) + '?method=con_login">' + t('seminar_signup.con_login') + '</a>').html_safe
end
else
contribute_action = ('<a href="'+ OrbitHelper.url_to_show(seminar.to_param) + '?method=con_login">' + t('seminar_signup.con_login') + '</a>').html_safe
end
end
{
'seminar' => seminar,
'sign_up' => sign_up,
'contribute_action' => contribute_action,
'time_now' => time_now,
'contribute_time_range' => SeminarMain.time_range(seminar.contribute_start_date, seminar.contribute_end_date),
'sign_up_time_range' => SeminarMain.time_range(seminar.signup_start_date, seminar.signup_end_date),
'seminar_time_range' => SeminarMain.time_range(seminar.seminar_start_date, seminar.seminar_end_date)
}
end
def show
params = OrbitHelper.params
module_app = ModuleApp.where(:key => "seminar").first
categories = module_app.categories
seminar = SeminarMain.where(uid: params[:uid]).first
2016-06-29 06:57:45 +00:00
seminar_signup = SeminarSignup.new
time_now = Time.now
{
"seminar" => seminar,
"seminar_signup" => seminar_signup,
"time_now" => time_now
}
end
def check_email
params = OrbitHelper.params
if !params[:email].blank?
count = SeminarSignup.where(:email => params[:email], :seminar_main_id => params[:no]).count
result = count > 0 ? t('seminar.already_used') : t('seminar.available')
2016-06-29 06:57:45 +00:00
else
result = t('seminar.please_enter_email')
2016-06-29 06:57:45 +00:00
end
{
"result" => result
}
end
def check_availability
params = OrbitHelper.params
response = {}
case params[:type]
when "seminar_signup_email"
response["success"] = SeminarSignup.where(:email => params[:value], :seminar_main_id => params[:no]).count > 0 ? false : true
end
render :json => response.to_json
end
def create
@signup = SeminarSignup.where(email: params[:seminar_signup][:email], seminar_main_id: params[:seminar_signup][:seminar_main_id] ).first
@seminar_signup = SeminarSignup.new(seminar_signup_params)
2020-01-12 06:39:32 +00:00
@seminar = SeminarMain.where(id:params[:seminar_signup][:seminar_main_id]).first
if @seminar.seminar_signup_field_sets.count!=0
flag = @seminar.seminar_signup_field_sets.select{|v| v.field_name=='recaptcha'}[0].disabled
else
flag = false
end
if gotcha_valid? || flag
2016-08-04 08:25:40 +00:00
if @signup.blank? && @seminar_signup.save
# if gotcha_valid? && @seminar_signup.save
if !params[:seminar_signup_values].nil?
seminar_signup_values_params.each_with_index do |r,i|
field_value = r.last[:id].present?
@custom_field_value = SeminarSignupValue.put_field_values(@seminar_signup, r.last, r.last[:seminar_signup_field_id], field_value)
end
seminar_reviews = SeminarReview.where(:seminar_main_id=>@seminar.id).to_a
seminar_reviews.each{|sr| sr.save}
else
@seminar.unassigned_seminar_signup_ids << @seminar_signup.id.to_s
@seminar.save
2016-08-04 08:25:40 +00:00
end
if params['seminar_signup']['status']=='C'
status_param = '&status='
2020-01-12 06:39:32 +00:00
send_mail('submission',params[:seminar_signup][:email],params[:seminar_signup][:seminar_main_id])
else
status_param = ''
2020-01-12 06:39:32 +00:00
send_mail('signup',params[:seminar_signup][:email],params[:seminar_signup][:seminar_main_id])
end
redirect_to "#{params[:referer_url]}/?method=signup_ok#{status_param}"
2016-08-04 08:25:40 +00:00
else
if !@signup.blank?
redirect_to "#{params[:referer_url]}", :notice => 'mail已存在'
else
redirect_to "#{params[:referer_url]}", :notice => t('recaptcha.errors.verification_failed')
2016-06-29 06:57:45 +00:00
end
end
2020-01-12 06:39:32 +00:00
else
redirect_to "#{params[:referer_url]}", :notice => t('recaptcha.errors.verification_failed')
end
2016-06-29 06:57:45 +00:00
end
def signup_ok
end
def add_file_proc
update_params = seminar_signup_contribute_params.merge('seminar_submission_values'=>params['seminar_submission_values'])
flag = true
update_params.permit!
2020-04-24 14:26:38 +00:00
if !update_params['seminar_submission_values'].nil?
if update_params['seminar_submission_values']['0']['value'].class==String
value = update_params['seminar_submission_values']['0']['value']
if !value.to_s.blank?
2019-11-28 04:39:36 +00:00
flag = false
end
2020-04-24 14:26:38 +00:00
else
update_params['seminar_submission_values']['0']['value'].each do |key,value|
if !value.to_s.blank?
flag = false
end
end
2019-11-28 04:39:36 +00:00
end
2020-04-24 14:26:38 +00:00
else
flag = false
end
if flag
update_params['seminar_submission_values']['0']['value']['en'] = ' '
end
@con = SeminarSignupContribute.new(update_params)
2016-06-29 06:57:45 +00:00
if @con.save
2020-01-12 06:39:32 +00:00
signup = SeminarSignup.where(id:update_params['seminar_signup_id']).first
seminar_id = signup.seminar_main_id
@seminar = SeminarMain.where(id:seminar_id).first
send_mail('add_file',signup.email,seminar_id)
seminar_reviews = SeminarReview.where(:seminar_main_id=>signup.seminar_main_id).to_a
seminar_reviews.each{|sr| sr.save}
@seminar = signup.seminar_main
unassigned_seminar_signup_ids = @seminar.seminar_signup_ids.map{|s| s.to_s}
@seminar.seminar_reviews.each do |sr|
unassigned_seminar_signup_ids = unassigned_seminar_signup_ids - sr.all_seminar_signup_ids
end
@seminar.unassigned_seminar_signup_ids = unassigned_seminar_signup_ids
@seminar.save
2016-06-29 06:57:45 +00:00
redirect_to "#{params[:referer_url]}/?method=con_upload"
else
redirect_to "#{params[:referer_url]}", :notice => t('recaptcha.errors.verification_failed')
end
end
def update
@con = SeminarSignupContribute.find(params[:seminar_signup_contribute][:id])
update_params = seminar_signup_contribute_params.merge('seminar_submission_values'=>params['seminar_submission_values'])
flag = true
update_params.permit!
if update_params['seminar_submission_values']['0']['value'].class==String
value = update_params['seminar_submission_values']['0']['value']
if !value.to_s.blank?
flag = false
end
else
update_params['seminar_submission_values']['0']['value'].each do |key,value|
if !value.to_s.blank?
flag = false
end
end
end
if flag
update_params['seminar_submission_values']['0']['value']['en'] = ' '
end
@con.update_attributes(update_params)
2016-06-29 06:57:45 +00:00
if @con.save
2020-01-12 06:39:32 +00:00
signup = SeminarSignup.where(id:update_params['seminar_signup_id']).first
seminar_id = signup.seminar_main_id
@seminar = SeminarMain.where(id:seminar_id).first
send_mail('edit_file',signup.email,seminar_id)
seminar_reviews = SeminarReview.where(:seminar_main_id=>signup.seminar_main_id).to_a
seminar_reviews.each{|sr| sr.save}
@seminar = signup.seminar_main
unassigned_seminar_signup_ids = @seminar.seminar_signup_ids.map{|s| s.to_s}
@seminar.seminar_reviews.each do |sr|
unassigned_seminar_signup_ids = unassigned_seminar_signup_ids - sr.all_seminar_signup_ids
end
@seminar.unassigned_seminar_signup_ids = unassigned_seminar_signup_ids
@seminar.save
2016-06-29 06:57:45 +00:00
redirect_to "#{params[:referer_url]}/?method=con_upload"
else
redirect_to "#{params[:referer_url]}", :notice => t('recaptcha.errors.verification_failed')
end
end
def del_file
con = SeminarSignupContribute.find(params[:con_no])
# seminar_signup = SeminarSignup.find_by(_id: con.seminar_signup_id )
# seminar = SeminarMain.find_by(_id: seminar_signup.seminar_main_id )
signup = con.seminar_signup
2016-06-29 06:57:45 +00:00
con.destroy
seminar_reviews = SeminarReview.where(:seminar_main_id=>signup.seminar_main_id).to_a
seminar_reviews.each{|sr| sr.save}
@seminar = SeminarMain.find(signup.seminar_main_id)
unassigned_seminar_signup_ids = @seminar.seminar_signup_ids.map{|s| s.to_s}
@seminar.seminar_reviews.each do |sr|
unassigned_seminar_signup_ids = unassigned_seminar_signup_ids - sr.all_seminar_signup_ids
end
@seminar.unassigned_seminar_signup_ids = unassigned_seminar_signup_ids
@seminar.save
2016-06-29 06:57:45 +00:00
redirect_to "#{params[:referer_url]}/?method=con_upload"
end
def add_file
if !@@session[:seminar_main_id].blank?
2016-06-29 06:57:45 +00:00
seminar = SeminarMain.find_by(id: @@session[:seminar_main_id])
seminar_signup = SeminarSignup.find_by(_id: @@session[:seminar_signup_id], seminar_main_id: @@session[:seminar_main_id] )
2016-06-29 06:57:45 +00:00
else
seminar = {}
seminar_signup = {}
end
con = SeminarSignupContribute.new
{
'con' => con,
'seminar' => seminar,
'seminar_signup' => seminar_signup
}
end
def edit_file
params = OrbitHelper.params
if !@@session[:seminar_main_id].blank?
seminar = SeminarMain.find_by(id: @@session[:seminar_main_id])
seminar_signup = SeminarSignup.find_by(_id: @@session[:seminar_signup_id], seminar_main_id: @@session[:seminar_main_id] )
else
seminar = {}
seminar_signup = {}
end
2016-06-29 06:57:45 +00:00
con = SeminarSignupContribute.find(params[:con_no])
{
'con' => con,
'seminar' => seminar,
'seminar_signup' => seminar_signup
2016-06-29 06:57:45 +00:00
}
end
def con_upload
time_now = Time.now
if !@@session[:seminar_main_id].blank?
2016-06-29 06:57:45 +00:00
seminar = SeminarMain.find_by(id: @@session[:seminar_main_id])
2016-06-29 06:57:45 +00:00
seminar_signup = SeminarSignup.find_by(_id: @@session[:seminar_signup_id], seminar_main_id: @@session[:seminar_main_id] )
2016-06-29 06:57:45 +00:00
else
seminar = {}
seminar_signup = {}
end
{
'time_now' => time_now,
'seminar' => seminar,
'seminar_signup' => seminar_signup
}
end
def con_logout
# redirect_to page_for_seminar_url
end
def con_login
time_now = Time.now
params = OrbitHelper.params
seminar = SeminarMain.find_by(uid: params[:uid])
{
'seminar' => seminar,
'time_now' => time_now
}
end
def con_login_proc
seminar = SeminarMain.find_by(id: params[:seminar_signup][:seminar_main_id])
@seminar_signup = SeminarSignup.where(:status=>'C', :email=> params[:user_name], :password => params[:password], :seminar_main_id => params[:seminar_signup][:seminar_main_id]).first
if !@seminar_signup.blank?
session[:seminar_signup_id] = @seminar_signup.id
session[:seminar_main_id] = @seminar_signup.seminar_main_id
redirect_to "#{params[:referer_url]}/?method=con_upload"
else
redirect_to "#{params[:referer_url]}/?method=con_login", :notice => '登入失敗'
end
end
def seminar_signup_params
params.require(:seminar_signup).permit!
end
def seminar_signup_contribute_params
params.require(:seminar_signup_contribute).permit!
end
def seminar_signup_values_params
params.require(:seminar_signup_values).permit!
end
2020-01-12 06:39:32 +00:00
def send_mail(field_name,email,seminar_id)
email_set = @seminar.seminar_email_sets.select{|v| v.field_name == field_name}
if email_set.length==0
mail = Email.create(mail_to:[email],
module_app_key:"seminar",
template:"email/#{field_name}_email.html.erb",
mail_sentdate:Time.current,
mail_subject: t("seminar.email_#{field_name}_success"),
template_data:{'seminar_id'=>seminar_id,'locale'=>I18n.locale})
elsif !(email_set[0].disabled)
mail = Email.create(mail_to:[email],
module_app_key:"seminar",
template:"email/#{field_name}_email.html.erb",
mail_sentdate:Time.current,
mail_subject: email_set[0].title[I18n.locale],
template_data:{'seminar_id'=>seminar_id,'locale'=>I18n.locale})
end
begin
mail.deliver
2020-05-06 08:34:35 +00:00
rescue => e
2020-01-12 06:39:32 +00:00
puts ["email can't deliver",e]
end
end
end