announcement-test/app/controllers/announcement_feeds_controll...

138 lines
3.8 KiB
Ruby
Raw Normal View History

2015-09-11 12:28:41 +00:00
require "rss"
2015-07-08 10:11:25 +00:00
class AnnouncementFeedsController < ApplicationController
2015-09-11 12:28:41 +00:00
include Admin::AnnouncementsHelper
2015-07-08 10:11:25 +00:00
def feed
uid = params[:uid]
2015-09-11 12:28:41 +00:00
anns = get_announcements(uid)
render :json => anns.to_json
end
def rssfeed
uid = params[:uid]
@bf = BulletinFeed.find_by(:uid => uid) rescue nil
if !@bf.nil?
tags = @bf.tag_ids
if !tags.empty?
@announcements = Bulletin.can_display.is_approved.filter_by_tags(tags)
end
end
respond_to do |format|
format.html {redirect_to "/xhr/announcements/rssfeed/#{@bf.uid}.rss"}
format.rss
end
end
def feeds
feeds = []
BulletinFeed.all.each do |bf|
feed = {}
feed["title_translations"] = bf.title_translations
feed["uid"] = bf.uid
feed["url"] = "#{request.base_url}/xhr/announcements/feed/#{bf.uid}"
feed["xml_url"] = "#{request.base_url}/xhr/announcements/rssfeed/#{bf.uid}.rss"
feed["tags"] = []
bf.tag_ids.each do |t|
tag = Tag.find(t)
d = {}
d["name_translations"] = tag.name_translations
feed["tags"] << d
end
feeds << feed
end
render :json => {"feeds" => feeds}.to_json
end
private
2016-03-07 09:11:07 +00:00
def smart_convertor(text)
html_string = text
links = html_string.scan(/img.*?src="(.*?)"/i)
links = links + html_string.scan(/a.*?href="(.*?)"/i)
links.uniq!
2016-03-07 09:11:07 +00:00
links.each do |link|
l = link.first
new_link = nil
if l.starts_with?("/")
new_link = request.protocol + request.host_with_port + l
elsif l.starts_with?("..")
l1 = l.gsub("../","")
new_link = request.protocol + request.host_with_port + "/" + l1
end
html_string = html_string.gsub(l,new_link) if !new_link.nil?
2016-03-07 09:11:07 +00:00
end
return html_string
end
2015-09-11 12:28:41 +00:00
def get_announcements(uid)
2015-07-08 10:11:25 +00:00
bf = BulletinFeed.find_by(:uid => uid) rescue nil
if !bf.nil?
tags = bf.tag_ids
if !tags.empty?
2016-04-11 08:40:23 +00:00
announcements = Bulletin.all.can_display.is_approved.filter_by_tags(tags)
2015-11-09 18:30:44 +00:00
else
announcements = []
2015-07-08 10:11:25 +00:00
end
end
all_anns = []
tag_names = []
tag_ids = []
announcements.each do |anns|
user = User.find(anns.create_user_id) rescue nil
if !user.nil?
author = user.member_profile && user.member_profile.name == "" ? user.user_name : user.member_profile.name
else
author = ""
end
a = {}
a["id"] = anns.uid
a["title_translations"] = anns.title_translations
a["subtitle_translations"] = anns.subtitle_translations
2016-03-07 10:19:56 +00:00
a["text_translations"] = {}
2016-03-07 10:11:58 +00:00
a["text_translations"]["en"] = smart_convertor(anns.text_translations["en"]) if !anns.text_translations["en"].blank?
a["text_translations"]["zh_tw"] = smart_convertor(anns.text_translations["zh_tw"]) if !anns.text_translations["zh_tw"].blank?
2015-07-08 10:11:25 +00:00
a["postdate"] = anns.postdate
a["image_description_translations"] = anns.image_description_translations
a["image"] = {}
a["image"]["original"] = ("#{request.base_url}" + anns.image.url rescue "")
a["image"]["thumb"] = ("#{request.base_url}" + anns.image.thumb.url rescue "")
a["image"]["mobile"] = ("#{request.base_url}" + anns.image.mobile.url rescue "")
a["tags"] = []
a["author"] = author
a["params"] = anns.to_param
a["bulletin_links"] = []
a["bulletin_files"] = []
anns.tags.each do |tag|
if !tag_ids.include?(tag.id.to_s)
tag_ids << tag.id.to_s
tag_names << {"name_translations" => tag.name_translations}
end
a["tags"] << {"name_translations" => tag.name_translations}
end
anns.bulletin_links.each do |bl|
b = {}
b["url"] = bl.url
b["title_translations"] = bf.title_translations
a["bulletin_links"] << b
end
anns.bulletin_files.each do |bf|
b = {}
b["description_translations"] = bf.description_translations
b["title_translations"] = bf.title_translations
2016-04-07 08:20:48 +00:00
b["url"] = ("#{request.base_url}" + bf.file.url rescue "")
2015-07-08 10:11:25 +00:00
a["bulletin_files"] << b
end
all_anns << a
end
2015-09-11 12:28:41 +00:00
{
"announcements" => all_anns,
"tags" => tag_names
}
2015-07-08 10:11:25 +00:00
end
end