personal-conference/app/models/writing_conference.rb

290 lines
11 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
HideTimeInLinK = (File.exist?("#{Rails.root}/hide_time_fields_for_plugins.txt") rescue false)
2014-07-01 10:19:07 +00:00
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
2022-08-23 10:40:36 +00:00
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
2014-07-01 10:19:07 +00:00
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
2022-07-26 11:11:56 +00:00
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") }
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?
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
2022-08-23 10:40:36 +00:00
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")
unless HideTimeInLinK
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
2022-08-23 10:40:36 +00:00
end
if self.location.present?
title << self.location
end
2017-08-16 10:22:58 +00:00
title = title.join(', ')
2022-08-23 10:40:36 +00:00
title.sub(/^\\s*,/,'').gsub(/,(\s*,)+/,',').strip
2014-07-01 10:19:07 +00:00
end
2022-08-23 10:40:36 +00:00
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
2016-04-06 06:31:26 +00:00
else
2022-08-23 10:40:36 +00:00
""
2016-04-06 06:31:26 +00:00
end
end
2022-08-23 10:40:36 +00:00
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]
if same_year && same_month
2022-08-23 10:56:40 +00:00
date = self.period_start_date.strftime(date_format.sub('%d', "%d-#{self.period_end_date.strftime('%d')}"))
2022-08-23 10:40:36 +00:00
elsif same_year
2022-08-23 12:26:46 +00:00
month_date_format = date_format.match(/(%b|%m|%d)[\-\/\. ]*(%b|%m|%d)/)[0] rescue ''
2022-08-23 10:52:59 +00:00
date = self.period_start_date.strftime(date_format.sub(month_date_format, "#{month_date_format}-#{self.period_end_date.strftime(month_date_format)}"))
2022-08-23 10:40:36 +00:00
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
2022-08-23 10:52:59 +00:00
date = self.period_start_date.strftime(date_format) + ' ~ ' + I18n.t('personal_conference.up_to_today')
2022-08-23 10:40:36 +00:00
else
2022-08-23 10:52:59 +00:00
date = self.period_end_date.strftime(date_format)
2022-08-23 10:40:36 +00:00
end
end
date
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)
get_setting
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 = @setting.display_fields_in_member_page.clone
fields_to_show << "paper_title"
2016-03-22 10:00:05 +00:00
end
fields_to_remove = []
2016-02-15 10:30:38 +00:00
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
2022-08-23 10:40:36 +00:00
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)
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
2022-08-23 10:52:59 +00:00
date_format = (I18n.locale == :zh_tw ? '%Y/%m/%d' : '%b. %d, %Y')
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
2022-08-23 10:40:36 +00:00
when "paper_title"
link = OrbitHelper.url_to_plugin_show(p.to_param,'personal_conference')
2022-12-15 11:08:43 +00:00
url_to_plugin_show_blank = OrbitHelper.instance_variable_get(:@url_to_plugin_show_blank)
tmp_title = p.paper_title
pd_data << { "data_title" => (url_to_plugin_show_blank ? tmp_title : "<a title=\"#{tmp_title}\" href=\"#{link}\" target=\"_blank\">#{tmp_title}</a>") }
2022-08-23 10:40:36 +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(', ') : "")}
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) }
2016-03-22 10:00:05 +00:00
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')
2022-12-15 11:08:43 +00:00
url_to_plugin_show_blank = OrbitHelper.instance_variable_get(:@url_to_plugin_show_blank)
tmp_title = p.create_link
pd_data << { "data_title" => (url_to_plugin_show_blank ? tmp_title : "<a title=\"#{tmp_title}\" href=\"#{link}\" target=\"_blank\">#{tmp_title}</a>") }
elsif t == "publication_date"
pd_data << { "data_title" => (p.publication_date.strftime(@setting.publication_date_format) rescue "") }
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(" / ")
2022-08-23 10:40:36 +00:00
when "period"
2022-08-23 10:52:59 +00:00
value = self.duration((I18n.locale == :zh_tw ? '%Y/%m/%d' : '%b. %d, %Y'))
2014-08-01 11:04:43 +00:00
when "publication_date"
get_setting
value = self.publication_date.strftime(@setting.publication_date_format) 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"
2022-08-18 06:35:01 +00:00
value = I18n.t(self.language, :default=> 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
def get_setting
@setting ||= WritingConferenceSetting.first
end
def self.get_setting
2022-01-12 00:06:16 +00:00
@setting = WritingConferenceSetting.first
end
2014-07-01 10:19:07 +00:00
protected
def add_http
unless self.url.blank? || self.url[/^http:\/\//] || self.url[/^https:\/\//]
self.url = 'http://' + self.url
end
end
end