personal-journal/app/helpers/admin/journal_papers_helper.rb

191 lines
5.3 KiB
Ruby

module Admin::JournalPapersHelper
def page_for_journal_paper(journal_paper_object)
journal_paper_page = nil
pages = Page.where(:module=>"journal_paper")
journal_paper_page = pages.first if journal_paper_page.nil?
request.protocol+(request.host_with_port+journal_paper_page.url+'/'+journal_paper_object.to_param).gsub('//','/') rescue "/"
end
def get_paper_list
user = current_user.nil? ? OrbitHelper.current_user : current_user
user_profile = user.member_profile
journals = JournalPaper.where(:member_profile_id => user_profile.id)
journals = journals.collect do |j|
files = j.journal_paper_files.collect do |jpf|
{
"title" => jpf.title,
"description" => jpf.description,
"link" => jpf.journal_file.url,
"extension" => (jpf.journal_file.url.split(".").last rescue "")
}
end
{
"id" => j.id.to_s,
"edit_url" => "/#{I18n.locale.to_s}/admin/members/#{user_profile.to_param}/journal_papers/#{j.to_param}/edit",
"delete_url" => "/#{I18n.locale.to_s}/admin/journal_papers/#{j.id.to_s}",
"paper_title" => j.paper_title,
"journal_title" => j.journal_title,
"keywords" => j.keywords,
"abstract" => j.abstract,
"files" => files
}
end
journals
end
def import_this_journal_paper(row,mp)
value = nil
jp = JournalPaper.new
row.cells.each_with_index do |cell,index|
next if index < 2
next if cell.nil?
val = cell.value
next if val.nil? || val == ""
case index
when 2
value = {"en" => val}
when 3
begin
value["zh_tw"] = val
rescue
value = {"zh_tw" => val}
end
jp.paper_title_translations = value
when 4
value = {"en" => val}
when 5
begin
value["zh_tw"] = val
rescue
value = {"zh_tw" => val}
end
jp.journal_title_translations = value
when 6
value = {"en" => val}
when 7
begin
value["zh_tw"] = val
rescue
value = {"zh_tw" => val}
end
jp.authors_translations = value
when 8
jp.year = val
when 9
jp.language = val
when 10
jp.vol_no = val
when 11
jp.issue_no = val
when 12
jp.total_pages = val
when 13
jls = JournalLevel.asc(:created_at).all.to_a
ts = val.to_s.split(",")
ts.each do |t|
jp.journal_level_ids << jls[t.to_i].id if t.to_s.is_i? && t.to_i < jls.count
end
when 14
jpts = JournalPaperType.asc(:created_at).all.to_a
jp.journal_paper_type = jpts[val.to_i] if val.to_s.is_i? && val.to_i < jpts.count
when 15
jpas = JournalPaperAuthorType.asc(:created_at).all.to_a
ts = val.to_s.split(",")
ts.each do |t|
jp.journal_paper_author_type_ids << jpas[t.to_i].id if t.to_s.is_i? && t.to_i < jpas.count
end
when 16
jp.number_of_authors = val
when 17
jp.isbn = val
when 18
jp.publication_date = val
when 19
jp.url = val
when 20
jp.keywords = val
when 21
jp.abstract = val
when 22
jp.note = val
when 23
jp.associated_project_translations = {"en" => val, "zh_tw" => val}
end
end
jp.member_profile = mp
jp.save
end
def get_data_for_excel(year_start,year_end)
data = []
roles = Role.where(:disabled => false, :title.ne => "", :title.ne => nil).asc(:key)
roles.each do |role|
d = {}
d["name"] = role.title
mps = role.member_profile_ids
d1 = DateTime.new(year_start,1,1,0,0)
d2 = DateTime.new(year_end,12,31,23,59)
d["data"] = JournalPaper.where(:publication_date.gte => d1, :publication_date.lte => d2, :member_profile_id.in => mps) rescue []
data << d
end
return data
end
def get_chart_data(year_start,year_end,role,type)
case type
when "paper_type"
jls = JournalPaperType.all
when "level_type"
jls = JournalLevel.all
when "author_type"
jls = JournalPaperAuthorType.all
end
finaldata = []
role = Role.find(role) rescue nil
mps = []
if !role.nil?
mps = role.member_profile_ids
end
jls.each do |jl|
data = {}
data["name"] = jl.title
data["data"] = {}
(year_start..year_end).each do |year|
d1 = DateTime.new(year,1,1,0,0)
d2 = DateTime.new(year,12,31,23,59)
t = jl.journal_papers.where(:publication_date.gte => d1, :publication_date.lte => d2, :member_profile_id.in => mps).count rescue 0
data["data"][year.to_s] = t
end
finaldata << data
end
data = {"name" => "N/A", "data" => {}}
(year_start..year_end).each do |year|
d1 = DateTime.new(year,1,1,0,0)
d2 = DateTime.new(year,12,31,23,59)
case type
when "paper_type"
t = JournalPaper.where(:publication_date.gte => d1, :publication_date.lte => d2, :member_profile_id.in => mps, :journal_paper_type_id => nil).count rescue 0
when "level_type"
t = JournalPaper.where(:publication_date.gte => d1, :publication_date.lte => d2, :member_profile_id.in => mps, :journal_level_ids => nil).count rescue 0
when "author_type"
t = JournalPaper.where(:publication_date.gte => d1, :publication_date.lte => d2, :member_profile_id.in => mps, :journal_paper_author_type_ids => nil).count rescue 0
end
data["data"][year.to_s] = t
end
finaldata << data
finaldata
end
end