personal-journal/app/models/journal_paper.rb

270 lines
9.0 KiB
Ruby

class JournalPaper
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Attributes::Dynamic
include Admin::JournalPapersHelper
include OrbitModel::Status
include MemberHelper
include Slug
HideTimeInLinK = (File.exist?("#{Rails.root}/hide_time_fields_for_plugins.txt") rescue false)
field :paper_title, as: :slug_title, type: String, localize: true
field :journal_title, type: String, localize: true
field :authors, type: String, localize: true
field :associated_project , type: String, localize: true
field :year, type: String
field :language, type: String
field :isbn, type: String
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 :keywords, type: String
field :abstract, type: String
field :publication_date, type: DateTime
field :url, type: String
field :note, type: String
field :number_of_authors
field :rss2_id
# paginates_per 10
#relations
has_and_belongs_to_many :member_profile, class_name: "MemberProfile", foreign_key: 'member_profile_id'
belongs_to :journal_paper_type
has_and_belongs_to_many :journal_types
has_and_belongs_to_many :journal_levels
has_many :journal_paper_files, autosave: true, dependent: :destroy
accepts_nested_attributes_for :journal_paper_files, :allow_destroy => true
# has_and_belongs_to_many :journal_paper_authors, dependent: :destroy
# accepts_nested_attributes_for :journal_paper_authors
has_and_belongs_to_many :journal_paper_author_types
# after_save :save_journal_paper_files, :save_journal_paper_authors
before_validation :add_http
# validates :paper_title, presence: true
index({year: -1, :publication_date => -1, _id: -1}, { unique: false, background: false })
scope :sort_year_date, ->{ order_by(:year => "desc", :publication_date => "desc", :id=>"desc") }
scope :sort_for_frontend, ->{ where(:is_hidden=>false).order_by(:year => "desc", :publication_date => "desc", :id=>"desc") }
def create_link
title = []
title << self.authors if !self.authors.blank?
title << self.paper_title if self.paper_title.present?
title << self.journal_title if self.journal_title.present?
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")
# title << ( !self.journal_levels.blank? ? "(#{self.journal_levels.collect{|x| x.title}.join(', ')})" : nil)
unless HideTimeInLinK
if !self.publication_date.nil?
title << (self.publication_date.strftime("%b. %Y") rescue nil)
elsif self.year.present?
title << self.year
end
end
title.delete(nil)
title.join(', ').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 self.get_plugin_datas_to_member(datas)
page = Page.where(:module => "personal_journal").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 = [
"paper_title",
"journal_title",
"authors",
"year",
"issue_no",
"vol_no",
"level_type"
]
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
t2 = t
case t
when "form_to"
t2 = "form_to_start"
when "author_type"
t2 = "journal_paper_author_types"
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_journal.#{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"
link = OrbitHelper.url_to_plugin_show(p.to_param,'personal_journal')
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>") }
when "level_type"
pd_data << {"data_title" => ( !p.journal_levels.blank? ? "(#{p.journal_levels.collect{|x| x.title}.join(', ')})" : nil)}
when "publication_date"
pd_data << {"data_title" => (p.send(t).strftime("%Y/%m") rescue "")}
when "author_type"
pd_data << {"data_title" => (p.journal_paper_author_types.collect{|jat| jat.title}.join(", ") rescue "")}
else
pd_data << {"data_title" => p.send(t)}
end
else
if t == "paper_title"
pd_data << { "data_title" => p.create_link_tag_for_title}
else
pd_data << { "data_title" => p.send(t) }
end
end
end
{
"pd_datas" => pd_data
}
end
return [pd_title,plugin_datas]
end
def create_link_tag_for_title
link = OrbitHelper.url_to_plugin_show(self.to_param,'personal_journal')
url_to_plugin_show_blank = OrbitHelper.instance_variable_get(:@url_to_plugin_show_blank)
tmp_title = self.create_link
(url_to_plugin_show_blank ? tmp_title : "<a title=\"#{tmp_title}\" href=\"#{link}\" target=\"_blank\">#{tmp_title}</a>")
end
# def self.member_data
# members = MemberProfile.all
# member_data = []
# members.each do |m|
# if (m.first_name || m.last_name)
# member_data << {"memberId" => m.id.to_s, "memberName" => m.name}
# else
# member_data << {"memberId" => m.id.to_s, "memberName" => "No Name"}
# end
# end
# member_data.to_json
# end
# def save_journal_paper_files
# self.journal_paper_files.each do |t|
# if t.should_destroy
# t.destroy
# end
# end
# end
# def save_journal_paper_authors
# self.journal_paper_authors.each do |t|
# if t.should_destroy
# t.destroy
# end
# end
# 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 "level_type"
value = self.journal_levels.collect{|level| level.title}.join(',') rescue ""
when "author_type"
value = self.journal_paper_author_types.collect{|type| type.title}.join(',') rescue ""
when "paper_type"
value = self.journal_paper_type.title 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 "language"
value = I18n.t(self.language) if !self.language.nil? rescue ""
when "abstract"
value = nl2br(self.abstract)
when "file"
files = []
self.journal_paper_files.each do |file|
url = file.journal_file.url
title = ((file.title.blank? ? File.basename(file.journal_file.path) : file.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
{
"key"=>field,
"title_class"=>"journal-#{field.gsub('_','-')}-field",
"value_class"=>"journal-#{field.gsub('_','-')}-value",
"title"=>I18n.t('personal_journal.'+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