personal_other_paper/app/helpers/admin/other_papers_helper.rb

188 lines
6.9 KiB
Ruby
Raw Normal View History

2022-10-24 09:02:42 +00:00
module Admin::OtherPapersHelper
include OrbitBackendHelper
include OrbitFormHelper
alias :org_datetime_picker :datetime_picker
def get_authors_text(other_paper, is_to_sentence=false, locale=nil)
authors_text = Nokogiri::HTML(other_paper.authors.to_s).text
split_text = authors_text.match(/[、,,\/]/)
split_text = split_text.nil? ? '/' : split_text[0]
full_authors_names = get_member(other_paper).collect(&:name)
if authors_text.present?
authors_names = authors_text.split(split_text).select{|a| !(full_authors_names.include?(a.strip()))}
full_authors_names += authors_names
end
if is_to_sentence
full_authors_names.to_sentence({:locale=>locale})
else
full_authors_names.join(split_text)
end
end
def get_authors_show(other_paper, is_to_sentence=false, locale=nil)
authors_text = Nokogiri::HTML(other_paper.authors.to_s).text
split_text = authors_text.match(/[、,,\/]/)
split_text = split_text.nil? ? '/' : split_text[0]
full_authors_names = []
full_authors = get_member(other_paper).collect do |member|
member_name = member.name
full_authors_names << member_name
"<a href='#{OrbitHelper.url_to_plugin_show(member.to_param,'member')}' title='#{member_name}'>#{member_name}</a>"
end
if authors_text.present?
authors_names = authors_text.split(split_text).select{|a| !(full_authors_names.include?(a.strip()))}
full_authors += authors_names
end
if is_to_sentence
full_authors.to_sentence({:locale=>locale})
else
full_authors.join(split_text)
end
end
def get_member(other_paper)
Array(MemberProfile.where(:id.in=>Array(other_paper).collect(&:member_profile_id).flatten))
end
def get_member_show(other_paper)
get_member(other_paper).collect{|member| "<a href='#{OrbitHelper.url_to_plugin_show(member.to_param,'member')}' title='#{member.name}'>#{member.name}</a>"}.join('/')
end
def datetime_picker(*arg,**args)
org_datetime_picker(arg,args)
end
def time_iterate(start_time, end_time, step, &block)
begin
start_time = start_time.to_date if end_time.class == Date
yield(start_time)
end while (start_time += step) <= end_time
end
def parse_time(time_str,timezone="+08:00")
DateTime.parse("0000-01-01 " + time_str + timezone)
end
def page_for_other_paper(other_paper_object)
page = Page.where(:module=>"personal_other_paper").first
("/#{I18n.locale}"+page.url+'/'+other_paper_object.to_param).gsub('//','/') rescue "#"
end
def get_data_for_excel(issue_date_start,issue_date_end,timezone)
issue_date_start = parse_date_time_field("issue_date",issue_date_start,timezone)
issue_date_end = parse_date_time_field("issue_date",issue_date_end,timezone)
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
d["data"] = filter_data(OtherPaper, issue_date_start, issue_date_end, mps)
data << d
end
return data
end
def filter_data(data,issue_date_start,issue_date_end,mps = nil)
result = []
if @periodic
all_ids = data.all.pluck(:id) rescue []
out_of_range_ids1 = data.where(:issue_date_start.gt => issue_date_end).pluck(:id) rescue []
out_of_range_ids2 = data.where(:issue_date_end.lt => issue_date_start).pluck(:id) rescue []
result = data.where(:id.in=>(all_ids - out_of_range_ids1 - out_of_range_ids2)) rescue []
else
result = data.where(:issue_date.gte => issue_date_start, :issue_date.lte => issue_date_end) rescue []
end
result = result.where(:member_profile_id.in => mps) rescue [] unless mps.nil?
return result
end
def get_chart_data(issue_date_start,issue_date_end,role,type,timezone)
issue_date_start = parse_date_time_field("issue_date",issue_date_start,timezone)
issue_date_end = parse_date_time_field("issue_date",issue_date_end,timezone)
main_field_name = ""
time_fields = []
max_iterate = 20
iterate_step = 1.minute
iterate_count = ((issue_date_end - issue_date_start) / iterate_step * 1.day.second).ceil
if iterate_count > max_iterate
iterate_step = (iterate_step * (iterate_count / max_iterate.to_f).ceil).second
end
case type
when "default"
jls = []
when "other_paper_status"
jls = OtherPaperStatus.all
main_field_name = "status"
else
jls = []
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.send(main_field_name) rescue "N/A"
data["data"] = {}
time_iterate(issue_date_start,issue_date_end,iterate_step) do |issue_date|
next_issue_date = issue_date + iterate_step
current_issue_date = issue_date
current_issue_date = issue_date.strftime("%H:%M") if time_fields.include?("issue_date")
next_issue_date = next_issue_date.strftime("%H:%M") if time_fields.include?("issue_date")
t = filter_data(jl.other_papers, current_issue_date, next_issue_date, mps)
if current_issue_date.class == DateTime
current_issue_date = display_date_time(current_issue_date,timezone,iterate_step)
end
data["data"][current_issue_date.to_s] = t
end
finaldata << data
end
data = {"name" => "N/A", "data" => {}}
time_iterate(issue_date_start,issue_date_end,iterate_step) do |issue_date|
next_issue_date = issue_date + iterate_step
current_issue_date = issue_date
current_issue_date = issue_date.strftime("%H:%M") if time_fields.include?("issue_date")
next_issue_date = next_issue_date.strftime("%H:%M") if time_fields.include?("issue_date")
case type
when "default"
t = filter_data(OtherPaper, current_issue_date, next_issue_date, mps).count rescue 0
when "other_paper_status"
t = filter_data(OtherPaper, current_issue_date, next_issue_date, mps).where(:other_paper_status_id => nil).count rescue 0
else
t = filter_data(OtherPaper, current_issue_date, next_issue_date, mps).count rescue 0
end
current_issue_date = current_issue_date.new_offset(timezone) if current_issue_date.class == DateTime
if current_issue_date.class == DateTime
current_issue_date = display_date_time(current_issue_date,timezone,iterate_step)
end
data["data"][current_issue_date.to_s] = t
end
finaldata << data
finaldata
end
def parse_date_time_field(field,value,timezone="+08:00")
time_fields = []
type = OtherPaper.fields[field].type rescue nil
if type.nil?
@periodic = true
type = OtherPaper.fields[field + "_start"].type
end
if time_fields.include?(field)
parse_time(value,timezone)
elsif type == Integer
value.to_i
elsif type == Date
Date.parse(value)
else
DateTime.parse(value+timezone).utc
end
end
def display_date_time(date_time,timezone,iterate_step)
date_time = date_time.new_offset(timezone)
if iterate_step > 1.year
date_time = date_time.strftime("%Y")
elsif iterate_step > 1.month
date_time = date_time.strftime("%Y/%m")
elsif iterate_step > 1.day
date_time = date_time.strftime("%Y/%m/%d")
else
date_time = date_time.strftime("%Y/%m/%d %H:%M")
end
return date_time
end
end