feeds added for annoucnement

This commit is contained in:
Harry Bomrah 2015-07-08 18:11:25 +08:00
parent 77a804f925
commit 1ff7b59123
12 changed files with 490 additions and 10 deletions

View File

@ -29,6 +29,44 @@ class Admin::AnnouncementsController < OrbitAdminController
end end
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 def new
@tags = @module_app.tags @tags = @module_app.tags
@statuses = [] @statuses = []
@ -219,4 +257,8 @@ class Admin::AnnouncementsController < OrbitAdminController
params[:bulletin][:email_sent] = params[:bulletin][:email_sent].nil? ? 0 : params[:bulletin][:email_sent] params[:bulletin][:email_sent] = params[:bulletin][:email_sent].nil? ? 0 : params[:bulletin][:email_sent]
params.require(:bulletin).permit! params.require(:bulletin).permit!
end end
def feed_params
params.require(:bulletin_feed).permit!
end
end end

View File

@ -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

View File

@ -1,10 +1,13 @@
class AnnouncementsController < ApplicationController class AnnouncementsController < ApplicationController
def index def index
if !OrbitHelper.params['tags'].blank? params = OrbitHelper.params
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) 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 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 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 end
anns = announcements.collect do |a| anns = announcements.collect do |a|
@ -35,14 +38,14 @@ class AnnouncementsController < ApplicationController
"view_count" => a.view_count "view_count" => a.view_count
} }
end end
#If no data , hide title&table #If no data , hide title&table
if announcements.count == 0 if announcements.count == 0
display = "hide" display = "hide"
end end
anns = anns.concat(feeds_anns)
sorted = anns.sort{ |k,v| v["postdate"] <=> k["postdate"] }
{ {
"announcements" => anns, "announcements" => sorted,
"extras" => { "extras" => {
"widget-title" =>t('announcement.announcement'), "widget-title" =>t('announcement.announcement'),
"title-head" => t('announcement.table.title'), "title-head" => t('announcement.table.title'),
@ -61,6 +64,31 @@ class AnnouncementsController < ApplicationController
end 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 def widget
tags = ["all"] if OrbitHelper.widget_tags.empty? 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) 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 end
mp = (anns[0]["img_src"] rescue "") mp = (anns[0]["img_src"] rescue "")
mpd = (anns[0]["img_description"] 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" => { "extras" => {
"more_url"=>OrbitHelper.widget_more_url, "more_url"=>OrbitHelper.widget_more_url,
"main_picture" => mp, "main_picture" => mp,
@ -112,9 +142,8 @@ class AnnouncementsController < ApplicationController
} }
end end
def show def show_local_announcement(uid)
params = OrbitHelper.params announcement = Bulletin.can_display.find_by(:uid => uid)
announcement = Bulletin.can_display.find_by(:uid=>params[:uid])
url_to_edit = OrbitHelper.user_can_edit?(announcement) ? "/admin/announcements/#{announcement.id.to_s}/edit" : "" url_to_edit = OrbitHelper.user_can_edit?(announcement) ? "/admin/announcements/#{announcement.id.to_s}/edit" : ""
access_level = OrbitHelper.user_access_level? access_level = OrbitHelper.user_access_level?
@ -157,4 +186,65 @@ class AnnouncementsController < ApplicationController
} }
end 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 end

View File

@ -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

View File

@ -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| %>
<fieldset>
<% @site_in_use_locales.each do |locale| %>
<%= f.fields_for :title_translations do |f| %>
<div class="control-group">
<label class="control-label muted" for="bulletin_feed_title_translations_<%= locale.to_s %>"><%= t(:title) + " (#{t(locale.to_s)})" %></label>
<div class="controls">
<%= f.text_field locale, data: {"fv-validation" => "required;","fv-messages" => "Cannot be empty.;"}, value: (@announcement_feed.title_translations[locale.to_s] rescue nil) %>
</div>
</div>
<% end %>
<% end %>
<hr />
<div class="tags">
<div id="tags-list">
<ul class="tags-groups checkbox-card module-tags">
<% @module_app.tags.each do |tag| %>
<li class="filter-item module">
<p class='card pull-left <%= @announcement_feed.tag_ids.include?(tag.id.to_s) ? "active" : "" %>'>
<input type="checkbox" <%= @announcement_feed.tag_ids.include?(tag.id.to_s) ? "checked=checked" : "" %> class="tag-checkbox" value="<%= tag.id.to_s %>" name="bulletin_feed[tag_ids][]">
</p>
<a href="#" onclick="return false;">
<% @site_in_use_locales.each_with_index do |locale,index| %>
<span class="tag"><%= tag.name_translations[locale] %></span>
<% if index < (@site_in_use_locales.count - 1) %>
/
<% end %>
<% end %>
</a>
</li>
<% end %>
</ul>
</div>
</div>
</fieldset>
<% end %>

View File

@ -0,0 +1,34 @@
<tr>
<td>
<%= feed.title %>
<div class="quick-edit">
<ul class="nav nav-pills">
<% if can_edit_or_delete?(feed) %>
<li><a href="#" class="edit-feed" data-feed-id="<%= feed.id.to_s %>"><%= t(:edit) %></a></li>
<li><a href="/admin/announcement/deletefeed?id=<%= feed.id.to_s %>" class="delete-feed text-error" ><%= t(:delete_) %></a></li>
<% end %>
</ul>
</div>
</td>
<td>
<div class="tags">
<div id="tags-list">
<ul class="tags-groups checkbox-card module-tags">
<% feed.tag_ids.each do |t| %>
<% tag = Tag.find(t) %>
<li class="filter-item module">
<a href="#" onclick="return false;">
<% @site_in_use_locales.each_with_index do |locale,index| %>
<span class="tag"><%= tag.name_translations[locale] %></span>
<% if index < (@site_in_use_locales.count - 1) %>
/
<% end %>
<% end %>
</a>
</li>
<% end %>
</ul>
</div>
</div>
</td>
</tr>

View File

@ -0,0 +1,38 @@
<%= form_for @announcement_feed, url: admin_announcement_createfeed_path, html: {class: "form-horizontal main-forms"} do |f| %>
<fieldset>
<% @site_in_use_locales.each do |locale| %>
<%= f.fields_for :title_translations do |f| %>
<div class="control-group">
<label class="control-label muted" for="bulletin_feed_title_translations_<%= locale.to_s %>"><%= t(:title) + " (#{t(locale.to_s)})" %></label>
<div class="controls">
<%= f.text_field locale, data: {"fv-validation" => "required;","fv-messages" => "Cannot be empty.;"}, value: (@announcement_feed.title_translations[locale.to_s] rescue nil) %>
</div>
</div>
<% end %>
<% end %>
<hr />
<div class="tags">
<div id="tags-list">
<ul class="tags-groups checkbox-card module-tags">
<% @module_app.tags.each do |tag| %>
<li class="filter-item module">
<p class="card pull-left">
<input type="checkbox" class="tag-checkbox" value="<%= tag.id.to_s %>" name="bulletin_feed[tag_ids][]">
</p>
<a href="#" onclick="return false;">
<% @site_in_use_locales.each_with_index do |locale,index| %>
<span class="tag"><%= tag.name_translations[locale] %></span>
<% if index < (@site_in_use_locales.count - 1) %>
/
<% end %>
<% end %>
</a>
</li>
<% end %>
</ul>
</div>
</div>
</fieldset>
<% end %>

View File

@ -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 class="table main-list">
<thead>
<tr class="sort-header">
<% @table_feed_fields.each do |f| %>
<%= thead(f) %>
<% end %>
</tr>
</thead>
<tbody>
<%= render :partial => "feed", :collection => @feeds %>
</tbody>
</table>
<% if current_user.is_admin? or current_user.is_manager?(@module_app) %>
<div class="bottomnav clearfix" style="left: 81px;">
<div class="action pull-right">
<a class="btn btn-primary new-feed" href="#">
<i class="icon-plus"></i> <%= t(:new_) %>
</a>
</div>
<div class="pagination pagination-centered"></div>
</div>
<div id="newFeedModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="newFeedModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="newFeedModalLabel">Create New Feed</h3>
</div>
<div class="modal-body">
<%#= render :partial => "feed_form" %>
</div>
<div class="modal-footer">
<button class="btn btn-primary" id="save_new_feed">Save changes</button>
</div>
</div>
<% end %>
<script type="text/javascript">
$(".new-feed").on("click",function(){
var modal = $("#newFeedModal");
modal.find("#newFeedModalLabel").text("Create New Feed");
modal.modal("show");
openFeedModal("new",null);
})
var bindEditButtons = function(){
$(".edit-feed").on("click",function(){
var modal = $("#newFeedModal");
modal.find("#newFeedModalLabel").text("Edit Feed");
modal.modal("show");
openFeedModal("edit",$(this).data("feed-id"));
return false;
})
$(".delete-feed").on("click",function(){
if(confirm("Are you sure?")){
var el = $(this);
$.ajax({
url : el.attr("href"),
type : "delete",
dataType : "html"
}).done(function(data){
$("table.main-list tbody").html(data);
bindEditButtons();
})
}
return false;
})
}
bindEditButtons();
var openFeedModal = function(type,feed_id){
$.ajax({
url : "/admin/announcements/feedform",
type : "get",
data : {"type" : type, "id" : feed_id},
dataType : "html"
}).done(function(form){
$("#newFeedModal .modal-body").html(form);
bindHandlers();
})
}
var bindHandlers = function(){
$(".tag-checkbox").on("click",function(){
if($(this).is(":checked")){
$(this).parent().addClass("active");
}else{
$(this).parent().removeClass("active");
}
})
var fv = new FormValidator($("#newFeedModal form"));
fv.form.on("submit",function(){
$.ajax({
url : fv.form.attr("action"),
data : fv.form.serializeArray(),
type : "post",
dataType : "html"
}).done(function(data){
$("table.main-list tbody").html(data);
bindEditButtons();
$("#newFeedModal").modal("hide");
fv.form.resetForm();
fv.form.find("ul.tags-groups p.active").removeClass("active");
})
return false;
})
$("#save_new_feed").on("click",function(){
if(fv.isFormValidated()){
fv.form.submit();
}else{
return false;
}
})
}
</script>

View File

@ -12,6 +12,8 @@ en:
File: File File: File
view_count: View Count view_count: View Count
add_new: Add New add_new: Add New
feed_name: Feed Name
feed_list: Feed List
approve: Approve approve: Approve
all_articles: All Articles all_articles: All Articles
announcement: Announcement announcement: Announcement

View File

@ -9,10 +9,12 @@ zh_tw:
category: 類別 category: 類別
author: 張貼人 author: 張貼人
link: 超連結 link: 超連結
file: 檔案下載 file: 檔案下載Fㄍㄩ
view_count: 瀏覽人次 view_count: 瀏覽人次
add_new: 新建 add_new: 新建
approve: 通過 approve: 通過
feed_name: Feed 標題
feed_list: 訂閱清單
all_articles: 文章列表 all_articles: 文章列表
announcement: 公告 announcement: 公告
approval_setting: 審核設定 approval_setting: 審核設定

View File

@ -5,9 +5,14 @@ Rails.application.routes.draw do
scope "(:locale)", locale: Regexp.new(locales.join("|")) do scope "(:locale)", locale: Regexp.new(locales.join("|")) do
namespace :admin do namespace :admin do
post 'announcement/preview', to: 'announcements#preview' 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/destroy_preview/:slug_title-:uid', to: 'announcements#destroy_preview'
get 'announcement/approve_bulletin', to: 'announcements#approve_bulletin' get 'announcement/approve_bulletin', to: 'announcements#approve_bulletin'
get 'announcement.json', to: 'bulletins#get_bulletins' get 'announcement.json', to: 'bulletins#get_bulletins'
get 'announcement/feed', to: 'announcements#feed'
get 'announcements/feedform', to: 'announcements#feedform'
resources :announcements resources :announcements
end end
@ -16,6 +21,9 @@ Rails.application.routes.draw do
get ':slug_title-:uid', to: 'announcements#show', as: :display get ':slug_title-:uid', to: 'announcements#show', as: :display
end end
end end
get "/xhr/announcements/feed/:uid" => "announcement_feeds#feed"
get "/xhr/announcements/feeds" => "announcement_feeds#feeds"
end end
end end

View File

@ -11,6 +11,7 @@ module Announcement
categorizable categorizable
authorizable authorizable
frontend_enabled frontend_enabled
feeds_url "/xhr/announcements/feeds"
data_count 1..30 data_count 1..30
side_bar do side_bar do
@ -43,6 +44,11 @@ module Announcement
:active_for_action=>{'admin/announcements'=>'tags'}, :active_for_action=>{'admin/announcements'=>'tags'},
:active_for_tag => 'Announcement', :active_for_tag => 'Announcement',
:available_for => 'managers' :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
end end