personal-conference/app/models/writing_conference.rb

286 lines
11 KiB
Ruby

class WritingConference
include Mongoid::Document
include Mongoid::Timestamps
include Admin::PersonalConferencesHelper
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 :vol_no, type: String
field :issue_no, type: String
field :form_to_start, type: String
field :form_to_end, type: String
field :total_pages, type: String
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
index({year: -1, publication_date: -1, period_start_date: -1, _id: -1}, { unique: false, background: false })
scope :sort_year_date, ->{ order_by(:year => "desc", :publication_date => "desc", :period_start_date => "desc", :id => "desc") }
scope :sort_for_frontend, ->{ where(:is_hidden=>false).order_by(:year => "desc", :publication_date => "desc", :period_start_date => "desc", :id => "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?
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 << "vol. " + self.vol_no.strip if (self.vol_no.present? && self.vol_no != "0")
title << self.issue_no if (self.issue_no.present? && self.issue_no != "0")
title << "pp." + self.form_to_start.to_s+"-"+self.form_to_end.to_s if (self.form_to_start.present? && self.form_to_start != "0")
if self.period_start_date.present? && self.period_end_date.present?
title << self.duration
elsif !self.publication_date.nil?
title << (self.publication_date.strftime("%b. %Y") rescue nil)
elsif self.year.present?
title << self.year
end
if self.location.present?
title << self.location
end
title = title.join(', ')
title.sub(/^\\s*,/,'').gsub(/,(\s*,)+/,',').strip
end
def form_to
if (self.form_to_start.present? && self.form_to_start != "0")
self.form_to_start.to_s+"-"+self.form_to_end.to_s
else
""
end
end
def duration(date_format="%b. %d, %Y")
period_start_date_present = self.period_start_date.present?
period_end_date_present = self.period_end_date.present?
date = ""
if period_start_date_present && period_end_date_present
if self.period_start_date == self.period_end_date
date = self.period_start_date.strftime(date_format)
else
period_start_ym = [self.period_start_date.year, self.period_start_date.month]
period_end_ym = [self.period_end_date.year, self.period_end_date.month]
same_year = period_start_ym[0] == period_end_ym[0]
same_month = period_start_ym[1] == period_end_ym[1]
month_date_format = date_format.sub('%Y','').gsub(',','').strip.sub(/^[\/\-\.]/,'').sub(/[\/\-\.]$/,'')
if same_year && same_month
date = self.period_start_date.strftime("#{month_date_format}-#{self.period_end_date.day}, %Y")
elsif same_year
date = self.period_start_date.strftime("#{month_date_format}-#{self.period_end_date.strftime(month_date_format)}, %Y")
else
date = self.period_start_date.strftime(date_format) + ' ~ ' + self.period_end_date.strftime(date_format)
end
end
elsif period_start_date_present || period_end_date_present
if self.period_start_date
date = self.period_start_date.strftime('%b. %d, %Y') + ' ~ ' + I18n.t('personal_conference.up_to_today')
else
date = self.period_end_date.strftime('%b. %d, %Y')
end
end
date
end
def self.get_plugin_datas_to_member(datas)
get_setting
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 = @setting.display_fields_in_member_page.clone
fields_to_show << "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
t2 = t
case t
when "form_to"
t2 = "form_to_start"
when "author_type"
t2 = "conference_author_types"
when "period"
t2 = "period_start_date"
end
fields_to_remove << t if (datas.where(t2.to_sym.ne => nil).count == 0 rescue false)
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
date_format = (I18n.locale == :zh_tw ? '%Y-%m-%d' : '%b. %d, %Y')
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"
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}
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(date_format)}
when "url"
pd_data << { "data_title" => "<a href='#{p.url}' title=\"#{p.url}\" target='_blank'>#{p.url}</a>" }
when "publication_date"
pd_data << { "data_title" => (p.publication_date.strftime(@setting.publication_date_format) rescue "") }
else
pd_data << { "data_title" => p.send(t) }
end
else
if t == "paper_title"
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 }
elsif t == "publication_date"
pd_data << { "data_title" => (p.publication_date.strftime(@setting.publication_date_format) rescue "") }
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 "period"
value = self.duration((I18n.locale == :zh_tw ? '%Y-%m-%d' : '%b. %d, %Y'))
when "publication_date"
get_setting
value = self.publication_date.strftime(@setting.publication_date_format) rescue ""
when "abstract"
value = nl2br(self.abstract)
when "language"
value = I18n.t(self.language, :default=> 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) rescue "")
files << (url.nil? ? "" : "<li><a href='#{url}' target='_blank'>#{title}</a></li>")
end
value = files.join("")
when "url"
value = self.url.to_s.blank? ? "" : "<a href='#{self.url}'>#{self.url}</a>"
when "authors"
value = get_authors_show(self) rescue ""
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
def get_setting
@setting ||= WritingConferenceSetting.first
end
def self.get_setting
@setting = WritingConferenceSetting.first
end
protected
def add_http
unless self.url.blank? || self.url[/^http:\/\//] || self.url[/^https:\/\//]
self.url = 'http://' + self.url
end
end
end