announcement-test/app/helpers/announcements_helper.rb

112 lines
4.9 KiB
Ruby

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 = []
top_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 !params["source"].present?
if !tags.blank?
top_anns = Bulletin.where(:title.ne => "",:is_preview.in=>[false,nil], :is_top => true).and(:title.ne => nil).can_display.filter_by_categories(page.categories || [],false).filter_by_tags(tags).to_a
else
top_anns = Bulletin.where(:title.ne => "",:is_preview.in=>[false,nil], :is_top => true).and(:title.ne => nil).can_display.filter_by_categories(page.categories || [],false).filter_by_tags.to_a
end
end
if !feeds_anns.blank?
announcements = announcements.concat(feeds_anns)
sorted = announcements.sort{ |k,v| v["postdate"] <=> k["postdate"] }
sorted = top_anns + sorted
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
announcements = top_anns + announcements
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(OrbitHelper.page_number).per(OrbitHelper.page_data_count) rescue []
end
[announcements,sorted]
end
end