class VenueManagementsController < ApplicationController
helper MemberHelper
include ActionView::Context #vary important (only add this can access @@session from view)
include Admin::VenueManagementsHelper
# include SimpleCaptcha::ControllerHelpers
def index
time_now = Time.now
venue_managements = VenueManagementMain.all.desc(:venue_management_start_date).filter_by_categories
{
"venue_managements" => venue_managements,
'time_now' => time_now,
"total_pages" => venue_managements.total_pages
}
end
def show_privacy
params = OrbitHelper.params
venue_management = VenueManagementMain.where(uid: params[:uid]).first
venue_management_agreement = VenueManagementAgreement.first
{
'venue_management' => venue_management,
"venue_management_agreement" => venue_management_agreement
}
end
def show_data
time_now = Time.now
params = OrbitHelper.params
venue_management = VenueManagementMain.find_by(uid: params[:uid])
if !venue_management.registration_status.blank? && venue_management.signup_start_date <= time_now && ( venue_management.signup_end_date.nil? || venue_management.signup_end_date+1 >= time_now )
sign_up = ('' + t('venue_management.signup') + '').html_safe
elsif venue_management.registration_status.blank?
sign_up = t('venue_management.sign_up_not_open')
elsif venue_management.signup_start_date > time_now
sign_up = t('venue_management.sign_up_not_yet')
else
sign_up = t('venue_management.sign_up_overdue')
end
if !venue_management.registration_status.blank? && venue_management.registration_status.include?('C') && venue_management.contribute_start_date <= time_now && ( venue_management.contribute_end_date.nil? || venue_management.contribute_end_date+1 >= time_now )
if !@@session[:venue_management_signup_id].blank?
if @@session[:venue_management_main_id] == venue_management.id
contribute_action = ('' + t('venue_management_signup.uploads') + '').html_safe
else
contribute_action = ('' + t('venue_management_signup.con_login') + '').html_safe
end
else
contribute_action = ('' + t('venue_management_signup.con_login') + '').html_safe
end
end
{
'venue_management' => venue_management,
'sign_up' => sign_up,
'contribute_action' => contribute_action,
'time_now' => time_now,
'contribute_time_range' => VenueManagementMain.time_range(venue_management.contribute_start_date, venue_management.contribute_end_date),
'sign_up_time_range' => VenueManagementMain.time_range(venue_management.signup_start_date, venue_management.signup_end_date),
'venue_management_time_range' => VenueManagementMain.time_range(venue_management.venue_management_start_date, venue_management.venue_management_end_date),
'params' => params
}
end
def show_inviting
params = OrbitHelper.params
venue_management_inviting = VenueManagementInviting.find(params[:uid]) rescue nil
{
'venue_management_inviting' => venue_management_inviting,
'params' => params
}
end
def show_contract
params = OrbitHelper.params
venue_management_contract = VenueManagementContract.find(params[:uid]) rescue nil
{
'venue_management_contract' => venue_management_contract,
'params' => params
}
end
def show
params = OrbitHelper.params
module_app = ModuleApp.where(:key => "venue_management").first
categories = module_app.categories
venue_management = VenueManagementMain.where(uid: params[:uid]).first
venue_management_signup = VenueManagementSignup.new
time_now = Time.now
{
"venue_management" => venue_management,
"venue_management_signup" => venue_management_signup,
"time_now" => time_now
}
end
def check_email
params = OrbitHelper.params
if !params[:email].blank?
count = VenueManagementSignup.where(:email => params[:email], :venue_management_main_id => params[:no]).count
result = count > 0 ? t('venue_management.already_used') : t('venue_management.available')
else
result = t('venue_management.please_enter_email')
end
{
"result" => result
}
end
def check_availability
params = OrbitHelper.params
response = {}
case params[:type]
when "venue_management_signup_email"
response["success"] = VenueManagementSignup.where(:email => params[:value], :venue_management_main_id => params[:no]).count > 0 ? false : true
end
render :json => response.to_json
end
def create
@signup = VenueManagementSignup.where(email: params[:venue_management_signup][:email], venue_management_main_id: params[:venue_management_signup][:venue_management_main_id] ).first
@venue_management_signup = VenueManagementSignup.new(venue_management_signup_params)
@venue_management = VenueManagementMain.where(id:params[:venue_management_signup][:venue_management_main_id]).first
if @venue_management.venue_management_signup_field_sets.count!=0
flag = @venue_management.venue_management_signup_field_sets.select{|v| v.field_name=='recaptcha'}[0].disabled
else
flag = false
end
if gotcha_valid? || flag
if @signup.blank? && @venue_management_signup.save
# if gotcha_valid? && @venue_management_signup.save
if !params[:venue_management_signup_values].nil?
venue_management_signup_values_params.each_with_index do |r,i|
field_value = r.last[:id].present?
@custom_field_value = VenueManagementSignupValue.put_field_values(@venue_management_signup, r.last, r.last[:venue_management_signup_field_id], field_value)
end
end
if params['venue_management_signup']['status']=='C'
status_param = '&status='
send_mail('submission',params[:venue_management_signup][:email],params[:venue_management_signup][:venue_management_main_id])
else
status_param = ''
send_mail('signup',params[:venue_management_signup][:email],params[:venue_management_signup][:venue_management_main_id])
end
redirect_to "#{params[:referer_url]}/?method=signup_ok#{status_param}"
else
if !@signup.blank?
redirect_to "#{params[:referer_url]}", :notice => 'mail已存在'
else
redirect_to "#{params[:referer_url]}", :notice => t('recaptcha.errors.verification_failed')
end
end
else
redirect_to "#{params[:referer_url]}", :notice => t('recaptcha.errors.verification_failed')
end
end
def signup_ok
end
def add_file_proc
update_params = venue_management_signup_contribute_params.merge('venue_management_submission_values'=>params['venue_management_submission_values'])
flag = true
update_params.permit!
if !update_params['venue_management_submission_values'].nil?
if update_params['venue_management_submission_values']['0']['value'].class==String
value = update_params['venue_management_submission_values']['0']['value']
if !value.to_s.blank?
flag = false
end
else
update_params['venue_management_submission_values']['0']['value'].each do |key,value|
if !value.to_s.blank?
flag = false
end
end
end
else
flag = false
end
if flag
update_params['venue_management_submission_values']['0']['value']['en'] = ' '
end
@con = VenueManagementSignupContribute.new(update_params)
if @con.save
signup = VenueManagementSignup.where(id:update_params['venue_management_signup_id']).first
venue_management_id = signup.venue_management_main_id
@venue_management = VenueManagementMain.where(id:venue_management_id).first
send_mail('add_file',signup.email,venue_management_id)
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 = VenueManagementSignupContribute.find(params[:venue_management_signup_contribute][:id])
update_params = venue_management_signup_contribute_params.merge('venue_management_submission_values'=>params['venue_management_submission_values'])
flag = true
update_params.permit!
if update_params['venue_management_submission_values']['0']['value'].class==String
value = update_params['venue_management_submission_values']['0']['value']
if !value.to_s.blank?
flag = false
end
else
update_params['venue_management_submission_values']['0']['value'].each do |key,value|
if !value.to_s.blank?
flag = false
end
end
end
if flag
update_params['venue_management_submission_values']['0']['value']['en'] = ' '
end
@con.update_attributes(update_params)
if @con.save
signup = VenueManagementSignup.where(id:update_params['venue_management_signup_id']).first
venue_management_id = signup.venue_management_main_id
@venue_management = VenueManagementMain.where(id:venue_management_id).first
send_mail('edit_file',signup.email,venue_management_id)
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 = VenueManagementSignupContribute.find(params[:con_no])
# venue_management_signup = VenueManagementSignup.find_by(_id: con.venue_management_signup_id )
# venue_management = VenueManagementMain.find_by(_id: venue_management_signup.venue_management_main_id )
con.destroy
redirect_to "#{params[:referer_url]}/?method=con_upload"
end
def add_file
if !@@session[:venue_management_main_id].blank?
venue_management = VenueManagementMain.find_by(id: @@session[:venue_management_main_id])
venue_management_signup = VenueManagementSignup.find_by(_id: @@session[:venue_management_signup_id], venue_management_main_id: @@session[:venue_management_main_id] )
else
venue_management = {}
venue_management_signup = {}
end
con = VenueManagementSignupContribute.new
{
'con' => con,
'venue_management' => venue_management,
'venue_management_signup' => venue_management_signup
}
end
def edit_file
params = OrbitHelper.params
if !@@session[:venue_management_main_id].blank?
venue_management = VenueManagementMain.find_by(id: @@session[:venue_management_main_id])
venue_management_signup = VenueManagementSignup.find_by(_id: @@session[:venue_management_signup_id], venue_management_main_id: @@session[:venue_management_main_id] )
else
venue_management = {}
venue_management_signup = {}
end
con = VenueManagementSignupContribute.find(params[:con_no])
{
'con' => con,
'venue_management' => venue_management,
'venue_management_signup' => venue_management_signup
}
end
def con_upload
time_now = Time.now
if !@@session[:venue_management_main_id].blank?
venue_management = VenueManagementMain.find_by(id: @@session[:venue_management_main_id])
venue_management_signup = VenueManagementSignup.find_by(_id: @@session[:venue_management_signup_id], venue_management_main_id: @@session[:venue_management_main_id] )
else
venue_management = {}
venue_management_signup = {}
end
{
'time_now' => time_now,
'venue_management' => venue_management,
'venue_management_signup' => venue_management_signup
}
end
def con_logout
# redirect_to page_for_venue_management_url
end
def con_login
time_now = Time.now
params = OrbitHelper.params
venue_management = VenueManagementMain.find_by(uid: params[:uid])
{
'venue_management' => venue_management,
'time_now' => time_now
}
end
def con_login_proc
venue_management = VenueManagementMain.find_by(id: params[:venue_management_signup][:venue_management_main_id])
@venue_management_signup = VenueManagementSignup.where(:status=>'C', :email=> params[:user_name], :password => params[:password], :venue_management_main_id => params[:venue_management_signup][:venue_management_main_id]).first
if !@venue_management_signup.blank?
session[:venue_management_signup_id] = @venue_management_signup.id
session[:venue_management_main_id] = @venue_management_signup.venue_management_main_id
redirect_to "#{params[:referer_url]}/?method=con_upload"
else
redirect_to "#{params[:referer_url]}/?method=con_login", :notice => '登入失敗'
end
end
def venue_management_signup_params
params.require(:venue_management_signup).permit!
end
def venue_management_signup_contribute_params
params.require(:venue_management_signup_contribute).permit!
end
def venue_management_signup_values_params
params.require(:venue_management_signup_values).permit!
end
def send_mail(field_name,email,venue_management_id)
email_set = @venue_management.venue_management_email_sets.select{|v| v.field_name == field_name}
if email_set.length==0
mail = Email.create(mail_to:[email],
module_app_key:"venue_management",
template:"email/#{field_name}_email.html.erb",
mail_sentdate:Time.current,
mail_subject: t("venue_management.email_#{field_name}_success"),
template_data:{'venue_management_id'=>venue_management_id,'locale'=>I18n.locale})
elsif !(email_set[0].disabled)
mail = Email.create(mail_to:[email],
module_app_key:"venue_management",
template:"email/#{field_name}_email.html.erb",
mail_sentdate:Time.current,
mail_subject: email_set[0].title[I18n.locale],
template_data:{'venue_management_id'=>venue_management_id,'locale'=>I18n.locale})
end
begin
mail.deliver
rescue => e
puts ["email can't deliver",e]
end
end
end