diff --git a/app/controllers/admin/announcements_controller.rb b/app/controllers/admin/announcements_controller.rb index 208542e..f04ce6a 100644 --- a/app/controllers/admin/announcements_controller.rb +++ b/app/controllers/admin/announcements_controller.rb @@ -29,6 +29,44 @@ class Admin::AnnouncementsController < OrbitAdminController end end + def feed + @table_feed_fields = ["announcement.feed_name", :tags] + @feeds = BulletinFeed.all.asc(:created_at) + + end + + def feedform + if params[:type] == "new" + @announcement_feed = BulletinFeed.new + render :partial => "feed_form" + else params[:type] == "edit" + @announcement_feed = BulletinFeed.find(params[:id]) + render :partial => "edit_feed_form" + end + end + + def createfeed + announcement_feed = BulletinFeed.new(feed_params) + announcement_feed.save + feeds = BulletinFeed.all.asc(:created_at) + render :partial => "feed", :collection => feeds + end + + def updatefeed + ann_feed = BulletinFeed.find(params[:id]) + ann_feed.update_attributes(feed_params) + ann_feed.save + feeds = BulletinFeed.all.asc(:created_at) + render :partial => "feed", :collection => feeds + end + + def deletefeed + ann_feed = BulletinFeed.find(params[:id]) + ann_feed.destroy + feeds = BulletinFeed.all.asc(:created_at) + render :partial => "feed", :collection => feeds + end + def new @tags = @module_app.tags @statuses = [] @@ -219,4 +257,8 @@ class Admin::AnnouncementsController < OrbitAdminController params[:bulletin][:email_sent] = params[:bulletin][:email_sent].nil? ? 0 : params[:bulletin][:email_sent] params.require(:bulletin).permit! end + + def feed_params + params.require(:bulletin_feed).permit! + end end diff --git a/app/controllers/announcement_feeds_controller.rb b/app/controllers/announcement_feeds_controller.rb new file mode 100644 index 0000000..5f47a10 --- /dev/null +++ b/app/controllers/announcement_feeds_controller.rb @@ -0,0 +1,88 @@ +class AnnouncementFeedsController < ApplicationController + + def feed + 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 + 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"] = anns.text_translations + 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 + b["url"] = "#{request.base_url}" + bf.file.url + a["bulletin_files"] << b + end + all_anns << a + end + render :json => {"announcements" => all_anns, "tags" => tag_names}.to_json + 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["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 + +end + + + + + + diff --git a/app/controllers/announcements_controller.rb b/app/controllers/announcements_controller.rb index 9d41c8b..6949e9b 100644 --- a/app/controllers/announcements_controller.rb +++ b/app/controllers/announcements_controller.rb @@ -1,10 +1,13 @@ class AnnouncementsController < ApplicationController def index - if !OrbitHelper.params['tags'].blank? - announcements = Bulletin.where(:title.ne => "",:is_preview.in=>[false,nil]).and(:title.ne => nil).can_display.is_approved.order_by(:postdate=>'desc').filter_by_tags(OrbitHelper.params['tags']).page(OrbitHelper.page_number).per(OrbitHelper.page_data_count) + params = OrbitHelper.params + feeds_anns = [] + if !params['tags'].blank? + announcements = Bulletin.where(:title.ne => "",:is_preview.in=>[false,nil]).and(:title.ne => nil).can_display.is_approved.order_by(:postdate=>'desc').filter_by_tags(params['tags']).page(OrbitHelper.page_number).per(OrbitHelper.page_data_count) else announcements = Bulletin.where(:title.ne => "",:is_preview.in=>[false,nil]).and(:title.ne => nil).can_display.is_approved.order_by(:postdate=>'desc').filter_by_categories.filter_by_tags + feeds_anns = get_feed_announcements("index") end anns = announcements.collect do |a| @@ -35,14 +38,14 @@ class AnnouncementsController < ApplicationController "view_count" => a.view_count } end - #If no data , hide title&table if announcements.count == 0 display = "hide" end - + anns = anns.concat(feeds_anns) + sorted = anns.sort{ |k,v| v["postdate"] <=> k["postdate"] } { - "announcements" => anns, + "announcements" => sorted, "extras" => { "widget-title" =>t('announcement.announcement'), "title-head" => t('announcement.table.title'), @@ -61,6 +64,31 @@ class AnnouncementsController < ApplicationController end + def get_feed_announcements(type) + feed_anns = OrbitHelper.get_feed_for_module(type) + fans = [] + locale = OrbitHelper.get_site_locale.to_s + feed_anns.each do |fa| + x = { + "bulletin_links" => fa["bulletin_links"], + "bulletin_files" => fa["bulletin_files"], + "title" => fa["title_translations"][locale], + "subtitle" => fa["subtitle_translations"][locale], + "statuses" => [], + "category" => fa["category"], + "postdate" => fa["postdate"], + "author" => fa["author"], + "link_to_show" => OrbitHelper.url_to_show(fa["params"]), + "img_src" => fa["image"]["thumb"] || "/assets/announcement-default.jpg", + "img_description" => fa["image_description_translations"][locale], + "more" => t(:more_plus), + "view_count" => "" + } + fans << x + end + fans + end + def widget tags = ["all"] if OrbitHelper.widget_tags.empty? announcements = Bulletin.where(:title.ne => "",:is_preview.in=>[false,nil]).and(:title.ne => nil).can_display.is_approved.order_by(:postdate=>'desc').filter_by_widget_categories.filter_by_tags(tags) @@ -93,8 +121,10 @@ class AnnouncementsController < ApplicationController end mp = (anns[0]["img_src"] rescue "") mpd = (anns[0]["img_description"] rescue "") + anns = anns.concat(get_feed_announcements("widget")) + sorted = anns.sort{ |k,v| v["postdate"] <=> k["postdate"] } { - "announcements" => anns, + "announcements" => sorted, "extras" => { "more_url"=>OrbitHelper.widget_more_url, "main_picture" => mp, @@ -112,9 +142,8 @@ class AnnouncementsController < ApplicationController } end - def show - params = OrbitHelper.params - announcement = Bulletin.can_display.find_by(:uid=>params[:uid]) + def show_local_announcement(uid) + announcement = Bulletin.can_display.find_by(:uid => uid) url_to_edit = OrbitHelper.user_can_edit?(announcement) ? "/admin/announcements/#{announcement.id.to_s}/edit" : "" access_level = OrbitHelper.user_access_level? @@ -157,4 +186,65 @@ class AnnouncementsController < ApplicationController } end + def show_feed_announcement(uid) + announcement = OrbitHelper.get_from_feed(uid) + locale = OrbitHelper.get_site_locale.to_s + url_to_edit = "#" + return {} if announcement.blank? + tags = [] + + announcement["tags"].each{|tag| + t = Tag.where(:name => tag["name_translations"][locale]).first rescue nil + if t.nil? + I18n.locale = (locale == "en" ? :zh_tw : :en) + t = Tag.where(:name => tag["name_translations"][locale]).first rescue nil + I18n.locale = locale.to_sym + end + tags << { + "tag" => tag["name_translations"][locale], + "url" => (t.nil? ? "#" : OrbitHelper.page_for_tag(t)) + } + } + + files = announcement["bulletin_files"].map{|file| { "file_url" => file["url"], "file_title" => (file["title_translations"][locale] == "" ? File.basename(file["url"]) : file["title_translations"][locale] rescue '') } } rescue [] + + links = announcement["bulletin_links"].map{|link| { "link_url" => link["url"], "link_title" => (link["title_translations"][locale] == "" ? link["url"] : link["title_translations"][locale]) } } rescue [] + + update_user = announcement["author"] + desc = announcement["image_description_translations"][locale] + desc = (desc.nil? || desc == "" ? "announcement image" : desc) + + request = OrbitHelper.request + meta_desc = announcement["subtitle_translations"][locale] == "" ? announcement["text_translations"][locale][0..200] : announcement["subtitle_translations"][locale] + OrbitHelper.render_meta_tags([{"property" => "og:title", "content" => announcement["title_translations"][locale]},{"property" => "og:site_name", "content" => Site.first.title},{"property" => "og:url", "content" => request.original_url},{"property" => "og:description", "content" => meta_desc},{"property" => "og:image", "content" => announcement["image"]["original"]},{"property" => "og:type", "content" => "Article"}]) + + datetime = DateTime.parse(announcement["postdate"]) + + { + "tags" => tags, + "bulletin_files" => files, + "bulletin_links" => links, + "data" => { + "title" => announcement["title_translations"][locale], + "update_user" => update_user, + "updated_at" => datetime.strftime('%Y-%m-%d %H:%M'), + "body" => announcement["text_translations"][locale], + "image" => announcement["image"]["original"], + "alt_title" => desc + }, + "impressionist" => nil, + "url_to_edit" => url_to_edit + } + end + + def show + params = OrbitHelper.params + uid = params[:uid] + if OrbitHelper.is_object_from_feed?(uid) + show_feed_announcement(uid) + else + show_local_announcement(uid) + end + end + end diff --git a/app/models/bulletin_feed.rb b/app/models/bulletin_feed.rb new file mode 100644 index 0000000..f577728 --- /dev/null +++ b/app/models/bulletin_feed.rb @@ -0,0 +1,9 @@ +class BulletinFeed + include Mongoid::Document + include Mongoid::Timestamps + include Slug + + field :title, as: :slug_title, type: String, localize: true + field :tag_ids, type: Array, default: [] + +end \ No newline at end of file diff --git a/app/views/admin/announcements/_edit_feed_form.html.erb b/app/views/admin/announcements/_edit_feed_form.html.erb new file mode 100644 index 0000000..571b946 --- /dev/null +++ b/app/views/admin/announcements/_edit_feed_form.html.erb @@ -0,0 +1,38 @@ +<%= form_for @announcement_feed, url: admin_announcement_updatefeed_path(:id => @announcement_feed.id), html: {class: "form-horizontal main-forms"} do |f| %> +
+<% end %> + + \ No newline at end of file diff --git a/app/views/admin/announcements/_feed.html.erb b/app/views/admin/announcements/_feed.html.erb new file mode 100644 index 0000000..f08181d --- /dev/null +++ b/app/views/admin/announcements/_feed.html.erb @@ -0,0 +1,34 @@ +