diff --git a/app/controllers/announcements_controller.rb b/app/controllers/announcements_controller.rb index b5329b3..43966e0 100644 --- a/app/controllers/announcements_controller.rb +++ b/app/controllers/announcements_controller.rb @@ -5,8 +5,6 @@ class AnnouncementsController < ApplicationController def initialize super @app_title = 'announcement' - @manually_sort = manually_sort - Bulletin.instance_variable_set('@manually_sort',@manually_sort) #self.request = OrbitHelper.request end def annc_depts_translations @@ -23,13 +21,6 @@ class AnnouncementsController < ApplicationController AnnouncementSetting.first.enable_annc_dept rescue false end end - def manually_sort - if defined?(OrbitHelper::SharedHash) && OrbitHelper::SharedHash - OrbitHelper::SharedHash['announcement'][:enable_manually_sort] rescue AnnouncementSetting.first.enable_manually_sort - else - AnnouncementSetting.first.enable_manually_sort rescue false - end - end def render_400 render(:file => "#{ViewRootDir}/announcements/400.html", :layout => false, :status => 400, :formats => [:html]) end @@ -57,8 +48,10 @@ class AnnouncementsController < ApplicationController @enable_search_flag = false @image_version = 'thumb' if page.respond_to?(:select_option_items) - ModuleApp.all.select{|tmp| tmp.key.to_s=='announcement'}.each do |modile_app| - @show_option_items = modile_app.show_option_items rescue nil + module_app = ModuleApp.where(key: 'announcement').first + @show_option_items = nil + if module_app && page.respond_to?(:select_option_items) + @show_option_items = module_app.show_option_items end I18n.with_locale(:en) do page.select_option_items.each do |select_option_item| @@ -111,8 +104,8 @@ class AnnouncementsController < ApplicationController desc = (desc.blank? ? "announcement 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 + title = a.title_plain_text + title = 'no content' if title.blank? if @image_version == 'thumb' image_url = a.image.thumb.url elsif @image_version == 'mobile' @@ -256,11 +249,7 @@ class AnnouncementsController < ApplicationController else cats = ["all"] + cats end - if @manually_sort - anns = anns.sort_by { |a| [ (a["is_top"] ? 1 : 0) , -a['sort_number'].to_i, a["postdate"].blank? ? nil : a["postdate"].to_time] }.reverse - else - anns = anns.sort_by { |a| [ (a["is_top"] ? 1 : 0) , a["postdate"].blank? ? nil : a["postdate"].to_time] }.reverse - end + anns = sort_announcements(anns) end cats = cats.uniq tags = tags.uniq @@ -509,9 +498,11 @@ class AnnouncementsController < ApplicationController @read_more_option = 0 @all_setting_option = 0 @image_version = 'thumb' + @show_options = nil if subpart.methods.include? 'select_options'.to_sym - ModuleApp.all.select{|tmp| tmp.key.to_s=='announcement'}.each do |modile_app| - @show_options = modile_app.show_options rescue nil + module_app = ModuleApp.where(key: 'announcement').first + if module_app + @show_options = module_app.show_options end I18n.with_locale(:en) do subpart.select_options.each do |select_option| @@ -621,19 +612,15 @@ class AnnouncementsController < ApplicationController @page_invalid_time = invalid_date end if devide_flag - feeds_anns = get_feed_announcements("widget",nil,cats,widget_data_count - top_anns.count) - top_anns = top_anns + feeds_anns.select{|v| v['is_top'] == true} - if @manually_sort - top_anns = top_anns.sort_by { |a| tmp=a["postdate"].blank?;[-a['sort_number'].to_i,tmp ? 0 : 1, tmp ? nil : a["postdate"].to_time] }.reverse - else - top_anns = top_anns.sort_by { |a| tmp=a["postdate"].blank?;[tmp ? 0 : 1, tmp ? nil : a["postdate"].to_time] }.reverse - end + feeds_anns, _ = get_feed_announcements("widget",nil,cats,widget_data_count - top_anns.count) + top_anns += feeds_anns.select{|v| v['is_top'] == true} + top_anns = sort_announcements(top_anns) rest_count = widget_data_count - top_anns.count if rest_count <= 0 anns = top_anns.take(widget_data_count) else rest_all_anns = feeds_anns.select{|v| v['is_top'] != true} + not_top_anns.take(rest_count) - rest_anns = rest_all_anns.sort_by { |a| tmp=a["postdate"].blank?;[tmp ? 0 : 1, tmp ? nil : a["postdate"].to_time] }.reverse.take(rest_count) + rest_anns = sort_announcements(rest_all_anns).take(rest_count) anns = top_anns + rest_anns end end @@ -1003,8 +990,8 @@ class AnnouncementsController < ApplicationController 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 + title = a.title_plain_text + title = 'no content' if title.blank? { "department" => author, "bulletin_links" => links, diff --git a/app/controllers/bulletins_controller.rb b/app/controllers/bulletins_controller.rb index 1bbbe16..c0193ee 100644 --- a/app/controllers/bulletins_controller.rb +++ b/app/controllers/bulletins_controller.rb @@ -15,13 +15,6 @@ class BulletinsController < ApplicationController AnnouncementSetting.first.enable_annc_dept rescue false end end - def manually_sort - if defined?(OrbitHelper::SharedHash) && OrbitHelper::SharedHash - OrbitHelper::SharedHash['announcement'][:enable_manually_sort] rescue AnnouncementSetting.first.enable_manually_sort - else - AnnouncementSetting.first.enable_manually_sort rescue false - end - end def get_bulletins page = Page.where(:module => "announcement").first rescue nil @@ -38,8 +31,8 @@ class BulletinsController < ApplicationController I18n.locale = :zh_tw keyword = params[:keyword].to_s if keyword.present? - keyword = Regexp.new(".*"+keyword+".*") - bulletins = Bulletin.any_of({:title=>keyword},{:subtitle=>keyword},{:text=>keyword}) + keyword_regex = OrbitHelper.get_keyword_regex(keyword) + bulletins = Bulletin.any_of({:title=>keyword_regex},{:subtitle=>keyword_regex},{:text=>keyword_regex}) else bulletins = Bulletin.all end @@ -56,7 +49,7 @@ class BulletinsController < ApplicationController bulletins = bulletins.where(:is_preview.in=>[false,nil]) bulletins = bulletins.where(:approved.ne => false , :rejected.ne => true) bulletins = bulletins.where(:postdate.lt=>Time.now) - if (manually_sort rescue false) + if (AnnouncementsHelper.enable_manually_sort rescue false) bulletins = bulletins.order({:is_top => -1, :sort_number => 1, :postdate => -1}).page(page_num).per(per_page) else bulletins = bulletins.desc( :is_top, :postdate).page(page_num).per(per_page) @@ -112,10 +105,9 @@ class BulletinsController < ApplicationController end # 計算總筆數 Start - keyword = params[:keyword].to_s if keyword.present? - keyword = Regexp.new(".*"+keyword+".*") - bulletin_count = Bulletin.any_of({:title=>keyword},{:subtitle=>keyword},{:text=>keyword}) + keyword_regex = OrbitHelper.get_keyword_regex(keyword) + bulletin_count = Bulletin.any_of({:title=>keyword_regex},{:subtitle=>keyword_regex},{:text=>keyword_regex}) else bulletin_count = Bulletin.all end diff --git a/app/helpers/announcements_helper.rb b/app/helpers/announcements_helper.rb index f0fa4b6..0272c8f 100644 --- a/app/helpers/announcements_helper.rb +++ b/app/helpers/announcements_helper.rb @@ -1,25 +1,4 @@ module AnnouncementsHelper - 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 data_to_human_type(a,set_tag_ids=nil,tmp_enable_annc_dept=false,annc_depts=[]) tmp_enable_annc_dept = @tmp_enable_annc_dept if @tmp_enable_annc_dept annc_depts = @annc_depts if @annc_depts @@ -67,7 +46,7 @@ module AnnouncementsHelper "img_description" => desc } end - def get_feed_annc(type,site_source,locale,categories=nil,max_len=nil) + def get_feed_annc(type,site_source,locale,categories=nil,max_len=nil,sort_maps=nil,extra_match_cond=nil) ma_key = 'announcement' if categories.nil? if type == "index" @@ -79,88 +58,45 @@ module AnnouncementsHelper end end categories = ["all"] if categories.length==0 - data = SiteFeedAnnc.get_feed_cache(ma_key,categories,site_source,locale,type=='widget',max_len) + data = SiteFeedAnnc.get_feed_cache( + ma_key, + categories, + site_source, + locale, + type=='widget', + max_len, + sort_maps, + extra_match_cond + ) data end - def get_feed_announcements(type,site_source=nil,categories=nil,max_len=nil) + def get_feed_announcements(type,site_source=nil,categories=nil,max_len=nil,extra_match_cond=[]) locale = OrbitHelper.get_site_locale.to_s + feeds = [] + feeds_count = 0 if !(defined? SiteFeedAnnc).nil? - fans = get_feed_annc(type,site_source,locale,categories,max_len) - else - feed_anns = OrbitHelper.get_feed_for_module(type) - fans = [] - feed_anns.each do |fa| - next if !site_source.nil? && site_source != fa["source-site-title"] - status = { - "status" => "#{fa["source-site-title"]}", - "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, - "bulletin_carousel_images" => fa["bulletin_carousel_images"].to_a, - "title" => fa["title_translations"][locale], - "subtitle" => fa["subtitle_translations"][locale], - "statuses" => [status], - "category" => fa["category"], - "postdate" => fa["postdate"], - "author" => fa["author"], - "source-site" => "#{fa["source-site-title"]}", - "source-site-title" => fa["source-site-title"], - "source-site-link" => fa["source-site"], - "link_to_show" => OrbitHelper.url_to_show(fa["params"]), - "target" => "_self", - "img_src" => fa["image"]["thumb"] || AnnouncementsController::DefaultImgSrc, - "img_description" => fa["image_description_translations"][locale], - "more" => t("announcement.more"), - "view_count" => "" - } - if (!x["title"].empty? rescue false) - fans << x - end - end - end - fans - end - def filter_by_keywords(sorted,keywords,stime,etime) - kflag = keywords.blank? - sflag = stime.blank? - eflag = etime.blank? - stime = stime.to_s.split('/') - stime = Time.zone.local(*stime) rescue nil - etime = etime.to_s.split('/') - etime = Time.zone.local(*etime) rescue nil - if !kflag || !sflag || !eflag - sorted.select{|anns| - if kflag - flag = true - else - if anns["source-site"].present? - title = Nokogiri::HTML(anns["title"].to_s).text - else - title = Nokogiri::HTML(anns.title.to_s).text - end - flag = title.include?(keywords.to_s) - end - if anns.postdate - if sflag && !eflag - flag = flag && (anns.postdate<=etime) - elsif !sflag && eflag - flag = flag && (anns.postdate>=stime) - elsif !sflag && !eflag - flag = flag && (anns.postdate>=stime) && (anns.postdate<=etime) - end - end - flag + sort_maps = {is_top: :desc, postdate: :desc, id: :desc} + match_cond = { + "is_hidden" => {"$ne" => true}, + "$and" => [ + {"postdate" => {"$lte"=> Time.now}}, + { + "$or" => [ + {"deadline" => {"$gte"=> Time.now}}, + {"deadline" => nil} + ] + } + ], + "title" => {"$gt"=>""} } - else - sorted + if !extra_match_cond.empty? + match_cond["$and"] += extra_match_cond + end + feeds, feeds_count = get_feed_annc(type,site_source,locale,categories,max_len,sort_maps,match_cond) end + return feeds, feeds_count end + def get_sorted_annc(data_count=nil) params = OrbitHelper.params locale = OrbitHelper.get_site_locale.to_s @@ -168,8 +104,7 @@ module AnnouncementsHelper page_number = 1 if page_number == 0 page_data_count = data_count || OrbitHelper.page_data_count.to_i feeds_anns = [] - page = OrbitHelper.page rescue nil - page = Page.where(url:params['url']).first if page.nil? + page = OrbitHelper.page if @type == "show_widget" tags = @tags categories = @categories @@ -181,71 +116,86 @@ module AnnouncementsHelper tags = ["all"] end end + announcements_list = [] + announcements = [] + feeds_count = 0 + extra_match_cond = [] + if !params[:keywords].blank? + extra_match_cond << { + "title_plain_text" => OrbitHelper.get_keyword_regex(params[:keywords]) + } + end + if !params[:stime].blank? + stime = OrbitHelper.get_time_from_str(params[:stime]) + extra_match_cond << { + "postdate" => {"$gte" => stime} + } + end + if !params[:etime].blank? + etime = OrbitHelper.get_time_from_str(params[:etime]) + 1.days + extra_match_cond << { + "postdate" => {"$lt" => etime} + } + end if !params["source"].present? + announcements = Bulletin.can_display_and_sorted + .filter_by_categories(categories, false).filter_by_tags(tags) + .where(:title.nin => ["",nil]) + if !extra_match_cond.empty? + announcements = announcements.and(extra_match_cond) + end if @type == "show_widget" - if params[:uids].blank? - announcements = Bulletin.where(:title.nin => ["",nil],:is_preview.in=>[false,nil]) - .can_display_and_sorted.is_approved - .filter_by_categories(categories,false).filter_by_tags(tags).to_a - else + if !params[:uids].blank? member_profile = MemberProfile.any_in(:uid=>params[:uids]) user_ids = member_profile.map{|m| m.user.id rescue nil}.select{|id| !id.nil?} - announcements = Bulletin.where(:title.nin => ["",nil],:is_preview.in=>[false,nil],:update_user_id.in=>user_ids) - .can_display_and_sorted.is_approved - .filter_by_categories(categories,false).filter_by_tags(tags).to_a - end - else - announcements = Bulletin.where(:title.nin => ["",nil],:is_preview.in=>[false,nil]) - .can_display_and_sorted.is_approved - .filter_by_categories(categories,false).filter_by_tags(tags).to_a - end - if !(defined? SiteFeed).nil? - if @type != "show_widget" - feeds_anns = get_feed_announcements("index",nil,categories,page_number*page_data_count) - else - feeds_anns = [] + announcements = announcements.where(:update_user_id.in=>user_ids) end end - else - announcements = [] - if @type != "show_widget" - feeds_anns = get_feed_announcements("index",params["source"],categories,page_number*page_data_count) - else - feeds_anns = [] + if !(defined? SiteFeed).nil? && @type != "show_widget" + feeds_anns, feeds_count = get_feed_announcements("index",nil,categories,page_number*page_data_count,extra_match_cond) end + elsif @type != "show_widget" + feeds_anns, feeds_count = get_feed_announcements("index",params["source"],categories,page_number*page_data_count,extra_match_cond) end if !feeds_anns.blank? - if announcements.count != 0 - top_anns = announcements.select{|v| v.is_top} + feeds_anns.select{|v| v['is_top']} - rest_all_anns = feeds_anns.select{|v| v['is_top'] != true} + announcements.select{|v| !v.is_top} - rest_anns = rest_all_anns.sort_by { |a| tmp=a["postdate"].blank?;[tmp ? 0 : 1, tmp ? nil : a["postdate"].to_time] }.reverse - if @manually_sort - all_sorted = top_anns.sort_by do |a| - tmp = a["postdate"].blank? - [-a['sort_number'].to_i,tmp ? 0 : 1, tmp ? nil : a["postdate"].to_time] - end.reverse + rest_anns - else - all_sorted = top_anns.sort_by do |a| - tmp = a["postdate"].blank? - [tmp ? 0 : 1, tmp ? nil : a["postdate"].to_time] - end.reverse + rest_anns - end - else - 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]) + top_anns = announcements.select{|v| v.is_top} + feeds_anns.select{|v| v['is_top']} + rest_all_anns = announcements.select{|v| !v.is_top} + feeds_anns.select{|v| v['is_top'] != true} + + all_filter = sort_announcements(top_anns) + sort_announcements(rest_all_anns) else - all_filter = filter_by_keywords(announcements,params[:keywords],params[:stime],params[:etime]) + all_filter = announcements end if page_data_count != 0 sorted = all_filter[(page_number-1)*page_data_count...page_number*page_data_count] else sorted = all_filter end - annc_count = all_filter.count + annc_count = announcements.count + feeds_count total_pages = page_data_count == 0 ? 1 : (annc_count.to_f / page_data_count).ceil [sorted,total_pages] end + + def sort_announcements(announcements) + enable_manually_sort = enable_manually_sort + if enable_manually_sort + announcements = announcements.sort_by { |announcement| + tmp1 = announcement["postdate"].blank? + [ + -a['sort_number'].to_i, + tmp1 ? 0 : 1, tmp1 ? nil : announcement["postdate"].to_time + ] + }.reverse + else + announcements = announcements.sort_by { |announcement| + tmp1 = announcement["postdate"].blank? + [ + tmp1 ? 0 : 1, tmp1 ? nil : announcement["postdate"].to_time + ] + }.reverse + end + return announcements + end + def render_view_for_annc(overridehtml=nil) @key = Site.first.template def render_link_to_edit(html, url_to_edit) @@ -513,4 +463,35 @@ module AnnouncementsHelper ").html_safe end + + class << self + def 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 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 enable_manually_sort + if defined?(OrbitHelper::SharedHash) && OrbitHelper::SharedHash + OrbitHelper::SharedHash['announcement'][:enable_manually_sort] + else + AnnouncementSetting.first.enable_manually_sort rescue false + end + end + end end diff --git a/app/models/announcement_setting.rb b/app/models/announcement_setting.rb index 06953c2..22cfe92 100644 --- a/app/models/announcement_setting.rb +++ b/app/models/announcement_setting.rb @@ -18,6 +18,9 @@ class AnnouncementSetting field :annc_depts, type: Array, default: [], localize: true has_many :anns_status_settings, :autosave => true, :dependent => :destroy accepts_nested_attributes_for :anns_status_settings, :allow_destroy => true + + field :migrate_flag, type: Array, default: [] + def self.check_limit_for_user(user_id, b_id = nil) limit = self.first.top_limit rescue 0 return true if limit == 0 diff --git a/app/models/bulletin.rb b/app/models/bulletin.rb index 19b0599..92ffd32 100644 --- a/app/models/bulletin.rb +++ b/app/models/bulletin.rb @@ -8,8 +8,8 @@ class Bulletin include OrbitTag::Taggable include OrbitCategory::Categorizable include Slug - require 'bulletin_model/cache' - include BulletinModel::Cache + require 'announcement/cache' + include Announcement::Cache attr_accessor :org_tag_ids,:org_category_id def tags=(ids) self.org_tag_ids = self.tag_ids @@ -35,8 +35,8 @@ class Bulletin end super(index,value) end - SubPart.class_eval { include BulletinModel::Cache } - Page.class_eval { include BulletinModel::Cache } + SubPart.class_eval { include Announcement::Cache } + Page.class_eval { include Announcement::Cache } before_destroy do if self.event_id.present? && !defined?(Calendar).nil? Event.where(:id=>self.event_id).destroy @@ -56,6 +56,7 @@ class Bulletin field :event_id field :page_id field :title, as: :slug_title, type: String, localize: true + field :title_plain_text, type: String, localize: true field :subtitle, localize: true field :text, localize: true field :create_user_id @@ -103,7 +104,7 @@ class Bulletin 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(:approved => true,:is_hidden.ne=>true,:is_preview.ne => true).valid_time_range} - scope :valid_time_range, ->{and_any_of([{"postdate"=>{"$lte"=> Time.now}, "deadline"=>{"$gte"=> Time.now}}, {"postdate"=>{"$lte"=> Time.now}, "deadline"=>nil}]).order((@manually_sort ? {is_top: :desc,sort_number: :asc,postdate: :desc,id: :desc} : {is_top: :desc,postdate: :desc,id: :desc}))} + scope :valid_time_range, ->{and_any_of([{"postdate"=>{"$lte"=> Time.now}, "deadline"=>{"$gte"=> Time.now}}, {"postdate"=>{"$lte"=> Time.now}, "deadline"=>nil}]).order((AnnouncementsHelper.enable_manually_sort ? {is_top: :desc,sort_number: :asc,postdate: :desc,id: :desc} : {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)} @@ -116,6 +117,7 @@ class Bulletin if self.is_top_changed? && !self.is_top self.sort_number = nil end + self.migrate_title_plain_text end index({postdate: 1}, { unique: false, background: true }) index({is_top: -1,postdate: -1, _id: -1}, { unique: false, background: true }) @@ -487,6 +489,11 @@ class Bulletin end end end + + def migrate_title_plain_text + self.title_plain_text_translations = OrbitHelper.get_plain_text_translations(self.title_translations) + end + def self.notify_all_feed(force_update=false) related_feeds = BulletinFeed.where(:remote_urls.nin=>[nil, []]).to_a related_feeds.each do |feed| diff --git a/app/views/announcements/index.html.erb b/app/views/announcements/index.html.erb index 41282cd..1b4c2e4 100644 --- a/app/views/announcements/index.html.erb +++ b/app/views/announcements/index.html.erb @@ -44,7 +44,7 @@ }