announcement-test/app/controllers/announcements_controller.rb

422 lines
17 KiB
Ruby
Raw Normal View History

2014-04-01 07:12:43 +00:00
class AnnouncementsController < ApplicationController
include AnnouncementsHelper
2014-04-01 07:12:43 +00:00
def index
Bulletin.remove_expired_status
sorted,total_pages = get_sorted_annc
anns = sorted.collect do |a|
2020-03-17 01:26:34 +00:00
if a["source-site"].blank?
statuses = a.statuses_with_classname.collect do |status|
{
"status" => status["name"],
"status-class" => "status-#{status['classname']}"
}
end
locale = OrbitHelper.get_site_locale.to_s
2016-05-09 10:11:18 +00:00
files = a.bulletin_files.map{|file| { "file_url" => file.file.url, "file_title" => (file.title.blank? ? File.basename(file.file.path) : file.title rescue '') } if file.enabled_for?(locale) } rescue []
files.delete(nil)
2019-10-29 06:45:45 +00:00
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
2020-03-17 01:26:34 +00:00
desc = (desc.blank? ? "announcement image" : desc)
2017-06-20 07:04:05 +00:00
link_to_show = a.is_external_link ? a.external_link : OrbitHelper.url_to_show(a.to_param)
target = a.is_external_link ? "_blank" : "_self"
doc = Nokogiri::HTML(a.title)
title = doc.text.empty? ? 'no content' : doc.text
{
2020-05-19 04:31:57 +00:00
"department" => author,
"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" => link_to_show+"\" title=\"#{title}\"",
2017-06-20 07:04:05 +00:00
"target" => target,
"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
a
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 sorted.count == 0
2015-02-25 02:58:51 +00:00
display = "hide"
end
# anns = anns.concat(feeds_anns)
# total_pages = announcements.total_pages
2019-10-29 06:45:45 +00:00
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'),
2019-10-29 06:45:45 +00:00
"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'),
2020-05-19 04:31:57 +00:00
"display" => display,
"department-head" => t('announcement.table.department')
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
2015-10-29 08:42:42 +00:00
def random_announcement_widget
pack_data(true)
2015-10-29 08:42:42 +00:00
end
2014-04-01 07:12:43 +00:00
def widget
pack_data()
2015-10-29 08:42:42 +00:00
end
2017-06-29 08:03:39 +00:00
def tag_cloud
ma = ModuleApp.where(:key => "announcement").first
temp = []
ma.tags.each do |tag|
t1 = tag.taggings.collect{|t| t.taggable_id.to_s}
count = Bulletin.where(:id.in => t1).can_display_and_sorted.count
2017-06-29 08:03:39 +00:00
temp << {
"tag-name" => tag.name,
"count" => count,
"tag-url" => OrbitHelper.widget_more_url + "?tags[]=" + tag.id.to_s
}
end
max = temp.max_by{|t| t["count"]}["count"]
tags = []
temp.each do |tag|
if tag["count"] > 0
percent = (tag["count"] * 100) / max
2017-06-29 08:43:57 +00:00
font_size = ((percent / 10).round) + 16
2017-06-29 08:03:39 +00:00
tag["font-size"] = font_size
tags << tag
end
end
{
"tags" => tags,
"extras" => {}
}
end
def pack_data(is_random=false)
tags = OrbitHelper.widget_tags || []
cats = OrbitHelper.widget_categories || []
subpart = OrbitHelper.get_current_widget
2020-04-07 02:24:41 +00:00
anns_cache = AnnsCache.where(parent_id: subpart.id.to_s,locale: I18n.locale.to_s)
widget_data_count = OrbitHelper.widget_data_count
set_image_version_for_widget()
devide_flag = (!(defined? SiteFeed).nil?)
if anns_cache.count != 1 || is_random
page = Page.where(:module => "announcement").first rescue nil
Bulletin.remove_expired_status
uid = OrbitHelper.params[:uid] rescue ""
2020-03-17 02:32:51 +00:00
sorted_anns = Bulletin.where(:title.nin => ["",nil],:is_preview.in=>[false,nil], :uid.ne => uid)
.can_display_and_sorted.is_approved
.filter_by_widget_categories(cats,false).filter_by_tags(tags)
if !is_random
sorted_anns = sorted_anns.limit(widget_data_count)
if anns_cache.count > 1
anns_cache.destroy
end
if devide_flag
now_anns = sorted_anns.to_a
top_anns = now_anns.select{|v| v.is_top}.map{|v| data_to_human_type(v)}
not_top_anns = now_anns.select{|v| !v.is_top}.map{|v| data_to_human_type(v)}
AnnsCache.create(parent_id: subpart.id.to_s,locale: I18n.locale.to_s,filter_result: {top: top_anns,not_top: not_top_anns})
else
anns = sorted_anns.map{|v| data_to_human_type(v)}
AnnsCache.create(parent_id: subpart.id.to_s,locale: I18n.locale.to_s,filter_result: anns)
end
else
anns = sorted_anns.sample(widget_data_count).map{|v| data_to_human_type(v)}
end
elsif devide_flag
now_anns = anns_cache.first.filter_result
top_anns = now_anns[:top]
not_top_anns = now_anns[:not_top]
2019-10-29 07:51:02 +00:00
else
anns = anns_cache.first.filter_result
end
if devide_flag
rest_count = widget_data_count - top_anns.count
if rest_count <= 0
anns = top_anns
2019-10-29 06:45:45 +00:00
else
feeds_anns = get_feed_announcements("widget")
2020-04-05 14:38:26 +00:00
top_anns = top_anns + feeds_anns.select{|v| v['is_top']}
top_anns = top_anns.sort{|v1,v2| v2["postdate"]<=>v1["postdate"]}
rest_all_anns = feeds_anns.select{|v| v['is_top'] != true} + not_top_anns.take(rest_count)
2020-03-04 10:39:21 +00:00
rest_anns = rest_all_anns.sort{|v1,v2| v2["postdate"]<=>v1["postdate"]}.take(rest_count)
2020-04-05 14:38:26 +00:00
anns = (top_anns + rest_anns).take(widget_data_count)
2019-10-29 06:45:45 +00:00
end
2014-04-01 07:12:43 +00:00
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)
2016-05-09 10:11:18 +00:00
locale = OrbitHelper.get_site_locale.to_s
2015-10-19 08:59:26 +00:00
if is_preview
2016-05-02 09:36:21 +00:00
announcement = Bulletin.where(:uid => uid).first
2015-10-19 08:59:26 +00:00
else
announcement = Bulletin.can_display_and_sorted.where(:uid => uid).first
2015-10-19 08:59:26 +00:00
end
2020-05-17 13:24:56 +00:00
announcement = Bulletin.where(:uid => uid).first if announcement.nil?
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")
2017-08-24 12:31:51 +00:00
if AnnouncementSetting.is_pro?
if !(access_level == "sub_manager" && AnnouncementSetting.first.approvers.include?(OrbitHelper.current_user.id.to_s))
return {}
end
elsif access_level != "sub_manager"
return {}
end
2014-09-22 08:32:30 +00:00
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-05-09 10:11:18 +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 '') } if file.enabled_for?(locale) } rescue []
files.delete(nil)
2019-10-29 06:45:45 +00:00
files.each do |file|
if file["file_url"] =="" || file["file_url"] == nil
files.delete(file)
end
end
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)
2019-10-29 06:45:45 +00:00
2015-07-03 11:53:24 +00:00
request = OrbitHelper.request
meta_desc = announcement.subtitle.nil? || announcement.subtitle == "" ? announcement.text[0..200] : announcement.subtitle
2019-01-16 09:37:32 +00:00
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.split("?").first},{"property" => "og:description", "content" => meta_desc},{"property" => "og:image", "content" => "#{request.base_url}#{announcement.image.url}"},{"property" => "og:type", "content" => "Article"}])
2015-07-03 11:53:24 +00:00
subtitle_ann = nil
img_src = nil
img_description = nil
subtitle_ann = announcement.subtitle if announcement.display_subtitle?
img_src = (announcement.image.thumb.url || "/assets/announcement-default.jpg") if announcement.display_img?
img_description = announcement.image_description if (announcement.image_description.present?) && (announcement.display_img?)
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,
"subtitle_ann" => subtitle_ann,
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,
"img_src" => img_src,
2020-01-13 09:24:21 +00:00
"img_description" => img_description,
2015-06-02 09:48:18 +00:00
"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 = []
2019-10-29 07:51:02 +00:00
announcement["tags"].each{|tag|
2015-07-08 10:11:25 +00:00
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 << {
2019-10-29 06:45:45 +00:00
"tag" => tag["name_translations"][locale],
"url" => (t.nil? ? "#" : OrbitHelper.page_for_tag(t))
}
}
2015-07-08 10:11:25 +00:00
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
files.each do |file|
2019-10-29 06:45:45 +00:00
if file["file_url"] =="" || file["file_url"] == nil
files.delete(file)
end
end
links = announcement["bulletin_links"].map{|link| { "link_url" => link["url"], "link_title" => (link["title_translations"][locale] == "" ? link["url"] : link["title_translations"][locale]) } } rescue []
2015-07-08 10:11:25 +00:00
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)
2019-10-29 06:45:45 +00:00
2015-07-08 10:11:25 +00:00
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
2019-01-16 09:37:32 +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.split("?").first},{"property" => "og:description", "content" => meta_desc},{"property" => "og:image", "content" => announcement["image"]["original"]},{"property" => "og:type", "content" => "Article"}])
2015-07-08 10:11:25 +00:00
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
2020-05-19 04:31:57 +00:00
def show_widget
@type = "show_widget"
@show_page = params[:show_page]
if params[:tags].nil?
@tags = ModuleApp.where(:key => "announcement").first.tags.map{|t| t.id.to_s}
else
@tags = params[:tags]
end
if params[:categories].nil?
@categories = ModuleApp.where(:key => "announcement").first.categories.map{|c| c.id.to_s}
else
@categories = params[:categories]
end
OrbitHelper.set_current_widget_module("announcement")
OrbitHelper.set_params(params,current_user)
Bulletin.remove_expired_status
sorted,total_pages = get_sorted_annc
OrbitHelper.set_page_number(params[:page_no].to_i)
OrbitHelper.set_page_data_count((params[:data_count].blank? ? 10 : params[:data_count].to_i))
anns = sorted.collect do |a|
if a["source-site"].blank?
statuses = a.statuses_with_classname.collect do |status|
{
"status" => status["name"],
"status-class" => "status-#{status['classname']}"
}
end
locale = I18n.locale.to_s
files = a.bulletin_files.map{|file| { "file_url" => file.file.url, "file_title" => (file.title.blank? ? File.basename(file.file.path) : file.title rescue '') } if file.enabled_for?(locale) } rescue []
files.delete(nil)
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.blank? ? "announcement image" : desc)
link_to_show = (a.is_external_link ? a.external_link : OrbitHelper.url_to_show(a.to_param)) rescue ""
target = a.is_external_link ? "_blank" : "_self"
doc = Nokogiri::HTML(a.title)
title = doc.text.empty? ? 'no content' : doc.text
{
"department" => author,
"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" => link_to_show+"\" title=\"#{title}\"",
"target" => target,
"img_src" => a.image.thumb.url || "/assets/announcement-default.jpg",
"img_description" => desc,
"more" => t(:more_plus),
"view_count" => a.view_count
}
else
a
end
end
#If no data , hide title&table
if sorted.count == 0
display = "hide"
end
# anns = anns.concat(feeds_anns)
# total_pages = announcements.total_pages
2015-07-08 10:11:25 +00:00
2020-05-19 04:31:57 +00:00
@data = {
"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,
"department-head" => t('announcement.table.department')
},
"total_pages" => total_pages
}
render :layout => false
end
2014-04-01 07:12:43 +00:00
end