252 lines
9.9 KiB
Ruby
252 lines
9.9 KiB
Ruby
|
class ApplicationFormSubmissionValue
|
||
|
include Mongoid::Document
|
||
|
include Mongoid::Timestamps
|
||
|
include Mongoid::Attributes::Dynamic
|
||
|
include ::AttributeValuesHelper
|
||
|
include ::Admin::ApplicationFormsValuesHelper
|
||
|
|
||
|
field :key, type: String
|
||
|
field :val
|
||
|
belongs_to :application_form_submission_field
|
||
|
belongs_to :application_form_signup_contribute
|
||
|
before_save :check_key
|
||
|
before_save :data_proc
|
||
|
after_save do
|
||
|
if (self.application_form_submission_field.markup == "application_form_preferred_session" rescue false) && self.application_form_signup_contribute
|
||
|
val_change = self.val_change
|
||
|
self.application_form_signup_contribute.update(:preferred_session=>val.to_i)
|
||
|
self.application_form_signup_contribute.application_form_signup.set_preferred_sessions
|
||
|
unless val_change.nil?
|
||
|
old_val = val_change[0]
|
||
|
new_val = val_change[1]
|
||
|
application_form_signup_id = self.application_form_signup_contribute.application_form_signup_id.to_s
|
||
|
application_form_sessions = self.application_form_submission_field.application_form_main.application_form_sessions.to_a
|
||
|
old_application_form_session = application_form_sessions.select{|ss| ss.key.to_s == old_val}.first
|
||
|
if old_application_form_session
|
||
|
old_application_form_session.default_application_form_signup_ids.delete application_form_signup_id
|
||
|
old_application_form_session.save
|
||
|
end
|
||
|
new_application_form_session = application_form_sessions.select{|ss| ss.key.to_s == new_val}.first
|
||
|
if new_application_form_session
|
||
|
new_application_form_session.default_application_form_signup_ids << application_form_signup_id
|
||
|
new_application_form_session.save
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
def add_more_counter
|
||
|
index_max = self["val"].count rescue 0
|
||
|
index_max == 0 ? 1 : index_max
|
||
|
end
|
||
|
|
||
|
def value(index = nil)
|
||
|
site = Site.first
|
||
|
result = case self.application_form_submission_field.markup
|
||
|
when 'text_field','text_area'
|
||
|
if self.application_form_submission_field.add_more and (self.application_form_submission_field.markup == "text_field")
|
||
|
index.nil? ? self["val"] : self["val"][index]
|
||
|
else
|
||
|
self.application_form_submission_field.get_data["cross_lang"] =="true" ? self["val"] : Hash[site.valid_locales.collect{|lang| [lang,self[lang.to_sym]]}]
|
||
|
end
|
||
|
when 'select','radio_button','address','application_form_preferred_session'
|
||
|
self["val"]
|
||
|
when 'date'
|
||
|
if !self["val"].blank? and !self["val"]['(1i)'].blank?
|
||
|
"#{self["val"]['(1i)']}/#{self["val"]['(2i)']}/#{self["val"]['(3i)']}"
|
||
|
else
|
||
|
self["val"]
|
||
|
end
|
||
|
when 'checkbox'
|
||
|
self["val"]
|
||
|
end #end of case self.application_form_submission_field.markup
|
||
|
end
|
||
|
|
||
|
def value=(value)
|
||
|
#save everything to temp_data waiting for futher process
|
||
|
self[:temp_data] = value
|
||
|
end
|
||
|
|
||
|
def get_field_value
|
||
|
application_form_submission_field = self.application_form_submission_field
|
||
|
if (application_form_submission_field.markup.eql?("text_field") || application_form_submission_field.markup.eql?("text_area"))
|
||
|
if self.value.class == String
|
||
|
field_value = self.value
|
||
|
else
|
||
|
field_value = self.value[I18n.locale]
|
||
|
end
|
||
|
elsif (application_form_submission_field.markup.eql?("select") || application_form_submission_field.markup.eql?("radio_button"))
|
||
|
field_value = application_form_submission_field.markup_value["#{self.value}"][I18n.locale] rescue nil
|
||
|
elsif application_form_submission_field.markup.eql?("application_form_preferred_session")
|
||
|
field_value = application_form_submission_field.application_form_main.summary_chioices[self.value.to_i] rescue nil
|
||
|
elsif application_form_submission_field.markup.eql?("address")
|
||
|
field_value = rf[:address_key][I18n.locale].map{|k,v| v}.delete_if(&:blank?).join(', ')
|
||
|
elsif application_form_submission_field.markup.eql?("date")
|
||
|
case application_form_submission_field.typeC['format']
|
||
|
when 'format1'
|
||
|
field_value = self.value.to_date.strftime("%Y/%m/%d")
|
||
|
when 'format2'
|
||
|
field_value = self.value.to_date.strftime("%Y/%m/%d")
|
||
|
when 'format3'
|
||
|
field_value = self.value.to_date.strftime("%Y/%m")
|
||
|
when 'format4'
|
||
|
field_value = self.value.to_date.strftime("%Y")
|
||
|
end
|
||
|
elsif application_form_submission_field.markup.eql?("checkbox")
|
||
|
field_value = self.value.map {|v| application_form_submission_field.markup_value["#{v}"][I18n.locale]}.join(', ') rescue nil
|
||
|
end
|
||
|
|
||
|
field_value = (field_value =~ /\A#{URI::regexp(['http', 'https'])}\z/) ? "<a href='#{field_value}' target='blank'>#{field_value}</a>" : field_value
|
||
|
field_value = (field_value =~ /\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i) ? "<a href='mailto:#{field_value}'>#{field_value}</a>" : field_value
|
||
|
|
||
|
if !field_value.blank?
|
||
|
{
|
||
|
"key" => application_form_submission_field.key,
|
||
|
"title" => application_form_submission_field.title,
|
||
|
"value" => field_value,
|
||
|
"val" => field_value = self.value
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
"key" => application_form_submission_field.key,
|
||
|
"title" => application_form_submission_field.title,
|
||
|
"value" => "",
|
||
|
"val" => field_value = self.value
|
||
|
}
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def get_value_by_locale(locale,add_more_index=nil)
|
||
|
application_form_submission_field = self.application_form_submission_field
|
||
|
case application_form_submission_field.markup
|
||
|
when "text_field"
|
||
|
case application_form_submission_field.add_more
|
||
|
when true
|
||
|
if application_form_submission_field.locale
|
||
|
add_more_index.nil? ? self.value.collect{|t| t[locale.to_s]}.join(",") : self.value(add_more_index)[locale]
|
||
|
else
|
||
|
add_more_index.nil? ? self.value.join(",") : self.value(add_more_index)
|
||
|
end
|
||
|
|
||
|
when false
|
||
|
application_form_submission_field.locale ? self[locale.to_s] : self.value
|
||
|
end
|
||
|
|
||
|
when "select"
|
||
|
markup_values = application_form_submission_field.self_defined_markup_options? ? application_form_submission_field.markup_value : self.application_form_submission_field.markup_value
|
||
|
markup_values[self.value][locale.to_s] rescue 'NoData'
|
||
|
|
||
|
when "text_area"
|
||
|
application_form_submission_field.locale ? self[locale.to_s] : self.value
|
||
|
|
||
|
when "date"
|
||
|
if application_form_submission_field.date_is_range?
|
||
|
get_date_by_format(:from) + ' ~ ' + get_date_by_format(:to)
|
||
|
# self.value["from"] + ' ~ ' + self.value["to"]
|
||
|
else
|
||
|
get_date_by_format
|
||
|
# self.value
|
||
|
end
|
||
|
|
||
|
when "address"
|
||
|
self.value[locale.to_s]
|
||
|
|
||
|
when "radio_button"
|
||
|
markup_values = application_form_submission_field.markup_value
|
||
|
markup_values[self.value][locale.to_s]
|
||
|
when "application_form_preferred_session"
|
||
|
application_form_submission_field.application_form_main.summary_chioices[self.value.to_i] rescue nil
|
||
|
when "checkbox"
|
||
|
markup_values = application_form_submission_field.markup_value
|
||
|
self.value.collect{|key| markup_values["#{key}"][I18n.locale]}.join(",")
|
||
|
when "date_durnation"
|
||
|
self.value
|
||
|
|
||
|
else
|
||
|
application_form_submission_field.locale ? self[locale.to_s] : self.value
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def get_date(item = nil)
|
||
|
case item
|
||
|
when :from
|
||
|
# data = self[:val]["from"]
|
||
|
data = self.value["from"]
|
||
|
when :to
|
||
|
# data = self[:val]["to"]
|
||
|
data = self.value["to"]
|
||
|
when nil
|
||
|
# data = self[:val]
|
||
|
data = self.value
|
||
|
end
|
||
|
|
||
|
# Date.new(data["(1i)"].to_i,data["(2i)"].to_i,data["(3i)"].to_i) rescue nil
|
||
|
end
|
||
|
|
||
|
def self.put_field_values(member, field_value_param, field_value_id=nil,field_value_status)
|
||
|
if field_value_status.eql?(true)
|
||
|
@application_form_submission_field_value = self.find(field_value_id) rescue nil
|
||
|
@application_form_submission_field_value.update(field_value_param) rescue nil
|
||
|
@application_form_submission_field_value.save rescue nil
|
||
|
else
|
||
|
@application_form_submission_field_value = member.application_form_submission_values.build(field_value_param) rescue nil
|
||
|
@application_form_submission_field_value.save
|
||
|
end
|
||
|
return @application_form_submission_field_value
|
||
|
end
|
||
|
|
||
|
|
||
|
protected
|
||
|
|
||
|
def unset_all_lang_values
|
||
|
VALID_LOCALES.each{|t| self.unset t}
|
||
|
end
|
||
|
|
||
|
def data_proc
|
||
|
application_form_submission_field = self.application_form_submission_field
|
||
|
unless self[:temp_data].nil?
|
||
|
case application_form_submission_field.markup
|
||
|
when "address"
|
||
|
self["val"] = self["temp_data"]
|
||
|
when 'text_field','text_area'
|
||
|
if application_form_submission_field.add_more
|
||
|
self["val"] = self["temp_data"]
|
||
|
else # if not add_more
|
||
|
if application_form_submission_field.can_muti_lang_input?
|
||
|
self[:temp_data].each do |key,val|
|
||
|
self[key] = val
|
||
|
end if(!application_form_submission_field.get_data[:cross_lang])
|
||
|
else
|
||
|
self["val"] = self[:temp_data]
|
||
|
end
|
||
|
end # of self.application_form_submission_field.add_more
|
||
|
|
||
|
when 'select','date','radio_button','application_form_preferred_session'
|
||
|
self["val"] = self[:temp_data]
|
||
|
when 'checkbox'
|
||
|
self["val"] = self[:temp_data].keys
|
||
|
end #end of case self.application_form_submission_field.markup
|
||
|
end # of self[:temp_data].nil?
|
||
|
self.unset('temp_data')
|
||
|
self.unset('temp')
|
||
|
end #of data_proc
|
||
|
|
||
|
def check_key
|
||
|
if self.application_form_submission_field_id.present? && self.application_form_submission_field.nil?
|
||
|
begin
|
||
|
self.application_form_submission_field = ApplicationFormSubmissionField.find(self.application_form_submission_field_id)
|
||
|
rescue
|
||
|
nil
|
||
|
end
|
||
|
end
|
||
|
self.key = self.application_form_submission_field.key rescue nil
|
||
|
end
|
||
|
|
||
|
def method_missing(*field)
|
||
|
if field.size < 1
|
||
|
self[field[0]]
|
||
|
else
|
||
|
self[(field[0].to_s.delete "=")] = field[1]
|
||
|
end
|
||
|
end
|
||
|
end
|