Edit feed feature.(Add categories filter)

This commit is contained in:
BoHung Chiu 2021-06-02 18:06:27 +08:00
parent 2bfedbceb4
commit f34be54da0
6 changed files with 105 additions and 6 deletions

View File

@ -64,7 +64,7 @@ class Admin::AnnouncementsController < OrbitAdminController
end end
def feed def feed
@table_feed_fields = ["announcement.feed_name", :tags, "announcement.rssfeed", "announcement.jsonfeed"] @table_feed_fields = ["announcement.feed_name",:tags , :category , "announcement.rssfeed", "announcement.jsonfeed"]
@feeds = BulletinFeed.all.asc(:created_at) @feeds = BulletinFeed.all.asc(:created_at)
end end

View File

@ -28,6 +28,10 @@ class AnnouncementFeedsController < ApplicationController
if !tags.empty? if !tags.empty?
@announcements = Bulletin.can_display_and_sorted.is_approved.filter_by_tags(tags) @announcements = Bulletin.can_display_and_sorted.is_approved.filter_by_tags(tags)
end end
categories = @bf.category_ids
if !categories.empty?
@announcements = @announcements.filter_by_categories(categories)
end
end end
respond_to do |format| respond_to do |format|
format.html {redirect_to "/xhr/announcements/rssfeed/#{@bf.uid}.rss"} format.html {redirect_to "/xhr/announcements/rssfeed/#{@bf.uid}.rss"}
@ -72,21 +76,28 @@ class AnnouncementFeedsController < ApplicationController
dt = params[:date] dt = params[:date]
if !bf.nil? if !bf.nil?
tags = bf.tag_ids tags = bf.tag_ids
if !tags.empty? categories = bf.category_ids
if !(categories.empty? && tags.empty?)
if !dt.nil? if !dt.nil?
dt = DateTime.parse(dt) dt = DateTime.parse(dt)
dtt = dt + 1.day dtt = dt + 1.day
announcements = Bulletin.where(:postdate.gt => dt, :postdate.lt => dtt).can_display_and_sorted.is_approved.filter_by_tags(tags) announcements = Bulletin.where(:postdate.gt => dt, :postdate.lt => dtt).can_display_and_sorted.is_approved
elsif !startdt.nil? && enddt.nil? elsif !startdt.nil? && enddt.nil?
startdt = DateTime.parse(startdt) startdt = DateTime.parse(startdt)
enddt = DateTime.now enddt = DateTime.now
announcements = Bulletin.where(:postdate.gt => startdt, :postdate.lt => enddt).can_display_and_sorted.is_approved.filter_by_tags(tags) announcements = Bulletin.where(:postdate.gt => startdt, :postdate.lt => enddt).can_display_and_sorted.is_approved
elsif !startdt.nil? && !enddt.nil? elsif !startdt.nil? && !enddt.nil?
startdt = DateTime.parse(startdt) startdt = DateTime.parse(startdt)
enddt = DateTime.parse(enddt) + 1.day 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) announcements = Bulletin.where(:postdate.gt => startdt, :postdate.lt => enddt).can_display_and_sorted.is_approved
else else
announcements = Bulletin.all.can_display_and_sorted.is_approved.filter_by_tags(tags) announcements = Bulletin.all.can_display_and_sorted.is_approved
end
if !tags.empty?
announcements = announcements.filter_by_tags(tags)
end
if !categories.empty?
announcements = announcements.filter_by_categories(categories)
end end
else else
announcements = [] announcements = []
@ -94,7 +105,9 @@ class AnnouncementFeedsController < ApplicationController
end end
all_anns = [] all_anns = []
tag_names = [] tag_names = []
category_titles = []
tag_ids = [] tag_ids = []
category_ids = []
announcements.each do |anns| announcements.each do |anns|
user = User.find(anns.create_user_id) rescue nil user = User.find(anns.create_user_id) rescue nil
if !user.nil? if !user.nil?
@ -117,6 +130,7 @@ class AnnouncementFeedsController < ApplicationController
a["image"]["thumb"] = ("#{request.base_url}" + anns.image.thumb.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["image"]["mobile"] = ("#{request.base_url}" + anns.image.mobile.url rescue "")
a["tags"] = [] a["tags"] = []
a["category"] = {}
a["author"] = author a["author"] = author
a["params"] = anns.to_param a["params"] = anns.to_param
a["subtitle_ann"] = anns.subtitle if anns.display_subtitle? a["subtitle_ann"] = anns.subtitle if anns.display_subtitle?
@ -130,6 +144,12 @@ class AnnouncementFeedsController < ApplicationController
end end
a["tags"] << {"name_translations" => tag.name_translations} a["tags"] << {"name_translations" => tag.name_translations}
end end
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}
anns.bulletin_links.each do |bl| anns.bulletin_links.each do |bl|
b = {} b = {}
b["url"] = bl.url b["url"] = bl.url

View File

@ -5,6 +5,7 @@ class BulletinFeed
field :title, as: :slug_title, type: String, localize: true field :title, as: :slug_title, type: String, localize: true
field :tag_ids, type: Array, default: [] field :tag_ids, type: Array, default: []
field :category_ids, type: Array, default: []
before_save do before_save do
BulletinFeedCache.where(uid: self.uid).destroy BulletinFeedCache.where(uid: self.uid).destroy
end end

View File

@ -12,6 +12,7 @@
<% end %> <% end %>
<hr /> <hr />
<div class="tags"> <div class="tags">
<h4><%=t(:tags)%></h4>
<div id="tags-list"> <div id="tags-list">
<ul class="tags-groups checkbox-card module-tags"> <ul class="tags-groups checkbox-card module-tags">
<% @module_app.tags.each do |tag| %> <% @module_app.tags.each do |tag| %>
@ -32,6 +33,28 @@
</ul> </ul>
</div> </div>
</div> </div>
<div class="categories">
<h4><%=t(:category)%></h4>
<div id="categories-list">
<ul class="tags-groups checkbox-card module-categories">
<% @module_app.categories.each do |category| %>
<li class="filter-item module">
<p class="card pull-left <%= @announcement_feed.category_ids.include?(category.id.to_s) ? "active" : "" %>">
<input type="checkbox" class="tag-checkbox" <%= @announcement_feed.category_ids.include?(category.id.to_s) ? "checked=checked" : "" %> value="<%= category.id.to_s %>" name="bulletin_feed[category_ids][]">
</p>
<a href="#" onclick="return false;">
<% @site_in_use_locales.each_with_index do |locale,index| %>
<span class="tag"><%= category.title_translations[locale] %></span>
<% if index < (@site_in_use_locales.count - 1) %>
/
<% end %>
<% end %>
</a>
</li>
<% end %>
</ul>
</div>
</div>
</fieldset> </fieldset>
<% end %> <% end %>

View File

@ -42,6 +42,38 @@
</div> </div>
</div> </div>
</td> </td>
<td>
<div class="categories">
<div id="categories-list">
<ul class="tags-groups checkbox-card module-categories">
<% categories_to_remove = [] %>
<% feed.category_ids.each do |c| %>
<% category = Category.find(c) rescue nil %>
<% if !category.nil? %>
<li class="filter-item module">
<a href="#" onclick="return false;">
<% @site_in_use_locales.each_with_index do |locale,index| %>
<span class="tag"><%= category.title_translations[locale] %></span>
<% if index < (@site_in_use_locales.count - 1) %>
/
<% end %>
<% end %>
</a>
</li>
<% else %>
<% categories_to_remove << c %>
<% end %>
<% end %>
<% if !categories_to_remove.blank?
categories_to_remove.each do |c|
feed.category_ids.delete(c)
end
feed.save
end %>
</ul>
</div>
</div>
</td>
<td> <td>
<a href="/xhr/announcements/rssfeed/<%= feed.uid %>.rss" target="_blank">RSS Feed</a> <a href="/xhr/announcements/rssfeed/<%= feed.uid %>.rss" target="_blank">RSS Feed</a>
</td> </td>

View File

@ -12,6 +12,7 @@
<% end %> <% end %>
<hr /> <hr />
<div class="tags"> <div class="tags">
<h4><%=t(:tags)%></h4>
<div id="tags-list"> <div id="tags-list">
<ul class="tags-groups checkbox-card module-tags"> <ul class="tags-groups checkbox-card module-tags">
<% @module_app.tags.each do |tag| %> <% @module_app.tags.each do |tag| %>
@ -32,6 +33,28 @@
</ul> </ul>
</div> </div>
</div> </div>
<div class="categories">
<h4><%=t(:category)%></h4>
<div id="categories-list">
<ul class="tags-groups checkbox-card module-categories">
<% @module_app.categories.each do |category| %>
<li class="filter-item module">
<p class="card pull-left">
<input type="checkbox" class="tag-checkbox" value="<%= category.id.to_s %>" name="bulletin_feed[category_ids][]">
</p>
<a href="#" onclick="return false;">
<% @site_in_use_locales.each_with_index do |locale,index| %>
<span class="tag"><%= category.title_translations[locale] %></span>
<% if index < (@site_in_use_locales.count - 1) %>
/
<% end %>
<% end %>
</a>
</li>
<% end %>
</ul>
</div>
</div>
</fieldset> </fieldset>
<% end %> <% end %>