event_ann/app/controllers/event_ann_feeds_controller.rb

157 lines
4.8 KiB
Ruby
Raw Normal View History

2020-07-13 08:04:57 +00:00
require "rss"
class EventAnnFeedsController < ApplicationController
include Admin::EventAnnsHelper
def feed
uid = params[:uid]
eanns = get_event_anns(uid)
render :json => eanns.to_json
end
def rssfeed
uid = params[:uid]
@bf = BulletinFeedEvent.find_by(:uid => uid) rescue nil
if !@bf.nil?
tags = @bf.tag_ids
if !tags.empty?
@event_anns = BulletinEvent.can_display_and_sorted.is_approved.filter_by_tags(tags)
end
end
respond_to do |format|
format.html {redirect_to "/xhr/event_anns/rssfeed/#{@bf.uid}.rss"}
format.rss
end
end
def feeds
feeds = []
BulletinFeedEvent.all.each do |bf|
feed = {}
feed["title_translations"] = bf.title_translations
feed["uid"] = bf.uid
feed["url"] = "#{request.base_url}/xhr/event_anns/feed/#{bf.uid}"
feed["xml_url"] = "#{request.base_url}/xhr/event_anns/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
def smart_convertor(text)
html_string = text
links = html_string.scan(/img.*?src="(.*?)"/i)
links = links + html_string.scan(/a.*?href="(.*?)"/i)
links.uniq!
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?
end
return html_string
end
def get_event_anns(uid)
bf = BulletinFeedEvent.find_by(:uid => uid) rescue nil
startdt = params[:start]
enddt = params[:end]
dt = params[:date]
if !bf.nil?
tags = bf.tag_ids
if !tags.empty?
if !dt.nil?
dt = DateTime.parse(dt)
dtt = dt + 1.day
event_anns = BulletinEvent.where(:postdate.gt => dt, :postdate.lt => dtt).can_display_and_sorted.is_approved.filter_by_tags(tags)
elsif !startdt.nil? && enddt.nil?
startdt = DateTime.parse(startdt)
enddt = DateTime.now
event_anns = BulletinEvent.where(:postdate.gt => startdt, :postdate.lt => enddt).can_display_and_sorted.is_approved.filter_by_tags(tags)
elsif !startdt.nil? && !enddt.nil?
startdt = DateTime.parse(startdt)
enddt = DateTime.parse(enddt) + 1.day
event_anns = BulletinEvent.where(:postdate.gt => startdt, :postdate.lt => enddt).can_display_and_sorted.is_approved.filter_by_tags(tags)
else
event_anns = BulletinEvent.all.can_display_and_sorted.is_approved.filter_by_tags(tags)
end
else
event_anns = []
end
end
all_eanns = []
tag_names = []
tag_ids = []
event_anns.each do |eanns|
user = User.find(eanns.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"] = eanns.uid
a["title_translations"] = eanns.title_translations
a["subtitle_translations"] = eanns.subtitle_translations
a["speaker_translations"] = eanns.speaker_translations
a["host_translations"] = eanns.host_translations
a["text_translations"] = {}
a["text_translations"]["en"] = smart_convertor(eanns.text_translations["en"]) if !eanns.text_translations["en"].blank?
a["text_translations"]["zh_tw"] = smart_convertor(eanns.text_translations["zh_tw"]) if !eanns.text_translations["zh_tw"].blank?
a["postdate"] = eanns.postdate
a["image_description_translations"] = eanns.image_description_translations
a["image"] = {}
a["image"]["original"] = ("#{request.base_url}" + eanns.image.url rescue "")
a["image"]["thumb"] = ("#{request.base_url}" + eanns.image.thumb.url rescue "")
a["image"]["mobile"] = ("#{request.base_url}" + eanns.image.mobile.url rescue "")
a["tags"] = []
a["author"] = author
a["params"] = eanns.to_param
a["bulletin_event_links"] = []
a["bulletin_event_files"] = []
eanns.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
eanns.bulletin_event_links.each do |bl|
b = {}
b["url"] = bl.url
b["title_translations"] = bl.title_translations
a["bulletin_event_links"] << b
end
eanns.bulletin_event_files.each do |bf|
b = {}
b["description_translations"] = bf.description_translations
b["title_translations"] = bf.title_translations
b["url"] = ("#{request.base_url}" + bf.file.url rescue "")
a["bulletin_event_files"] << b
end
all_eanns << a
end
{
"event_anns" => all_eanns,
"tags" => tag_names
}
end
end