module Admin::ProjectsHelper
include OrbitBackendHelper
include OrbitFormHelper
alias :org_datetime_picker :datetime_picker
def get_authors_text(project, is_to_sentence=false, locale=nil)
authors_text = Nokogiri::HTML(project.authors.to_s).text
split_text = authors_text.match(/[、,,\/]/)
split_text = split_text.nil? ? '/' : split_text[0]
full_authors_names = get_member(project).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(project, is_to_sentence=false, locale=nil)
authors_text = Nokogiri::HTML(project.authors.to_s).text
split_text = authors_text.match(/[、,,\/]/)
split_text = split_text.nil? ? '/' : split_text[0]
full_authors_names = []
full_authors = get_member(project).collect do |member|
member_name = member.name
full_authors_names << member_name
"#{member_name}"
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(project)
Array(MemberProfile.where(:id.in=>Array(project).collect(&:member_profile_id).flatten))
end
def get_member_show(project)
get_member(project).collect{|member| "#{member.name}"}.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_project(project_object)
page = Page.where(:module=>"personal_project").first
("/#{I18n.locale}"+page.url+'/'+project_object.to_param).gsub('//','/') rescue "javascript:void(0)"
end
def get_data_for_excel(year_start,year_end,timezone)
year_start = parse_date_time_field("year",year_start,timezone)
year_end = parse_date_time_field("year",year_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(Project, year_start, year_end, mps)
data << d
end
return data
end
def filter_data(data,year_start,year_end,mps = nil)
result = []
if @periodic
all_ids = data.all.pluck(:id) rescue []
out_of_range_ids1 = data.where(:year_start.gt => year_end).pluck(:id) rescue []
out_of_range_ids2 = data.where(:year_end.lt => year_start).pluck(:id) rescue []
result = data.where(:id.in=>(all_ids - out_of_range_ids1 - out_of_range_ids2)) rescue []
else
result = data.where(:year.gte => year_start, :year.lte => year_end) rescue []
end
result = result.where(:member_profile_id.in => mps) rescue [] unless mps.nil?
return result
end
def get_chart_data(year_start,year_end,role,type,timezone)
year_start = parse_date_time_field("year",year_start,timezone)
year_end = parse_date_time_field("year",year_end,timezone)
main_field_name = ""
time_fields = []
max_iterate = 20
iterate_step = 1
iterate_count = ((year_end - year_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 = []
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(year_start,year_end,iterate_step) do |year|
next_year = year + iterate_step
current_year = year
current_year = year.strftime("%H:%M") if time_fields.include?("year")
next_year = next_year.strftime("%H:%M") if time_fields.include?("year")
t = filter_data(jl.projects, current_year, next_year, mps)
if current_year.class == DateTime
current_year = display_date_time(current_year,timezone,iterate_step)
end
data["data"][current_year.to_s] = t
end
finaldata << data
end
data = {"name" => "N/A", "data" => {}}
time_iterate(year_start,year_end,iterate_step) do |year|
next_year = year + iterate_step
current_year = year
current_year = year.strftime("%H:%M") if time_fields.include?("year")
next_year = next_year.strftime("%H:%M") if time_fields.include?("year")
case type
when "default"
t = filter_data(Project, current_year, next_year, mps).count rescue 0
else
t = filter_data(Project, current_year, next_year, mps).count rescue 0
end
current_year = current_year.new_offset(timezone) if current_year.class == DateTime
if current_year.class == DateTime
current_year = display_date_time(current_year,timezone,iterate_step)
end
data["data"][current_year.to_s] = t
end
finaldata << data
finaldata
end
def parse_date_time_field(field,value,timezone="+08:00")
time_fields = []
type = Project.fields[field].type rescue nil
if type.nil?
@periodic = true
type = Project.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