seminar/app/controllers/admin/seminars_controller.rb

545 lines
21 KiB
Ruby

# encoding: utf-8
class Admin::SeminarsController < OrbitAdminController
require 'axlsx'
include Admin::SeminarsHelper
helper Admin::SeminarsFieldHelper
before_action ->(module_app = @app_title) { set_variables module_app }
before_action :set_seminar, only: [:edit, :set_write_off, :seminar_signup, :destroy,:seminar_signup_admin_setting,:update_seminar_signup_admin_setting,:get_reviewer_block,:update_seminar_review,:reviewer_setting,:update_reviewer_setting]
before_action :set_seminar_signup_admin_setting, only: [:seminar_signup,:seminar_signup_admin_setting,:update_seminar_signup_admin_setting,:get_reviewer_block]
before_action :check_permission_for_seminar, only: [:seminar_signup,:get_reviewer_block]
before_action :check_manager_for_seminar, only: [:seminar_signup_admin_setting,:update_seminar_signup_admin_setting,:update_seminar_review]
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 check_manager_for_seminar
OrbitHelper.set_params(params,current_user)
access_level = OrbitHelper.user_access_level?
if (access_level.nil? || access_level == "user") && (@seminar.organizer_id != current_user.member_profile_id rescue true)
render_401
end
end
def check_permission_for_seminar
OrbitHelper.set_params(params,current_user)
access_level = OrbitHelper.user_access_level?
if (access_level.nil? || access_level == "user")
unless (@seminar.reviewer_ids.include?(current_user.member_profile_id.to_s) rescue false) || (@seminar.organizer_id == current_user.member_profile_id rescue false)
render_401
else
@user_authenticated_categories = ['all']
end
end
end
def reviewer_setting
@seminar_reviews = @seminar.seminar_reviews
@fields = []
@fields_translations = []
access_markup = ["select","radio_button","checkbox"]
@fields_values = []
@seminar.seminar_signup_fields.each do |f|
next if f.disabled
if access_markup.include?(f.markup)
@fields << "seminar_signup_fields.#{f.id}"
@fields_translations << f.title
@fields_values << f.option_list
end
end
@seminar.seminar_submission_fields.each do |f|
next if f.disabled
if access_markup.include?(f.markup)
@fields << "seminar_submission_fields.#{f.id}"
@fields_translations << f.title
@fields_values << f.option_list
end
end
@fields_values = @fields_values.collect.with_index{|fields_values,i| [i, fields_values.values.map{|v| v[I18n.locale]} ] }.to_h
end
def update_reviewer_setting
seminar_params[:seminar_reviews_attributes].keys.each do |i|
if seminar_params[:seminar_reviews_attributes][i][:default_topics].present?
seminar_params[:seminar_reviews_attributes][i][:default_topics] = [] if seminar_params[:seminar_reviews_attributes][i][:default_topics].nil?
default_topics_values = []
(0...seminar_params[:seminar_reviews_attributes][i][:default_topics].count).each do |ii|
default_topics_values << (Array(seminar_params[:seminar_reviews_attributes][i][:default_topics_values][ii.to_s]) rescue [])
end
seminar_params[:seminar_reviews_attributes][i][:default_topics_values] = default_topics_values
else
seminar_params[:seminar_reviews_attributes][i][:default_topics] = []
seminar_params[:seminar_reviews_attributes][i][:default_topics_values] = []
end
end
#render :html => seminar_params and return
@seminar.update_attributes(seminar_params)
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
redirect_to admin_seminars_path
end
def enquiry_for_applicants
OrbitHelper.set_params(params,current_user)
@access_level = OrbitHelper.user_access_level?
@can_edit = true
if @access_level == "user" || @access_level.nil?
@can_edit = false
end
reviewer_id = current_user.member_profile_id.to_s
if !@can_edit && SeminarMain.where(:reviewer_ids=>reviewer_id).count == 0 && SeminarMain.where(:organizer_id=>reviewer_id).count == 0
render_401 and return
end
@seminar_signup_admin_setting = SeminarSignupAdminSetting.where(:seminar_main_id=>"enquiry_for_applicants").first
if @seminar_signup_admin_setting.nil?
@seminar_signup_admin_setting = SeminarSignupAdminSetting.create(:seminar_main_id=>"enquiry_for_applicants")
end
if params[:name].present?
@seminar_signups = SeminarSignup.where(:name => /#{params[:name]}/).page(params[:page]).per(10)
else
@seminar_signups = []
end
unless @can_edit
if @seminar_signups.class != Array
seminar_signup_ids = SeminarReview.where(:reviewer_id=>reviewer_id).pluck(:seminar_signup_ids).flatten
remove_seminar_signup_ids = SeminarReview.where(:reviewer_id=>reviewer_id).pluck(:remove_seminar_signup_ids).flatten
default_seminar_signup_ids = SeminarReview.where(:reviewer_id=>reviewer_id).pluck(:default_seminar_signup_ids).flatten
all_seminar_signup_ids = seminar_signup_ids - remove_seminar_signup_ids + default_seminar_signup_ids
ids2 = SeminarSignup.where(:seminar_main_id.in=> SeminarMain.where(:organizer_id=>current_user.member_profile_id).pluck(:id)).pluck(:id)
@seminar_signups = @seminar_signups.where(:id.in=>all_seminar_signup_ids + ids2).page(params[:page]).per(10)
end
end
end
def get_hide_reviewer
seminar_main = SeminarMain.find(params[:id]) rescue nil
if seminar_main.nil?
render :nothing => true
else
@hide_reviewer_ids = Array(seminar_main.reviewer_ids) - Array(seminar_main.reviewer_show_ids)
@hide_reviewers = MemberProfile.where(:id.in=>@hide_reviewer_ids)
render :layout => false
end
end
def get_reviewer_block
@reviewer_id = params[:reviewer_id]
unless (@seminar.reviewer_show_ids.include?(@reviewer_id) rescue false)
@seminar.reviewer_show_ids << @reviewer_id
@seminar.save
end
render :layout => false
end
def update_seminar_review
if @seminar_review.nil?
render :json => {:success => false}
else
seminar_main = @seminar_review.seminar_main
if params[:mode] == "append"
@seminar_review.seminar_signup_ids << params[:signup_id]
@seminar_review.remove_seminar_signup_ids.delete(params[:signup_id])
seminar_main.unassigned_seminar_signup_ids.delete(params[:signup_id])
elsif params[:mode] == "remove"
@seminar_review.seminar_signup_ids.delete(params[:signup_id])
@seminar_review.remove_seminar_signup_ids << params[:signup_id]
seminar_main.unassigned_seminar_signup_ids << params[:signup_id]
elsif params[:mode] == "hide"
if seminar_main.reviewer_show_ids.nil?
seminar_main.reviewer_show_ids = seminar_main.reviewer_ids[0..1]
end
seminar_main.reviewer_show_ids = seminar_main.reviewer_show_ids - Array(@seminar_review.reviewer_id).flatten
elsif params[:mode] == "show"
seminar_main.reviewer_show_ids = seminar_main.reviewer_show_ids.to_a + Array(@seminar_review.reviewer_id).flatten
else
render :json => {:success => false} and return
end
seminar_main.save
@seminar_review.save
render :json => {:success => true}
end
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)
OrbitHelper.set_params(params,current_user)
@access_level = OrbitHelper.user_access_level?
@can_edit = true
if @access_level == "user" || @access_level.nil?
@can_edit = false
end
unless @can_edit
reviewer_id = current_user.member_profile_id.to_s
ids1 = @seminars.where(:reviewer_ids=>reviewer_id).pluck(:id)
ids2 = @seminars.where(:organizer_id=>current_user.member_profile_id).pluck(:id)
@seminars = @seminars.where(:id.in=>ids1+ids2)
end
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"
if can_see_seminar_signup(@seminar)
if !admin_for_seminar(@seminar)
@seminar_review = @seminar.seminar_reviews.where(:reviewer_id => current_user.member_profile_id.to_s).first
@seminar_signups = @seminar_signups.where(:id.in=>@seminar_review.all_seminar_signup_ids) rescue []
end
respond_to do |format|
format.xlsx {
response.headers['Content-Disposition'] = 'attachment; filename="' + filename + '"'
}
end
else
render :nothing => true
end
end
def new
@member = nil
@seminar = SeminarMain.new
create_set (false)
@signup_sets = @seminar.seminar_signup_field_sets
@submission_set = @seminar.seminar_submission_field_sets
@custom_set = @seminar.seminar_signup_field_customs
@seminar_items = SeminarItem.all
@review = @seminar.seminar_reviews.new()
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_main_params = seminar_params
seminar_signup_set_params = seminar_main_params['seminar_signup_field_sets']
seminar_submission_set_params = seminar_main_params['seminar_submission_field_sets']
seminar_email_sets_params = seminar_main_params['seminar_email_sets']
seminar_main_params.delete(:seminar_signup_field_sets)
seminar_main_params.delete(:seminar_submission_field_sets)
seminar_main_params.delete(:seminar_email_sets)
seminar = SeminarMain.new(seminar_main_params)
seminar.create_user_id = current_user.id
seminar.update_user_id = current_user.id
seminar.save
seminar_signup_set_params.each do |key,value|
seminar.seminar_signup_field_sets.create(value)
end
seminar_submission_set_params.each do |key,value|
seminar.seminar_submission_field_sets.create(value)
end
seminar_email_sets_params.each do |key,value|
seminar.seminar_email_sets.create(value)
end
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 (true)
@signup_sets = @seminar.seminar_signup_field_sets
@submission_set = @seminar.seminar_submission_field_sets
@custom_set = @seminar.seminar_signup_field_customs
@member = MemberProfile.where(:id.in=>@seminar.reviewer_ids).to_a rescue []
if (@seminar.organizer_id == current_user.member_profile_id rescue false)
@user_authorized_categories = @module_app.categories.enabled.to_a
else
@user_authorized_categories = nil
end
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)
seminar_params["reviewer_ids"] = [] if seminar_params["reviewer_ids"].nil?
seminar_params["organizer_id"] = nil if seminar_params["organizer_id"].nil?
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
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
def seminar_signup
@seminar_signups = []
if params[:type] == "table"
@seminar_signups = @seminar.seminar_signups.where(:name=>/#{params[:search]}/)
else
@seminar_signups = SeminarSignup.where(:id.in => @seminar.unassigned_seminar_signup_ids, :name=>/#{params[:search]}/)
end
OrbitHelper.set_params(params,current_user)
@access_level = OrbitHelper.user_access_level?
@can_edit = true
if @access_level == "user" || @access_level.nil?
@can_edit = false
end
unless @can_edit
@can_edit = (@seminar.organizer_id == current_user.member_profile_id rescue false)
end
unless @can_edit
@seminar_review = @seminar.seminar_reviews.where(:reviewer_id => current_user.member_profile_id.to_s).first
@seminar_signups = @seminar.seminar_signups.where(:id.in=>@seminar_review.all_seminar_signup_ids)
end
@seminar_signups = @seminar_signups.page(params[:page]).per(10)
if request.xhr?
render :layout => false
end
end
def seminar_signup_admin_setting
if @seminar.nil? && params[:id] != "enquiry_for_applicants"
redirect_to admin_seminars_path
end
end
def update_seminar_signup_admin_setting
@seminar_signup_admin_setting.update_attributes(params.require(:seminar_signup_admin_setting).permit!)
if params[:id] != "enquiry_for_applicants"
redirect_to seminar_signup_admin_seminar_path(params[:id])
else
redirect_to admin_seminar_enquiry_for_applicants_path
end
end
def set_seminar_signup_admin_setting
@seminar_signup_admin_setting = SeminarSignupAdminSetting.where(:seminar_main_id=>params[:id]).first
if @seminar_signup_admin_setting.nil?
@seminar_signup_admin_setting = SeminarSignupAdminSetting.create(:seminar_main_id=>params[:id])
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]) rescue nil
if params[:action] == "update_seminar_review"
@seminar_review = SeminarReview.find(params[:id]) rescue nil
@seminar = @seminar_review.seminar_main
end
end
def seminar_params
params.require(:seminar_main).permit!
end
def create_set (save_flag)
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
if save_flag
@seminar.seminar_signup_field_sets.create(field_name:attr_signup,disabled:disabled,hidden:hidden,name:name1,placeholder:name1)
else
@seminar.seminar_signup_field_sets.new(field_name:attr_signup,disabled:disabled,hidden:hidden,name:name1,placeholder:name1)
end
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
if save_flag
@seminar.seminar_signup_field_sets.create(field_name:'recaptcha',name:name1,placeholder:name1,hidden:true)
else
@seminar.seminar_signup_field_sets.new(field_name:'recaptcha',name:name1,placeholder:name1,hidden:true)
end
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
if save_flag
@seminar.seminar_submission_field_sets.create(field_name:attr_submission,name:name1,placeholder:name1)
else
@seminar.seminar_submission_field_sets.new(field_name:attr_submission,name:name1,placeholder:name1)
end
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
@email_set = []
['signup','submission','add_file','edit_file'].each do |field_name|
email_set = @seminar.seminar_email_sets.select{|v| v.field_name==field_name}
if email_set.length==0
title = Hash.new
content = Hash.new
now_locale = I18n.locale
I18n.available_locales.each do |locale|
I18n.locale = locale
title[locale] = t("seminar.email_#{field_name}_success")
content[locale] = t("seminar.email_#{field_name}_content")
end
I18n.locale = now_locale
if save_flag
email_set = @seminar.seminar_email_sets.create(field_name:field_name,title:title,content:content)
else
email_set = @seminar.seminar_email_sets.new(field_name:field_name,title:title,content:content)
end
else
email_set = email_set[0]
end
@email_set << email_set
end
end
end