feeds/app/models/site_feed_annc.rb

147 lines
6.0 KiB
Ruby

class SiteFeedAnnc
include Mongoid::Document
include Mongoid::Timestamps
field :top_list,type: Array,default: []
field :hot_list,type: Array,default: []
field :all_contents_for_feed
field :channel_key
field :feed_id
field :feed_name
field :category_title
field :hidden_annc,type: Array,default: []
field :merge_with_category
field :remote_site_url
field :channel_title
def get_annc(annc_uid)
Array(self[:all_contents_for_feed]).select{|v| v['id']==annc_uid}[0] rescue {}
end
def all_contents_for_feed(site_source=nil,locale=I18n.locale.to_s,is_widget=false)
cat = self.category_title
Array(self[:all_contents_for_feed]).collect do |v|
tmp = v
if hidden_annc.exclude?(v['id']) && !tmp["title_translations"][locale].blank?
if self.channel_key == "announcement"
tmp['statuses'] = []
if self[:top_list].count == 0 || self[:top_list].exclude?(tmp['id'])
tmp[:is_top] = false
else
tmp[:is_top] = true
tmp['statuses'] << {
"status" => I18n.t(:top),
"status-class" => "status-top"
}
end
if self[:hot_list].count == 0 || self[:top_list].exclude?(tmp['id'])
tmp[:is_hot] = false
else
tmp[:is_hot] = true
tmp['statuses'] << {
"status" => I18n.t(:hot),
"status-class" => "status-hot"
}
end
tmp["category"] = cat
tmp["source-site"] = self.remote_site_url
tmp["source-site-title"] = (self[:channel_title][locale] rescue "")
tmp["params"] = tmp["params"].to_s + "_" + self.feed_id.to_s + "h"
next if !site_source.nil? && site_source != tmp["source-site-title"]
tmp['statuses'] << {
"status" => "<a href='#{tmp["source-site"]}' target='_blank' class='feed-source'>#{tmp["source-site-title"]}</a>",
"status-class" => "status-source"
}
files = tmp["bulletin_files"].collect{|bf| { "file_url" => bf["url"], "file_title" => (bf["title_translations"][locale].blank? ? File.basename(bf["url"]) : bf["title_translations"][locale] rescue '') }} rescue []
links = tmp["bulletin_links"].map{|link| { "link_url" => link["url"], "link_title" => (link["title_translations"][locale].blank? ? link["url"] : link["title_translations"][locale]) } } rescue []
tmp["bulletin_links"] = links
tmp["bulletin_files"] = files
tmp["title"] = tmp["title_translations"][locale]
tmp["subtitle"] = tmp["subtitle_translations"][locale]
tmp["source-site-link"] = tmp["source-site"]
tmp["source-site"] = "<a href='#{tmp["source-site"]}' target='_blank' class='feed-source'>#{tmp["source-site-title"]}</a>"
if !is_widget
tmp["link_to_show"] = OrbitHelper.url_to_show(tmp["params"]) rescue ''
else
tmp["link_to_show"] = OrbitHelper.widget_item_url(tmp["params"]) rescue ''
end
tmp["target"] = "_self"
tmp["img_src"] = tmp["image"]["thumb"] || "/assets/announcement-default.jpg"
tmp["img_description"] = tmp["image_description_translations"][locale]
tmp["more"] = I18n.t(:more_plus)
tmp["view_count"] = ""
else
tmp['statuses'] = []
if self[:top_list].count == 0 || self[:top_list].exclude?(tmp['id'])
tmp[:is_top] = false
else
tmp[:is_top] = true
tmp['statuses'] << {
"status" => I18n.t(:top),
"status-class" => "status-top"
}
end
if self[:hot_list].count == 0 || self[:top_list].exclude?(tmp['id'])
tmp[:is_hot] = false
else
tmp[:is_hot] = true
tmp['statuses'] << {
"status" => I18n.t(:hot),
"status-class" => "status-hot"
}
end
tmp["category"] = cat
tmp["source-site"] = self.remote_site_url
tmp["source-site-title"] = (self[:channel_title][locale] rescue "")
tmp["params"] = tmp["params"].to_s + "_" + self.feed_id.to_s + "h"
next if !site_source.nil? && site_source != tmp["source-site-title"]
tmp['statuses'] << {
"status" => "<a href='#{tmp["source-site"]}' target='_blank' class='feed-source'>#{tmp["source-site-title"]}</a>",
"status-class" => "status-source"
}
tmp["source-site-link"] = tmp["source-site"]
tmp["source-site"] = "<a href='#{tmp["source-site"]}' target='_blank' class='feed-source'>#{tmp["source-site-title"]}</a>"
if !is_widget
tmp["link_to_show"] = OrbitHelper.url_to_show(tmp["params"]) rescue ''
else
tmp["link_to_show"] = OrbitHelper.widget_item_url(tmp["params"]) rescue ''
end
tmp["target"] = "_self"
tmp["more"] = I18n.t(:more_plus)
tmp["view_count"] = ""
new_tmp = {}
tmp.each do |key,value|
if key.include? "_translations"
new_tmp[key.sub("_translations","")] = value[locale].to_s rescue ""
elsif key.include?("date") || key.include?("Date")
new_tmp[key] = DateTime.parse(value) rescue nil
else
if value.class == Hash
value.each do |sub_k,sub_v|
if sub_k.include? "_translations"
new_tmp[key][sub_k.sub("_translations","")] = sub_v[locale].to_s rescue ""
else
new_tmp[key][sub_k] = sub_v
end
end
else
new_tmp[key] = value
end
end
end
tmp = BSON::Document.new(new_tmp)
end
else
tmp = nil
end
tmp
end.compact
end
def channel_title(locale=I18n.locale)
self[:channel_title][locale] rescue self[:channel_title]
end
def category
Category.find(self.merge_with_category) rescue nil
end
def category_title
self[:category_title][I18n.locale]
end
end