From 9b48e6abfc24e045d391e158471fa6caab4a2897 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B1=E5=8D=9A=E4=BA=9E?= Date: Mon, 30 Sep 2024 21:57:01 +0800 Subject: [PATCH] add show_today_data_first for index --- app/helpers/event_news_helper.rb | 69 +++++++++++++++++------- app/models/event_news.rb | 4 +- app/views/event_news_mods/index.html.erb | 29 ++-------- lib/event_news_mod/engine.rb | 14 ++--- 4 files changed, 63 insertions(+), 53 deletions(-) diff --git a/app/helpers/event_news_helper.rb b/app/helpers/event_news_helper.rb index 5f4ef83..c60195b 100644 --- a/app/helpers/event_news_helper.rb +++ b/app/helpers/event_news_helper.rb @@ -204,37 +204,67 @@ module EventNewsHelper 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? if @type == "show_widget" tags = @tags categories = @categories else + page = OrbitHelper.page rescue nil + page = Page.where(url:params['url']).first if page.nil? + 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? tags = ["all"] end - end - if !params["source"].present? - if @type == "show_widget" - if params[:uids].blank? - event_news = EventNews.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 - member_prfile = MemberProfile.any_in(:uid=>params[:uids]) - user_ids = member_prfile.map{|m| m.user.id rescue nil}.select{|id| !id.nil?} - event_news = EventNews.where(:title.nin => ["",nil],:is_preview.in=>[false,nil],:create_user_id.in=>user_ids) - .can_display_and_sorted.is_approved - .filter_by_categories(categories,false).filter_by_tags(tags).to_a + + @enable_search_flag = false + module_app = ModuleApp.where(key: 'event_news_mod').first + @show_option_items = nil + @show_today_data_first = false + if module_app && page.respond_to?(:select_option_items) + @show_option_items = module_app.show_option_items + if !(@show_option_items.nil?) + page.select_option_items.each do |select_option_item| + value = YAML.load(select_option_item.value)[I18n.locale] + case select_option_item.field_name + when @show_option_items.keys[1].to_s + if value == t('event_news.yes') + @enable_search_flag = true + end + when @show_option_items.keys[2].to_s + if value == t('event_news.yes') + @show_today_data_first = true + end + end + end end - else - event_news = EventNews.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 categories.include? 'all' + @categories = module_app.categories + else + cat_maps = Category.where(:id.in => categories).collect{|cat| [cat.id.to_s, cat]}.to_h + @categories = categories.map{|v| cat_maps[v.to_s]}.compact + end + end + event_news = [] + if !params["source"].present? + event_news = @show_today_data_first ? + EventNews.can_display_and_sorted_according_today : + EventNews.can_display_and_sorted + + event_news = event_news + .where(:title.nin => ["",nil]) + .filter_by_categories(categories,false).filter_by_tags(tags) + if @type == "show_widget" + 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?} + event_news = event_news.where(:create_user_id.in=>user_ids) + end + end + event_news = event_news.to_a if !(defined? SiteFeed).nil? if @type != "show_widget" feeds_anns = get_feed_event_news("index",nil,nil,page_number*page_data_count) @@ -243,7 +273,6 @@ module EventNewsHelper end end else - event_news = [] if @type != "show_widget" feeds_anns = get_feed_event_news("index",params["source"],nil,page_number*page_data_count) else diff --git a/app/models/event_news.rb b/app/models/event_news.rb index 9d3081f..66b30bc 100644 --- a/app/models/event_news.rb +++ b/app/models/event_news.rb @@ -107,7 +107,7 @@ class EventNews 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) + is_approved_and_show .valid_time_range .order( EventNewsHelper.is_postdate_sort_first ? @@ -116,7 +116,7 @@ class EventNews ) } scope :can_display_and_sorted_according_today, ->{ - where(:is_hidden.ne=>true,:is_preview.ne => true) + is_approved_and_show .order(event_date: :asc).valid_time_range.order({postdate: :asc, id: :asc}).where(:event_date.gte => Date.today.to_time) } scope :valid_time_range, ->{ diff --git a/app/views/event_news_mods/index.html.erb b/app/views/event_news_mods/index.html.erb index 504f432..4926b1d 100644 --- a/app/views/event_news_mods/index.html.erb +++ b/app/views/event_news_mods/index.html.erb @@ -1,21 +1,7 @@ -<% params = OrbitHelper.params - page = Page.where(url:params['url']).first - enable_search_flag = false - if page.respond_to?(:select_option_items) - ModuleApp.all.select{|tmp| tmp.key.to_s=='event_news_mod'}.each do |modile_app| - @show_option_items = modile_app.show_option_items rescue nil - end - page.select_option_items.each do |select_option_item| - if !(@show_option_items.nil?) && select_option_item.field_name == @show_option_items.keys[1].to_s - value = YAML.load(select_option_item.value) - if value[I18n.locale] == t('event_news.yes') - enable_search_flag = true - end - end - end - end +<% + params = OrbitHelper.params %> -<% if enable_search_flag %> +<% if @enable_search_flag %>
- <% cats = Array(page.categories) - if cats.include? 'all' - cats = ModuleApp.where(key: 'event_news').first.categories - else - cats = cats.map{|v| Category.where(id: v).first}.compact - end + <% all_cat = [[t('event_news.all'),'all']] %>
- <%= select_tag('category',options_for_select(all_cat.concat(cats.map{|v| [v.title,v.id.to_s]}),:selected => params['category'].to_s),:id=>"category_select_box",:prompt => t('event_news.select_prompt')) %> + <%= select_tag('category',options_for_select(all_cat.concat(@categories.map{|v| [v.title,v.id.to_s]}),:selected => params['category'].to_s),:id=>"category_select_box",:prompt => t('event_news.select_prompt')) %> " placeholder="<%= t('event_news.keywords') %>">
diff --git a/lib/event_news_mod/engine.rb b/lib/event_news_mod/engine.rb index 95e4f81..59ac3f4 100644 --- a/lib/event_news_mod/engine.rb +++ b/lib/event_news_mod/engine.rb @@ -19,17 +19,17 @@ module EventNewsMod key_item1 = {} key_item2 = {} key_item3 = {} + key_item4 = {} value_item1 = {} value_item2 = {} value_item3 = {} - value2_item1 = {} - value2_item2 = {} + value_item4 = {} key1_options = ['small_size','medium_size','orignal_size'] key2_options = ['not_enable_tabs','enable_tabs_with_categories_include_all','enable_tabs_with_categories'] key3_options = ['default','upper_left','lower_left','upper_right','lower_right'] key4_options = ['the_same_as_data_count','display_all_in_other_tabs'] #After fix I18n.load_path, translation can work there - key5_attr = ['no_','yes_'].map{|v| I18n.available_locales.map{|k| I18n.with_locale(k){[k,I18n.t(v)]}}.to_h} + yes_no_options = ['no_','yes_'].map{|v| I18n.available_locales.map{|k| I18n.with_locale(k){[k,I18n.t(v)]}}.to_h} key1_options.each_with_index do |k,i| key1_attr[i] = {} end @@ -68,17 +68,17 @@ module EventNewsMod value_item1[k] = v[0]['event_news']['not_show'] value_item2[k] = v[0]['event_news']['show_bottom'] value_item3[k] = v[0]['event_news']['show_top'] - value2_item1[k] = v[0]['event_news']['no'] - value2_item2[k] = v[0]['event_news']['yes'] end + key_item4 = key5 data[key1] = key1_attr data[key2] = key2_attr data[key3] = key3_attr data[key4] = key4_attr - data[key5] = key5_attr + data[key5] = yes_no_options data_item[key_item1] = [value_item1,value_item2,value_item3] - data_item[key_item2] = [value2_item1,value2_item2] + data_item[key_item2] = yes_no_options data_item[key_item3] = [value_item2,value_item3] + data_item[key_item4] = yes_no_options 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)