seminar2/app/controllers/admin/seminars_controller.rb

294 lines
9.3 KiB
Ruby
Raw Normal View History

2016-06-29 06:57:45 +00:00
# encoding: utf-8
class Admin::SeminarsController < OrbitAdminController
require 'axlsx'
helper Admin::SeminarsFieldHelper
2016-06-29 06:57:45 +00:00
before_action ->(module_app = @app_title) { set_variables module_app }
before_action :set_seminar, only: [:edit, :set_write_off, :seminar_signup, :destroy]
def initialize
super
@app_title = "seminar"
end
def filter_fields(categories)
{
:category=>categories.map{|c| {:title=>(c.title.blank? ? " " : c.title), :id=>c.id}}
}
end
def index
@categories = @module_app.categories.enabled
@filter_fields = filter_fields(@categories)
@table_fields = [:category, 'seminar.title', 'seminar.event_during', 'seminar.signup_during', 'seminar.signup_count', 'seminar.export']
if !params[:sort].blank?
if params[:sort] == 'event_during'
sort = {:seminar_start_date.to_sym=>params[:order]}
elsif params[:sort] == 'signup_during'
sort = {:signup_start_date.to_sym=>params[:order]}
else
sort = {params[:sort].to_sym=>params[:order]}
end
else
sort = {:signup_start_date=>"desc"}
end
@seminars = SeminarMain.all.order_by(sort).with_categories(filters("category"))
@seminars = search_data(@seminars,[:title]).page(params[:page]).per(10)
if request.xhr?
render :partial => "index"
end
end
def export
@seminar = SeminarMain.find(params[:id])
@seminar_signups = SeminarSignup.where(:seminar_main_id => params[:id])
filename = @seminar.title.parameterize.underscore + "_export.xlsx"
2016-06-29 06:57:45 +00:00
respond_to do |format|
format.xlsx {
response.headers['Content-Disposition'] = 'attachment; filename="' + filename + '"'
2016-06-29 06:57:45 +00:00
}
end
end
def new
2019-10-04 16:11:39 +00:00
@member = nil
2016-06-29 06:57:45 +00:00
@seminar = SeminarMain.new
create_set
@signup_sets = @seminar.seminar_signup_field_sets
@submission_set = @seminar.seminar_submission_field_sets
@custom_set = @seminar.seminar_signup_field_customs
2016-06-29 06:57:45 +00:00
@seminar_items = SeminarItem.all
2019-10-04 16:11:39 +00:00
@review = @seminar.seminar_reviews.new()
2016-06-29 06:57:45 +00:00
end
def create
if !seminar_params['seminar_links_attributes'].nil?
seminar_params['seminar_links_attributes'].each do |idx,link|
seminar_params['seminar_links_attributes'].delete(idx.to_s) if link['url'].blank?
end
end
seminar = SeminarMain.new(seminar_params)
seminar.create_user_id = current_user.id
seminar.update_user_id = current_user.id
seminar.save
redirect_to params['referer_url']
end
def edit
@seminar_items = SeminarItem.all
@seminar = SeminarMain.where(id: params[:id]).first
@seminar.seminar_signup_field_sets.each do |signup_field_set|
if !(SeminarSignup.attribute_names.include? signup_field_set.field_name) && signup_field_set.field_name != 'recaptcha'
signup_field_set.delete
end
end
@seminar.seminar_submission_field_sets.each do |submission_field_set|
if !(SeminarSignupContribute.attribute_names.include? submission_field_set.field_name)
submission_field_set.delete
end
end
@seminar.seminar_signup_field_customs.each do |custom_set|
if !(@seminar.seminar_signup_fields.collect(&:id).include? custom_set.seminar_signup_field_id)
custom_set.delete
end
end
create_set
@signup_sets = @seminar.seminar_signup_field_sets
@submission_set = @seminar.seminar_submission_field_sets
@custom_set = @seminar.seminar_signup_field_customs
if @seminar.seminar_reviews.first.nil?
@review = @seminar.seminar_reviews.new()
2019-10-04 16:11:39 +00:00
else
@review = @seminar.seminar_reviews.first
2019-10-04 16:11:39 +00:00
end
@member = MemberProfile.find(@review.reviewer_id) rescue nil
2016-06-29 06:57:45 +00:00
end
def update
params[:seminar_main][:registration_status] = (params[:seminar_main][:registration_status] ? params[:seminar_main][:registration_status] : [])
uid = params[:id].split('-').last
@seminar = SeminarMain.find_by(:uid=>uid)
if params[:seminar_main][:seminar_signup_fields]
seminar_params[:seminar_signup_fields].each do |a|
@field_name = 'seminar_main'
field_status = a.last[:id].present?
@attribute_field = SeminarSignupField.add_seminar_signup_field(@seminar, a.last, a.last[:id], field_status)
@attribute = @seminar
end
flash.now[:notice] = "Updated Fields"
@seminar.seminar_signup_fields.each{|t| t.destroy if t["to_delete"] == true}
redirect_to admin_seminars_path
elsif params[:seminar_main][:seminar_submission_fields]
params[:seminar_main][:seminar_submission_fields].each do |a|
@field_name = 'seminar_main'
field_status = a.last[:id].present?
@attribute_field = SeminarSubmissionField.add_seminar_signup_field(@seminar, a.last, a.last[:id], field_status)
@attribute = @seminar
end
flash.now[:notice] = "Updated Fields"
@seminar.seminar_submission_fields.each{|t| t.destroy if t["to_delete"] == true}
redirect_to admin_seminars_path
2016-06-29 06:57:45 +00:00
else
if !seminar_params['seminar_links_attributes'].nil?
seminar_params['seminar_links_attributes'].each do |idx,link|
seminar_params['seminar_links_attributes'].delete(idx.to_s) if link['url'].blank?
end
end
@seminar.update_user_id = current_user.id
if @seminar.update_attributes(seminar_params)
@seminar.seminar_signup_fields.each{|t| t.destroy if t["to_delete"] == true}
redirect_to params['referer_url']
else
flash.now[:error] = t('update.error.category')
render action: :edit
end
end
end
def set_write_off
@seminar.donation_write_off_status = true
@seminar.save
redirect_to "/admin/seminars?page=#{params['page']}"
end
def destroy
@seminar.destroy
redirect_to "/admin/seminars"
end
def seminar_signup_field
@field_name = 'seminar_main'
@attribute = SeminarMain.find(params[:id])
@attribute_type = 'seminar'
@class = 'seminars'
end
def seminar_submission_field
@field_name = 'seminar_main'
@attribute = SeminarMain.find(params[:id])
@attribute_type = 'seminar'
@class = 'seminars'
end
2016-06-29 06:57:45 +00:00
def seminar_signup
if !params[:search].blank?
@seminar_signups = SeminarSignup.where(:seminar_main_id => @seminar.id, :name=>params[:search]).page(params[:page]).per(10)
else
@seminar_signups = SeminarSignup.where(:seminar_main_id => @seminar.id).page(params[:page]).per(10)
end
end
def edit_seminar_signup
@seminar_signup = SeminarSignup.find(params[:id])
@seminar = SeminarMain.find(@seminar_signup.seminar_main_id)
end
def delete_seminar_signup
@seminar_signup = SeminarSignup.find(params[:id])
@seminar_id = @seminar_signup.seminar_main_id
@seminar_signup.destroy
redirect_to "/admin/seminars/@seminar_id.to_s/seminar_signup"
end
private
def set_seminar
@seminar = SeminarMain.find(params[:id])
end
def seminar_params
params.require(:seminar_main).permit!
end
def create_set
SeminarSignup.attribute_names.each do |attr_signup|
if !(['_id', 'created_at', 'updated_at','seminar_main_id'].include? attr_signup)
signup_set = @seminar.seminar_signup_field_sets.select{|v| v.field_name==attr_signup }
if signup_set.length==0
if ['status','name','tel','phone','email','password'].include? attr_signup
disabled = false
else
disabled = true
end
if ['status','name','tel'].include? attr_signup
hidden = false
else
hidden = true
end
name1 = Hash.new
now_locale = I18n.locale
I18n.available_locales.each do |locale|
I18n.locale = locale
name1[locale] = t("seminar_signup.#{attr_signup}")
end
I18n.locale = now_locale
@seminar.seminar_signup_field_sets.create(field_name:attr_signup,disabled:disabled,hidden:hidden,name:name1,placeholder:name1)
end
end
end
signup_set = @seminar.seminar_signup_field_sets.select{|v| v.field_name== 'recaptcha' }
if signup_set.length==0
name1 = Hash.new
now_locale = I18n.locale
I18n.available_locales.each do |locale|
I18n.locale = locale
name1[locale] = t("seminar_signup.recaptcha")
end
I18n.locale = now_locale
@seminar.seminar_signup_field_sets.create(field_name:'recaptcha',name:name1,placeholder:name1,hidden:true)
end
SeminarSignupContribute.attribute_names.each do |attr_submission|
if !(['_id', 'created_at', 'updated_at','seminar_signup_id'].include? attr_submission)
signup_set = @seminar.seminar_submission_field_sets.select{|v| v.field_name==attr_submission }
if signup_set.length==0
name1 = Hash.new
now_locale = I18n.locale
I18n.available_locales.each do |locale|
I18n.locale = locale
name1[locale] = t("seminar_signup.#{attr_submission}")
end
I18n.locale = now_locale
@seminar.seminar_submission_field_sets.create(field_name:attr_submission,name:name1,placeholder:name1)
end
end
end
@seminar.seminar_signup_fields.all.map{|v| v}.each do |field|
set = @seminar.seminar_signup_field_customs.select{|v| v.seminar_signup_field_id==field.id}
if set.length == 0
@seminar.seminar_signup_field_customs.create(seminar_signup_field_id:field.id)
end
end
end
2016-06-29 06:57:45 +00:00
end