personal-book/app/helpers/admin/personal_books_helper.rb

173 lines
4.7 KiB
Ruby

module Admin::PersonalBooksHelper
def get_paper_list
user = current_user.nil? ? OrbitHelper.current_user : current_user
user_profile = user.member_profile
books = Book.where(:member_profile_id => user_profile.id)
books = books.collect do |b|
files = b.book_files.collect do |bf|
{
"title" => bf.title,
"description" => bf.description,
"link" => bf.member_book_file.url,
"extension" => (bf.file.url.split(".").last rescue "")
}
end
{
"id" => b.id.to_s,
"edit_url" => "/#{I18n.locale.to_s}/admin/members/#{user_profile.to_param}/books/#{b.to_param}/edit",
"delete_url" => "/#{I18n.locale.to_s}/admin/books/#{b.id.to_s}",
"paper_title" => b.book_title,
"keywords" => b.keywords,
"files" => files
}
end
books
end
def import_this_book(row, mp)
value = nil
book = Book.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
book.book_title_translations = value
when 4
value = {"en" => val}
when 5
begin
value["zh_tw"] = val
rescue
value = {"zh_tw" => val}
end
book.extracted_chapters_translations = value
when 6
value = {"en" => val}
when 7
begin
value["zh_tw"] = val
rescue
value = {"zh_tw" => val}
end
book.publisher_translations = value
when 8
value = {"en" => val}
when 9
begin
value["zh_tw"] = val
rescue
value = {"zh_tw" => val}
end
book.editor_translations = value
when 10
value = {"en" => val}
when 11
begin
value["zh_tw"] = val
rescue
value = {"zh_tw" => val}
end
book.authors_translations = value
when 12
book.year = val
when 13
book.language = val
when 14
bts = BookType.asc(:created_at).all.to_a
book.book_type = bts[val.to_i] if val.to_s.is_i? && val.to_i < bts.count
when 15
book.pages = val
when 16
bats = BookAuthorType.asc(:created_at).all.to_a
ts = val.to_s.split(",")
ts.each do |t|
book.book_author_type_ids << bats[t.to_i].id if t.to_s.is_i? && t.to_i < bats.count
end
when 17
book.number_of_authors = val
when 18
book.publish_date = val
when 19
book.isbn = val
when 20
book.url = val
when 21
book.keywords = val
when 22
book.note = val
end
end
book.member_profile = mp
book.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"] = Book.where(:year.gte => year_start, :year.lte => year_end, :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 "book_type"
jls = BookType.all
when "author_type"
jls = BookAuthorType.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.books.where(:year.gte => year, :year.lte => year, :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 "book_type"
t = Book.where(:year.gte => year, :year.lte => year, :member_profile_id.in => mps, :book_type_id => nil).count rescue 0
when "author_type"
t = Book.where(:year.gte => year, :year.lte => year, :member_profile_id.in => mps, :book_author_type_ids => nil).count rescue 0
end
data["data"][year.to_s] = t
end
finaldata << data
finaldata
end
end