require "rss" class AnnouncementFeedsController < ApplicationController include Admin::AnnouncementsHelper def feed uid = params[:uid] feed_cache = BulletinFeedCache.where(uid: uid).first anns = '' if feed_cache.nil? BulletinFeedCache.create(uid: uid,content: '') Thread.new do anns = get_announcements(uid).to_json feed_cache = BulletinFeedCache.where(uid: uid).first if !feed_cache.nil? feed_cache.update_attributes(content: anns) end end else anns = feed_cache.content end render :json => anns 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_and_sorted.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 def smart_convertor(text) html_string = text url = request.protocol + request.host_with_port html_string = html_string.gsub(/img.*?src="(?=\/)(.*?)|a.*?href="(?=\/)(.*?)/i){|w| w+url} html_string = html_string.gsub(/img.*?src="\.\.(?=\/)(.*?)|a.*?href="\.\.(?=\/)(.*?)/i){|w| w[0...-2]+url} return html_string end def get_announcements(uid) bf = BulletinFeed.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 announcements = Bulletin.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 announcements = Bulletin.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 announcements = Bulletin.where(:postdate.gt => startdt, :postdate.lt => enddt).can_display_and_sorted.is_approved.filter_by_tags(tags) else announcements = Bulletin.all.can_display_and_sorted.is_approved.filter_by_tags(tags) end else announcements = [] 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 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["postdate"] = anns.postdate a["image_description_translations"] = anns.image_description_translations a["image"] = {} a["display_img"] = anns.display_img 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["subtitle_ann"] = anns.subtitle if anns.display_subtitle? a["bulletin_links"] = [] a["bulletin_files"] = [] a["bulletin_carousel_images"] = anns.bulletin_carousel_images.map{|image| {"src"=>"#{request.base_url}" + image.file.url,"description"=>image.description.to_s,"description_text"=>image.description_text }} 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"] = bl.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 b["url"] = ("#{request.base_url}" + bf.file.url rescue "") a["bulletin_files"] << b end all_anns << a end { "announcements" => all_anns, "tags" => tag_names } end end