class EventNewsController < ApplicationController
include EventNewsHelper
def index
EventNews.remove_expired_status
sorted,total_pages = get_sorted_event_news
event_news = 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.event_news_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.event_news_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? ? "EventNews 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,
"event_news_links" => links,
"event_news_files" => files,
"title" => a.title,
"speaker" => a.speaker,
"host" => a.host,
"notes" => a.notes,
"source-site" => "",
"source-site-title" => "",
"source-site-link" => "",
"subtitle" => a.subtitle,
"statuses" => statuses,
"category" => (a.category.title rescue ""),
"postdate" => a.event_date,
"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_news-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
# event_news = event_news.concat(feeds_event_news)
# total_pages = event_news.total_pages
{
"event_news" => event_news,
"extras" => {
"widget-title" =>t('event_news.event_news'),
"title-head" => t('event_news.table.title'),
"speaker-head" => t('event_news.speaker'),
"host-head" => t('event_news.host'),
"notes-head" => t('event_news.notes'),
"event_date-head" => t('date_'),
"date-head" => t('event_news.table.date'),
"status-head" => t('event_news.table.status'),
"author-head" => t('event_news.table.author'),
"subtitle-head" => t('event_news.table.sub_title'),
"category-head" => t('event_news.table.category'),
"link-head" => t('event_news.table.link'),
"file-head" => t('event_news.table.file'),
"view-count-head" => t('event_news.table.view_count'),
"display" => display,
"department-head" => t('event_news.table.department')
},
"total_pages" => total_pages
}
end
def random_event_news_widget
pack_data(true)
end
def widget
pack_data()
end
def tag_cloud
ma = ModuleApp.where(:key => "event_news").first
temp = []
ma.tags.each do |tag|
t1 = tag.taggings.collect{|t| t.taggable_id.to_s}
count = EventNews.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 get_tabs_option
subpart = OrbitHelper.get_current_widget
tab_options = ["not_enable_tabs","enable_tabs_with_categories_include_all","enable_tabs_with_categories"]
read_more_options = ['default','upper_left','lower_left','upper_right','lower_right']
all_setting_options = ['the_same_as_data_count','display_all_in_other_tabs']
@tab_option = 0
@read_more_option = 0
@all_setting_option = 0
if subpart.methods.include? 'select_options'.to_sym
ModuleApp.all.select{|tmp| tmp.key.to_s=='event_news'}.each do |modile_app|
@show_options = modile_app.show_options rescue nil
end
subpart.select_options.each do |select_option|
if !(@show_options.nil?) && select_option.field_name == @show_options.keys[1].to_s
value = YAML.load(select_option.value)
tmp = value[:en]
I18n.with_locale(:en) do
tab_options.each_with_index do |option,i|
if tmp == t("event_news.#{option}")
@tab_option = i
break
end
end
end
end
if !(@show_options.nil?) && select_option.field_name == @show_options.keys[2].to_s
value = YAML.load(select_option.value)
tmp = value[:en]
I18n.with_locale(:en) do
read_more_options.each_with_index do |option,i|
if tmp == t("announcement.#{option}")
@read_more_option = i
break
end
end
end
end
if !(@show_options.nil?) && select_option.field_name == @show_options.keys[3].to_s
value = YAML.load(select_option.value)
tmp = value[:en]
I18n.with_locale(:en) do
all_setting_options.each_with_index do |option,i|
if tmp == t("announcement.#{option}")
@all_setting_option = i
break
end
end
end
end
end
end
end
def pack_data(is_random=false)
cats = OrbitHelper.widget_categories || []
tags = OrbitHelper.widget_tags || []
subpart = OrbitHelper.get_current_widget
get_tabs_option
event_news = []
use_tag = false
if @tab_option == 0
event_news = get_event_news_for_pack_data(cats,tags,nil,is_random)
else
if cats.count != 1 || tags == ["all"] || (cats[0] != "all" && tags.count <= 1)
cats.each do |cat|
event_news = event_news + get_event_news_for_pack_data([cat],tags,'')
end
else
tags.each do |tag|
event_news = event_news + get_event_news_for_pack_data(cats,[tag],tag)
end
use_tag = true
end
end
mp = (event_news[0]["img_src"] rescue "")
mpd = (event_news[0]["img_description"] rescue "")
if @tab_option == 1
if use_tag
tags = ["all"] + tags
else
cats = ["all"] + cats
end
event_news = event_news.sort{|v1,v2| v2["postdate"]<=>v1["postdate"]}
end
cats = cats.uniq
tags = tags.uniq
tags_translations = tags.map{|tag_id|
if tag_id == "all"
t = I18n.t(:all)
else
t = Tag.find(tag_id).name rescue ""
end
[tag_id,t]
}.to_h
cats_translations = cats.map{|cat_id|
if cat_id == "all"
t = I18n.t(:all)
else
t = Category.find(cat_id).title rescue ""
end
[cat_id,t]
}.to_h
cats_relations = cats_translations.map{|cat_id,t|
if cat_id == "all"
t = "all"
end
[cat_id,t]
}.to_h
page = Page.where(:page_id=> subpart.read_more_page_id).first rescue nil
page = Page.where(:module => "event_news").first rescue nil if page.nil?
all_cats = cats.dup
all_cats.delete "all"
if all_cats.count == 0
all_cats = ["all"]
end
all_tags = tags.dup
all_tags.delete "all"
if all_tags.count == 0
all_tags = ["all"]
end
max_all_count = [OrbitHelper.widget_data_count,event_news.count].min
if @tab_option != 0
OrbitHelper.set_widget_title(OrbitHelper.widget_title +
"
" +
"" +
(use_tag ? tags.map.with_index{|tag,i|
read_more_url = "/#{I18n.locale.to_s + page.url}" rescue ""
read_more_url = read_more_url + "?" + {"category"=>all_cats,"tags"=>(tag == 'all' ? all_tags : [tag])}.to_param if read_more_url != ""
read_more_text = I18n.t("event_news.more")
if tag != "all"
begin
read_more_text = I18n.t("event_news.more_") + tags_translations[tag]
rescue
nil
end
end
"- #{tags_translations[tag]}
"
}.join("") : cats.map.with_index{|cat,i|
read_more_url = "/#{I18n.locale.to_s + page.url}" rescue ""
read_more_url = read_more_url + "?" + {"category"=>(cat == 'all' ? all_cats : cat)}.to_param if read_more_url != ""
read_more_text = I18n.t("event_news.more")
if cat != "all"
begin
read_more_text = I18n.t("event_news.more_") + cats_translations[cat]
rescue
nil
end
end
"- #{cats_translations[cat]}
"
}.join("")) +
"
"
)
filter_attr = (use_tag ? 'data-tags' : 'data-category')
extra_html = '
'
else
read_more_text = I18n.t("event_news.more")
if cats.count == 1 && cats[0] != "all"
begin
read_more_text = I18n.t("event_news.more_") + ((all_tags.count == 1 && all_tags[0] != 'all') ? tags_translations[tags[0]] : cats_translations[cats[0]])
rescue
nil
end
end
extra_html = "
"
end
if (@read_more_option != 0 rescue false)
extra_html += "
"
end
extra_after_html = ""
if @all_setting_option == 0 && @tab_option == 1
extra_after_html = "
"
end
if @tab_option == 0
read_more_url = "/#{I18n.locale.to_s + page.url}" rescue ""
read_more_url = read_more_url + "?" + {"category"=>all_cats,"tags"=>all_tags}.to_param if read_more_url != ""
extra_after_html += "
"
end
{
"event_news" => event_news,
"extras" => {
"more_url"=>OrbitHelper.widget_more_url,
"main_picture" => mp,
"main_picture_description" => mpd,
"title-head" => t('event_news.table.title'),
"date-head" => t('event_news.table.date'),
"author-head" => t('event_news.table.author'),
"status-head" => t('event_news.table.status'),
"subtitle-head" => t('event_news.table.sub_title'),
"category-head" => t('event_news.table.category'),
"link-head" => t('event_news.table.link'),
"file-head" => t('event_news.table.file'),
"read_more" => read_more_url,
"read_more_text" => "read more",
"extra_brefore_html" => extra_html,
"extra_after_html" => extra_after_html
}
}
end
def get_event_news_for_pack_data(cats,tags,set_tags=nil,is_random = false)
if tags.blank?
tags = ["all"]
end
subpart = OrbitHelper.get_current_widget
widget_data_count = OrbitHelper.widget_data_count
event_news_cache = EventNewsCache.where(parent_id: subpart.id.to_s + cats.to_s + tags.to_s + widget_data_count.to_s,locale: I18n.locale.to_s)
set_image_version_for_widget()
devide_flag = (!(defined? SiteFeed).nil?)
if event_news_cache.count != 1 || is_random
EventNews.remove_expired_status
uid = OrbitHelper.params[:uid] rescue ""
sorted_event_news = EventNews.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_event_news = sorted_event_news.limit(widget_data_count)
if event_news_cache.count > 1
event_news_cache.destroy
end
if devide_flag
now_event_news = sorted_event_news.to_a
top_event_news = now_event_news.select{|v| v.is_top}.map{|v| data_to_human_type(v,set_tags)}
not_top_event_news = now_event_news.select{|v| !v.is_top}.map{|v| data_to_human_type(v,set_tags)}
EventNewsCache.create(parent_id: subpart.id.to_s + cats.to_s + tags.to_s + widget_data_count.to_s,locale: I18n.locale.to_s,filter_result: {top: top_event_news,not_top: not_top_event_news})
else
event_news = sorted_event_news.map{|v| data_to_human_type(v,set_tags)}
EventNewsCache.create(parent_id: subpart.id.to_s + cats.to_s + tags.to_s + widget_data_count.to_s,locale: I18n.locale.to_s,filter_result: event_news)
end
else
if devide_flag
event_news = sorted_event_news.sample(widget_data_count)
top_event_news = event_news.select{|v| v.is_top}.map{|v| data_to_human_type(v,set_tags)}
not_top_event_news = event_news.select{|v| !v.is_top}.map{|v| data_to_human_type(v,set_tags)}
else
event_news = sorted_event_news.sample(widget_data_count).map{|v| data_to_human_type(v,set_tags)}
end
end
elsif devide_flag
now_event_news = event_news_cache.first.filter_result
top_event_news = now_event_news[:top]
not_top_event_news = now_event_news[:not_top]
else
event_news = event_news_cache.first.filter_result
end
if devide_flag
rest_count = widget_data_count - top_event_news.count
if rest_count <= 0
event_news = top_event_news
else
feeds_event_news = get_feed_event_news("widget")
top_event_news = top_event_news + feeds_event_news.select{|v| v['is_top']}
top_event_news = top_event_news.sort{|v1,v2| v2["postdate"]<=>v1["postdate"]}
rest_all_event_news = feeds_event_news.select{|v| v['is_top'] != true} + not_top_event_news.take(rest_count)
rest_event_news = rest_all_event_news.sort{|v1,v2| v2["postdate"]<=>v1["postdate"]}.take(rest_count)
event_news = (top_event_news + rest_event_news).take(widget_data_count)
end
end
event_news.each{|a| a["postdate"] = a["postdate"].in_time_zone(Time.zone.utc_offset / 3600).strftime('%Y-%m-%d %H:%M') rescue nil }
event_news
end
def show_local_event_news(uid, is_preview)
locale = OrbitHelper.get_site_locale.to_s
if is_preview
event_news = EventNews.where(:uid => uid).first
else
event_news = EventNews.can_display_and_sorted.where(:uid => uid).first
end
event_news = EventNews.where(:uid => uid).first if event_news.nil?
url_to_edit = OrbitHelper.user_can_edit?(event_news) ? "/admin/event_news/#{event_news.id.to_s}/edit" : ""
access_level = OrbitHelper.user_access_level?
if !event_news.approved && (access_level != "manager" && access_level != "admin")
if EventNewsSetting.is_pro?
if !(access_level == "sub_manager" && EventNewsSetting.first.approvers.include?(OrbitHelper.current_user.id.to_s))
return {}
end
elsif access_level != "sub_manager"
return {}
end
end
return {} if (event_news.category.disable rescue false)
tags = event_news.tags.map{|tag| {
"tag" => tag.name ,
"url" => OrbitHelper.page_for_tag(tag)
} } rescue []
files = event_news.event_news_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_news.event_news_links.map{|link| { "link_url" => link.url, "link_title" => (link.title.blank? ? link.url : link.title) } } rescue []
update_user = event_news.update_user.member_profile.name rescue ""
desc = event_news.image_description
desc = (desc.nil? || desc == "" ? "EventNews image" : desc)
request = OrbitHelper.request
meta_desc = event_news.subtitle.nil? || event_news.subtitle == "" ? event_news.text[0..200] : event_news.subtitle
OrbitHelper.render_meta_tags([{"property" => "og:title", "content" => event_news.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_news.image.url}"},{"property" => "og:type", "content" => "Article"}])
subtitle_ann = nil
img_src = nil
img_description = nil
subtitle_ann = event_news.subtitle if event_news.display_subtitle?
img_src = (event_news.image.thumb.url || "/assets/event_news-default.jpg") if event_news.display_img?
img_description = event_news.image_description if (event_news.image_description.present?) && (event_news.display_img?)
{
"tags" => tags,
"event_news_files" => files,
"event_news_links" => links,
"extra" => {
"speaker-head" => t('event_news.speaker'),
"host-head" => t('event_news.host'),
"notes-head" => t('event_news.notes'),
"speaker" => event_news.speaker,
"host" => event_news.host,
"notes" => event_news.notes,
"title" => event_news.title,
"subtitle_ann" => subtitle_ann,
"update_user" => update_user,
"updated_at" => (event_news.event_date.strftime('%Y-%m-%d %H:%M') rescue ""),
"body" =>event_news.text,
"image" => event_news.image.url,
"img_src" => img_src,
"img_description" => img_description,
"hide_class" => event_news.display_img? ? event_news.image_display_class : ' hide',
"alt_title" => desc
},
"impressionist" => (event_news.is_preview ? nil : event_news),
"url_to_edit"=>url_to_edit
}
end
def show_feed_event_news(uid)
event_news = OrbitHelper.get_from_feed(uid)
locale = OrbitHelper.get_site_locale.to_s
url_to_edit = "#"
return {} if event_news.blank?
tags = []
event_news["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_news["event_news_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_news["event_news_links"].map{|link| { "link_url" => link["url"], "link_title" => (link["title_translations"][locale] == "" ? link["url"] : link["title_translations"][locale]) } } rescue []
update_user = event_news["author"]
desc = event_news["image_description_translations"][locale] rescue ""
desc = (desc.nil? || desc == "" ? "EventNews image" : desc)
request = OrbitHelper.request
if event_news["subtitle_translations"].present?
meta_desc = event_news["subtitle_translations"][locale] != "" ? event_news["subtitle_translations"][locale] : event_news["text_translations"][locale][0..200] rescue ""
else
meta_desc = ""
end
OrbitHelper.render_meta_tags([{"property" => "og:title", "content" => event_news["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_news["image"]["original"]},{"property" => "og:type", "content" => "Article"}])
datetime = DateTime.parse(event_news["event_date"].to_s) rescue ""
{
"tags" => tags,
"event_news_files" => files,
"event_news_links" => links,
"extra" => {
"speaker-head" => t('event_news.speaker'),
"host-head" => t('event_news.host'),
"notes-head" => t('event_news.notes'),
"event_date-head" => t('date_'),
"speaker" => (event_news["speaker_translations"][locale].to_s rescue ""),
"host" => (event_news["host_translations"][locale].to_s rescue ""),
"notes" => (event_news["notes_translations"][locale].to_s rescue ""),
"title" => event_news["title_translations"][locale],
"update_user" => update_user,
"updated_at" => (datetime.strftime('%Y-%m-%d %H:%M') rescue ""),
"body" => event_news["text_translations"][locale],
"image" => event_news["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_news(uid)
else
show_local_event_news(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_site_locale(I18n.locale)
OrbitHelper.set_current_widget_module("event_news")
OrbitHelper.set_params(params,current_user)
EventNews.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_news
event_news = 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.event_news_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.event_news_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? ? "EventNews 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,
"event_news_links" => links,
"event_news_files" => files,
"title" => a.title,
"speaker" => a.speaker,
"host" => a.host,
"notes" => a.notes,
"source-site" => "",
"source-site-title" => "",
"source-site-link" => "",
"subtitle" => a.subtitle,
"statuses" => statuses,
"category" => (a.category.title rescue ""),
"postdate" => a.event_date,
"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_news-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
# event_news = event_news.concat(feeds_event_news)
# total_pages = event_news.total_pages
@data = {
"event_news" => event_news,
"extras" => {
"widget-title" =>t('event_news.event_news'),
"title-head" => t('event_news.table.title'),
"speaker-head" => t('event_news.speaker'),
"host-head" => t('event_news.host'),
"notes-head" => t('event_news.notes'),
"event_date-head" => t('date_'),
"date-head" => t('event_news.table.date'),
"status-head" => t('event_news.table.status'),
"author-head" => t('event_news.table.author'),
"subtitle-head" => t('event_news.table.sub_title'),
"category-head" => t('event_news.table.category'),
"link-head" => t('event_news.table.link'),
"file-head" => t('event_news.table.file'),
"view-count-head" => t('event_news.table.view_count'),
"display" => display,
"department-head" => t('event_news.table.department')
},
"total_pages" => total_pages
}
render :layout => false
end
end