announcement-test/app/helpers/announcements_helper.rb

188 lines
7.4 KiB
Ruby

module AnnouncementsHelper
def set_image_version_for_widget
subpart = OrbitHelper.get_current_widget
@image_version = 'thumb'
if subpart.methods.include? 'select_options'.to_sym
ModuleApp.all.select{|tmp| tmp.key.to_s=='announcement'}.each do |modile_app|
@show_options = modile_app.show_options rescue nil
end
subpart.select_options.each do |select_option|
if !(@show_options.nil?) && select_option.field_name == @show_options.keys.first.to_s
value = YAML.load(select_option.value)
if value[I18n.locale] == t('announcement.small_size')
@image_version = 'thumb'
elsif value[I18n.locale] == t('announcement.medium_size')
@image_version = 'mobile'
elsif value[I18n.locale] == t('announcement.orignal_size')
@image_version = 'orignal'
end
end
end
end
end
def data_to_human_type(a)
statuses = a.statuses_with_classname.collect do |status|
{
"status" => status["name"],
"status-class" => "status-#{status['classname']}"
}
end
files = a.bulletin_files.map{|file| { "file_url" => file.file.url, "file_title" => (file.title.blank? ? File.basename(file.file.path) : file.title rescue '') } if file.enabled_for?(locale) } rescue []
files.delete(nil)
links = a.bulletin_links.map{|link| { "link_url" => link.url, "link_title" => (link.title.blank? ? link.url : link.title) } } rescue []
author = User.find(a.create_user_id).member_profile.name rescue ""
desc = a.image_description
desc = (desc.nil? || desc == "" ? "announcement image" : desc)
link_to_show = a.is_external_link? ? a.external_link : OrbitHelper.widget_item_url(a.to_param)
target = a.is_external_link ? "_blank" : "_self"
if @image_version == 'thumb'
image_url = a.image.thumb.url
elsif @image_version == 'mobile'
image_url = a.image.mobile.url
else
image_url = a.image.url
end
{
"bulletin_links" => links,
"bulletin_files" => files,
"title" => a.title,
"source-site" => "",
"source-site-title" => "",
"source-site-link" => "",
"subtitle" => a.subtitle,
"statuses" => statuses,
"category" => a.category.title,
"postdate" => a.postdate,
"author" => author,
"link_to_show" => link_to_show,
"target" => target,
"img_src" => image_url || "/assets/announcement-default.jpg",
"img_description" => desc
}
end
def get_feed_annc(type,site_source,locale)
ma_key = 'announcement'
if type == "index"
categories = Array(OrbitHelper.widget_categories)
elsif type == "widget"
categories = Array(OrbitHelper.widget_categories)
else
categories = []
end
if categories.include?("all")
feeds = SiteFeedAnnc.where(:channel_key => ma_key)
else
feeds = SiteFeedAnnc.where(:channel_key => ma_key, :merge_with_category.in => categories)
end
if feeds.count > 0
temp_ids = []
data = feeds.collect do |feed|
feed.all_contents_for_feed(site_source,locale)
end.flatten.compact
else
data = []
end
data
end
def get_feed_announcements(type,site_source=nil)
locale = OrbitHelper.get_site_locale.to_s
if !(defined? SiteFeedAnnc).nil?
fans = get_feed_annc(type,site_source,locale)
else
feed_anns = OrbitHelper.get_feed_for_module(type)
fans = []
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"],
"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
end
fans
end
def filter_by_keywords(sorted,keywords)
if !keywords.to_s.empty?
sorted.select{|anns|
if anns["source-site"].present?
title = Nokogiri::HTML(anns["title"].to_s).text
else
title = Nokogiri::HTML(anns.title.to_s).text
end
title.include?(keywords.to_s)
}
else
sorted
end
end
def get_sorted_annc
params = OrbitHelper.params
locale = OrbitHelper.get_site_locale.to_s
page = Page.where(url:params['url']).first
page_number = OrbitHelper.page_number.to_i
page_number = 1 if page_number == 0
page_data_count = OrbitHelper.page_data_count.to_i
feeds_anns = []
tags = page.tags
categories = params['category']=='all' ? (page.categories || []) : ([Category.find(params['category'])] rescue (page.categories || []))
if !params["source"].present?
announcements = Bulletin.where(:title.nin => ["",nil],:is_preview.in=>[false,nil])
.can_display_and_sorted.is_approved
.filter_by_categories(categories,false).filter_by_tags(tags).to_a
if !(defined? SiteFeed).nil?
feeds_anns = get_feed_announcements("index")
end
else
announcements = []
feeds_anns = get_feed_announcements("index",params["source"])
end
if !feeds_anns.blank?
if announcements.count != 0
top_anns = announcements.select{|v| v.is_top} + feeds_anns.select{|v| v['is_top']}
rest_all_anns = feeds_anns.select{|v| v['is_top'] != true} + announcements.select{|v| !v.is_top}
rest_anns = rest_all_anns.sort{|v1,v2| v2["postdate"]<=>v1["postdate"]}
all_sorted = top_anns.sort{|v1,v2| v2["postdate"]<=>v1["postdate"]} + rest_anns
else
all_sorted = feeds_anns.sort{|v1,v2| [v2['is_top'],v2["postdate"]]<=>[v1['is_top'],v1["postdate"]]}
end
all_filter = filter_by_keywords(all_sorted,params[:keywords])
else
all_filter = filter_by_keywords(announcements,params[:keywords])
end
if page_data_count != 0
sorted = all_filter[(page_number-1)*page_data_count...page_number*page_data_count]
else
sorted = all_filter
end
annc_count = all_filter.count
total_pages = page_data_count == 0 ? 1 : (annc_count.to_f / page_data_count).ceil
[sorted,total_pages]
end
end