personal-conference/app/models/writing_conference.rb

220 lines
7.5 KiB
Ruby

class WritingConference
include Mongoid::Document
include Mongoid::Timestamps
include OrbitModel::Status
include OrbitTag::Taggable
include MemberHelper
include Slug
field :paper_title, as: :slug_title, localize: true
field :conference_title, localize: true
field :authors, localize: true
field :location, localize: true
field :sponsor, localize: true
field :year
field :language
field :period_start_date, :type => Date
field :period_end_date, :type => Date
field :keywords
field :abstract
field :publication_date, :type => Date
field :url
field :note
field :create_user_id, :type => BSON::ObjectId
field :update_user_id, :type => BSON::ObjectId
field :isbn
field :isi_number
field :number_of_authors
field :rss2_id
belongs_to :member_profile
has_and_belongs_to_many :conference_author_types
has_and_belongs_to_many :conference_paper_types
has_and_belongs_to_many :conference_paper_levels
has_many :writing_conference_files, :autosave => true, :dependent => :destroy
accepts_nested_attributes_for :writing_conference_files, :allow_destroy => true
before_validation :add_http
scope :sort_for_frontend, ->{ where(:is_hidden=>false).order_by(:year => "desc", :publication_date => "desc", :period_start_date => "desc") }
def create_link
title = []
# title << self.member_profile.name if self.member_profile_id.present?
title << self.authors if self.authors.present?
title << self.paper_title if self.paper_title.present?
title << self.conference_title if self.conference_title.present?
title << self.location if self.location.present?
paper_types = ( !self.conference_paper_types.blank? ? self.conference_paper_types.collect{|x| x.title}.join(', ') : "")
paper_levels = ( !self.conference_paper_levels.blank? ? "(#{self.conference_paper_levels.collect{|x| x.title}.join(', ')})" : "")
title << paper_types + paper_levels
title << "#{period_start_date}-#{period_end_date}" if (self.period_start_date.present? && self.period_end_date.present?)
# if !self.publication_date.nil?
# pd = self.publication_date.strftime("%Y-%m-%d").split('-')
# title << pd[0]
# end
# title << self.year
title = title.join(', ')
title.gsub(/\s+/, "").chomp(",")
end
def duration
if !self.send('period_start_date').nil? or !self.send('period_end_date').nil?
date = (self.send('period_start_date').strftime('%Y.%m.%d') rescue "")+' ~ '+(self.send('period_end_date').strftime('%Y.%m.%d') rescue I18n.t('personal_project.up_to_today'))
else
date = ""
end
end
def self.get_plugin_datas_to_member(datas)
page = Page.where(:module => "personal_conference").first rescue nil
if !page.nil? && page.custom_string_field == "table"
if !page.custom_array_field.blank?
fields_to_show = page.custom_array_field
else
fields_to_show = [
"authors",
"paper_title",
"conference_title",
"location",
"paper_types",
"paper_levels"
]
end
else
fields_to_show = [
"year",
"paper_title"
]
end
fields_to_remove = []
pd_title = []
fields_to_show.each do |t|
if (self.fields[t].type.to_s == "String" rescue false)
fields_to_remove << t if (datas.where(t.to_sym.ne => nil).or(t.to_sym.ne => "").count == 0 rescue false)
else
t = "conference_author_types" if t == "author_type"
t = "period_start_date" if t == "period"
fields_to_remove << t if (datas.where(t.to_sym.ne => nil).count == 0 rescue false)
t = "author_type" if t == "conference_author_types"
t = "period" if t == "period_start_date"
end
pd_title << {
"plugin_data_title" => I18n.t("personal_conference.#{t}")
} if !fields_to_remove.include?(t)
end
fields_to_show = fields_to_show - fields_to_remove
plugin_datas = datas.sort_for_frontend.collect do |p|
pd_data = []
fields_to_show.collect do |t|
if !page.nil? && page.custom_string_field == "table"
case t
when "paper_title"
pd_data << { "data_title" => "<a href='#{OrbitHelper.url_to_plugin_show(p.to_param,'personal_conference')}' target='_blank'>#{p.paper_title}</a>" }
when "paper_types"
pd_data << {"data_title" => ( !p.conference_paper_types.blank? ? p.conference_paper_types.collect{|x| x.title}.join(', ') : "")}
when "paper_levels"
pd_data << {"data_title" => ( !p.conference_paper_levels.blank? ? p.conference_paper_levels.collect{|x| x.title}.join(', ') : "")}
when "author_type"
pd_data << {"data_title" => (p.conference_author_types.collect{|cat| cat.title}.join(", ") rescue "")}
when "period"
pd_data << {"data_title" => p.duration}
else
pd_data << { "data_title" => p.send(t) }
end
else
if t == "paper_title"
pd_data << { "data_title" => "<a href='#{OrbitHelper.url_to_plugin_show(p.to_param,'personal_conference')}' target='_blank'>#{p.create_link}</a>" }
else
pd_data << { "data_title" => p.send(t) }
end
end
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)
case field
when "paper_level"
value = self.conference_paper_levels.collect{|level| level.title}.join(',') rescue ""
when "author_type"
value = self.conference_author_types.collect{|type| type.title}.join(',') rescue ""
when "paper_type"
value = self.conference_paper_types.collect{|type| type.title}.join(',') rescue ""
when "author_name"
value = []
([I18n.locale]+(Site.first.in_use_locales-[I18n.locale])).each do |locale|
if self.member_profile.first_name_translations[locale] || self.member_profile.last_name_translations[locale]
value << "#{self.member_profile.first_name_translations[locale]} #{self.member_profile.last_name_translations[locale]}"
end
end
value = value.join(" / ")
when "publication_date"
value = self.publication_date.strftime("%Y-%m-%d") rescue ""
when "abstract"
value = nl2br(self.abstract)
when "language"
value = I18n.t(self.language) if !self.language.nil? rescue ""
when "file"
files = []
self.writing_conference_files.each do |conference_files|
url = conference_files.file.url
title = (conference_files.title.blank? ? File.basename(conference_files.file.path) : conference_files.title)
files << "<li><a href='#{url}'' target='_blank'>#{title}</li>"
end
value = files.join("")
else
value = self.send(field) rescue ""
end
value = (value =~ /\A#{URI::regexp(['http', 'https'])}\z/) ? "<a href='#{value}' target='blank'>#{value}</a>" : value
{
"key"=>field,
"title_class"=>"conference-#{field.gsub('_','-')}-field",
"value_class"=>"conference-#{field.gsub('_','-')}-value",
"title"=>I18n.t('personal_conference.'+field),
"value"=>value
}
end
protected
def add_http
unless self.url.blank? || self.url[/^http:\/\//] || self.url[/^https:\/\//]
self.url = 'http://' + self.url
end
end
end