From 1ff7b59123578a92fca2b84d2f076efda48b8772 Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Wed, 8 Jul 2015 18:11:25 +0800 Subject: [PATCH] feeds added for annoucnement --- .../admin/announcements_controller.rb | 42 ++++++ .../announcement_feeds_controller.rb | 88 +++++++++++++ app/controllers/announcements_controller.rb | 108 +++++++++++++-- app/models/bulletin_feed.rb | 9 ++ .../announcements/_edit_feed_form.html.erb | 38 ++++++ app/views/admin/announcements/_feed.html.erb | 34 +++++ .../admin/announcements/_feed_form.html.erb | 38 ++++++ app/views/admin/announcements/feed.html.erb | 123 ++++++++++++++++++ config/locales/en.yml | 2 + config/locales/zh_tw.yml | 4 +- config/routes.rb | 8 ++ lib/announcement/engine.rb | 6 + 12 files changed, 490 insertions(+), 10 deletions(-) create mode 100644 app/controllers/announcement_feeds_controller.rb create mode 100644 app/models/bulletin_feed.rb create mode 100644 app/views/admin/announcements/_edit_feed_form.html.erb create mode 100644 app/views/admin/announcements/_feed.html.erb create mode 100644 app/views/admin/announcements/_feed_form.html.erb create mode 100644 app/views/admin/announcements/feed.html.erb 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| %> +
+ <% @site_in_use_locales.each do |locale| %> + <%= f.fields_for :title_translations do |f| %> +
+ +
+ <%= f.text_field locale, data: {"fv-validation" => "required;","fv-messages" => "Cannot be empty.;"}, value: (@announcement_feed.title_translations[locale.to_s] rescue nil) %> +
+
+ <% end %> + <% end %> +
+
+
+ +
+
+
+<% 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 @@ + + + <%= feed.title %> +
+ +
+ + +
+ +
+ + \ No newline at end of file diff --git a/app/views/admin/announcements/_feed_form.html.erb b/app/views/admin/announcements/_feed_form.html.erb new file mode 100644 index 0000000..e7652eb --- /dev/null +++ b/app/views/admin/announcements/_feed_form.html.erb @@ -0,0 +1,38 @@ +<%= form_for @announcement_feed, url: admin_announcement_createfeed_path, html: {class: "form-horizontal main-forms"} do |f| %> +
+ <% @site_in_use_locales.each do |locale| %> + <%= f.fields_for :title_translations do |f| %> +
+ +
+ <%= f.text_field locale, data: {"fv-validation" => "required;","fv-messages" => "Cannot be empty.;"}, value: (@announcement_feed.title_translations[locale.to_s] rescue nil) %> +
+
+ <% end %> + <% end %> +
+ +
+<% 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..02cdae9 --- /dev/null +++ b/app/views/admin/announcements/feed.html.erb @@ -0,0 +1,123 @@ +<% content_for :page_specific_css do %> + <%= stylesheet_link_tag("admin/tags") %> +<% end %> +<% content_for :page_specific_javascript do %> + <%= javascript_include_tag "validator" %> +<% end %> + + + + + <% @table_feed_fields.each do |f| %> + <%= thead(f) %> + <% end %> + + + + <%= render :partial => "feed", :collection => @feeds %> + +
+<% if current_user.is_admin? or current_user.is_manager?(@module_app) %> + +
+ + +
+ + +<% end %> + + \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index f1e14c3..db8851d 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -12,6 +12,8 @@ en: File: File view_count: View Count add_new: Add New + feed_name: Feed Name + feed_list: Feed List approve: Approve all_articles: All Articles announcement: Announcement diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index 97104bf..d08484a 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -9,10 +9,12 @@ zh_tw: category: 類別 author: 張貼人 link: 超連結 - file: 檔案下載 + file: 檔案下載Fㄍㄩ view_count: 瀏覽人次 add_new: 新建 approve: 通過 + feed_name: Feed 標題 + feed_list: 訂閱清單 all_articles: 文章列表 announcement: 公告 approval_setting: 審核設定 diff --git a/config/routes.rb b/config/routes.rb index 7f9a09b..81add08 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,9 +5,14 @@ Rails.application.routes.draw do scope "(:locale)", locale: Regexp.new(locales.join("|")) do namespace :admin do post 'announcement/preview', to: 'announcements#preview' + post 'announcement/createfeed', to: 'announcements#createfeed' + patch 'announcement/updatefeed', to: 'announcements#updatefeed' + delete 'announcement/deletefeed', to: 'announcements#deletefeed' get 'announcement/destroy_preview/:slug_title-:uid', to: 'announcements#destroy_preview' get 'announcement/approve_bulletin', to: 'announcements#approve_bulletin' get 'announcement.json', to: 'bulletins#get_bulletins' + get 'announcement/feed', to: 'announcements#feed' + get 'announcements/feedform', to: 'announcements#feedform' resources :announcements end @@ -16,6 +21,9 @@ Rails.application.routes.draw do get ':slug_title-:uid', to: 'announcements#show', as: :display end end + + get "/xhr/announcements/feed/:uid" => "announcement_feeds#feed" + get "/xhr/announcements/feeds" => "announcement_feeds#feeds" end end diff --git a/lib/announcement/engine.rb b/lib/announcement/engine.rb index 49372f5..1e46f16 100644 --- a/lib/announcement/engine.rb +++ b/lib/announcement/engine.rb @@ -11,6 +11,7 @@ module Announcement categorizable authorizable frontend_enabled + feeds_url "/xhr/announcements/feeds" data_count 1..30 side_bar do @@ -43,6 +44,11 @@ module Announcement :active_for_action=>{'admin/announcements'=>'tags'}, :active_for_tag => 'Announcement', :available_for => 'managers' + context_link 'announcement.feed_list', + :link_path=>"admin_announcement_feed_path" , + :priority=>5, + :active_for_action=>{'admin/announcements'=>'feed'}, + :available_for => 'managers' end end