event_ann/app/controllers/event_anns_controller.rb

442 lines
17 KiB
Ruby

class EventAnnsController < ApplicationController
include EventAnnsHelper
def index
BulletinEvent.remove_expired_status
sorted,total_pages = get_sorted_event_ann
eanns = 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 = OrbitHelper.get_site_locale.to_s
files = a.bulletin_event_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_event_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? ? "event_ann image" : desc)
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
{
"department" => author,
"bulletin_event_links" => links,
"bulletin_event_files" => files,
"title" => a.title,
"source-site" => "",
"source-site-title" => "",
"source-site-link" => "",
"subtitle" => a.subtitle,
"speaker" => a.speaker,
"host" => a.host,
"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/event_ann-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
# eanns = eanns.concat(feeds_anns)
# total_pages = event_anns.total_pages
{
"event_anns" => eanns,
"extras" => {
"widget-title" =>t('event_ann.event_ann'),
"title-head" => t('event_ann.table.title'),
"date-head" => t('event_ann.table.date'),
"status-head" => t('event_ann.table.status'),
"author-head" => t('event_ann.table.author'),
"subtitle-head" => t('event_ann.table.sub_title'),
"speaker-head" => t('event_ann.table.speaker'),
"host-head" => t('event_ann.table.host'),
"category-head" => t('event_ann.table.category'),
"link-head" => t('event_ann.table.link'),
"file-head" => t('event_ann.table.file'),
"view-count-head" => t('event_ann.table.view_count'),
"display" => display,
"department-head" => t('event_ann.table.department')
},
"total_pages" => total_pages
}
end
def random_event_ann_widget
pack_data(true)
end
def widget
pack_data()
end
def tag_cloud
ma = ModuleApp.where(:key => "event_ann").first
temp = []
ma.tags.each do |tag|
t1 = tag.taggings.collect{|t| t.taggable_id.to_s}
count = BulletinEvent.where(:id.in => t1).can_display_and_sorted.count
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
font_size = ((percent / 10).round) + 16
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
eanns_cache = EventAnnsCache.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 eanns_cache.count != 1 || is_random
page = Page.where(:module => "event_ann").first rescue nil
BulletinEvent.remove_expired_status
uid = OrbitHelper.params[:uid] rescue ""
sorted_eanns = BulletinEvent.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_eanns = sorted_eanns.limit(widget_data_count)
if eanns_cache.count > 1
eanns_cache.destroy
end
if devide_flag
now_eanns = sorted_eanns.to_a
top_eanns = now_eanns.select{|v| v.is_top}.map{|v| data_to_human_type(v)}
not_top_eanns = now_eanns.select{|v| !v.is_top}.map{|v| data_to_human_type(v)}
EventAnnsCache.create(parent_id: subpart.id.to_s,locale: I18n.locale.to_s,filter_result: {top: top_eanns,not_top: not_top_eanns})
else
eanns = sorted_eanns.map{|v| data_to_human_type(v)}
EventAnnsCache.create(parent_id: subpart.id.to_s,locale: I18n.locale.to_s,filter_result: eanns)
end
else
if devide_flag
eanns = sorted_eanns.sample(widget_data_count)
top_eanns = eanns.select{|v| v.is_top}.map{|v| data_to_human_type(v)}
not_top_eanns = eanns.select{|v| !v.is_top}.map{|v| data_to_human_type(v)}
else
eanns = sorted_eanns.sample(widget_data_count).map{|v| data_to_human_type(v)}
end
end
elsif devide_flag
now_eanns = enns_cache.first.filter_result
top_eanns = now_eanns[:top]
not_top_eanns = now_eanns[:not_top]
else
eanns = eanns_cache.first.filter_result
end
if devide_flag
rest_count = widget_data_count - top_eanns.count
if rest_count <= 0
eanns = top_eanns
else
feeds_eanns = get_feed_event_anns("widget")
top_eanns = top_eanns + feeds_eanns.select{|v| v['is_top']}
top_eanns = top_eanns.sort{|v1,v2| v2["postdate"]<=>v1["postdate"]}
rest_all_eanns = feeds_eanns.select{|v| v['is_top'] != true} + not_top_eanns.take(rest_count)
rest_eanns = rest_all_eanns.sort{|v1,v2| v2["postdate"]<=>v1["postdate"]}.take(rest_count)
eanns = (top_eanns + rest_eanns).take(widget_data_count)
end
end
mp = (eanns[0]["img_src"] rescue "")
mpd = (eanns[0]["img_description"] rescue "")
{
"event_anns" => eanns,
"extras" => {
"more_url"=>OrbitHelper.widget_more_url,
"main_picture" => mp,
"main_picture_description" => mpd,
"title-head" => t('event_ann.table.title'),
"date-head" => t('event_ann.table.date'),
"author-head" => t('event_ann.table.author'),
"status-head" => t('event_ann.table.status'),
"subtitle-head" => t('event_ann.table.sub_title'),
"speaker-head" => t('event_ann.table.speaker'),
"host-head" => t('event_ann.table.host'),
"category-head" => t('event_ann.table.category'),
"link-head" => t('event_ann.table.link'),
"file-head" => t('event_ann.table.file'),
"read_more" => ("/#{I18n.locale.to_s + page.url}" rescue "")
}
}
end
def show_local_event_ann(uid, is_preview)
locale = OrbitHelper.get_site_locale.to_s
if is_preview
event_ann = BulletinEvent.where(:uid => uid).first
else
event_ann = BulletinEvent.can_display_and_sorted.where(:uid => uid).first
end
event_ann = BulletinEvent.where(:uid => uid).first if event_ann.nil?
url_to_edit = OrbitHelper.user_can_edit?(event_ann) ? "/admin/event_anns/#{event_ann.id.to_s}/edit" : ""
access_level = OrbitHelper.user_access_level?
if !event_ann.approved && (access_level != "manager" && access_level != "admin")
if EventAnnSetting.is_pro?
if !(access_level == "sub_manager" && EventAnnSetting.first.approvers.include?(OrbitHelper.current_user.id.to_s))
return {}
end
elsif access_level != "sub_manager"
return {}
end
end
return {} if event_ann.category.disable
tags = event_ann.tags.map{|tag| {
"tag" => tag.name ,
"url" => OrbitHelper.page_for_tag(tag)
} } rescue []
files = event_ann.bulletin_event_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)
files.each do |file|
if file["file_url"] =="" || file["file_url"] == nil
files.delete(file)
end
end
links = event_ann.bulletin_event_links.map{|link| { "link_url" => link.url, "link_title" => (link.title.blank? ? link.url : link.title) } } rescue []
update_user = event_ann.update_user.member_profile.name rescue ""
desc = event_ann.image_description
desc = (desc.nil? || desc == "" ? "event_ann image" : desc)
request = OrbitHelper.request
meta_desc = event_ann.subtitle.nil? || event_ann.subtitle == "" ? event_ann.text[0..200] : event_ann.subtitle
OrbitHelper.render_meta_tags([{"property" => "og:title", "content" => event_ann.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}#{event_ann.image.url}"},{"property" => "og:type", "content" => "Article"}])
subtitle_ann = nil
img_src = nil
img_description = nil
subtitle_ann = event_ann.subtitle if event_ann.display_subtitle?
img_src = (event_ann.image.thumb.url || "/assets/event_ann-default.jpg") if event_ann.display_img?
img_description = event_ann.image_description if (event_ann.image_description.present?) && (event_ann.display_img?)
{
"tags" => tags,
"bulletin_event_files" => files,
"bulletin_event_links" => links,
"data" => {
"title" => event_ann.title,
"subtitle_ann" => subtitle_ann,
"speaker" => event_ann.speaker,
"host" => event_ann.host,
"update_user" => update_user,
"updated_at" => event_ann.postdate.strftime('%Y-%m-%d %H:%M'),
"body" =>event_ann.text,
"notes" => event_ann.notes,
"image" => event_ann.image.url,
"img_src" => img_src,
"img_description" => img_description,
"alt_title" => desc
},
"impressionist" => (event_ann.is_preview ? nil : event_ann),
"url_to_edit"=>url_to_edit
}
end
def show_feed_event_ann(uid)
event_ann = OrbitHelper.get_from_feed(uid)
locale = OrbitHelper.get_site_locale.to_s
url_to_edit = "#"
return {} if event_ann.blank?
tags = []
event_ann["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 = event_ann["bulletin_event_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 []
files.each do |file|
if file["file_url"] =="" || file["file_url"] == nil
files.delete(file)
end
end
links = event_ann["bulletin_event_links"].map{|link| { "link_url" => link["url"], "link_title" => (link["title_translations"][locale] == "" ? link["url"] : link["title_translations"][locale]) } } rescue []
update_user = event_ann["author"]
desc = event_ann["image_description_translations"][locale] rescue ""
desc = (desc.nil? || desc == "" ? "event_ann image" : desc)
request = OrbitHelper.request
if event_ann["subtitle_translations"].present?
meta_desc = event_ann["subtitle_translations"][locale] != "" ? event_ann["subtitle_translations"][locale] : event_ann["text_translations"][locale][0..200] rescue ""
else
meta_desc = ""
end
OrbitHelper.render_meta_tags([{"property" => "og:title", "content" => event_ann["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" => event_ann["image"]["original"]},{"property" => "og:type", "content" => "Article"}])
datetime = DateTime.parse(event_ann["postdate"])
{
"tags" => tags,
"bulletin_event_files" => files,
"bulletin_event_links" => links,
"data" => {
"title" => event_ann["title_translations"][locale],
"update_user" => update_user,
"updated_at" => datetime.strftime('%Y-%m-%d %H:%M'),
"body" => event_ann["text_translations"][locale],
"image" => event_ann["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_event_ann(uid)
else
show_local_event_ann(uid, (params["preview"] == "true" ? true : false))
end
end
def show_widget
@type = "show_widget"
@show_page = params[:show_page]
if params[:tags].nil?
@tags = ['all']
else
@tags = params[:tags]
end
if params[:categories].nil?
@categories = ['all']
else
@categories = params[:categories]
end
OrbitHelper.set_current_widget_module("event_ann")
OrbitHelper.set_params(params,current_user)
BulletinEvent.remove_expired_status
OrbitHelper.set_page_number(params[:page_no].to_i)
OrbitHelper.set_page_data_count((params[:data_count].blank? ? 10 : params[:data_count].to_i))
sorted,total_pages = get_sorted_event_ann
eanns = 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_event_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_event_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? ? "event_ann 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_event_links" => links,
"bulletin_event_files" => files,
"title" => a.title,
"source-site" => "",
"source-site-title" => "",
"source-site-link" => "",
"subtitle" => a.subtitle,
"speaker" => a.speaker,
"host" => a.host,
"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/event_ann-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
# eanns = eanns.concat(feeds_eanns)
# total_pages = event_anns.total_pages
@data = {
"event_anns" => eanns,
"extras" => {
"widget-title" =>t('event_ann.event_ann'),
"title-head" => t('event_ann.table.title'),
"date-head" => t('event_ann.table.date'),
"status-head" => t('event_ann.table.status'),
"author-head" => t('event_ann.table.author'),
"subtitle-head" => t('event_ann.table.sub_title'),
"speaker-head" => t('event_ann.table.speaker'),
"host-head" => t('event_ann.table.host'),
"category-head" => t('event_ann.table.category'),
"link-head" => t('event_ann.table.link'),
"file-head" => t('event_ann.table.file'),
"view-count-head" => t('event_ann.table.view_count'),
"display" => display,
"department-head" => t('event_ann.table.department')
},
"total_pages" => total_pages
}
render :layout => false
end
end