fix some error

This commit is contained in:
chiu 2021-09-16 17:16:20 +08:00
parent c116425287
commit 135d4e9e4d
11 changed files with 477 additions and 145 deletions

View File

@ -12,8 +12,7 @@ class EventNewsController < ApplicationController
}
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)
files = a.event_news_files.to_fronted(locale)
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
@ -126,61 +125,10 @@ class EventNewsController < ApplicationController
"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 || []
tags = ['all'] if tags.blank?
subpart = OrbitHelper.get_current_widget
get_tabs_option
event_news = []
@ -190,11 +138,11 @@ class EventNewsController < ApplicationController
else
if cats.count != 1 || tags == ["all"]
cats.each do |cat|
event_news = event_news + get_event_news_for_pack_data([cat],tags,'')
event_news = event_news + get_event_news_for_pack_data([cat],tags,'',is_random)
end
else
tags.each do |tag|
event_news = event_news + get_event_news_for_pack_data(cats,[tag],tag)
event_news = event_news + get_event_news_for_pack_data(cats,[tag],tag,is_random)
end
use_tag = true
end
@ -207,7 +155,7 @@ class EventNewsController < ApplicationController
else
cats = ["all"] + cats
end
event_news = event_news.sort{|v1,v2| v2["postdate"]<=>v1["postdate"]}
event_news = event_news.sort_by { |a| tmp=a["event_date"].blank?;[tmp ? 0 : 1, tmp ? nil : a["event_date"].to_time] }.reverse
end
cats = cats.uniq
tags = tags.uniq
@ -233,8 +181,16 @@ class EventNewsController < ApplicationController
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?
home_page = Page.first
page = nil
locale = I18n.locale.to_s
if home_page.respond_to?(:find_page)
page = home_page.find_page(:page_id=> subpart.read_more_page_id,:enabled_for=>locale).first rescue nil
page = home_page.find_page(:module=>"event_news",:enabled_for=>locale).first rescue nil if page.nil?
else
page = Page.where(:page_id=> subpart.read_more_page_id,:enabled_for=>locale).first rescue nil
page = Page.where(:module=>"event_news",:enabled_for=>locale).first rescue nil if page.nil?
end
all_cats = cats.dup
all_cats.delete "all"
if all_cats.count == 0
@ -251,7 +207,7 @@ class EventNewsController < ApplicationController
"<div style=\"clear: both;\"></div>" +
"<ul class=\"nav_tabs_filter\">" +
(use_tag ? tags.map.with_index{|tag,i|
read_more_url = "/#{I18n.locale.to_s + page.url}" rescue ""
read_more_url = "/#{locale + 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"
@ -263,7 +219,7 @@ class EventNewsController < ApplicationController
end
"<li class=\"filter_tab#{i == 0 ? ' active' : ''}\" #{(tag == 'all' && @all_setting_option == 0) ? "data-count_limit=\"#{max_all_count}\"" : ''} data-read_more_text=\"#{read_more_text}\" data-read_more=\"#{read_more_url}\" data-tags=\"#{tag}\">#{tags_translations[tag]}</li>"
}.join("") : cats.map.with_index{|cat,i|
read_more_url = "/#{I18n.locale.to_s + page.url}" rescue ""
read_more_url = "/#{locale + 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"
@ -383,6 +339,10 @@ class EventNewsController < ApplicationController
div_clearfix = read_more_block;
}
first_element.before(div_clearfix);
var annc_title = $(\"[data-subpart-id=\\\"#{subpart.id}\\\"] .w-annc__widget-title\").eq(0);
if(annc_title.length != 0){
read_more_block.appendTo(\"[data-subpart-id=\\\"#{subpart.id}\\\"] .w-annc__widget-title:eq(0)\");
}
}else{
var div_clearfix = $('<div class=\"clearfix\"></div>');
read_more_block.appendTo(div_clearfix);
@ -402,7 +362,7 @@ class EventNewsController < ApplicationController
"
end
if @tab_option == 0
read_more_url = "/#{I18n.locale.to_s + page.url}" rescue ""
read_more_url = "/#{locale + page.url}" rescue ""
read_more_url = read_more_url + "?" + {"category"=>all_cats,"tags"=>all_tags}.to_param if read_more_url != ""
extra_after_html += "
<script>
@ -439,6 +399,58 @@ class EventNewsController < ApplicationController
}
}
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 get_event_news_for_pack_data(cats,tags,set_tags=nil,is_random = false)
if tags.blank?
tags = ["all"]
@ -448,25 +460,29 @@ class EventNewsController < ApplicationController
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
event_news_cache.where(:invalid_date.lte => Time.now).destroy
count = event_news_cache.count
if count > 1
event_news_cache.limit(count-1).destroy
end
if event_news_cache.count == 0 || 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)
event_news_for_locale = EventNews.where(:title.nin => ["",nil], :uid.ne => uid).is_approved_and_show.filter_cats_and_tags(cats,tags)
sorted_event_news = event_news_for_locale.can_display_and_sorted
if !is_random
sorted_event_news = sorted_event_news.limit(widget_data_count)
if event_news_cache.count > 1
event_news_cache.destroy
end
first_deadline = sorted_event_news.pluck(:deadline).compact.sort[0]
first_postdate = event_news_for_locale.open_in_future.limit(1).pluck(:postdate)[0]
invalid_date = [first_postdate,first_deadline].compact.sort[0]
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})
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},invalid_date: invalid_date)
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)
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,invalid_date: invalid_date)
end
else
if devide_flag
@ -485,21 +501,60 @@ class EventNewsController < ApplicationController
event_news = event_news_cache.first.filter_result
end
if devide_flag
feeds_event_news = get_feed_event_news("widget",nil,cats,widget_data_count - top_event_news.count)
top_event_news = top_event_news + feeds_event_news.select{|v| v['is_top'] == true}
top_event_news = top_event_news.sort_by { |a| tmp=a["postdate"].blank?;[tmp ? 0 : 1, tmp ? nil : a["postdate"].to_time] }.reverse
rest_count = widget_data_count - top_event_news.count
if rest_count <= 0
event_news = top_event_news
event_news = top_event_news.take(widget_data_count)
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)
rest_event_news = rest_all_event_news.sort_by { |a| tmp=a["postdate"].blank?;[tmp ? 0 : 1, tmp ? nil : a["postdate"].to_time] }.reverse.take(rest_count)
event_news = top_event_news + rest_event_news
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 get_file
@url = request.path
begin
if @url.match(/\/\.\./)
render :file => "#{Rails.root}/app/views/errors/404.html", :layout => false, :status => :not_found, :content_type => 'text/html'
return
end
file = EventNewsFile.find(params[:id])
if File.basename(file.file.path) != URI.decode(params[:f_name])
render :file => "#{Rails.root}/app/views/errors/403.html", :layout => false, :status => :not_found, :content_type => 'text/html'
return
end
@url = file.file.url
if file.can_access?(OrbitHelper.current_user)
@path = file.file.path rescue ""
@filename = @path.split("/").last
@ext = @path.split("/").last.to_s.split(".").last
if @ext == "png" || @ext == "jpg" || @ext == "bmp" || @ext == "pdf"
render "archives/download_file.html",:layout=>false
else
if (current_site.accessibility_mode rescue false)
render "archives/redirect_to_file.html",:layout=>false
return
else
send_file(@path)
return
end
end
else
render :file => "#{Rails.root}/app/views/errors/403.html", :layout => false, :status => :not_found, :content_type => 'text/html'
return
end
rescue
render :file => "#{Rails.root}/app/views/errors/404.html", :layout => false, :status => :not_found, :content_type => 'text/html'
return
end
end
def show_local_event_news(uid, is_preview)
locale = OrbitHelper.get_site_locale.to_s
if is_preview
@ -528,13 +583,7 @@ class EventNewsController < ApplicationController
"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
files = event_news.event_news_files.to_fronted(locale)
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
@ -732,8 +781,7 @@ class EventNewsController < ApplicationController
}
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)
files = a.event_news_files.to_fronted(locale)
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

View File

@ -1,4 +1,25 @@
module EventNewsHelper
def self.complementaryColor(my_hex)
if my_hex[0] == '#'
my_hex = my_hex[1..-1]
end
rgb = my_hex.split(//).each_slice(my_hex.length/3).map{|v| v.join}
comp = rgb.map{|a| (255 - a.to_i(16)).to_s(16).rjust(2,'0')}
'#'+comp.join
end
def self.lighten_color(my_hex,percent)
if my_hex[0] == '#'
my_hex = my_hex[1..-1]
end
rgb = my_hex.split(//).each_slice(my_hex.length/3).map{|v| v.join}
comp = rgb.collect do |a|
tmp = a.to_i(16)*(1+percent/100.0)
tmp = 255 if tmp>255
tmp = 0 if tmp < 0
tmp.to_i.to_s(16).rjust(2,'0')
end
'#'+comp.join
end
def set_image_version_for_widget
subpart = OrbitHelper.get_current_widget
@image_version = 'thumb'
@ -23,13 +44,6 @@ module EventNewsHelper
end
end
end
def date_transform(variable)
if variable.class != DateTime
return (DateTime.parse(variable) rescue DateTime.new)
else
return variable
end
end
def data_to_human_type(a,set_tag_ids=nil)
statuses = a.statuses_with_classname.collect do |status|
{
@ -78,15 +92,18 @@ module EventNewsHelper
"img_description" => desc
}
end
def get_feed_annc(type,site_source,locale)
def get_feed_annc(type,site_source,locale,categories=nil)
ma_key = 'event_news'
if type == "index"
categories = Array(OrbitHelper.page_categories)
elsif type == "widget"
categories = Array(OrbitHelper.widget_categories)
else
categories = []
if categories.nil?
if type == "index"
categories = Array(OrbitHelper.page_categories)
elsif type == "widget"
categories = Array(OrbitHelper.widget_categories)
else
categories = []
end
end
categories = ["all"] if categories.length==0
if categories.include?("all")
feeds = SiteFeedAnnc.where(:channel_key => ma_key)
else
@ -102,10 +119,10 @@ module EventNewsHelper
end
data
end
def get_feed_event_news(type,site_source=nil)
def get_feed_event_news(type,site_source=nil,categories=nil,max_len=nil)
locale = OrbitHelper.get_site_locale.to_s
if !(defined? SiteFeedAnnc).nil?
fans = get_feed_annc(type,site_source,locale)
fans = get_feed_annc(type,site_source,locale,categories)
else
feed_anns = OrbitHelper.get_feed_for_module(type)
fans = []
@ -122,6 +139,7 @@ module EventNewsHelper
x = {
"event_news_links" => links,
"event_news_files" => files,
"event_carousel_images" => fa["bulletin_carousel_images"].to_a,
"title" => fa["title_translations"][locale],
"speaker" => (fa["speaker_translations"][locale].to_s rescue ""),
"place" => (fa["place_translations"][locale].to_s rescue ""),
@ -196,6 +214,7 @@ module EventNewsHelper
tags = @tags
categories = @categories
else
tags = page.tags
tags = params[:tags] if params[:tags].present?
categories = params['category']=='all' ? (page.categories || []) : (Array(params['category']) rescue (page.categories || []))
if params['category'].present? && tags.blank?
@ -222,7 +241,7 @@ module EventNewsHelper
end
if !(defined? SiteFeed).nil?
if @type != "show_widget"
feeds_anns = get_feed_event_news("index")
feeds_anns = get_feed_event_news("index",nil,nil,page_number*page_data_count)
else
feeds_anns = []
end
@ -230,7 +249,7 @@ module EventNewsHelper
else
event_news = []
if @type != "show_widget"
feeds_anns = get_feed_event_news("index",params["source"])
feeds_anns = get_feed_event_news("index",params["source"],nil,page_number*page_data_count)
else
feeds_anns = []
end
@ -239,10 +258,10 @@ module EventNewsHelper
if event_news.count != 0
top_anns = event_news.select{|v| v.is_top} + feeds_anns.select{|v| v['is_top']}
rest_all_anns = feeds_anns.select{|v| v['is_top'] != true} + event_news.select{|v| !v.is_top}
rest_anns = rest_all_anns.sort{|v1,v2| date_transform(v2["event_date"]) <=> date_transform(v1["event_date"])}
all_sorted = top_anns.sort{|v1,v2| date_transform(v2["event_date"]) <=> date_transform(v1["event_date"])} + rest_anns
rest_anns = rest_all_anns.sort_by { |a| tmp=a["postdate"].blank?;[tmp ? 0 : 1, tmp ? nil : a["postdate"].to_time] }.reverse
all_sorted = top_anns.sort_by { |a| tmp=a["postdate"].blank?;[tmp ? 0 : 1, tmp ? nil : a["postdate"].to_time] }.reverse + rest_anns
else
all_sorted = feeds_anns.select{|v| v['is_top']}.sort{|v1,v2| date_transform(v2["event_date"]) <=> date_transform(v1["event_date"])} + feeds_anns.select{|v| v['is_top'] != true}.sort{|v1,v2| (DateTime.parse(v2["event_date"].to_s) rescue DateTime.new)<=>(DateTime.parse(v1["event_date"].to_s) rescue DateTime.new)}
all_sorted = feeds_anns.select{|v| v['is_top']}.sort_by { |a| tmp=a["postdate"].blank?;[tmp ? 0 : 1, tmp ? nil : a["postdate"].to_time] }.reverse + feeds_anns.select{|v| v['is_top'] != true}.sort_by { |a| tmp=a["postdate"].blank?;[tmp ? 0 : 1, tmp ? nil : a["postdate"].to_time] }.reverse
end
all_filter = filter_by_keywords(all_sorted,params[:keywords],params[:stime],params[:etime])
else
@ -438,6 +457,7 @@ module EventNewsHelper
end
end
end
html.css("body")[0].inner_html = html.css("body")[0].inner_html.gsub("{{page-title}}","")
html.css("body").to_html.html_safe
end
end

View File

@ -10,11 +10,24 @@ class EventNews
include Slug
require 'event_news_model/cache'
include EventNewsModel::Cache
attr_accessor :org_tag_ids
def tags=(ids)
self.org_tag_ids = self.tag_ids
super(ids)
end
def []=(index,value)
if index.to_s=='tags'
self.org_tag_ids = self.tag_ids
end
super(index,value)
end
SubPart.class_eval { include EventNewsModel::Cache }
Page.class_eval { include EventNewsModel::Cache }
before_destroy do
EventNewsCache.all.destroy
end
field :is_edit, type: Boolean, default: false #use to check whether the preview record changed
field :copy_id
field :custom_carousel_image_width, type: String, default: ""
field :image_display_class, type: String, default: "full-size-img" #3 choices: full-size-img , pull-left , pull-right
field :add_to_calendar,type: Boolean,default: false
@ -24,7 +37,7 @@ class EventNews
field :calendar_type_id
field :event_id
field :place, type: String, localize: true
field :title, type: String, localize: true
field :title, as: :slug_title, type: String, localize: true
field :speaker, type: String, localize: true
field :host, type: String, localize: true
field :subtitle, localize: true
@ -67,12 +80,31 @@ class EventNews
accepts_nested_attributes_for :event_carousel_images, :allow_destroy => true
before_destroy :destroy_email
scope :can_display_and_sorted, ->{where(:is_hidden=>false,:is_preview => false).any_of({:postdate.lte=>Time.now, :deadline.gte=>Time.now},{:postdate.lte=>Time.now, :deadline=>nil}).order(is_top: :desc,event_date: :desc,id: :desc)}
scope :open_in_future, ->{where(:is_hidden.ne=>true,:is_preview.ne => true,:postdate.gt=>Time.now).order(postdate: :asc)}
scope :can_display_and_sorted, ->{where(:is_hidden.ne=>true,:is_preview.ne => true).any_of({:postdate.lte=>Time.now, :deadline.gte=>Time.now},{:postdate.lte=>Time.now, :deadline=>nil},{:postdate=>nil}).order(is_top: :desc,postdate: :desc,id: :desc)}
scope :is_approved, ->{where(:approved => true)}
scope :is_approved_and_show, ->{where(:approved => true,:is_hidden.ne=>true,:is_preview.ne => true)}
scope :filter_cats_and_tags, ->(cats,tags) {filter_by_widget_categories(cats,false).filter_by_tags(tags)}
before_create :set_expire
before_save :check_limit
index({postdate: 1}, { unique: false, background: true })
index({is_top: -1,postdate: -1, _id: -1}, { unique: false, background: true })
def get_org_model
if self.is_preview
org_model = nil
if self.copy_id
org_model = self.class.find(self.copy_id) rescue nil
else
org_model = self.class.where(:title=>self.title,:is_preview.ne=>true).desc(:updated_at).first
end
org_model.nil? ? self : org_model
else
self
end
end
def to_calendar_param
self.to_param
end
def calendar_type
CalendarType.where(:category_id.in => self.calendar_type_id)
end
@ -95,7 +127,7 @@ class EventNews
if status_settings.count != 0
reach_limit = status_settings.collect do |status_setting|
status = status_setting.status
if status_setting.top_limit.to_i <= self.class.where(:update_user_id.in => Role.find(status_setting.role_id).member_profiles.collect(&:user).flatten.uniq.map{|v| v.id},status => true).count
if status_setting.top_limit.to_i <= self.class.where(:is_preview.ne=>true,:update_user_id.in => Role.find(status_setting.role_id).member_profiles.collect(&:user).flatten.uniq.map{|v| v.id},status => true).count
if !check_only
if self[status] && !self.class.where(id:self.id).first[status]
self[status] = false
@ -118,10 +150,6 @@ class EventNews
end
reach_limit
end
def slug_title
doc = Nokogiri::HTML(self.title)
title = doc.text.gsub('/','-')
end
def set_expire
self.expirable_created_at = Time.now if self.is_preview
return true
@ -220,7 +248,7 @@ class EventNews
:note=>re.subtitle || "",
:allDay => false,
:color => nil,
:url_linked => (re.is_external_link? ? re.external_link : "#{read_more_url}/#{re.to_param}" rescue ""),
:url_linked => (re.is_external_link ? re.external_link : "#{read_more_url}/#{re.to_param}" rescue ""),
:start => re.event_date,
:end => re.event_end_date}
end

View File

@ -3,4 +3,5 @@ class EventNewsCache
field :parent_id
field :filter_result
field :locale,type: String,default: 'zh_tw'
field :invalid_date, type: DateTime
end

View File

@ -5,13 +5,20 @@ class EventNewsCustomTitle
field :title, type: String, localize: true
KEYS = ['event_date','speaker','place','host']
def self.get_map
KEYS.map{|k| self.where(key: k).first || self.create(key: k,title_translations: I18n.available_locales.map{|l| [l,I18n.with_locale(l){I18n.t("event_news.#{k}")}]}.to_h)}
KEYS.map do |k|
s = self.where(key: k).first || self.create(key: k,title_translations: I18n.available_locales.map{|l| [l,I18n.with_locale(l){I18n.t("event_news.#{k}")}]}.to_h)
if s.title_translations.select{|k,v| v.include?("translation missing")}.length>0
s.update_attributes(title_translations: I18n.available_locales.map{|l| [l,I18n.with_locale(l){I18n.t("event_news.#{k}")}]}.to_h)
end
s
end
end
def default_title
I18n.t("event_news.#{self.key}")
end
def self.get_trans(key)
TitleMap[key][I18n.locale] rescue I18n.t("event_news.#{key}")
tmp = TitleMap[key][I18n.locale] rescue I18n.t("event_news.#{key}")
tmp.blank? ? I18n.t("event_news.#{key}") : tmp
end
TitleMap = self.get_map.map{|v| [v.key,v.title_translations]}.to_h
end

View File

@ -3,7 +3,169 @@ class EventNewsFeed
include Mongoid::Timestamps
include Slug
field :title, as: :slug_title, type: String, localize: true
field :tag_ids, type: Array, default: []
field :title, as: :slug_title, type: String, localize: true
field :tag_ids, type: Array, default: []
field :category_ids, type: Array, default: []
before_save do
EventNewsCache.where(uid: self.uid).each do |cache|
cache.regenerate
end
end
def self.create_feed_cache(bulletin=nil,bulletin_feed=nil)
Thread.new do
if !bulletin.nil?
self.where(:tag_ids.in => Array(bulletin.tag_ids).collect{|v| v.to_s}).each do |bulletin_feed|
uid = bulletin_feed.uid
EventNewsCache.where(:uid => uid).each do |cache|
cache.regenerate
end
end
elsif !bulletin_feed.nil?
uid = bulletin_feed.uid
EventNewsCache.where(:uid => uid).each do |cache|
cache.regenerate
end
end
end
end
def generate_one_cache_timeout(startdt: nil,enddt: nil,dt: nil, base_url: nil, timeout: nil)
timeout = 100000 if timeout.nil?
begin
Timeout::timeout(timeout) {
feed_cache = nil
Thread.new do
feed_cache = self.generate_one_cache(startdt: startdt,enddt: enddt,dt: dt,base_url: base_url)
end
(1..(timeout.to_i+1)).each do
sleep(1)
break if !feed_cache.nil?
end
feed_cache.content
}
rescue=> e
puts [e,e.backtrace]
""
end
end
def generate_one_cache(startdt: nil,enddt: nil,dt: nil, base_url: nil)
base_url = Site.first.root_url if base_url.nil?
uid = self.uid
bf = self
if !bf.nil?
tags = bf.tag_ids
categories = bf.category_ids
if !(categories.empty? && tags.empty?)
anns_before_filter = EventNews.any_of(I18n.available_locales.map{|v| {"title.#{v}"=>{"$nin"=>["", nil]}}}).is_approved_and_show
if !dt.nil?
dt = DateTime.parse(dt)
dtt = dt + 1.day
anns_before_filter = anns_before_filter.where(:postdate.gt => dt, :postdate.lt => dtt)
elsif !startdt.blank? && enddt.blank?
startdt = DateTime.parse(startdt)
enddt = DateTime.now
anns_before_filter = anns_before_filter.where(:postdate.gt => startdt, :postdate.lt => enddt)
elsif !startdt.blank? && !enddt.blank?
startdt = DateTime.parse(startdt)
enddt = DateTime.parse(enddt) + 1.day
anns_before_filter = anns_before_filter.where(:postdate.gt => startdt, :postdate.lt => enddt)
end
anns_before_filter = anns_before_filter.can_display_and_sorted
if !tags.empty?
anns_before_filter = anns_before_filter.filter_by_tags(tags)
end
if !categories.empty?
anns_before_filter = anns_before_filter.filter_by_categories(categories)
end
announcements = anns_before_filter.can_display_and_sorted
else
announcements = []
end
end
all_anns = []
tag_names = []
category_titles = []
tag_ids = []
category_ids = []
first_postdate = anns_before_filter.open_in_future.limit(1).pluck(:postdate)[0]
first_deadline = nil
announcements.each do |anns|
deadline = anns.deadline
if !deadline.blank?
if first_deadline.nil? || first_deadline>deadline
first_deadline = deadline
end
end
user = User.find(anns.create_user_id) rescue nil
if !user.nil?
author = user.member_profile && user.member_profile.name == "" ? user.user_name : user.member_profile.name
else
author = ""
end
a = {}
a["id"] = anns.uid
a["title_translations"] = anns.title_translations
a["subtitle_translations"] = anns.subtitle_translations
a["text_translations"] = {}
a["text_translations"]["en"] = self.class.smart_convertor(anns.text_translations["en"],base_url) if !anns.text_translations["en"].blank?
a["text_translations"]["zh_tw"] = self.class.smart_convertor(anns.text_translations["zh_tw"],base_url) if !anns.text_translations["zh_tw"].blank?
a["postdate"] = anns.postdate
a["image_description_translations"] = anns.image_description_translations
a["image"] = {}
a["display_img"] = anns.display_img
a["image"]["original"] = ("#{base_url}" + anns.image.url rescue "")
a["image"]["thumb"] = ("#{base_url}" + anns.image.thumb.url rescue "")
a["image"]["mobile"] = ("#{base_url}" + anns.image.mobile.url rescue "")
a["tags"] = []
a["category"] = {}
a["author"] = author
a["params"] = anns.to_param
a["subtitle_ann"] = anns.subtitle if anns.display_subtitle?
a["bulletin_links"] = []
a["bulletin_files"] = []
a["bulletin_carousel_images"] = anns.bulletin_carousel_images.map{|image| {"src"=>"#{base_url}" + image.file.url,"description"=>image.description.to_s,"description_text"=>image.description_text }}
a["external_link"] = anns["is_external_link"] ? anns.external_link : nil
anns.tags.each do |tag|
if !tag_ids.include?(tag.id.to_s)
tag_ids << tag.id.to_s
tag_names << {"name_translations" => tag.name_translations}
end
a["tags"] << {"name_translations" => tag.name_translations}
end
cat = anns.category
if (!category_ids.include?(cat.id.to_s) rescue false)
category_ids << cat.id.to_s
category_titles << {"title_translations" => cat.title_translations}
end
a["category"] = {"title_translations" => (cat.title_translations rescue {})}
anns.bulletin_links.each do |bl|
b = {}
b["url"] = bl.url
b["title_translations"] = bl.title_translations
a["bulletin_links"] << b
end
anns.bulletin_files.each do |bf|
b = {}
b["description_translations"] = bf.description_translations
b["title_translations"] = bf.title_translations
b["url"] = ("#{base_url}" + bf.file.url rescue "")
a["bulletin_files"] << b
end
all_anns << a
end
invalid_date = [first_postdate,first_deadline].compact.sort[0]
anns = {
"announcements" => all_anns,
"tags" => tag_names,
"categories" => category_titles
}.to_json
feed_cache = EventNewsFeedCache.create(uid: uid,content: anns,start: startdt,end: enddt,date: dt,invalid_date: invalid_date)
end
def self.smart_convertor(text,url)
html_string = text
html_string = html_string.gsub(/img.*?src="(?=\/)(.*?)|a.*?href="(?=\/)(.*?)/i){|w| w+url}
html_string = html_string.gsub(/img.*?src="\.\.(?=\/)(.*?)|a.*?href="\.\.(?=\/)(.*?)/i){|w| w[0...-2]+url}
return html_string
end
end

View File

@ -0,0 +1,29 @@
class EventNewsFeedCache
include Mongoid::Document
include Mongoid::Timestamps
field :content, type: String, default: ''
field :uid
field :start
field :end
field :date
field :invalid_date, type: DateTime
def self.regenerate_all
caches = self.all.to_a
caches.each do |cache|
cache.regenerate
end
uids = EventNewsFeed.all.pluck(:uid) - caches.collect(&:uid)
EventNewsFeed.where(:uid.in=> uids).each do |feed|
feed.generate_one_cache()
end
end
def regenerate
st = self.start
et = self.end
dt = self.date
uid = self.uid
EventNewsFeed.where(uid: uid).first.generate_one_cache(startdt: st,enddt: et,dt: dt) rescue nil
self.destroy
end
end

View File

@ -9,10 +9,25 @@ class EventNewsFile
field :description, localize: true
field :title, localize: true
field :choose_lang, :type => Array, :default => ["en","zh_tw"]
field :privacy_type, type: String, default: 'public'
belongs_to :event_news
scope :to_fronted, ->(locale=I18n.locale){ self.map{|file| file.to_fronted(locale)}.compact rescue [] }
def to_fronted(locale=I18n.locale)
file = self
file.enabled_for?(locale) && !file[:file].blank? ? { "file_url" => "/xhr/event_news/file/#{file.id}/#{file['file']}" + "\" title=\"#{file.file_title}",
"file_title" => (file.title.blank? ? URI.unescape(File.basename(file.file.path)) : file.title rescue '')
} : nil rescue nil
end
def file_title
if self.description.present?
return self.description
elsif self.title.present?
return self.title
else
return File.basename(self.file.path)
end
end
def enabled_for?(lang)
if lang.nil?
return true
@ -20,5 +35,11 @@ class EventNewsFile
return self.choose_lang.include?(lang)
end
end
def can_access?(user)
if user.nil? && self.privacy_type == 'logged_in'
return false
else
return true
end
end
end

View File

@ -1,9 +1,13 @@
Rails.application.routes.draw do
if ENV['worker_num']=='0' && File.basename($0) != 'rake' && !Rails.const_defined?('Console')
Thread.new do
EventNewsFeedCache.regenerate_all
end
end
locales = Site.first.in_use_locales rescue I18n.available_locales
scope "(:locale)", locale: Regexp.new(locales.join("|")) do
namespace :admin do
namespace :admin do
post 'event_news/preview', to: 'event_news#preview'
post 'event_news/createfeed', to: 'event_news#createfeed'
post 'event_news/importanns', to: 'event_news#importanns'
@ -28,19 +32,21 @@ Rails.application.routes.draw do
post 'update_custom_title'
end
end
end
resources :event_news do
collection do
get ':slug_title-:uid', to: 'event_news#show', as: :display
end
end
get '/xhr/event_news/agenda' => 'event_news#agenda'
get "/xhr/event_news/feed/:uid" => "event_news_feeds#feed"
get "/xhr/event_news/rssfeed/:uid" => "event_news_feeds#rssfeed"
get "/xhr/event_news/feeds" => "event_news_feeds#feeds"
get '/xhr/event_news/event_news.json', to: 'event_news_module#get_event_news'
get '/xhr/panel/event_news/widget/sync_data' => 'event_news#show_widget'
resources :event_news do
collection do
get ':slug_title-:uid', to: 'event_news#show', as: :display
end
end
get '/xhr/event_news/agenda' => 'event_news#agenda'
get "/xhr/event_news/feed/:uid" => "event_news_feeds#feed"
get "/xhr/event_news/rssfeed/:uid" => "event_news_feeds#rssfeed"
get "/xhr/event_news/feeds" => "event_news_feeds#feeds"
get '/xhr/event_news/event_news.json', to: 'event_news_module#get_event_news'
get '/xhr/panel/event_news/widget/sync_data' => 'event_news#show_widget'
get '/xhr/event_news/file/:id/*f_name', to: 'event_news#get_file', format: false
end
end

View File

@ -73,13 +73,15 @@ module EventNewsMod
data_item[key_item1] = [value_item1,value_item2,value_item3]
data_item[key_item2] = [value2_item1,value2_item2]
data_item[key_item3] = [value_item2,value_item3]
require File.expand_path('../../../app/models/event_news_cache', __FILE__)
if defined? EventNewsCache
EventNewsCache.destroy_all
end
require File.expand_path('../../../app/models/event_news_custom_title', __FILE__)
if defined? EventNewsCustomTitle
EventNewsCustomTitle.get_map
if ENV['worker_num']=='0' && File.basename($0) != 'rake' && !Rails.const_defined?('Console')
require File.expand_path('../../../app/models/event_news_cache', __FILE__)
if defined?(EventNewsCache)
EventNewsCache.destroy_all
end
require File.expand_path('../../../app/models/event_news_custom_title', __FILE__)
if defined? EventNewsCustomTitle
EventNewsCustomTitle.get_map
end
end
rescue => e
puts ['error in event_news',e]

View File

@ -9,6 +9,14 @@ module EventNewsModel
if self.class == SubPart
EventNewsCache.where(parent_id:self.id).destroy
elsif self.class == EventNews || (self.class == Page && self.module == "event_news")
if self.class == EventNews
tmp_tag_ids = (Array(self.tag_ids) + Array(self.org_tag_ids)).uniq
Thread.new do
EventNewsCache.where(:uid.in => EventNewsFeed.where(:tag_ids.in => tmp_tag_ids.collect{|v| v.to_s}).pluck(:uid)).each do |cache|
cache.regenerate
end
end
end
EventNewsCache.all.destroy
end
end