2016-06-29 06:57:45 +00:00
|
|
|
# encoding: utf-8
|
2022-03-10 02:11:18 +00:00
|
|
|
class ApplicationFormSignup
|
2022-06-14 13:19:52 +00:00
|
|
|
require 'openssl/cipher'
|
2016-06-29 06:57:45 +00:00
|
|
|
include Mongoid::Document
|
|
|
|
include Mongoid::Timestamps
|
2022-08-13 03:35:29 +00:00
|
|
|
HiddenFields = ['ip', 'application_form_signup_id', 'password', 'status', 'reviewer_ids_list','_id', 'created_at', 'updated_at','application_form_main_id',"serial_number","final_session","final_sessions","preferred_sessions",'application_form_session_id',"application_form_session_ids","preferred_session","sort_number","abstract_number","presentation_type", "cipher_stage_num", "process_stage", "review_status", "agree_reviewer_ids", "agree_reviewer_times"]
|
2022-06-14 13:19:52 +00:00
|
|
|
DefaultEnableFields = ['name','tel','phone','email', 'recaptcha']
|
2022-08-13 03:35:29 +00:00
|
|
|
field :ip, type: String
|
2021-04-24 10:41:50 +00:00
|
|
|
field :sort_number , type: Integer, default: 10000
|
2022-06-14 13:19:52 +00:00
|
|
|
field :process_stage, type: Integer, default: 1
|
|
|
|
field :review_status, type: Integer, default: 0 # 0 => 審核中, 1 => 不通過, 2=> 申請通過
|
|
|
|
field :cipher_stage_num, type: Array, default: []
|
2016-06-29 06:57:45 +00:00
|
|
|
field :status
|
2019-11-26 15:52:52 +00:00
|
|
|
field :name # become Last Name for TICC
|
|
|
|
field :tel, type: String # become First Name for TICC
|
2022-08-13 03:35:29 +00:00
|
|
|
field :unit, localize: true #Only localize for preserving old record
|
2016-06-29 06:57:45 +00:00
|
|
|
field :phone, type: String
|
|
|
|
field :fax, type: String
|
|
|
|
field :email, type: String
|
|
|
|
field :address, localize: true
|
|
|
|
field :password
|
|
|
|
field :note, localize: true
|
2021-03-29 14:27:08 +00:00
|
|
|
field :serial_number
|
2021-04-24 10:41:50 +00:00
|
|
|
field :final_session #only store old data , not used
|
|
|
|
field :final_sessions
|
|
|
|
field :preferred_sessions, type: Array, default: []
|
2022-05-21 13:48:39 +00:00
|
|
|
field :reviewer_ids_list, type: Array, default: []
|
2022-06-26 08:16:18 +00:00
|
|
|
field :agree_reviewer_ids, type: Hash, default: {}
|
2022-08-13 03:35:29 +00:00
|
|
|
field :agree_reviewer_times, type: Hash, default: {}
|
2022-05-21 13:48:39 +00:00
|
|
|
def reviewer_ids_list
|
|
|
|
main = self.application_form_main
|
|
|
|
main.reviewer_enables.each_with_index.collect do |e,k|
|
|
|
|
if e
|
|
|
|
if main.reviewer_type_list[k]=='fixed'
|
|
|
|
main.reviewer_ids_list[k]
|
|
|
|
else
|
|
|
|
super()[k]
|
|
|
|
end
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-03-10 02:11:18 +00:00
|
|
|
belongs_to :application_form_main
|
|
|
|
field :application_form_session_id #only store old data , not used
|
|
|
|
field :application_form_session_ids, type: Array, default: []
|
|
|
|
has_many :application_form_signup_values, :autosave => true, :dependent => :destroy
|
|
|
|
has_many :application_form_signup_contributes, :autosave => true, :dependent => :destroy
|
2016-06-29 06:57:45 +00:00
|
|
|
|
2022-03-10 02:11:18 +00:00
|
|
|
accepts_nested_attributes_for :application_form_signup_values, allow_destroy: true
|
|
|
|
accepts_nested_attributes_for :application_form_signup_contributes, allow_destroy: true
|
2021-04-24 10:41:50 +00:00
|
|
|
scope :sort_ordered, ->{ order_by(:sort_number=>1,:created_at=>1) }
|
2021-03-29 14:27:08 +00:00
|
|
|
|
2021-03-24 13:06:49 +00:00
|
|
|
before_create do
|
|
|
|
unit = self.unit_translations.values.select{|v| v.present?}.first
|
2022-08-13 03:35:29 +00:00
|
|
|
tmp_unit_translations = self.unit_translations
|
|
|
|
self.unit_translations = I18n.available_locales.map do |l|
|
|
|
|
l = l.to_s
|
|
|
|
tmp = tmp_unit_translations[l]
|
|
|
|
[l, (tmp ? tmp : unit)]
|
|
|
|
end.to_h
|
2022-06-14 13:19:52 +00:00
|
|
|
self.random_cipher(false)
|
2022-06-26 08:16:18 +00:00
|
|
|
if self.application_form_main
|
|
|
|
enable_idx = self.application_form_main.reviewer_enables.index(true)
|
|
|
|
self.process_stage = enable_idx.nil? ? 0 : (enable_idx + 1)
|
|
|
|
end
|
2021-04-03 16:23:52 +00:00
|
|
|
if self.serial_number.nil?
|
2022-06-14 13:19:52 +00:00
|
|
|
s = ApplicationFormSetting.first
|
2021-04-03 16:23:52 +00:00
|
|
|
s.last_serial_number += 1
|
|
|
|
self.serial_number = s.last_serial_number
|
|
|
|
s.save(:validate=>false)
|
|
|
|
end
|
2021-03-24 13:06:49 +00:00
|
|
|
end
|
2021-03-17 07:33:31 +00:00
|
|
|
after_destroy do
|
2022-03-10 02:11:18 +00:00
|
|
|
application_form_main = self.application_form_main
|
|
|
|
if application_form_main.present?
|
|
|
|
application_form_reviews = application_form_main.application_form_reviews
|
2021-04-03 16:23:52 +00:00
|
|
|
id = self.id.to_s
|
2022-03-10 02:11:18 +00:00
|
|
|
application_form_reviews.each do |sr|
|
|
|
|
if sr.all_application_form_signup_ids.include?(id)
|
|
|
|
sr.application_form_signup_ids.delete(id)
|
|
|
|
sr.remove_application_form_signup_ids.delete(id)
|
|
|
|
sr.default_application_form_signup_ids.delete(id)
|
2021-04-03 16:23:52 +00:00
|
|
|
sr.save
|
|
|
|
end
|
|
|
|
end
|
2022-03-10 02:11:18 +00:00
|
|
|
if application_form_main.unassigned_application_form_signup_ids.delete(id)
|
|
|
|
application_form_main.save
|
2021-03-17 07:33:31 +00:00
|
|
|
end
|
|
|
|
end
|
2022-03-10 02:11:18 +00:00
|
|
|
application_form_session = ApplicationFormSession.where(:id=>self.application_form_session_id).first
|
|
|
|
if application_form_session
|
|
|
|
application_form_session.application_form_signup_ids.delete(self.id.to_s)
|
|
|
|
application_form_session.save
|
2021-03-17 07:33:31 +00:00
|
|
|
end
|
|
|
|
end
|
2022-06-14 13:19:52 +00:00
|
|
|
def clean_cipher
|
|
|
|
self.update(:cipher_stage_num => [])
|
|
|
|
end
|
|
|
|
def random_cipher(save_flag=true)
|
|
|
|
cipher = OpenSSL::Cipher::AES.new(128, :CBC)
|
|
|
|
cipher.encrypt
|
|
|
|
key = cipher.random_key
|
|
|
|
iv = cipher.random_iv
|
|
|
|
(1..5).each do |i|
|
|
|
|
encrypted = cipher.update(i.to_s) + cipher.final
|
|
|
|
self.cipher_stage_num << Base64.encode64(encrypted).strip()
|
|
|
|
end
|
|
|
|
self.save if save_flag
|
|
|
|
end
|
|
|
|
def update_process_stage(new_stage) #stage start from 0
|
|
|
|
(0..new_stage - 1).each{|i| self.cipher_stage_num[i] = nil}
|
|
|
|
new_stage = new_stage + 1
|
|
|
|
self.process_stage = new_stage
|
|
|
|
self.save
|
|
|
|
end
|
2022-08-13 03:35:29 +00:00
|
|
|
def procedure(agree_link=nil)
|
|
|
|
stage_trans = I18n.t('application_form.stage')
|
|
|
|
text = ""
|
|
|
|
main = self.application_form_main
|
|
|
|
tmp_process_idx = self.process_stage - 1
|
|
|
|
if main
|
|
|
|
main.reviewer_enables.each_with_index do |t, i|
|
|
|
|
if t
|
|
|
|
text += "<span>#{I18n.t("application_form.num.#{i + 1}")}#{stage_trans}: "
|
|
|
|
if main.reviewer_type_list[i]=='fixed'
|
|
|
|
member_ids = main.reviewer_ids_list[i]
|
|
|
|
is_fixed = true
|
|
|
|
else
|
|
|
|
member_ids = self[:reviewer_ids_list][i] rescue []
|
|
|
|
is_fixed = false
|
|
|
|
end
|
|
|
|
if member_ids.present?
|
|
|
|
text += member_ids.map{|id| MemberProfile.find(id).name rescue nil}.compact.join(', ')
|
|
|
|
if is_fixed
|
|
|
|
text += " (#{I18n.t('application_form.fixed')})"
|
|
|
|
else
|
|
|
|
text += " (#{I18n.t('application_form.defined_by_applicant')})"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if i == tmp_process_idx && self.review_status == 0 && agree_link
|
|
|
|
text += "<a style=\"margin-left: 0.5em;\" class=\"btn btn-success agree_link\" href=\"#{agree_link}&type=agree\">#{I18n.t('application_form.link_choice.agree')}</a>"
|
|
|
|
text += "<a style=\"margin-left: 0.5em;\" class=\"btn btn-danger disagree_link\" href=\"#{agree_link}&type=disagree\">#{I18n.t('application_form.link_choice.disagree')}</a>"
|
|
|
|
end
|
|
|
|
text += "</span><br>"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
text
|
|
|
|
end
|
|
|
|
def review_progress(with_details=true, include_time=false)
|
2022-06-14 13:19:52 +00:00
|
|
|
stage_trans = I18n.t('application_form.stage')
|
|
|
|
max_stage = @max_stage
|
|
|
|
if max_stage.nil?
|
|
|
|
max_stage = self.application_form_main.get_max_stage
|
|
|
|
end
|
|
|
|
max_stage += 1
|
2022-06-26 09:01:35 +00:00
|
|
|
text = "#{I18n.t('application_form.num.'+self.process_stage.to_s)}#{stage_trans} / #{I18n.t('application_form.num.'+max_stage.to_s)}#{stage_trans} : #{I18n.t('application_form.status.'+review_status.to_s)}"
|
|
|
|
if with_details
|
|
|
|
text += "<br>"
|
2022-08-13 03:35:29 +00:00
|
|
|
text += review_details(include_time)
|
2022-06-26 09:01:35 +00:00
|
|
|
end
|
|
|
|
text
|
|
|
|
end
|
2022-08-13 03:35:29 +00:00
|
|
|
def review_details(include_time=true)
|
2022-06-26 09:01:35 +00:00
|
|
|
text = ""
|
|
|
|
stage_trans = I18n.t('application_form.stage')
|
2022-08-13 03:35:29 +00:00
|
|
|
last_is_disagree = (self.review_status == 1)
|
|
|
|
last_idx = (self.process_stage - 1).to_s
|
2022-06-26 09:01:35 +00:00
|
|
|
self.agree_reviewer_ids.each do |i, member_id|
|
|
|
|
m = MemberProfile.find(member_id) rescue nil
|
2022-08-13 03:35:29 +00:00
|
|
|
t = agree_reviewer_times[i]
|
2022-06-26 09:01:35 +00:00
|
|
|
if m
|
2022-08-13 03:35:29 +00:00
|
|
|
text += "#{I18n.t('application_form.num.'+(i.to_i+1).to_s)}#{stage_trans}: #{m.tmp_name} "
|
|
|
|
if i == last_idx && last_is_disagree
|
|
|
|
text += I18n.t('application_form.link_choice.disagree')
|
|
|
|
else
|
|
|
|
text += I18n.t('application_form.link_choice.agree')
|
|
|
|
end
|
|
|
|
if include_time && t
|
|
|
|
text += " (#{t.to_time.strftime("%Y/%m/%d %H:%M")})"
|
|
|
|
end
|
|
|
|
text += "<br>"
|
2022-06-26 09:01:35 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
text
|
2022-06-14 13:19:52 +00:00
|
|
|
end
|
2022-06-26 08:16:18 +00:00
|
|
|
def get_reviewer_email(stage=self.process_stage, override_reviewer_ids_list=nil, only_email=false)
|
2022-06-14 13:19:52 +00:00
|
|
|
stage = stage - 1
|
|
|
|
if override_reviewer_ids_list.nil?
|
|
|
|
override_reviewer_ids_list = self.reviewer_ids_list
|
|
|
|
end
|
|
|
|
member_ids = override_reviewer_ids_list[stage]
|
2022-06-26 08:16:18 +00:00
|
|
|
if(only_email)
|
|
|
|
return MemberProfile.where(:id.in=>member_ids).pluck(:email).select{|s| s.present?}.uniq
|
|
|
|
else
|
|
|
|
return MemberProfile.where(:id.in=>member_ids).pluck(:id,:email).select{|id,s| s.present?}.uniq{|id,s| s}
|
|
|
|
end
|
2022-06-14 13:19:52 +00:00
|
|
|
end
|
|
|
|
def get_cipher_stage(stage)
|
|
|
|
self.cipher_stage_num[stage - 1]
|
|
|
|
end
|
|
|
|
def get_stage_num(cipher)
|
|
|
|
return self.cipher_stage_num.index(cipher)
|
|
|
|
end
|
2021-03-29 14:27:08 +00:00
|
|
|
def display_serial_number
|
|
|
|
display_format_string(self.serial_number,4)
|
|
|
|
end
|
|
|
|
def display_format_string(num, str_length)
|
|
|
|
return format("%0#{str_length}d", num)
|
|
|
|
end
|
2021-04-24 10:41:50 +00:00
|
|
|
def set_preferred_sessions
|
2022-03-10 02:11:18 +00:00
|
|
|
self.preferred_sessions = self.application_form_signup_contributes.pluck(:preferred_session)
|
2021-04-24 10:41:50 +00:00
|
|
|
self.save
|
|
|
|
end
|
2021-11-18 09:59:20 +00:00
|
|
|
def get_display_fields
|
2022-03-10 02:11:18 +00:00
|
|
|
application_form = self.application_form_main
|
|
|
|
if application_form.nil?
|
2021-11-18 09:59:20 +00:00
|
|
|
return []
|
|
|
|
else
|
|
|
|
locale = I18n.locale.to_s
|
|
|
|
except_fields = ["password", "recaptcha"]
|
|
|
|
all_fields = []
|
2022-03-10 02:11:18 +00:00
|
|
|
if application_form.application_form_signup_field_sets.count != 0
|
|
|
|
all_fields = application_form.application_form_signup_field_sets.map do |set|
|
2021-11-18 09:59:20 +00:00
|
|
|
field_name = set.field_name
|
|
|
|
next if set.disabled || except_fields.include?(field_name)
|
|
|
|
value = self.send(field_name) rescue nil
|
2022-08-13 03:35:29 +00:00
|
|
|
localize = self.fields[field_name].options[:localize]
|
|
|
|
next if value.nil? && !localize
|
2021-11-18 09:59:20 +00:00
|
|
|
if field_name == "status"
|
2022-03-10 02:11:18 +00:00
|
|
|
value = I18n.t("application_form.registration_status_#{value}")
|
2022-08-13 03:35:29 +00:00
|
|
|
elsif localize && field_name != "unit"
|
2021-11-18 09:59:20 +00:00
|
|
|
values = self.send("#{field_name}_translations").select{|k,v| v.present?}
|
|
|
|
value = ""
|
|
|
|
if values.count == 0
|
|
|
|
next
|
|
|
|
elsif values.count == 1
|
|
|
|
value = values.values.first
|
|
|
|
else
|
|
|
|
last_idx = values.count - 1
|
|
|
|
values.each_with_index do |(l, v), i|
|
|
|
|
value += (I18n.t(l) + ": " + v )
|
|
|
|
if i != last_idx
|
|
|
|
value += "<br>"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
{
|
|
|
|
"field_name"=>field_name,
|
|
|
|
"title"=>set.name[locale],
|
|
|
|
"value"=> value
|
|
|
|
}
|
|
|
|
end.compact
|
|
|
|
else
|
|
|
|
field_names = self.class::DefaultEnableFields
|
|
|
|
all_fields = field_names.map do |field_name|
|
|
|
|
next if except_fields.include?(field_name)
|
|
|
|
value = self.send(field_name) rescue nil
|
|
|
|
next if value.nil?
|
2022-03-10 02:11:18 +00:00
|
|
|
field_name_trans = I18n.t("application_form_signup.#{field_name}")
|
2021-11-18 09:59:20 +00:00
|
|
|
if field_name == "status"
|
2022-03-10 02:11:18 +00:00
|
|
|
I18n.t("application_form.registration_status_#{value}")
|
2021-11-18 09:59:20 +00:00
|
|
|
elsif self.fields[field_name].options[:localize]
|
|
|
|
values = self.send("#{field_name}_translations").select{|k,v| v.present?}
|
|
|
|
value = ""
|
|
|
|
if values.count == 0
|
|
|
|
next
|
|
|
|
elsif values.count == 1
|
|
|
|
value = values.values.first
|
|
|
|
else
|
|
|
|
last_idx = values.count - 1
|
|
|
|
values.each_with_index do |(l, v), i|
|
|
|
|
value += (I18n.t(l) + ": " + v )
|
|
|
|
if i != last_idx
|
|
|
|
value += "<br>"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
{
|
|
|
|
"field_name"=>field_name,
|
|
|
|
"title"=> field_name_trans,
|
|
|
|
"value"=> value
|
|
|
|
}
|
|
|
|
end.compact
|
|
|
|
end
|
2022-03-10 02:11:18 +00:00
|
|
|
application_form.application_form_signup_fields.asc(:_id).each do |rf|
|
|
|
|
application_form_signup_value = rf.application_form_signup_values.where(:application_form_signup_id=>self.id).last
|
|
|
|
if application_form_signup_value
|
|
|
|
value = application_form_signup_value.get_field_value rescue nil
|
2021-11-18 09:59:20 +00:00
|
|
|
if value
|
|
|
|
all_fields << value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return all_fields
|
|
|
|
end
|
|
|
|
end
|
2016-06-29 06:57:45 +00:00
|
|
|
end
|