announcement-test/app/helpers/announcements_helper.rb

116 lines
5.1 KiB
Ruby
Raw Normal View History

module AnnouncementsHelper
def get_feed_announcements(type,site_source=nil)
feed_anns = OrbitHelper.get_feed_for_module(type)
fans = []
locale = OrbitHelper.get_site_locale.to_s
feed_anns.each do |fa|
next if !site_source.nil? && site_source != fa["source-site-title"]
status = {
"status" => "<a href='#{fa["source-site"]}' target='_blank' class='feed-source'>#{fa["source-site-title"]}</a>",
"status-class" => "status-source"
}
files = fa["bulletin_files"].collect{|bf| { "file_url" => bf["url"], "file_title" => (fa["title_translations"][locale].blank? ? File.basename(fa["url"]) : fa["title_translations"][locale] rescue '') }} rescue []
links = fa["bulletin_links"].map{|link| { "link_url" => link["url"], "link_title" => (link["title_translations"][locale].blank? ? link["url"] : link["title_translations"][locale]) } } rescue []
x = {
"bulletin_links" => links,
"bulletin_files" => files,
"title" => fa["title_translations"][locale],
"subtitle" => fa["subtitle_translations"][locale],
"statuses" => [status],
"category" => fa["category"],
"postdate" => fa["postdate"],
"author" => fa["author"],
"is_top" => 0,
"source-site" => "<a href='#{fa["source-site"]}' target='_blank' class='feed-source'>#{fa["source-site-title"]}</a>",
"source-site-title" => fa["source-site-title"],
"source-site-link" => fa["source-site"],
"link_to_show" => OrbitHelper.url_to_show(fa["params"]),
"target" => "_self",
"img_src" => fa["image"]["thumb"] || "/assets/announcement-default.jpg",
"img_description" => fa["image_description_translations"][locale],
"more" => t(:more_plus),
"view_count" => ""
}
if (!x["title"].empty? rescue false)
fans << x
end
end
fans
end
def test
123
end
def get_sorted_annc
params = OrbitHelper.params
locale = OrbitHelper.get_site_locale.to_s
page = Page.where(url:params['url']).first
feeds_anns = []
tags = page.tags
if !tags.blank?
announcements = Bulletin.where(:title.ne => "",:is_preview.in=>[false,nil]).and(:title.ne => nil).can_display.is_approved.filter_by_categories(page.categories || [],false).filter_by_tags(tags).page(OrbitHelper.page_number).per(OrbitHelper.page_data_count).to_a
else
if !params["source"].present?
announcements = Bulletin.where(:title.ne => "",:is_preview.in=>[false,nil]).and(:title.ne => nil, :is_top.ne => true).can_display.is_approved.filter_by_categories(page.categories || [],false).filter_by_tags.to_a
feeds_anns = get_feed_announcements("index")
else
announcements = []
feeds_anns = get_feed_announcements("index",params["source"])
end
end
# (OrbitHelper.page_number == 1 or OrbitHelper.page_number.nil?) &&
if !feeds_anns.blank?
announcements = announcements.concat(feeds_anns)
sorted = announcements.sort{ |k,v| v["postdate"] <=> k["postdate"] }
if params["keywords"].present?
sorted = sorted.find_all{|anns|
if anns["source-site"].present?
/#{params[:keywords].to_s}/i.match anns["title"]
else
/#{params[:keywords].to_s}/i.match anns.title
end
}
end
if params["postdate"].present?
sorted = sorted.find_all{|anns|
if anns["source-site"].present?
/#{params[:postdate].to_s}/i.match anns["postdate"].strftime("%Y-%m") if !anns["postdate"].nil?
else
/#{params[:postdate].to_s}/i.match anns.postdate.strftime("%Y-%m") if !anns.postdate.nil?
end
}
end
sorted = Kaminari.paginate_array(sorted).page(OrbitHelper.page_number).per(OrbitHelper.page_data_count) rescue []
else
if params["keywords"].present?
announcements = announcements.find_all{|anns| /#{params[:keywords].to_s}/i.match anns.title}
end
if params["postdate"].present?
announcements = announcements.find_all{|anns|
if !anns.postdate.nil?
/#{params[:postdate].to_s}/i.match anns.postdate.strftime("%Y-%m")
end
}
end
sorted = Kaminari.paginate_array(announcements).page(1).per(OrbitHelper.page_data_count) rescue []
end
begin
if !tags.blank?
annc_count = Bulletin.where(:title.ne => "",:is_preview.in=>[false,nil]).and(:title.ne => nil).can_display.is_approved.filter_by_categories(page.categories || [],false).filter_by_tags(tags).count
else
if !params["source"].present?
annc_count = Bulletin.where(:title.ne => "",:is_preview.in=>[false,nil]).and(:title.ne => nil, :is_top.ne => true).can_display.is_approved.filter_by_categories(page.categories || [],false).filter_by_tags.count
annc_count += get_feed_announcements("index").count
else
annc_count = get_feed_announcements("index",params["source"]).count
end
end
total_pages = (annc_count * 1.0 / OrbitHelper.page_data_count).ceil
rescue
total_pages = 1
end
[announcements,sorted,total_pages]
end
end