201 lines
7.2 KiB
Ruby
201 lines
7.2 KiB
Ruby
class Activity
|
|
include Mongoid::Document
|
|
include Mongoid::Timestamps
|
|
include OrbitModel::Status
|
|
include MemberHelper
|
|
include Admin::ActivitiesHelper
|
|
include Slug
|
|
|
|
field :year, :type => Integer, :default => DateTime.now.year
|
|
field :publish_date, :type => Date, :default => Date.today
|
|
field :start_date, :type => Date, :default => Date.today
|
|
field :end_date, :type => Date, :default => Date.today
|
|
field :activity_name, :type => String, :default => "", :localize => true, as: :slug_title
|
|
field :work_title, :type => String, :default => "", :localize => true
|
|
field :activity_organizer, :type => String, :default => "", :localize => true
|
|
field :activity_co_organizer, :type => String, :default => "", :localize => true
|
|
field :region, :type => String, :default => "", :localize => true
|
|
field :road_show, :type => String, :default => "", :localize => true
|
|
field :tour_details, :type => String, :default => "", :localize => true
|
|
field :award_name, :type => String, :default => "", :localize => true
|
|
field :awarding_unit, :type => String, :default => "", :localize => true
|
|
field :authors_type, :type => String, :default => "", :localize => true
|
|
field :authors, :type => String, :default => "", :localize => true
|
|
|
|
|
|
|
|
|
|
field :rss2_id
|
|
belongs_to :member_profile
|
|
index({:year=>-1, :start_date=>-1, :id=>-1}, { unique: false, background: false })
|
|
scope :sort_hash, ->{ order_by({:year=>-1, :start_date=>-1, :id=>-1}) }
|
|
scope :sort_for_frontend, ->{ where(:is_hidden=>false).order_by({:year=>-1, :start_date=>-1, :id=>-1}) }
|
|
|
|
before_save do
|
|
|
|
end
|
|
def parse_time(time_str)
|
|
DateTime.parse("0000-01-01 " + time_str)
|
|
end
|
|
def self.get_plugin_datas_to_member(datas)
|
|
page = Page.where(:module => "personal_activity").first rescue nil
|
|
title_is_paper_format = false
|
|
if !page.nil? && !page.custom_array_field.blank?
|
|
fields_to_show = page.custom_array_field
|
|
else
|
|
fields_to_show = ["year", "activity_name"]
|
|
end
|
|
|
|
fields_to_remove = []
|
|
|
|
pd_title = []
|
|
|
|
fields_to_show.each do |t|
|
|
if t == 'authors'
|
|
t_f = 'member_profile'
|
|
else
|
|
t_f = t
|
|
end
|
|
if (self.fields[t_f].type.to_s == "String" || self.fields[t_f].type.to_s == "Object" rescue false)
|
|
fields_to_remove << t if (datas.where(t.to_sym.ne => nil, t.to_sym.ne => "").count == 0 rescue false)
|
|
elsif (self.relations.include?(t.pluralize) rescue false)
|
|
fields_to_remove << t if (datas.where(t.pluralize.to_sym.ne=>[]).count == 0 rescue false)
|
|
elsif [].include?(t)
|
|
fields_to_remove << t if (datas.select{|d| d.send(t) != " ~ " }.count == 0 rescue false)
|
|
else
|
|
fields_to_remove << t if (datas.where(t.to_sym.ne => nil).count == 0 rescue false)
|
|
end
|
|
pd_title << {
|
|
"plugin_data_title" => I18n.t("personal_activity.#{t}")
|
|
} if !fields_to_remove.include?(t)
|
|
end
|
|
|
|
fields_to_show = fields_to_show - fields_to_remove
|
|
|
|
plugin_datas = datas.sort_for_frontend.collect.with_index do |p,idx|
|
|
|
|
pd_data = []
|
|
fields_to_show.collect do |t|
|
|
pd_data << { "data_title" => p.display_field(t, false, title_is_paper_format) }
|
|
end
|
|
{
|
|
"pd_datas" => pd_data
|
|
}
|
|
end
|
|
return [pd_title,plugin_datas]
|
|
end
|
|
|
|
def get_plugin_data(fields_to_show)
|
|
plugin_datas = []
|
|
fields_to_show.each do |field|
|
|
plugin_data = self.get_plugin_field_data(field) rescue nil
|
|
next if plugin_data.blank? or plugin_data['value'].blank?
|
|
plugin_datas << plugin_data
|
|
end
|
|
plugin_datas
|
|
end
|
|
|
|
def get_plugin_field_data(field)
|
|
activity = self
|
|
value = activity.send(field) rescue ""
|
|
if field.include?(".")
|
|
value = activity
|
|
field.split(".").each{|f| value = value.send(f) rescue nil }
|
|
end
|
|
file_fields = []
|
|
link_fields = []
|
|
member_fields = []
|
|
if file_fields.include?(field)
|
|
files = activity.send(field.pluralize)
|
|
value = files.map do |file|
|
|
url = file.file.url
|
|
title = (file.title.blank? ? File.basename(file.file.path) : file.title)
|
|
"<li><a href='#{url}'' target='_blank'>#{title}</li>"
|
|
end
|
|
value = value.join("")
|
|
elsif link_fields.include?(field)
|
|
links = activity.send(field.pluralize)
|
|
value = links.map do |link|
|
|
url = link.url
|
|
title = (link.title.blank? ? url : link.title)
|
|
"<li><a href='#{url}'' target='_blank'>#{title}</li>"
|
|
end
|
|
value = value.join("")
|
|
elsif member_fields.include?(field)
|
|
members = activity.send(field.pluralize)
|
|
value = members.map{|m|
|
|
path = OrbitHelper.url_to_plugin_show(m.to_param, 'member') rescue '#'
|
|
((text_only rescue false) ? m.name : "<a href='#{path}'>#{m.name}</a>")
|
|
}
|
|
join_text = (text_only rescue false) ? "," : "<br>"
|
|
value = value.join(join_text)
|
|
elsif field == "member_profile" || field == "authors"
|
|
value = get_authors_show(activity)
|
|
end
|
|
strftime_hash = {"publish_date"=>"%Y/%m/%d", "start_date"=>"%Y/%m/%d", "end_date"=>"%Y/%m/%d"}
|
|
if strftime_hash.keys.include?(field)
|
|
value = value.strftime(strftime_hash[field]) rescue value
|
|
end
|
|
value
|
|
|
|
value = (value =~ /\A#{URI::regexp(['http', 'https'])}\z/) ? "<a href='#{value}' target='blank'>#{value}</a>" : value
|
|
|
|
{
|
|
"key"=>field,
|
|
"title_class"=>"activity-#{field.gsub('_','-')}-field",
|
|
"value_class"=>"activity-#{field.gsub('_','-')}-value",
|
|
"title"=>I18n.t('personal_activity.'+field),
|
|
"value"=>value
|
|
}
|
|
end
|
|
|
|
def display_field(field,text_only=false,title_is_paper_format=false)
|
|
activity = self
|
|
value = activity.send(field) rescue ""
|
|
if field.include?(".")
|
|
value = activity
|
|
field.split(".").each{|f| value = value.send(f) rescue nil }
|
|
end
|
|
file_fields = []
|
|
link_fields = []
|
|
member_fields = []
|
|
if file_fields.include?(field)
|
|
files = activity.send(field.pluralize)
|
|
value = files.map do |file|
|
|
url = file.file.url
|
|
title = (file.title.blank? ? File.basename(file.file.path) : file.title)
|
|
"<li><a href='#{url}'' target='_blank'>#{title}</li>"
|
|
end
|
|
value = value.join("")
|
|
elsif link_fields.include?(field)
|
|
links = activity.send(field.pluralize)
|
|
value = links.map do |link|
|
|
url = link.url
|
|
title = (link.title.blank? ? url : link.title)
|
|
"<li><a href='#{url}'' target='_blank'>#{title}</li>"
|
|
end
|
|
value = value.join("")
|
|
elsif member_fields.include?(field)
|
|
members = activity.send(field.pluralize)
|
|
value = members.map{|m|
|
|
path = OrbitHelper.url_to_plugin_show(m.to_param, 'member') rescue '#'
|
|
((text_only rescue false) ? m.name : "<a href='#{path}'>#{m.name}</a>")
|
|
}
|
|
join_text = (text_only rescue false) ? "," : "<br>"
|
|
value = value.join(join_text)
|
|
elsif field == "member_profile" || field == "authors"
|
|
value = get_authors_show(activity)
|
|
end
|
|
strftime_hash = {"publish_date"=>"%Y/%m/%d", "start_date"=>"%Y/%m/%d", "end_date"=>"%Y/%m/%d"}
|
|
if strftime_hash.keys.include?(field)
|
|
value = value.strftime(strftime_hash[field]) rescue value
|
|
end
|
|
if field == "activity_name"
|
|
link = OrbitHelper.url_to_plugin_show(activity.to_param,'personal_activity')
|
|
tmp_title = value
|
|
url_to_plugin_show_blank = OrbitHelper.instance_variable_get(:@url_to_plugin_show_blank)
|
|
value = url_to_plugin_show_blank ? tmp_title : "<a href='#{link}' target='_blank' title='#{tmp_title}'>#{tmp_title}</a>"
|
|
end
|
|
value
|
|
end
|
|
end |