announcement-test/app/controllers/announcements_controller.rb

332 lines
14 KiB
Ruby

class AnnouncementsController < ApplicationController
def index
Bulletin.remove_expired_status
params = OrbitHelper.params
feeds_anns = []
top_anns = []
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
else
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")
end
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
sorted = Kaminari.paginate_array(sorted).page(OrbitHelper.page_number).per(OrbitHelper.page_data_count) rescue []
else
announcements = top_anns + announcements
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
}
else
anns << a
end
end
#If no data , hide title&table
if announcements.count == 0
display = "hide"
end
# anns = anns.concat(feeds_anns)
# total_pages = announcements.total_pages
{
"announcements" => anns,
"extras" => {
"widget-title" =>t('announcement.announcement'),
"title-head" => t('announcement.table.title'),
"date-head" => t('announcement.table.date'),
"status-head" => t('announcement.table.status'),
"author-head" => t('announcement.table.author'),
"subtitle-head" => t('announcement.table.sub_title'),
"category-head" => t('announcement.table.category'),
"link-head" => t('announcement.table.link'),
"file-head" => t('announcement.table.file'),
"view-count-head" => t('announcement.table.view_count'),
"display" => display
},
"total_pages" => total_pages
}
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|
status = {
"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 []
links = fa["bulletin_links"].map{|link| { "link_url" => link["url"], "link_title" => (link["title_translations"][locale].blank? ? link["url"] : link["title_translations"][locale]) } } rescue []
x = {
"bulletin_links" => links,
"bulletin_files" => files,
"title" => fa["title_translations"][locale],
"subtitle" => fa["subtitle_translations"][locale],
"statuses" => [status],
"category" => fa["category"],
"postdate" => fa["postdate"],
"author" => fa["author"],
"is_top" => 0,
"source-site" => "<a href='#{fa["source-site"]}' target='_blank' class='feed-source'>#{fa["source-site-title"]}</a>",
"source-site-title" => fa["source-site-title"],
"source-site-link" => fa["source-site"],
"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
end
fans
end
def random_announcement_widget
uid = OrbitHelper.params[:uid] rescue ""
tags = OrbitHelper.widget_tags
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, [])
end
def widget
Bulletin.remove_expired_status
uid = OrbitHelper.params[:uid] rescue ""
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
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).to_a
pack_data(announcements, top_anns)
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
}
else
anns << a
end
end
mp = (anns[0]["img_src"] rescue "")
mpd = (anns[0]["img_description"] rescue "")
{
"announcements" => anns,
"extras" => {
"more_url"=>OrbitHelper.widget_more_url,
"main_picture" => mp,
"main_picture_description" => mpd,
"title-head" => t('announcement.table.title'),
"date-head" => t('announcement.table.date'),
"author-head" => t('announcement.table.author'),
"status-head" => t('announcement.table.status'),
"subtitle-head" => t('announcement.table.sub_title'),
"category-head" => t('announcement.table.category'),
"link-head" => t('announcement.table.link'),
"file-head" => t('announcement.table.file'),
"read_more" => ("/#{I18n.locale.to_s + page.url}" rescue "")
}
}
end
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
url_to_edit = OrbitHelper.user_can_edit?(announcement) ? "/admin/announcements/#{announcement.id.to_s}/edit" : ""
access_level = OrbitHelper.user_access_level?
if !announcement.approved && (access_level != "manager" && access_level != "admin")
return {}
end
return {} if announcement.category.disable
tags = announcement.tags.map{|tag| {
"tag" => tag.name ,
"url" => OrbitHelper.page_for_tag(tag)
} } rescue []
files = announcement.bulletin_files.map{|file| { "file_url" => file.file.url, "file_title" => (file.title.blank? ? File.basename(file.file.path) : file.title rescue '') } } rescue []
links = announcement.bulletin_links.map{|link| { "link_url" => link.url, "link_title" => (link.title.blank? ? link.url : link.title) } } rescue []
update_user = announcement.update_user.member_profile.name rescue ""
desc = announcement.image_description
desc = (desc.nil? || desc == "" ? "announcement image" : desc)
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"}])
{
"tags" => tags,
"bulletin_files" => files,
"bulletin_links" => links,
"data" => {
"title" => announcement.title,
"update_user" => update_user,
"updated_at" => announcement.postdate.strftime('%Y-%m-%d %H:%M'),
"body" =>announcement.text,
"image" => announcement.image.url,
"alt_title" => desc
},
"impressionist" => (announcement.is_preview ? nil : announcement),
"url_to_edit"=>url_to_edit
}
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] rescue ""
desc = (desc.nil? || desc == "" ? "announcement image" : desc)
request = OrbitHelper.request
meta_desc = announcement["subtitle_translations"].present? && announcement["subtitle_translations"][locale] != "" ? announcement["subtitle_translations"][locale] : announcement["text_translations"][locale][0..200]
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, (params["preview"] == "true" ? true : false))
end
end
end