personal-journal/app/models/journal_paper.rb

244 lines
7.8 KiB
Ruby

class JournalPaper
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Attributes::Dynamic
include Admin::JournalPapersHelper
include OrbitModel::Status
include MemberHelper
include Slug
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
belongs_to :member_profile
belongs_to :journal_paper_type
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
scope :sort_for_frontend, ->{ where(:is_hidden=>false).order_by(:year => "desc", :publication_date => "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 << self.vol_no 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)
# if !self.publication_date.nil?
# pd = self.publication_date.strftime("%Y-%m-%d").split('-')
# title << pd[0]
# end
# title << self.year
title.delete(nil)
title.join(', ').gsub(', ,',',')
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
t = "journal_paper_author_types" if t == "author_type"
fields_to_remove << t if (datas.where(t.to_sym.ne => nil).count == 0 rescue false)
t = "author_type" if t == "journal_paper_author_types"
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"
pd_data << { "data_title" => "<a href='#{OrbitHelper.url_to_plugin_show(p.to_param,'personal_journal')}' title='#{p.paper_title}' target='_blank'>#{p.paper_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" => "<a href='#{OrbitHelper.url_to_plugin_show(p.to_param,'personal_journal')}' title='#{p.create_link}' 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 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}</li>")
end
value = files.join("")
when "url"
value = self.url.to_s.blank? ? "" : "<a href='#{self.url}'>#{self.url}</a>"
when "authors"
value = get_member_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