2020-07-22 02:23:36 +00:00
|
|
|
require "rss"
|
|
|
|
class EventNewsFeedsController < ApplicationController
|
|
|
|
include Admin::EventNewsHelper
|
|
|
|
def feed
|
|
|
|
uid = params[:uid]
|
|
|
|
anns = get_event_news(uid)
|
|
|
|
render :json => anns.to_json
|
|
|
|
end
|
|
|
|
|
|
|
|
def rssfeed
|
|
|
|
uid = params[:uid]
|
|
|
|
@bf = EventNewsFeed.find_by(:uid => uid) rescue nil
|
|
|
|
if !@bf.nil?
|
|
|
|
tags = @bf.tag_ids
|
|
|
|
if !tags.empty?
|
|
|
|
@event_news = EventNews.can_display_and_sorted.is_approved.filter_by_tags(tags)
|
2021-07-14 04:56:02 +00:00
|
|
|
end
|
|
|
|
categories = @bf.category_ids
|
|
|
|
if !categories.empty?
|
|
|
|
@event_news = @event_news.filter_by_categories(categories)
|
2020-07-22 02:23:36 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
@event_news = []
|
|
|
|
end
|
|
|
|
respond_to do |format|
|
|
|
|
format.html {redirect_to "/xhr/event_news/rssfeed/#{@bf.uid}.rss"}
|
|
|
|
format.rss
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def feeds
|
|
|
|
feeds = []
|
|
|
|
EventNewsFeed.all.each do |bf|
|
|
|
|
feed = {}
|
|
|
|
feed["title_translations"] = bf.title_translations
|
|
|
|
feed["uid"] = bf.uid
|
|
|
|
feed["url"] = "#{request.base_url}/xhr/event_news/feed/#{bf.uid}"
|
|
|
|
feed["xml_url"] = "#{request.base_url}/xhr/event_news/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_news(uid)
|
|
|
|
bf = EventNewsFeed.find_by(:uid => uid) rescue nil
|
|
|
|
startdt = params[:start]
|
|
|
|
enddt = params[:end]
|
|
|
|
dt = params[:date]
|
|
|
|
if !bf.nil?
|
|
|
|
tags = bf.tag_ids
|
2021-07-14 04:56:02 +00:00
|
|
|
categories = bf.category_ids
|
|
|
|
if !(categories.empty? && tags.empty?)
|
2020-07-22 02:23:36 +00:00
|
|
|
if !dt.nil?
|
|
|
|
dt = DateTime.parse(dt)
|
|
|
|
dtt = dt + 1.day
|
2021-07-14 04:56:02 +00:00
|
|
|
event_news = EventNews.where(:postdate.gt => dt, :postdate.lt => dtt).can_display_and_sorted.is_approved
|
2020-07-22 02:23:36 +00:00
|
|
|
elsif !startdt.nil? && enddt.nil?
|
|
|
|
startdt = DateTime.parse(startdt)
|
|
|
|
enddt = DateTime.now
|
2021-07-14 04:56:02 +00:00
|
|
|
event_news = EventNews.where(:postdate.gt => startdt, :postdate.lt => enddt).can_display_and_sorted.is_approved
|
2020-07-22 02:23:36 +00:00
|
|
|
elsif !startdt.nil? && !enddt.nil?
|
|
|
|
startdt = DateTime.parse(startdt)
|
|
|
|
enddt = DateTime.parse(enddt) + 1.day
|
2021-07-14 04:56:02 +00:00
|
|
|
event_news = EventNews.where(:postdate.gt => startdt, :postdate.lt => enddt).can_display_and_sorted.is_approved
|
2020-07-22 02:23:36 +00:00
|
|
|
else
|
2021-07-14 04:56:02 +00:00
|
|
|
event_news = EventNews.all.can_display_and_sorted.is_approved
|
|
|
|
end
|
|
|
|
if !tags.empty?
|
|
|
|
event_news = event_news.filter_by_tags(tags)
|
|
|
|
end
|
|
|
|
if !categories.empty?
|
|
|
|
event_news = event_news.filter_by_categories(categories)
|
2020-07-22 02:23:36 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
event_news = []
|
|
|
|
end
|
|
|
|
end
|
|
|
|
all_anns = []
|
|
|
|
tag_names = []
|
2021-07-14 04:56:02 +00:00
|
|
|
category_titles = []
|
2020-07-22 02:23:36 +00:00
|
|
|
tag_ids = []
|
2021-07-14 04:56:02 +00:00
|
|
|
category_ids = []
|
2020-07-22 02:23:36 +00:00
|
|
|
event_news.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
|
2021-07-14 04:56:02 +00:00
|
|
|
translations_fields = ["title","subtitle","speaker","place","host","notes"]
|
2020-07-22 02:23:36 +00:00
|
|
|
translations_fields.each do |translations_field|
|
2021-07-14 04:56:02 +00:00
|
|
|
a[translations_field+"_translations"] = anns.send(translations_field+"_translations") rescue {}
|
2020-07-22 02:23:36 +00:00
|
|
|
end
|
|
|
|
a["text_translations"] = {}
|
|
|
|
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?
|
|
|
|
a["event_date"] = anns.event_date
|
2021-08-09 02:45:06 +00:00
|
|
|
a["event_end_date"] = anns.event_end_date
|
2020-07-31 06:52:33 +00:00
|
|
|
a["postdate"] = anns.event_date#anns.postdate
|
2020-07-22 02:23:36 +00:00
|
|
|
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 "")
|
2020-07-31 06:52:33 +00:00
|
|
|
a["img_src"] = a["image"]["thumb"] || "/assets/announcement-default.jpg"
|
2020-07-22 02:23:36 +00:00
|
|
|
a["tags"] = []
|
2021-07-14 04:56:02 +00:00
|
|
|
a["category"] = {}
|
2020-07-22 02:23:36 +00:00
|
|
|
a["author"] = author
|
|
|
|
a["params"] = anns.to_param
|
|
|
|
a["event_news_links"] = []
|
|
|
|
a["event_news_files"] = []
|
2021-08-09 02:45:06 +00:00
|
|
|
a["event_carousel_images"] = anns.event_carousel_images.map{|image| {"src"=>"#{request.base_url}" + image.file.url,"description"=>image.description.to_s,"description_text"=>image.description_text }}
|
2020-07-22 02:23:36 +00:00
|
|
|
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
|
2021-07-14 04:56:02 +00:00
|
|
|
cat = anns.category
|
|
|
|
if (!category_ids.include?(cat.id.to_s) rescue false)
|
|
|
|
category_ids << cat.id.to_s
|
|
|
|
category_titles << {"title_translations" => cat.title_translations}
|
|
|
|
end
|
|
|
|
a["category"] = {"title_translations" => (cat.title_translations rescue {})}
|
2020-07-22 02:23:36 +00:00
|
|
|
anns.event_news_links.each do |bl|
|
|
|
|
b = {}
|
|
|
|
b["url"] = bl.url
|
|
|
|
b["title_translations"] = bl.title_translations
|
2020-07-31 06:52:33 +00:00
|
|
|
b["link_url"] = b["url"]
|
|
|
|
b["link_title_translations"] = bl.title_translations.map{|k,v| [k,(v.blank? ? File.basename(b["url"]) : v rescue '')]}.to_h
|
2020-07-22 02:23:36 +00:00
|
|
|
a["event_news_links"] << b
|
|
|
|
end
|
|
|
|
anns.event_news_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 "")
|
2020-07-31 06:52:33 +00:00
|
|
|
b["file_url"] = b["url"]
|
|
|
|
b["file_title_translations"] = bf.title_translations.map{|k,v| [k,(v.blank? ? File.basename(b["url"]) : v rescue '')]}.to_h
|
2020-07-22 02:23:36 +00:00
|
|
|
a["event_news_files"] << b
|
|
|
|
end
|
|
|
|
all_anns << a
|
|
|
|
end
|
|
|
|
{
|
|
|
|
"event_news" => all_anns,
|
2021-07-14 04:56:02 +00:00
|
|
|
"tags" => tag_names,
|
|
|
|
"categories" => category_titles
|
2020-07-22 02:23:36 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|