class ApplicationFormMain require 'fileutils' include Mongoid::Document include Mongoid::Timestamps include OrbitModel::Impression # encoding: utf-8 include OrbitCategory::Categorizable include Slug ChoiceTypes = ["checkbox","radio","select"] # field :annc_count, :type => Integer, :default => 0 # field :album_count, :type => Integer, :default => 0 field :copy_id field :except_clone_relations, :type=>Array, :default => [] field :signup_note, localize: true field :title, as: :slug_title, type: String, localize: true field :speaker, localize: true field :content, localize: true field :act_place, localize: true field :version field :confidentiality 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", "D. Photocatalysis","E. Catalytic and Porous Materials", "F. Environmental and Reaction Engineering","G. Industrial Applications", "H. Student Oral Presentation Contest (in English)","I. Student Poster Competition" ] field :application_form_start_date, :type => Date, :default => Time.now field :application_form_end_date, :type => Date, :default => Time.now 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 field :reviewer_type_list, default: [], :type=>Array field :reviewer_ids_list, default: [], :type=>Array field :reviewer_enables, default: [], :type=> Array field :reviewer_1st field :reviewer_1st_type field :reviewer_1st_ids field :reviewer_2nd field :reviewer_2nd_type field :reviewer_2nd_ids field :reviewer_3rd field :reviewer_3rd_type field :reviewer_3rd_ids field :reviewer_show_ids field :session_show_ids field :unassigned_application_form_signup_ids field :unassigned_mode_1_application_form_signup_ids field :review_start_date, :type => Date field :review_end_date, :type => Date 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 belongs_to :application_form_item belongs_to :organizer , :class_name=>"MemberProfile", :foreign_key => :organizer_id 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 before_save do module_app_key = "application_form" """ add_module_app_member_ids = [] remove_module_app_member_ids = [] if self.reviewer_ids_changed? && (self.copy_id.nil? || !self.new_record?) 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| ApplicationFormReview.create(:reviewer_id=>reviewer_id,:application_form_main=>self) add_module_app_member_ids << reviewer_id end remove_reviewer_ids.each do |reviewer_id| ApplicationFormReview.where(:reviewer_id=>reviewer_id,:application_form_main=>self).destroy remove_module_app_member_ids << reviewer_id end 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 end 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 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) self.reviewer_ids.each{|r| remove_module_app_member_ids.delete(r)} rescue nil 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 """ if self.enable_summary_choice (0...self.summary_chioices.count).to_a.each do |i| application_form_session = self.application_form_sessions.where(:key=>i).first if application_form_session.nil? self.application_form_sessions << ApplicationFormSession.new(:key=>i) end end (self.summary_chioices.count...self.summary_chioices_was.to_a.count).to_a.each do |i| self.application_form_sessions.where(:key=>i).destroy end end end before_create do self.unassigned_application_form_signup_ids = [] self.unassigned_mode_1_application_form_signup_ids = [] 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? 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) end 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) end """ current_reviewer_ids = self.reviewer_ids.to_a self.application_form_reviews.where(:reviewer_id.nin=>current_reviewer_ids).destroy module_app_key = 'application_form' current_reviewer_ids.each do |reviewer_id| 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) 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 """ end end after_initialize do end 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] end return MemberProfile.where(:id.in=>member_ids).pluck(:email).select{|s| s.present?}.uniq end 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 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", data_count: annc_data_count,custom_data_field: {"bind_module_app"=>"application_form", "bind_uid"=>self.uid}) 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 self.application_form_template_setting.announcement_limit rescue 4 end def album_data_count self.application_form_template_setting.album_limit rescue 4 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", data_count: album_data_count,custom_data_field: {"bind_module_app"=>"application_form", "bind_uid"=>self.uid}) 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 def get_attribute_value(attribute_field, signup_id) ApplicationFormSignupValue.find_by(application_form_signup_field_id: attribute_field.id, application_form_signup_id: signup_id) end def get_attribute_values(attribute_type=nil) @attribute_values = attribute_type.application_form_signup_values rescue nil end def get_value_from_field_id(field_id,attribute_type=nil) values = get_attribute_values(attribute_type) value = values.detect {|value| value.application_form_signup_field_id == field_id} rescue nil value ? value : nil end 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 application_form,clone_target = clone_new_for_object(self,clone_target,clone_mode) if clone_mode copy_settings = ApplicationFormSignupAdminSetting.where(:application_form_main_id=>clone_target.id.to_s).to_a copy_settings.each do |setting| new_setting = setting.dup new_setting.application_form_main_id = application_form.id.to_s new_setting.save end count_array = (0...self.application_form_sessions.to_a.count).to_a count_array.each do |i| 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) end 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) 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 count_array.each do |i| 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 [] end end 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] count_array.each do |i| application_form_review = self.application_form_reviews[i] change_fields.each do |f| application_form_review.send("#{f}=",map_application_form_signup_ids( application_form_review.send(f))) end 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| fn,id = default_topic.split(".") 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 end "#{fn}.#{id}" end end end application_form end 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?} else [] end end 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 end 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 end 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 end end 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 end end tmp_relations_fields << approve_append if approve_append.present? approve_append = nil relations_fields = relations_fields - tmp_relations_fields end relations_fields = tmp_relations_fields fields_to_delete.each{|f| relations_fields.delete(f)} @clone_mode = clone_mode end @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 end end 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 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 end 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? 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 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) else new_object.send("#{f}=",clone_relations) end 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) end end 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 self.application_form_signups.each do |ss| ss.final_sessions = Array(ss.final_session) ss.application_form_session_ids = Array(ss.application_form_session_id) if ss.final_session 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} end ss.save end self.application_form_sessions.each{|ss| ss.fix_application_form_signup_contribute_ids} self.update(:update_old_flag=>true) end end def enable_custom_template self.application_form_template_setting && self.application_form_template_setting.enable_custom_template end def enable_review_result self.assign_mode == 2 end end