2022-03-10 02:11:18 +00:00
|
|
|
class ApplicationFormMain
|
2021-04-03 16:23:52 +00:00
|
|
|
require 'fileutils'
|
2016-06-29 06:57:45 +00:00
|
|
|
include Mongoid::Document
|
|
|
|
include Mongoid::Timestamps
|
|
|
|
|
|
|
|
include OrbitModel::Impression
|
|
|
|
# encoding: utf-8
|
|
|
|
include OrbitCategory::Categorizable
|
|
|
|
include Slug
|
2022-08-13 03:35:29 +00:00
|
|
|
StaticSignupFields = ["application_form_signup.created_at", "application_form_signup.ip"]
|
|
|
|
DefaultSignupFields = ["created_at", "ip", "serial_number", "review_progress"]
|
|
|
|
ExceptFieldSetDisplays = ["password", "recaptcha"]
|
2021-03-24 13:06:49 +00:00
|
|
|
ChoiceTypes = ["checkbox","radio","select"]
|
2022-06-14 13:19:52 +00:00
|
|
|
# field :annc_count, :type => Integer, :default => 0
|
|
|
|
# field :album_count, :type => Integer, :default => 0
|
2021-04-03 16:23:52 +00:00
|
|
|
field :copy_id
|
|
|
|
field :except_clone_relations, :type=>Array, :default => []
|
2020-06-26 03:56:02 +00:00
|
|
|
field :signup_note, localize: true
|
2016-06-29 06:57:45 +00:00
|
|
|
field :title, as: :slug_title, type: String, localize: true
|
|
|
|
field :speaker, localize: true
|
|
|
|
field :content, localize: true
|
|
|
|
field :act_place, localize: true
|
2022-06-26 08:16:18 +00:00
|
|
|
field :version
|
|
|
|
field :confidentiality
|
2021-03-24 13:06:49 +00:00
|
|
|
field :enable_summary_choice, :type => Boolean, :default => false
|
|
|
|
field :summary_chioice_type, :type => String, :default => "checkbox"
|
|
|
|
field :summary_chioices, :type => Array, :default => ["A. Energy Conversion","B. Biocatalysis","C. Electrocatalysis",
|
2021-03-26 03:55:59 +00:00
|
|
|
"D. Photocatalysis","E. Catalytic and Porous Materials",
|
2021-03-24 13:06:49 +00:00
|
|
|
"F. Environmental and Reaction Engineering","G. Industrial Applications",
|
|
|
|
"H. Student Oral Presentation Contest (in English)","I. Student Poster Competition"
|
|
|
|
]
|
2022-03-10 02:11:18 +00:00
|
|
|
field :application_form_start_date, :type => Date, :default => Time.now
|
|
|
|
field :application_form_end_date, :type => Date, :default => Time.now
|
2016-06-29 06:57:45 +00:00
|
|
|
|
|
|
|
field :contribute_start_date, :type => Date
|
|
|
|
field :contribute_end_date, :type => Date
|
|
|
|
|
|
|
|
field :contribute_file_count, type: String
|
|
|
|
|
|
|
|
field :registration_status , :type => Array #C: 投稿者 G:一般
|
|
|
|
|
|
|
|
field :create_user_id
|
|
|
|
field :update_user_id
|
2022-03-10 05:52:39 +00:00
|
|
|
|
2022-05-21 13:48:39 +00:00
|
|
|
field :reviewer_type_list, default: [], :type=>Array
|
|
|
|
field :reviewer_ids_list, default: [], :type=>Array
|
|
|
|
field :reviewer_enables, default: [], :type=> Array
|
|
|
|
|
2022-03-10 05:57:36 +00:00
|
|
|
field :reviewer_1st
|
|
|
|
field :reviewer_1st_type
|
2022-03-10 05:52:39 +00:00
|
|
|
field :reviewer_1st_ids
|
|
|
|
|
2022-03-10 05:57:36 +00:00
|
|
|
field :reviewer_2nd
|
|
|
|
field :reviewer_2nd_type
|
2022-03-10 05:52:39 +00:00
|
|
|
field :reviewer_2nd_ids
|
|
|
|
|
2022-03-10 05:57:36 +00:00
|
|
|
field :reviewer_3rd
|
|
|
|
field :reviewer_3rd_type
|
2022-03-10 05:52:39 +00:00
|
|
|
field :reviewer_3rd_ids
|
|
|
|
|
2021-03-17 07:33:31 +00:00
|
|
|
field :reviewer_show_ids
|
2021-04-03 16:23:52 +00:00
|
|
|
field :session_show_ids
|
2022-03-10 02:11:18 +00:00
|
|
|
field :unassigned_application_form_signup_ids
|
|
|
|
field :unassigned_mode_1_application_form_signup_ids
|
2021-03-17 07:33:31 +00:00
|
|
|
field :review_start_date, :type => Date
|
|
|
|
field :review_end_date, :type => Date
|
2021-04-24 10:41:50 +00:00
|
|
|
field :assign_mode, :type => Integer, :default => 0 # 0 => 用default signup ids來分配 ,1 => 用final_session來分配, 2 => 用final_session來分配(當有preffered session欄位時)
|
|
|
|
field :update_old_flag, :type => Boolean, :default => false
|
2022-08-13 03:35:29 +00:00
|
|
|
field :email_regex_enable, type: Boolean, default: false
|
|
|
|
field :email_regex, type: String, default: '\A[^@\s]+@([^@.\s]+\.)+[^@.\s]+\z'
|
2022-03-10 02:11:18 +00:00
|
|
|
belongs_to :application_form_item
|
2021-03-17 07:33:31 +00:00
|
|
|
belongs_to :organizer , :class_name=>"MemberProfile", :foreign_key => :organizer_id
|
2022-03-10 02:11:18 +00:00
|
|
|
has_many :application_form_sessions, :autosave => true, :dependent => :destroy
|
|
|
|
has_many :application_form_links, :autosave => true, :dependent => :destroy
|
|
|
|
has_many :application_form_files, :autosave => true, :dependent => :destroy
|
|
|
|
has_many :application_form_item_contents, :autosave => true, :dependent => :destroy
|
|
|
|
has_many :application_form_signups, :autosave => true, :dependent => :destroy
|
|
|
|
has_many :application_form_submission_fields, :autosave => true, :dependent => :destroy
|
|
|
|
has_many :application_form_signup_fields, :autosave => true, :dependent => :destroy
|
|
|
|
has_many :application_form_reviews, :autosave => true, :dependent => :destroy
|
|
|
|
has_many :application_form_signup_field_sets, autosave: true, dependent: :destroy
|
|
|
|
has_many :application_form_submission_field_sets, autosave: true, dependent: :destroy
|
|
|
|
has_many :application_form_email_sets, autosave: true, dependent: :destroy
|
|
|
|
has_many :application_form_signup_field_customs, :autosave => true, :dependent => :destroy
|
|
|
|
has_one :application_form_template_setting, :autosave => true, :dependent => :destroy
|
|
|
|
accepts_nested_attributes_for :application_form_sessions, :allow_destroy => true
|
|
|
|
accepts_nested_attributes_for :application_form_links, :allow_destroy => true
|
|
|
|
accepts_nested_attributes_for :application_form_files, :allow_destroy => true
|
|
|
|
accepts_nested_attributes_for :application_form_item_contents, :allow_destroy => true
|
|
|
|
accepts_nested_attributes_for :application_form_signups, :allow_destroy => true
|
|
|
|
accepts_nested_attributes_for :application_form_submission_fields, :allow_destroy => true
|
|
|
|
accepts_nested_attributes_for :application_form_signup_fields, :allow_destroy => true
|
|
|
|
accepts_nested_attributes_for :application_form_reviews, :allow_destroy => true
|
|
|
|
accepts_nested_attributes_for :application_form_signup_field_sets, :allow_destroy => true
|
|
|
|
accepts_nested_attributes_for :application_form_submission_field_sets, :allow_destroy => true
|
|
|
|
accepts_nested_attributes_for :application_form_email_sets, :allow_destroy => true
|
|
|
|
accepts_nested_attributes_for :application_form_signup_field_customs, :allow_destroy => true
|
|
|
|
accepts_nested_attributes_for :application_form_template_setting, :allow_destroy => true
|
2021-03-17 07:33:31 +00:00
|
|
|
before_save do
|
2022-03-10 02:11:18 +00:00
|
|
|
module_app_key = "application_form"
|
2022-05-21 13:48:39 +00:00
|
|
|
"""
|
2021-03-17 07:33:31 +00:00
|
|
|
add_module_app_member_ids = []
|
|
|
|
remove_module_app_member_ids = []
|
2021-04-03 16:23:52 +00:00
|
|
|
if self.reviewer_ids_changed? && (self.copy_id.nil? || !self.new_record?)
|
2021-03-17 07:33:31 +00:00
|
|
|
new_reviewer_ids = self.reviewer_ids.to_a - self.reviewer_ids_was.to_a
|
|
|
|
remove_reviewer_ids = self.reviewer_ids_was.to_a - self.reviewer_ids.to_a
|
|
|
|
new_reviewer_ids.each do |reviewer_id|
|
2022-03-10 02:11:18 +00:00
|
|
|
ApplicationFormReview.create(:reviewer_id=>reviewer_id,:application_form_main=>self)
|
2021-03-17 07:33:31 +00:00
|
|
|
add_module_app_member_ids << reviewer_id
|
|
|
|
end
|
|
|
|
remove_reviewer_ids.each do |reviewer_id|
|
2022-03-10 02:11:18 +00:00
|
|
|
ApplicationFormReview.where(:reviewer_id=>reviewer_id,:application_form_main=>self).destroy
|
2021-03-17 07:33:31 +00:00
|
|
|
remove_module_app_member_ids << reviewer_id
|
|
|
|
end
|
2022-03-10 02:11:18 +00:00
|
|
|
unassigned_application_form_signup_ids = self.application_form_signup_ids.map{|s| s.to_s}
|
|
|
|
application_form_reviews = self.application_form_reviews
|
|
|
|
unassigned_mode_1_application_form_signup_ids = unassigned_application_form_signup_ids.dup
|
|
|
|
application_form_reviews.each do |sr|
|
|
|
|
unassigned_mode_1_application_form_signup_ids = unassigned_mode_1_application_form_signup_ids - sr.get_all_application_form_signup_ids(1)
|
|
|
|
unassigned_application_form_signup_ids = unassigned_application_form_signup_ids - sr.all_application_form_signup_ids
|
2021-03-17 07:33:31 +00:00
|
|
|
end
|
2022-03-10 02:11:18 +00:00
|
|
|
self.unassigned_mode_1_application_form_signup_ids = unassigned_mode_1_application_form_signup_ids
|
|
|
|
self.unassigned_application_form_signup_ids = unassigned_application_form_signup_ids
|
2021-03-17 07:33:31 +00:00
|
|
|
end
|
|
|
|
if self.organizer_id_changed?
|
|
|
|
if self.organizer_id_was.present?
|
|
|
|
remove_module_app_member_ids << self.organizer_id_was
|
|
|
|
end
|
|
|
|
if self.organizer_id.present?
|
|
|
|
add_module_app_member_ids << self.organizer_id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
remove_module_app_member_ids.delete(self.organizer_id)
|
2021-03-17 09:16:08 +00:00
|
|
|
self.reviewer_ids.each{|r| remove_module_app_member_ids.delete(r)} rescue nil
|
2021-03-17 07:33:31 +00:00
|
|
|
remove_module_app_member_ids.each do |m_id|
|
|
|
|
user = User.where(:member_profile_id=>m_id).first
|
|
|
|
begin
|
|
|
|
user.extra_link_module_app_keys.delete(module_app_key)
|
|
|
|
user.save
|
|
|
|
rescue
|
|
|
|
next
|
|
|
|
end
|
|
|
|
end
|
|
|
|
add_module_app_member_ids.each do |m_id|
|
|
|
|
user = User.where(:member_profile_id=>m_id).first
|
|
|
|
begin
|
|
|
|
user.extra_link_module_app_keys << module_app_key
|
|
|
|
user.save
|
|
|
|
rescue
|
|
|
|
next
|
|
|
|
end
|
|
|
|
end
|
2022-05-21 13:48:39 +00:00
|
|
|
"""
|
2021-04-03 16:23:52 +00:00
|
|
|
if self.enable_summary_choice
|
|
|
|
(0...self.summary_chioices.count).to_a.each do |i|
|
2022-03-10 02:11:18 +00:00
|
|
|
application_form_session = self.application_form_sessions.where(:key=>i).first
|
|
|
|
if application_form_session.nil?
|
|
|
|
self.application_form_sessions << ApplicationFormSession.new(:key=>i)
|
2021-04-03 16:23:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
(self.summary_chioices.count...self.summary_chioices_was.to_a.count).to_a.each do |i|
|
2022-03-10 02:11:18 +00:00
|
|
|
self.application_form_sessions.where(:key=>i).destroy
|
2021-04-03 16:23:52 +00:00
|
|
|
end
|
|
|
|
end
|
2021-03-17 07:33:31 +00:00
|
|
|
end
|
|
|
|
before_create do
|
2022-03-10 02:11:18 +00:00
|
|
|
self.unassigned_application_form_signup_ids = []
|
|
|
|
self.unassigned_mode_1_application_form_signup_ids = []
|
2021-04-03 16:23:52 +00:00
|
|
|
if self.copy_id.present?
|
|
|
|
clone_new(true)
|
|
|
|
self.created_at = DateTime.now
|
|
|
|
self.updated_at = DateTime.now
|
|
|
|
end
|
|
|
|
end
|
|
|
|
after_create do
|
|
|
|
if self.copy_id.present?
|
2022-03-10 02:11:18 +00:00
|
|
|
self.application_form_signup_fields.each do |application_form_signup_field|
|
|
|
|
application_form_signup_field.application_form_signup_values.update_all(:key=>application_form_signup_field.key)
|
2021-04-03 16:23:52 +00:00
|
|
|
end
|
2022-03-10 02:11:18 +00:00
|
|
|
self.application_form_submission_fields.each do |application_form_submission_field|
|
|
|
|
application_form_submission_field.application_form_submission_values.update_all(:key=>application_form_submission_field.key)
|
2021-04-03 16:23:52 +00:00
|
|
|
end
|
2022-05-21 13:48:39 +00:00
|
|
|
"""
|
2021-04-03 16:23:52 +00:00
|
|
|
current_reviewer_ids = self.reviewer_ids.to_a
|
2022-03-10 02:11:18 +00:00
|
|
|
self.application_form_reviews.where(:reviewer_id.nin=>current_reviewer_ids).destroy
|
2022-05-21 13:48:39 +00:00
|
|
|
module_app_key = 'application_form'
|
2021-04-03 16:23:52 +00:00
|
|
|
current_reviewer_ids.each do |reviewer_id|
|
2022-03-10 02:11:18 +00:00
|
|
|
application_form_review = self.application_form_reviews.select{|sr| sr.reviewer_id == reviewer_id}.first
|
|
|
|
if application_form_review.nil?
|
|
|
|
self.application_form_reviews << ApplicationFormReview.create(:reviewer_id=>reviewer_id,:application_form_main=>self)
|
2021-04-03 16:23:52 +00:00
|
|
|
user = User.where(:member_profile_id=>reviewer_id).first
|
|
|
|
if user.present?
|
|
|
|
user.extra_link_module_app_keys << module_app_key
|
|
|
|
user.save
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-05-21 13:48:39 +00:00
|
|
|
"""
|
2021-04-03 16:23:52 +00:00
|
|
|
end
|
2021-03-17 07:33:31 +00:00
|
|
|
end
|
|
|
|
after_initialize do
|
2022-06-14 13:19:52 +00:00
|
|
|
end
|
2022-08-13 03:35:29 +00:00
|
|
|
def get_email_regex(frontend=false)
|
|
|
|
tmp = self.email_regex
|
|
|
|
if frontend && tmp
|
|
|
|
tmp = tmp.gsub("\\A","^").gsub("\\z","$").gsub("\\","\\\\\\\\")
|
|
|
|
end
|
|
|
|
tmp = nil if !(self.email_regex_enable) || (self.application_form_signup_field_sets.where(:field_name=>'email').pluck(:disabled)[0] rescue false)
|
|
|
|
tmp
|
|
|
|
end
|
2022-06-14 13:19:52 +00:00
|
|
|
def get_max_stage
|
|
|
|
self.reviewer_enables.rindex(true) || 0
|
|
|
|
end
|
|
|
|
def get_reviewer_email(stage=1 , form_params={})
|
|
|
|
stage = stage - 1
|
|
|
|
member_ids = []
|
|
|
|
if self.reviewer_type_list[stage] == "defined_by_applicant"
|
|
|
|
member_ids = Array(form_params[:reviewer_ids_list][stage]) rescue []
|
|
|
|
else
|
|
|
|
member_ids = self.reviewer_ids_list[stage]
|
2021-03-17 07:33:31 +00:00
|
|
|
end
|
2022-06-14 13:19:52 +00:00
|
|
|
return MemberProfile.where(:id.in=>member_ids).pluck(:email).select{|s| s.present?}.uniq
|
2021-03-17 07:33:31 +00:00
|
|
|
end
|
2016-06-29 06:57:45 +00:00
|
|
|
def self.time_range(date1 = null, date2 = null)
|
|
|
|
|
|
|
|
if !date1.blank?
|
|
|
|
|
|
|
|
r = "#{date1.strftime('%Y-%m-%d')}"
|
|
|
|
|
|
|
|
if date2
|
|
|
|
r += " - #{date2.strftime('%Y-%m-%d')}"
|
|
|
|
else
|
|
|
|
r += " - #{I18n.t(:no_deadline)}"
|
|
|
|
end
|
|
|
|
|
|
|
|
r
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2021-06-17 10:07:43 +00:00
|
|
|
def manage_announcement_count(num)
|
|
|
|
if defined?(@@in_use_locales).nil?
|
|
|
|
@@in_use_locales = Site.first.in_use_locales.map{|l| l.to_s}
|
|
|
|
end
|
|
|
|
self.annc_count += num
|
|
|
|
home_page = Page.where(:parent_page_id=>self.id).first
|
|
|
|
page_part = home_page.page_parts.where(:part_id=>annc_part_id).first
|
|
|
|
if self.annc_count <= 0
|
|
|
|
if page_part
|
|
|
|
page_part.sub_parts.where(kind: "module_widget",module: "custom_announcement").destroy
|
|
|
|
end
|
|
|
|
self.annc_count = 0
|
|
|
|
home_page.find_page(:page_id=>"news").update_all(:enabled_for=>[],:enabled_for_mobile=>[],:menu_enabled_for => [])
|
|
|
|
else
|
|
|
|
if page_part.nil?
|
|
|
|
page_part = home_page.page_parts.create(:part_id=>annc_part_id)
|
|
|
|
end
|
|
|
|
if page_part.sub_parts.where(kind: "module_widget",module: "custom_announcement").count == 0
|
|
|
|
sub_part = page_part.sub_parts.create(kind: "module_widget",module: "custom_announcement",
|
|
|
|
title_translations: {"zh_tw"=>"公告", "en"=>"News"},read_more_page_id: "news",
|
|
|
|
widget_type: "annc_widget11",widget_method: "widget",
|
2022-03-10 02:11:18 +00:00
|
|
|
data_count: annc_data_count,custom_data_field: {"bind_module_app"=>"application_form", "bind_uid"=>self.uid})
|
2021-06-17 10:07:43 +00:00
|
|
|
sub_part.select_options.create(annc_default_select_option_attr)
|
|
|
|
end
|
|
|
|
home_page.find_page(:page_id=>"news").update_all(:enabled_for=>@@in_use_locales,:enabled_for_mobile=>@@in_use_locales,:menu_enabled_for => @@in_use_locales)
|
|
|
|
end
|
|
|
|
self.save
|
|
|
|
end
|
|
|
|
def annc_default_select_option_attr
|
|
|
|
module_app = ModuleApp.where(:key=>'custom_announcement').first
|
|
|
|
field_name = module_app.show_option_keys.first.all[2].to_s
|
|
|
|
value = module_app.show_option_values.first.all[2][3].to_yaml
|
|
|
|
return {field_name: field_name, value: value}
|
|
|
|
end
|
|
|
|
def annc_data_count
|
2022-03-10 02:11:18 +00:00
|
|
|
self.application_form_template_setting.announcement_limit rescue 4
|
2021-06-17 10:07:43 +00:00
|
|
|
end
|
|
|
|
def album_data_count
|
2022-03-10 02:11:18 +00:00
|
|
|
self.application_form_template_setting.album_limit rescue 4
|
2021-06-17 10:07:43 +00:00
|
|
|
end
|
|
|
|
def annc_part_id
|
|
|
|
200
|
|
|
|
end
|
|
|
|
def album_part_id
|
|
|
|
201
|
|
|
|
end
|
|
|
|
def manage_album_count(num)
|
|
|
|
if defined?(@@in_use_locales).nil?
|
|
|
|
@@in_use_locales = Site.first.in_use_locales.map{|l| l.to_s}
|
|
|
|
end
|
|
|
|
self.album_count += num
|
|
|
|
home_page = Page.where(:parent_page_id=>self.id).first
|
|
|
|
page_part = home_page.page_parts.where(:part_id=>album_part_id).first
|
|
|
|
if self.annc_count <= 0
|
|
|
|
if page_part
|
|
|
|
page_part.sub_parts.where(kind: "module_widget",module: "custom_gallery").destroy
|
|
|
|
end
|
|
|
|
home_page.find_page(:page_id=>"album").update_all(:enabled_for=>[],:enabled_for_mobile=>[],:menu_enabled_for => [])
|
|
|
|
self.annc_count = 0
|
|
|
|
else
|
|
|
|
if page_part.nil?
|
|
|
|
page_part = home_page.page_parts.create(:part_id=>annc_part_id)
|
|
|
|
end
|
|
|
|
if page_part.sub_parts.where(kind: "module_widget",module: "custom_gallery").count == 0
|
|
|
|
page_part.sub_parts.create(kind: "module_widget",module: "custom_gallery",
|
|
|
|
title_translations: {"zh_tw"=>"相簿", "en"=>"Album"},read_more_page_id: "album",
|
|
|
|
widget_type: "custom_gallery_widget2",widget_method: "widget",
|
2022-03-10 02:11:18 +00:00
|
|
|
data_count: album_data_count,custom_data_field: {"bind_module_app"=>"application_form", "bind_uid"=>self.uid})
|
2021-06-17 10:07:43 +00:00
|
|
|
end
|
|
|
|
home_page.find_page(:page_id=>"album").update_all(:enabled_for=>@@in_use_locales,:enabled_for_mobile=>@@in_use_locales,:menu_enabled_for => @@in_use_locales)
|
|
|
|
end
|
|
|
|
self.save
|
|
|
|
end
|
2016-06-29 06:57:45 +00:00
|
|
|
def get_attribute_value(attribute_field, signup_id)
|
2022-03-10 02:11:18 +00:00
|
|
|
ApplicationFormSignupValue.find_by(application_form_signup_field_id: attribute_field.id, application_form_signup_id: signup_id)
|
2016-06-29 06:57:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_attribute_values(attribute_type=nil)
|
2022-03-10 02:11:18 +00:00
|
|
|
@attribute_values = attribute_type.application_form_signup_values rescue nil
|
2016-06-29 06:57:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_value_from_field_id(field_id,attribute_type=nil)
|
|
|
|
values = get_attribute_values(attribute_type)
|
2022-03-10 02:11:18 +00:00
|
|
|
value = values.detect {|value| value.application_form_signup_field_id == field_id} rescue nil
|
2016-06-29 06:57:45 +00:00
|
|
|
value ? value : nil
|
|
|
|
end
|
2021-04-03 16:23:52 +00:00
|
|
|
def clone_new(clone_mode=false)
|
|
|
|
@records_all = {}
|
|
|
|
if clone_mode
|
|
|
|
clone_target = self.class.find(object.copy_id) rescue nil
|
|
|
|
else
|
|
|
|
clone_target = self
|
|
|
|
end
|
2022-03-10 02:11:18 +00:00
|
|
|
application_form,clone_target = clone_new_for_object(self,clone_target,clone_mode)
|
2021-04-03 16:23:52 +00:00
|
|
|
if clone_mode
|
2022-03-10 02:11:18 +00:00
|
|
|
copy_settings = ApplicationFormSignupAdminSetting.where(:application_form_main_id=>clone_target.id.to_s).to_a
|
2021-04-03 16:23:52 +00:00
|
|
|
copy_settings.each do |setting|
|
|
|
|
new_setting = setting.dup
|
2022-03-10 02:11:18 +00:00
|
|
|
new_setting.application_form_main_id = application_form.id.to_s
|
2021-04-03 16:23:52 +00:00
|
|
|
new_setting.save
|
|
|
|
end
|
2022-03-10 02:11:18 +00:00
|
|
|
count_array = (0...self.application_form_sessions.to_a.count).to_a
|
2021-04-03 16:23:52 +00:00
|
|
|
count_array.each do |i|
|
2022-03-10 02:11:18 +00:00
|
|
|
application_form_session = self.application_form_sessions[i]
|
|
|
|
application_form_session.application_form_signup_ids = map_application_form_signup_ids(application_form_session.application_form_signup_ids)
|
2021-04-03 16:23:52 +00:00
|
|
|
end
|
2022-03-10 02:11:18 +00:00
|
|
|
self.unassigned_application_form_signup_ids = map_application_form_signup_ids(clone_target.unassigned_application_form_signup_ids)
|
|
|
|
self.unassigned_mode_1_application_form_signup_ids = map_application_form_signup_ids(clone_target.unassigned_mode_1_application_form_signup_ids)
|
2016-06-29 06:57:45 +00:00
|
|
|
|
2022-03-10 02:11:18 +00:00
|
|
|
need_change_application_form_signups = self.application_form_signups.where(:application_form_session_ids.nin=>[nil,[]]).to_a
|
|
|
|
count_array = (0...need_change_application_form_signups.count).to_a
|
2021-04-03 16:23:52 +00:00
|
|
|
count_array.each do |i|
|
2022-03-10 02:11:18 +00:00
|
|
|
application_form_signup = need_change_application_form_signups[i]
|
|
|
|
if application_form_signup.application_form_session_ids.present?
|
|
|
|
application_form_signup.application_form_session_ids = application_form_signup.application_form_session_ids.map{|id| @records_all["application_form_session_ids"][id]} rescue []
|
2021-04-03 16:23:52 +00:00
|
|
|
end
|
|
|
|
end
|
2022-03-10 02:11:18 +00:00
|
|
|
count_array = (0...self.application_form_reviews.to_a.count).to_a
|
|
|
|
change_fields = [:application_form_signup_ids, :remove_application_form_signup_ids, :default_application_form_signup_ids]
|
2021-04-03 16:23:52 +00:00
|
|
|
count_array.each do |i|
|
2022-03-10 02:11:18 +00:00
|
|
|
application_form_review = self.application_form_reviews[i]
|
2021-04-03 16:23:52 +00:00
|
|
|
change_fields.each do |f|
|
2022-03-10 02:11:18 +00:00
|
|
|
application_form_review.send("#{f}=",map_application_form_signup_ids( application_form_review.send(f)))
|
2021-04-03 16:23:52 +00:00
|
|
|
end
|
2022-03-10 02:11:18 +00:00
|
|
|
application_form_review.application_form_session_ids = application_form_review.application_form_session_ids.map{|application_form_session_id| @records_all["application_form_session_ids"][application_form_session_id] rescue nil}.select{|id| !id.nil?}
|
|
|
|
application_form_review.default_topics = application_form_review.default_topics.map do |default_topic|
|
2021-04-03 16:23:52 +00:00
|
|
|
fn,id = default_topic.split(".")
|
2022-03-10 02:11:18 +00:00
|
|
|
if fn == "application_form_signup_fields"
|
|
|
|
id = @records_all["application_form_signup_field_ids"][BSON::ObjectId(id)].to_s rescue nil
|
|
|
|
elsif fn == "application_form_submission_fields"
|
|
|
|
id = @records_all["application_form_submission_field_ids"][BSON::ObjectId(id)].to_s rescue nil
|
2021-04-03 16:23:52 +00:00
|
|
|
end
|
|
|
|
"#{fn}.#{id}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-03-10 02:11:18 +00:00
|
|
|
application_form
|
2021-04-03 16:23:52 +00:00
|
|
|
end
|
2022-03-10 02:11:18 +00:00
|
|
|
def map_application_form_signup_ids(old_application_form_signup_ids)
|
|
|
|
if @records_all["application_form_signup_ids"].present?
|
|
|
|
old_application_form_signup_ids.map{|application_form_signup_id| @records_all["application_form_signup_ids"][BSON::ObjectId(application_form_signup_id)].to_s rescue ""}.select{|t| t.present?}
|
2021-04-24 10:41:50 +00:00
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
2021-04-03 16:23:52 +00:00
|
|
|
end
|
2021-04-24 10:41:50 +00:00
|
|
|
def clone_new_for_object(object,clone_target=nil,clone_mode=false)
|
|
|
|
if clone_mode
|
|
|
|
new_object = object
|
|
|
|
clone_target = object.class.find(object.copy_id) rescue nil if clone_target.nil?
|
|
|
|
else
|
|
|
|
clone_target = object if clone_target.nil?
|
|
|
|
new_object = object.dup
|
2021-04-03 16:23:52 +00:00
|
|
|
end
|
2021-04-24 10:41:50 +00:00
|
|
|
return if self.except_clone_relations.to_s.include?(new_object.class.to_s.underscore)
|
|
|
|
@records_all["#{new_object.class.to_s.underscore.singularize}_ids"] = {} if @records_all["#{new_object.class.to_s.underscore.singularize}_ids"].nil?
|
|
|
|
begin
|
|
|
|
@records_all["#{new_object.class.to_s.underscore.singularize}_ids"][clone_target.id] = object.id
|
|
|
|
rescue
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
if !clone_target.nil? && !new_object.nil?
|
|
|
|
initialize_fields = ["uid","created_at","updated_at"]
|
|
|
|
initialize_fields.each do |f|
|
|
|
|
new_object.send("#{f}=",nil) if new_object.fields.keys.include?(f)
|
|
|
|
end
|
|
|
|
relations_fields = clone_target.relations.except("impressions").keys
|
|
|
|
all_fields = clone_target.fields.keys - relations_fields
|
|
|
|
all_fields = all_fields - relations_fields.map{|k| "#{k}_id"}
|
|
|
|
all_fields = all_fields - relations_fields.map{|k| "#{k.singularize}_ids"}
|
|
|
|
new_object_class_name = new_object.class.to_s.underscore
|
|
|
|
relations_fields = clone_target.relations.except("impressions").keys
|
|
|
|
unsort_relation_keys = clone_target.relations.keys
|
|
|
|
unless @parent_level
|
|
|
|
fields_to_delete = [new_object_class_name]
|
|
|
|
tmp_relations_fields = [new_object_class_name]
|
|
|
|
while relations_fields.count > 0
|
|
|
|
tmp_singularize_relations_fields = tmp_relations_fields.map{|f| f.singularize}
|
|
|
|
approve_append = nil
|
|
|
|
relations_fields.each do |k|
|
|
|
|
belongs_to_class = clone_target.relations[k].class_name.constantize.relations.select{|k,v| v.macro == :belongs_to}.keys
|
|
|
|
has_many_class = clone_target.relations[k].class_name.constantize.relations.select{|k,v| v.macro == :has_many}.keys
|
|
|
|
if (belongs_to_class - tmp_singularize_relations_fields).count == 0
|
|
|
|
other_has_many_class = (has_many_class - unsort_relation_keys)
|
|
|
|
if other_has_many_class.count == 0
|
|
|
|
tmp_relations_fields << k
|
|
|
|
else
|
|
|
|
result = other_has_many_class.map do |k|
|
|
|
|
belongs_to_class = k.classify.constantize.relations.select{|kk,v| v.macro == :belongs_to}.keys
|
|
|
|
has_many_class = k.classify.constantize.relations.select{|kk,v| v.macro == :has_many}.keys
|
|
|
|
if (belongs_to_class - tmp_singularize_relations_fields).count == 0
|
|
|
|
true
|
|
|
|
else
|
|
|
|
fields_to_delete = fields_to_delete.concat(belongs_to_class)
|
|
|
|
tmp_relations_fields.concat(belongs_to_class)
|
|
|
|
false
|
|
|
|
end
|
2021-04-03 16:23:52 +00:00
|
|
|
end
|
2021-04-24 10:41:50 +00:00
|
|
|
if result.select{|t| !t}.count == 0
|
|
|
|
if (fields_to_delete.map{|f| f.pluralize} - tmp_relations_fields).count == 0
|
|
|
|
tmp_relations_fields << k
|
|
|
|
elsif clone_target.relations[k].class_name.constantize.fields.keys.include?("key")
|
|
|
|
tmp_relations_fields << k
|
|
|
|
elsif (clone_target.relations[k].class_name.constantize.relations.keys.map{|f| f.singularize} & fields_to_delete).count != 0
|
|
|
|
approve_append = k
|
|
|
|
end
|
2021-04-03 16:23:52 +00:00
|
|
|
end
|
|
|
|
end
|
2021-04-24 10:41:50 +00:00
|
|
|
elsif !unsort_relation_keys.include?(clone_target.relations[k].class_name.underscore) && !unsort_relation_keys.include?(clone_target.relations[k].class_name.underscore.pluralize)
|
|
|
|
tmp_relations_fields << k
|
2021-04-03 16:23:52 +00:00
|
|
|
end
|
|
|
|
end
|
2021-04-24 10:41:50 +00:00
|
|
|
tmp_relations_fields << approve_append if approve_append.present?
|
|
|
|
approve_append = nil
|
|
|
|
relations_fields = relations_fields - tmp_relations_fields
|
2021-04-03 16:23:52 +00:00
|
|
|
end
|
2021-04-24 10:41:50 +00:00
|
|
|
relations_fields = tmp_relations_fields
|
|
|
|
fields_to_delete.each{|f| relations_fields.delete(f)}
|
|
|
|
@clone_mode = clone_mode
|
2021-04-03 16:23:52 +00:00
|
|
|
end
|
2021-04-24 10:41:50 +00:00
|
|
|
@parent_level = true
|
|
|
|
if clone_mode
|
|
|
|
all_fields.each do |f|
|
|
|
|
next if f == "uid"
|
|
|
|
unless new_object.send("#{f}_changed?") && new_object.send("#{f}_changed_from_default?")
|
|
|
|
new_object.send("#{f}=",clone_target.send(f))
|
|
|
|
end
|
2021-04-03 16:23:52 +00:00
|
|
|
end
|
|
|
|
end
|
2021-04-24 10:41:50 +00:00
|
|
|
relations_fields.each do |f|
|
|
|
|
no_dup_flag = false
|
|
|
|
if clone_target.relations[f].macro == :belongs_to || clone_target.relations[f].macro == :has_one
|
|
|
|
no_dup_flag = new_object.send(f).present?
|
|
|
|
elsif clone_target.relations[f].macro == :has_many
|
|
|
|
no_dup_flag = new_object.send(f).to_a.count != 0
|
2021-08-31 10:58:02 +00:00
|
|
|
elsif clone_target.relations[f].macro == :embeds_many #Fix localize fields
|
|
|
|
if new_object.send(f).to_a.count != 0
|
|
|
|
need_fix_fields = new_object.send(f).to_a[0].fields.select{|k,v| (v.options[:localize] rescue false)}.keys
|
|
|
|
locale = I18n.locale.to_s
|
|
|
|
embeded_records = new_object.send(f).map do |embeded_record|
|
|
|
|
need_fix_fields.each do |f|
|
|
|
|
if (embeded_record[f][locale].class != String rescue false)
|
|
|
|
embeded_record.send("#{f}_translations=",embeded_record[f][locale])
|
|
|
|
else
|
|
|
|
embeded_record.send("#{f}_translations=",embeded_record[f])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
embeded_record
|
|
|
|
end
|
|
|
|
new_object.send("#{f}=",embeded_records)
|
|
|
|
end
|
2021-04-03 16:23:52 +00:00
|
|
|
end
|
2021-04-24 10:41:50 +00:00
|
|
|
if clone_target.relations[f].macro == :belongs_to || clone_target.relations[f].class_name == "MemberProfile"
|
|
|
|
if @records_all["#{f}_ids"].nil?
|
|
|
|
new_object.send("#{f}_id=",clone_target.send("#{f}_id"))
|
|
|
|
else
|
|
|
|
new_object.send("#{f}_id=",(@records_all["#{f}_ids"][clone_target.send("#{f}_id")]))
|
|
|
|
end
|
|
|
|
elsif clone_target.relations[f].macro == :has_one
|
|
|
|
next if self.except_clone_relations.to_s.include?(f)
|
|
|
|
need_clone_relation = clone_target.send(f)
|
|
|
|
clone_relation = new_object.send(f)
|
|
|
|
clone_relation = need_clone_relation.dup if clone_relation.nil?
|
2021-04-03 16:23:52 +00:00
|
|
|
initialize_fields.each do |f|
|
|
|
|
clone_relation.send("#{f}=",nil) if clone_relation.fields.keys.include?(f)
|
|
|
|
end
|
|
|
|
check_fields = clone_relation.fields.except(initialize_fields)
|
|
|
|
check_fields.keys.each do |f|
|
|
|
|
if (clone_relation.send(f).class.to_s.match(/uploader/i) rescue false)
|
2021-04-04 01:49:24 +00:00
|
|
|
if clone_relation[f].blank? && (clone_relation.send(f).file.nil? rescue true)
|
2021-04-03 16:23:52 +00:00
|
|
|
clone_relation[f] = r[f]
|
|
|
|
source_filepath = r.send(f).file.file
|
2021-04-04 01:49:24 +00:00
|
|
|
if @clone_mode
|
2021-04-03 16:23:52 +00:00
|
|
|
dest_filepath = clone_relation.send(f).file.file
|
|
|
|
FileUtils.mkdir_p(File.dirname(dest_filepath))
|
|
|
|
FileUtils.cp(source_filepath,dest_filepath)
|
|
|
|
end
|
2021-04-04 01:49:24 +00:00
|
|
|
elsif (clone_relation.send(f).file rescue nil)
|
|
|
|
clone_relation[f] = File.basename(clone_relation.send(f).file.file.to_s)
|
2021-04-03 16:23:52 +00:00
|
|
|
end
|
|
|
|
file_flag = true
|
|
|
|
end
|
|
|
|
end
|
2021-04-24 10:41:50 +00:00
|
|
|
new_object.send("#{f}=",clone_relation)
|
|
|
|
elsif clone_target.relations[f].macro == :has_many
|
|
|
|
next if self.except_clone_relations.to_s.include?(f)
|
|
|
|
clone_relations = []
|
|
|
|
need_clone_relations = clone_target.send(f).asc(:_id).to_a
|
|
|
|
file_flag = false
|
|
|
|
need_clone_relations.each_with_index do |r,i|
|
|
|
|
clone_relation = new_object.send(f)[i]
|
|
|
|
clone_relation = r.dup if clone_relation.nil?
|
|
|
|
initialize_fields.each do |f|
|
|
|
|
clone_relation.send("#{f}=",nil) if clone_relation.fields.keys.include?(f)
|
|
|
|
end
|
|
|
|
check_fields = clone_relation.fields.except(initialize_fields)
|
|
|
|
check_fields.keys.each do |f|
|
|
|
|
if (clone_relation.send(f).class.to_s.match(/uploader/i) rescue false)
|
|
|
|
if clone_relation[f].blank? && (clone_relation.send(f).file.nil? rescue true)
|
|
|
|
clone_relation[f] = r[f]
|
|
|
|
source_filepath = r.send(f).file.file
|
|
|
|
if @clone_mode
|
|
|
|
dest_filepath = clone_relation.send(f).file.file
|
|
|
|
FileUtils.mkdir_p(File.dirname(dest_filepath))
|
|
|
|
FileUtils.cp(source_filepath,dest_filepath)
|
|
|
|
end
|
|
|
|
elsif (clone_relation.send(f).file rescue nil)
|
|
|
|
clone_relation[f] = File.basename(clone_relation.send(f).file.file.to_s)
|
|
|
|
end
|
|
|
|
file_flag = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
clone_relations << clone_relation
|
|
|
|
end
|
|
|
|
if !no_dup_flag || (no_dup_flag && file_flag)
|
|
|
|
new_object_relations = new_object.send(f).to_a
|
|
|
|
if new_object_relations.count != 0
|
|
|
|
if clone_relations.count > new_object_relations.count
|
|
|
|
clone_relations = clone_relations[0...new_object_relations.count]
|
|
|
|
else
|
|
|
|
clone_relations = clone_relations.concat(new_object.send(f)[clone_relations.count...new_object_relations.count])
|
|
|
|
end
|
|
|
|
new_object.send("#{f}=",clone_relations)
|
2021-04-03 16:23:52 +00:00
|
|
|
else
|
2021-04-24 10:41:50 +00:00
|
|
|
new_object.send("#{f}=",clone_relations)
|
2021-04-03 16:23:52 +00:00
|
|
|
end
|
2021-04-24 10:41:50 +00:00
|
|
|
end
|
|
|
|
count_array = (0...new_object.send(f).to_a.count).to_a
|
|
|
|
count_array.each do |i|
|
|
|
|
clone_new_for_object(new_object.send(f)[i],need_clone_relations[i],true)
|
2021-04-03 16:23:52 +00:00
|
|
|
end
|
|
|
|
end
|
2021-04-24 10:41:50 +00:00
|
|
|
end
|
|
|
|
new_object.copy_id = clone_target.id if new_object.fields.keys.include?("copy_id")
|
|
|
|
return new_object, clone_target
|
|
|
|
end
|
|
|
|
end
|
|
|
|
def fix_old_data
|
|
|
|
unless self.update_old_flag
|
2022-03-10 02:11:18 +00:00
|
|
|
self.application_form_signups.each do |ss|
|
2021-04-24 10:41:50 +00:00
|
|
|
ss.final_sessions = Array(ss.final_session)
|
2022-03-10 02:11:18 +00:00
|
|
|
ss.application_form_session_ids = Array(ss.application_form_session_id)
|
2021-04-24 10:41:50 +00:00
|
|
|
if ss.final_session
|
2022-03-10 02:11:18 +00:00
|
|
|
ss.application_form_signup_contributes.update_all(:final_session=>final_session)
|
|
|
|
ApplicationFormSubmissionValue.where(:application_form_signup_contribute_id.in=> ss.application_form_signup_contributes.pluck(:id)).each{|ssv| ssv.save}
|
2021-04-03 16:23:52 +00:00
|
|
|
end
|
2021-04-24 10:41:50 +00:00
|
|
|
ss.save
|
2021-04-03 16:23:52 +00:00
|
|
|
end
|
2022-03-10 02:11:18 +00:00
|
|
|
self.application_form_sessions.each{|ss| ss.fix_application_form_signup_contribute_ids}
|
2021-04-24 10:41:50 +00:00
|
|
|
self.update(:update_old_flag=>true)
|
2021-04-03 16:23:52 +00:00
|
|
|
end
|
2021-04-24 10:41:50 +00:00
|
|
|
end
|
2021-06-17 10:07:43 +00:00
|
|
|
def enable_custom_template
|
2022-03-10 02:11:18 +00:00
|
|
|
self.application_form_template_setting && self.application_form_template_setting.enable_custom_template
|
2021-06-17 10:07:43 +00:00
|
|
|
end
|
2021-04-24 10:41:50 +00:00
|
|
|
def enable_review_result
|
|
|
|
self.assign_mode == 2
|
2021-04-03 16:23:52 +00:00
|
|
|
end
|
|
|
|
end
|