announcement-test/app/controllers/announcements_controller.rb

354 lines
15 KiB
Ruby
Raw Normal View History

2014-04-01 07:12:43 +00:00
class AnnouncementsController < ApplicationController
def index
Bulletin.remove_expired_status
2015-07-08 10:11:25 +00:00
params = OrbitHelper.params
feeds_anns = []
top_anns = []
2015-07-08 10:11:25 +00:00
if !params['tags'].blank?
announcements = Bulletin.where(:title.ne => "",:is_preview.in=>[false,nil]).and(:title.ne => nil).can_display.is_approved.filter_by_tags(params['tags']).page(OrbitHelper.page_number).per(OrbitHelper.page_data_count).to_a
2014-12-25 02:54:58 +00:00
else
2016-04-27 10:55:14 +00:00
if !params["source"].present?
announcements = Bulletin.where(:title.ne => "",:is_preview.in=>[false,nil]).and(:title.ne => nil, :is_top.ne => true).can_display.is_approved.filter_by_categories([],false).filter_by_tags.to_a
feeds_anns = get_feed_announcements("index")
else
announcements = []
feeds_anns = get_feed_announcements("index",params["source"])
end
2014-12-25 02:54:58 +00:00
end
2014-07-11 10:30:52 +00:00
if OrbitHelper.page_number == 1 or OrbitHelper.page_number.nil?
top_anns = Bulletin.where(:title.ne => "",:is_preview.in=>[false,nil], :is_top => true).and(:title.ne => nil).can_display.filter_by_categories([],false).to_a
end
if !feeds_anns.blank?
announcements = announcements.concat(feeds_anns)
sorted = announcements.sort{ |k,v| v["postdate"] <=> k["postdate"] }
sorted = top_anns + sorted
if params["keywords"].present?
sorted = sorted.find_all{|anns|
if anns["source-site"].present?
/#{params[:keywords].to_s}/i.match anns["title"]
else
/#{params[:keywords].to_s}/i.match anns.title
end
}
end
sorted = Kaminari.paginate_array(sorted).page(OrbitHelper.page_number).per(OrbitHelper.page_data_count) rescue []
else
announcements = top_anns + announcements
if params["keywords"].present?
announcements = announcements.find_all{|anns| /#{params[:keywords].to_s}/i.match anns.title}
end
sorted = Kaminari.paginate_array(announcements).page(OrbitHelper.page_number).per(OrbitHelper.page_data_count) rescue []
end
total_pages = sorted.total_pages
anns = []
sorted.each do |a|
if !a["source-site"].present?
statuses = a.statuses_with_classname.collect do |status|
{
"status" => status["name"],
"status-class" => "status-#{status['classname']}"
}
end
files = a.bulletin_files.map{|file| { "file_url" => file.file.url, "file_title" => (file.title.blank? ? File.basename(file.file.path) : file.title rescue '') } } rescue []
links = a.bulletin_links.map{|link| { "link_url" => link.url, "link_title" => (link.title.blank? ? link.url : link.title) } } rescue []
author = User.find(a.create_user_id).member_profile.name rescue ""
desc = a.image_description
desc = (desc.nil? || desc == "" ? "announcement image" : desc)
anns << {
"bulletin_links" => links,
"bulletin_files" => files,
"title" => a.title,
"source-site" => "",
"source-site-title" => "",
"source-site-link" => "",
"subtitle" => a.subtitle,
"statuses" => statuses,
"category" => a.category.title,
"postdate" => a.postdate,
"author" => author,
"is_top" => (a.is_top? ? 1 : 0),
"link_to_show" => OrbitHelper.url_to_show(a.to_param),
"img_src" => a.image.thumb.url || "/assets/announcement-default.jpg",
"img_description" => desc,
"more" => t(:more_plus),
"view_count" => a.view_count
2014-06-06 09:48:33 +00:00
}
else
2016-04-27 10:55:14 +00:00
anns << a if a["source-site-title"] == params["source"]
2014-06-06 09:48:33 +00:00
end
2014-04-01 07:12:43 +00:00
end
2015-02-25 02:58:51 +00:00
#If no data , hide title&table
if announcements.count == 0
display = "hide"
end
# anns = anns.concat(feeds_anns)
# total_pages = announcements.total_pages
2014-04-02 06:44:36 +00:00
{
2015-10-08 09:17:13 +00:00
"announcements" => anns,
2014-07-11 10:30:52 +00:00
"extras" => {
2014-09-19 07:28:17 +00:00
"widget-title" =>t('announcement.announcement'),
2014-09-19 07:54:41 +00:00
"title-head" => t('announcement.table.title'),
"date-head" => t('announcement.table.date'),
"status-head" => t('announcement.table.status'),
"author-head" => t('announcement.table.author'),
2014-09-19 07:54:41 +00:00
"subtitle-head" => t('announcement.table.sub_title'),
2015-01-16 09:01:33 +00:00
"category-head" => t('announcement.table.category'),
"link-head" => t('announcement.table.link'),
2015-02-25 02:58:51 +00:00
"file-head" => t('announcement.table.file'),
2015-03-16 08:56:53 +00:00
"view-count-head" => t('announcement.table.view_count'),
2015-02-25 02:58:51 +00:00
"display" => display
2014-07-11 10:30:52 +00:00
},
"total_pages" => total_pages
2014-11-19 09:50:08 +00:00
}
2014-04-01 07:12:43 +00:00
end
2016-04-27 10:55:14 +00:00
def get_feed_announcements(type,source=nil)
2015-07-08 10:11:25 +00:00
feed_anns = OrbitHelper.get_feed_for_module(type)
fans = []
locale = OrbitHelper.get_site_locale.to_s
feed_anns.each do |fa|
2016-04-27 10:55:14 +00:00
next if !source.nil? && source != fa["source-site-title"]
2015-11-11 09:54:10 +00:00
status = {
2016-04-12 07:56:29 +00:00
"status" => "<a href='#{fa["source-site"]}' target='_blank' class='feed-source'>#{fa["source-site-title"]}</a>",
"status-class" => "status-source"
}
files = fa["bulletin_files"].collect{|bf| { "file_url" => bf["url"], "file_title" => (fa["title_translations"][locale].blank? ? File.basename(fa["url"]) : fa["title_translations"][locale] rescue '') }} rescue []
2016-04-12 07:56:29 +00:00
links = fa["bulletin_links"].map{|link| { "link_url" => link["url"], "link_title" => (link["title_translations"][locale].blank? ? link["url"] : link["title_translations"][locale]) } } rescue []
2015-07-08 10:11:25 +00:00
x = {
2016-04-12 07:59:50 +00:00
"bulletin_links" => links,
"bulletin_files" => files,
2015-07-08 10:11:25 +00:00
"title" => fa["title_translations"][locale],
"subtitle" => fa["subtitle_translations"][locale],
2015-11-11 09:54:10 +00:00
"statuses" => [status],
2015-07-08 10:11:25 +00:00
"category" => fa["category"],
"postdate" => fa["postdate"],
"author" => fa["author"],
"is_top" => 0,
2015-11-10 06:05:31 +00:00
"source-site" => "<a href='#{fa["source-site"]}' target='_blank' class='feed-source'>#{fa["source-site-title"]}</a>",
2015-11-10 06:31:52 +00:00
"source-site-title" => fa["source-site-title"],
"source-site-link" => fa["source-site"],
2015-07-08 10:11:25 +00:00
"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" => ""
}
if !x["title"].empty?
fans << x
end
2015-07-08 10:11:25 +00:00
end
fans
end
2015-10-29 08:42:42 +00:00
def random_announcement_widget
uid = OrbitHelper.params[:uid] rescue ""
2015-12-16 08:01:09 +00:00
tags = OrbitHelper.widget_tags
2015-10-29 08:42:42 +00:00
categories = OrbitHelper.widget_categories || []
announcements = Bulletin.where(:title.ne => "",:is_preview.in=>[false,nil],:uid.ne => uid, :category_id.in => categories).and(:title.ne => nil).can_display.is_approved.filter_by_tags(tags).sample(OrbitHelper.widget_data_count)
pack_data(announcements, [])
2015-10-29 08:42:42 +00:00
end
2014-04-01 07:12:43 +00:00
def widget
Bulletin.remove_expired_status
uid = OrbitHelper.params[:uid] rescue ""
2015-12-16 08:01:09 +00:00
tags = OrbitHelper.widget_tags
announcements = Bulletin.where(:title.ne => "",:is_preview.in=>[false,nil],:uid.ne => uid).and(:title.ne => nil, :is_top.ne => true).can_display.is_approved.filter_by_widget_categories([],false).filter_by_tags(tags).to_a
2016-04-21 07:53:58 +00:00
top_anns = Bulletin.where(:title.ne => "",:is_preview.in=>[false,nil], :is_top => true, :uid.ne => uid).and(:title.ne => nil).can_display.filter_by_widget_categories([],false).filter_by_tags(tags).to_a
pack_data(announcements, top_anns)
2015-10-29 08:42:42 +00:00
end
def pack_data(announcements, top_anns = [])
page = Page.where(:module => "announcement").first rescue nil
feeds_anns = get_feed_announcements("widget")
announcements = announcements + feeds_anns
if !feeds_anns.blank?
sorted = announcements.sort{ |k,v| v["postdate"] <=> k["postdate"] }
sorted = top_anns + sorted
sorted = Kaminari.paginate_array(sorted).page(1).per(OrbitHelper.widget_data_count) rescue []
else
announcements = top_anns + announcements
sorted = Kaminari.paginate_array(announcements).page(1).per(OrbitHelper.widget_data_count) rescue []
end
anns = []
sorted.each do |a|
if !a["source-site"].present?
statuses = a.statuses_with_classname.collect do |status|
{
"status" => status["name"],
"status-class" => "status-#{status['classname']}"
}
end
files = a.bulletin_files.map{|file| { "file_url" => file.file.url, "file_title" => (file.title.blank? ? File.basename(file.file.path) : file.title rescue '') } } rescue []
links = a.bulletin_links.map{|link| { "link_url" => link.url, "link_title" => (link.title.blank? ? link.url : link.title) } } rescue []
author = User.find(a.create_user_id).member_profile.name rescue ""
desc = a.image_description
desc = (desc.nil? || desc == "" ? "announcement image" : desc)
anns << {
"bulletin_links" => links,
"bulletin_files" => files,
"title" => a.title,
"source-site" => "",
"source-site-title" => "",
"source-site-link" => "",
"subtitle" => a.subtitle,
"statuses" => statuses,
"category" => a.category.title,
"postdate" => a.postdate,
"author" => author,
"link_to_show" => OrbitHelper.widget_item_url(a.to_param),
"img_src" => a.image.thumb.url || "/assets/announcement-default.jpg",
"img_description" => desc
2014-06-06 09:48:33 +00:00
}
else
anns << a
2014-04-01 07:12:43 +00:00
end
end
2014-12-03 10:02:47 +00:00
mp = (anns[0]["img_src"] rescue "")
mpd = (anns[0]["img_description"] rescue "")
2014-04-02 06:44:36 +00:00
{
2015-10-13 08:48:45 +00:00
"announcements" => anns,
2014-07-11 10:30:52 +00:00
"extras" => {
2014-09-19 07:32:54 +00:00
"more_url"=>OrbitHelper.widget_more_url,
2014-12-03 10:07:52 +00:00
"main_picture" => mp,
"main_picture_description" => mpd,
2014-09-19 07:54:41 +00:00
"title-head" => t('announcement.table.title'),
"date-head" => t('announcement.table.date'),
"author-head" => t('announcement.table.author'),
2014-09-19 07:54:41 +00:00
"status-head" => t('announcement.table.status'),
"subtitle-head" => t('announcement.table.sub_title'),
"category-head" => t('announcement.table.category'),
2015-01-16 09:03:02 +00:00
"link-head" => t('announcement.table.link'),
2015-01-16 09:06:21 +00:00
"file-head" => t('announcement.table.file'),
2015-02-11 10:22:49 +00:00
"read_more" => ("/#{I18n.locale.to_s + page.url}" rescue "")
2014-07-11 10:30:52 +00:00
}
2014-11-19 09:50:08 +00:00
}
2014-04-01 07:12:43 +00:00
end
2014-11-19 09:50:08 +00:00
2015-10-19 08:59:26 +00:00
def show_local_announcement(uid, is_preview)
if is_preview
announcement = Bulletin.find_by(:uid => uid)
else
announcement = Bulletin.can_display.find_by(:uid => uid)
end
2014-08-05 06:37:33 +00:00
url_to_edit = OrbitHelper.user_can_edit?(announcement) ? "/admin/announcements/#{announcement.id.to_s}/edit" : ""
2014-09-22 08:32:30 +00:00
access_level = OrbitHelper.user_access_level?
if !announcement.approved && (access_level != "manager" && access_level != "admin")
return {}
end
2014-12-11 09:41:42 +00:00
return {} if announcement.category.disable
2014-07-11 10:30:52 +00:00
tags = announcement.tags.map{|tag| {
"tag" => tag.name ,
"url" => OrbitHelper.page_for_tag(tag)
} } rescue []
2016-04-12 10:14:17 +00:00
files = announcement.bulletin_files.map{|file| { "file_url" => file.file.url, "file_title" => (file.title.blank? ? URI.unescape(File.basename(file.file.path)) : file.title rescue '') } } rescue []
2014-10-06 09:59:40 +00:00
links = announcement.bulletin_links.map{|link| { "link_url" => link.url, "link_title" => (link.title.blank? ? link.url : link.title) } } rescue []
2014-05-14 11:51:17 +00:00
update_user = announcement.update_user.member_profile.name rescue ""
2015-06-02 09:48:18 +00:00
desc = announcement.image_description
desc = (desc.nil? || desc == "" ? "announcement image" : desc)
2015-07-03 11:53:24 +00:00
request = OrbitHelper.request
meta_desc = announcement.subtitle.nil? || announcement.subtitle == "" ? announcement.text[0..200] : announcement.subtitle
OrbitHelper.render_meta_tags([{"property" => "og:title", "content" => announcement.title},{"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" => "#{request.base_url}#{announcement.image.url}"},{"property" => "og:type", "content" => "Article"}])
2014-05-05 07:48:49 +00:00
{
"tags" => tags,
2014-05-05 07:48:49 +00:00
"bulletin_files" => files,
"bulletin_links" => links,
"data" => {
2014-05-02 10:21:51 +00:00
"title" => announcement.title,
2014-05-14 11:51:17 +00:00
"update_user" => update_user,
2014-12-27 03:20:50 +00:00
"updated_at" => announcement.postdate.strftime('%Y-%m-%d %H:%M'),
2014-05-05 07:48:49 +00:00
"body" =>announcement.text,
2015-06-02 09:48:18 +00:00
"image" => announcement.image.url,
"alt_title" => desc
2014-05-15 11:27:05 +00:00
},
2014-08-05 06:37:33 +00:00
"impressionist" => (announcement.is_preview ? nil : announcement),
"url_to_edit"=>url_to_edit
2014-11-19 09:50:08 +00:00
}
2014-06-17 10:46:52 +00:00
end
2014-11-19 09:50:08 +00:00
2015-07-08 10:11:25 +00:00
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))
}
}
2016-04-12 10:14:17 +00:00
files = announcement["bulletin_files"].map{|file| { "file_url" => file["url"], "file_title" => (file["title_translations"][locale] == "" ? URI.unescape(File.basename(file["url"])) : file["title_translations"][locale] rescue '') } } rescue []
2015-07-08 10:11:25 +00:00
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] rescue ""
2015-07-08 10:11:25 +00:00
desc = (desc.nil? || desc == "" ? "announcement image" : desc)
request = OrbitHelper.request
2016-04-12 09:52:50 +00:00
if announcement["subtitle_translations"].present?
2016-04-12 09:55:50 +00:00
meta_desc = announcement["subtitle_translations"][locale] != "" ? announcement["subtitle_translations"][locale] : announcement["text_translations"][locale][0..200] rescue ""
2016-04-12 09:52:50 +00:00
else
meta_desc = ""
end
2015-07-08 10:11:25 +00:00
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
2015-10-19 08:59:26 +00:00
show_local_announcement(uid, (params["preview"] == "true" ? true : false))
2015-07-08 10:11:25 +00:00
end
end
2014-04-01 07:12:43 +00:00
end