announcement-test/app/helpers/announcements_helper.rb

191 lines
7.6 KiB
Ruby
Raw Normal View History

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)
2020-04-25 04:35:30 +00:00
tmp = value[:en]
I18n.with_locale(:en) do
if tmp == t('announcement.small_size')
@image_version = 'thumb'
elsif tmp == t('announcement.medium_size')
@image_version = 'mobile'
elsif tmp == t('announcement.orignal_size')
@image_version = 'orignal'
end
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)
2020-03-27 02:18:00 +00:00
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
2020-04-05 14:38:26 +00:00
def get_feed_annc(type,site_source,locale)
ma_key = 'announcement'
if type == "index"
2020-04-08 12:55:16 +00:00
categories = Array(OrbitHelper.page_categories)
2020-04-05 14:38:26 +00:00
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|
2020-04-10 02:13:23 +00:00
feed.all_contents_for_feed(site_source,locale,type=='widget')
2020-04-05 14:38:26 +00:00
end.flatten.compact
else
data = []
end
data
end
def get_feed_announcements(type,site_source=nil)
locale = OrbitHelper.get_site_locale.to_s
2020-04-05 14:38:26 +00:00
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"
}
2020-04-05 14:38:26 +00:00
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 []
2020-04-05 14:38:26 +00:00
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)
2020-03-17 01:26:34 +00:00
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
2020-04-25 04:35:30 +00:00
def get_sorted_annc(data_count=nil)
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
2020-04-25 04:35:30 +00:00
page_data_count = 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
2020-03-17 02:32:51 +00:00
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
2020-04-05 14:38:26 +00:00
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}
2020-03-17 01:26:34 +00:00
rest_anns = rest_all_anns.sort{|v1,v2| v2["postdate"]<=>v1["postdate"]}
2020-04-05 14:38:26 +00:00
all_sorted = top_anns.sort{|v1,v2| v2["postdate"]<=>v1["postdate"]} + rest_anns
else
2020-04-08 12:55:16 +00:00
all_sorted = feeds_anns.select{|v| v['is_top']}.sort{|v1,v2| v2["postdate"]<=>v1["postdate"]} + feeds_anns.select{|v| v['is_top'] != true}.sort{|v1,v2| v2["postdate"]<=>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