personal-conference/app/models/writing_conference.rb

231 lines
8.1 KiB
Ruby
Raw Normal View History

2014-07-01 10:19:07 +00:00
class WritingConference
include Mongoid::Document
include Mongoid::Timestamps
include Admin::PersonalConferencesHelper
2014-07-01 10:19:07 +00:00
include OrbitModel::Status
include OrbitTag::Taggable
2015-01-09 07:32:31 +00:00
include MemberHelper
2014-07-01 10:19:07 +00:00
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
2014-08-11 11:18:02 +00:00
field :number_of_authors
2014-07-15 07:06:17 +00:00
field :rss2_id
2014-07-01 10:19:07 +00:00
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
2016-04-11 10:01:45 +00:00
scope :sort_for_frontend, ->{ where(:is_hidden=>false).order_by(:year => "desc", :publication_date => "desc", :period_start_date => "desc") }
2015-12-02 10:30:10 +00:00
2014-07-01 10:19:07 +00:00
def create_link
title = []
2015-05-08 08:36:21 +00:00
# title << self.member_profile.name if self.member_profile_id.present?
2014-07-01 10:19:07 +00:00
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?
2015-01-09 07:32:31 +00:00
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
2014-07-01 10:19:07 +00:00
title << "#{period_start_date}-#{period_end_date}" if (self.period_start_date.present? && self.period_end_date.present?)
2015-01-09 07:32:31 +00:00
# if !self.publication_date.nil?
# pd = self.publication_date.strftime("%Y-%m-%d").split('-')
# title << pd[0]
# end
2016-01-11 12:36:16 +00:00
# title << self.year
2015-01-09 07:32:31 +00:00
2017-08-16 10:22:58 +00:00
title = title.join(', ')
2017-08-16 10:29:59 +00:00
title.rstrip.chomp(",")
2014-07-01 10:19:07 +00:00
end
2016-04-06 06:31:26 +00:00
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
2016-03-22 10:00:05 +00:00
2014-07-01 10:19:07 +00:00
2016-01-11 12:36:16 +00:00
def self.get_plugin_datas_to_member(datas)
2016-03-22 10:00:05 +00:00
page = Page.where(:module => "personal_conference").first rescue nil
2016-03-24 17:32:54 +00:00
if !page.nil? && page.custom_string_field == "table"
2016-04-01 05:58:56 +00:00
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
2016-03-22 10:00:05 +00:00
else
fields_to_show = [
"year",
"paper_title"
]
end
2016-01-11 12:36:16 +00:00
2016-02-15 10:30:38 +00:00
fields_to_remove = []
pd_title = []
fields_to_show.each do |t|
if (self.fields[t].type.to_s == "String" rescue false)
2016-02-15 10:50:23 +00:00
fields_to_remove << t if (datas.where(t.to_sym.ne => nil).or(t.to_sym.ne => "").count == 0 rescue false)
2016-02-15 10:30:38 +00:00
else
2016-04-06 06:07:46 +00:00
t = "conference_author_types" if t == "author_type"
2016-04-06 06:35:47 +00:00
t = "period_start_date" if t == "period"
2016-02-15 10:30:38 +00:00
fields_to_remove << t if (datas.where(t.to_sym.ne => nil).count == 0 rescue false)
2016-04-06 06:07:46 +00:00
t = "author_type" if t == "conference_author_types"
2016-04-06 06:35:47 +00:00
t = "period" if t == "period_start_date"
2016-02-15 10:30:38 +00:00
end
pd_title << {
"plugin_data_title" => I18n.t("personal_conference.#{t}")
} if !fields_to_remove.include?(t)
2016-01-11 12:36:16 +00:00
end
2016-02-15 10:30:38 +00:00
fields_to_show = fields_to_show - fields_to_remove
2016-01-11 12:36:16 +00:00
plugin_datas = datas.sort_for_frontend.collect do |p|
pd_data = []
fields_to_show.collect do |t|
2016-03-24 17:37:18 +00:00
if !page.nil? && page.custom_string_field == "table"
2016-03-22 10:00:05 +00:00
case t
when "paper_title"
2022-01-03 14:48:45 +00:00
link = OrbitHelper.url_to_plugin_show(p.to_param,'personal_conference')
link = (link == "#" ? p.paper_title : "<a href='#{link}' title=\"#{p.paper_title}\" target='_blank'>#{p.paper_title}</a>")
pd_data << { "data_title" => link}
2016-03-22 10:00:05 +00:00
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(', ') : "")}
2016-04-01 12:58:59 +00:00
when "author_type"
2016-04-06 04:53:45 +00:00
pd_data << {"data_title" => (p.conference_author_types.collect{|cat| cat.title}.join(", ") rescue "")}
2016-04-06 06:31:26 +00:00
when "period"
pd_data << {"data_title" => p.duration}
2017-12-18 08:21:45 +00:00
when "url"
2020-11-27 07:06:42 +00:00
pd_data << { "data_title" => "<a href='#{p.url}' title=\"#{p.url}\" target='_blank'>#{p.url}</a>" }
2016-03-22 10:00:05 +00:00
else
pd_data << { "data_title" => p.send(t) }
end
2016-01-11 12:36:16 +00:00
else
2016-03-22 10:00:05 +00:00
if t == "paper_title"
2022-01-03 14:48:45 +00:00
link = OrbitHelper.url_to_plugin_show(p.to_param,'personal_conference')
link = (link == "#" ? p.create_link : "<a href='#{link}' title=\"#{p.create_link}\" target='_blank'>#{p.create_link}</a>")
pd_data << { "data_title" => link }
2016-03-22 10:00:05 +00:00
else
pd_data << { "data_title" => p.send(t) }
end
2016-01-11 12:36:16 +00:00
end
end
{
"pd_datas" => pd_data
}
end
return [pd_title,plugin_datas]
end
2014-08-01 11:04:43 +00:00
def get_plugin_data(fields_to_show)
plugin_datas = []
fields_to_show.each do |field|
2014-10-03 06:14:27 +00:00
plugin_data = self.get_plugin_field_data(field) rescue nil
2014-08-01 11:04:43 +00:00
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 ""
2014-08-11 11:18:02 +00:00
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(" / ")
2014-08-01 11:04:43 +00:00
when "publication_date"
2016-04-06 06:14:41 +00:00
value = self.publication_date.strftime("%Y-%m-%d") rescue ""
2015-01-09 07:32:31 +00:00
when "abstract"
value = nl2br(self.abstract)
2014-08-01 11:04:43 +00:00
when "language"
2016-04-06 06:14:41 +00:00
value = I18n.t(self.language) if !self.language.nil? rescue ""
2014-08-01 11:04:43 +00:00
when "file"
files = []
self.writing_conference_files.each do |conference_files|
url = conference_files.file.url
2019-08-19 08:48:36 +00:00
title = ((conference_files.title.blank? ? File.basename(conference_files.file.path) : conference_files.title) rescue "")
2020-11-27 07:06:42 +00:00
files << (url.nil? ? "" : "<li><a href='#{url}' target='_blank'>#{title}</a></li>")
2014-08-01 11:04:43 +00:00
end
value = files.join("")
when "url"
value = self.url.to_s.blank? ? "" : "<a href='#{self.url}'>#{self.url}</a>"
when "authors"
2021-12-16 07:23:06 +00:00
value = get_authors_show(self) rescue ""
2014-08-01 11:04:43 +00:00
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
}
2014-07-01 10:19:07 +00:00
end
protected
def add_http
unless self.url.blank? || self.url[/^http:\/\//] || self.url[/^https:\/\//]
self.url = 'http://' + self.url
end
end
end