diff --git a/app/controllers/admin/event_news_controller.rb b/app/controllers/admin/event_news_controller.rb index 1a42086..19b87c1 100644 --- a/app/controllers/admin/event_news_controller.rb +++ b/app/controllers/admin/event_news_controller.rb @@ -10,6 +10,31 @@ class Admin::EventNewsController < OrbitAdminController super @app_title = "event_news_mod" end + def update_sort + ids = params[:ids] + ids.each_with_index do |id,i| + EventNews.where(id: id).update(sort_number: i) + end + EventNewsCache.all.delete + edit_sort + render 'update_sort',layout: false + + end + + def update_sort_setting + setting = @event_news_setting + setting.update_attributes(settings_params) + if defined?(OrbitHelper::SharedHash) && OrbitHelper::SharedHash + OrbitHelper::SharedHash['event_news_mod'][:enable_manually_sort] = setting.enable_manually_sort + end + redirect_to edit_sort_admin_event_news_index_path + end + + def edit_sort + @setting = @event_news_setting + @event_news = EventNews.where(is_top: true).order_by({is_top: -1,sort_number: 1,postdate: -1, _id: -1}) + @table_fields = ['event_news.table.sort_number','event_news.table.title','event_news.event_date','event_news.start_date'] + end def index EventNews.remove_expired_status diff --git a/app/controllers/event_news_mods_controller.rb b/app/controllers/event_news_mods_controller.rb index 7e056c7..1afb3ad 100644 --- a/app/controllers/event_news_mods_controller.rb +++ b/app/controllers/event_news_mods_controller.rb @@ -1,5 +1,19 @@ class EventNewsModsController < ApplicationController include EventNewsHelper + def initialize + super + @app_title = 'event_news_mod' + @manually_sort = manually_sort + EventNews.instance_variable_set('@manually_sort',@manually_sort) + #self.request = OrbitHelper.request + end + def manually_sort + if defined?(OrbitHelper::SharedHash) && OrbitHelper::SharedHash + OrbitHelper::SharedHash['event_news_mod'][:enable_manually_sort] rescue EventNewsSetting.first.enable_manually_sort + else + EventNewsSetting.first.enable_manually_sort rescue false + end + end def index EventNews.remove_expired_status sorted,total_pages = get_sorted_event_news diff --git a/app/models/event_news.rb b/app/models/event_news.rb index 9e50f4c..4237bcb 100644 --- a/app/models/event_news.rb +++ b/app/models/event_news.rb @@ -88,16 +88,32 @@ class EventNews before_destroy :destroy_email 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, event_date: :desc,postdate: :desc,id: :desc)} - scope :can_display_and_sorted_according_today, ->{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, event_date: :asc,postdate: :desc,id: :desc).where(:event_date.gte => Date.today.to_time)} + scope :can_display_and_sorted, ->{where(:is_hidden.ne=>true,:is_preview.ne => true).valid_time_range.order(event_date: :desc)} + scope :can_display_and_sorted_according_today, ->{where(:is_hidden.ne=>true,:is_preview.ne => true).order(event_date: :asc).valid_time_range.where(:event_date.gte => Date.today.to_time)} + 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 :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 + before_save do + if @is_hidden_changed.nil? || @is_hidden_changed != true + @is_hidden_changed = self.is_hidden_changed? + end + if self.is_top_changed? && !self.is_top + self.sort_number = nil + end + end index({postdate: 1}, { unique: false, background: true }) index({is_top: -1, postdate: -1, event_date: -1, _id: -1}, { unique: false, background: true }) index({is_top: -1, event_date: 1, postdate: -1, _id: -1}, { unique: false, background: true }) + index({approved: -1, is_hidden: 1, is_preview: 1, is_top: -1, postdate: -1, event_date: -1, _id: -1, deadline: -1}, { unique: false, background: true }) + index({approved: -1, is_hidden: 1, is_preview: 1, is_top: -1, event_date: 1, postdate: -1, event_date: -1, _id: -1, deadline: -1}, { unique: false, background: true }) + if EventNewsSetting.pluck(:enable_manually_sort).first == true + index({approved: -1, is_hidden: 1, is_preview: 1, is_top: -1, sort_number: 1, postdate: -1, event_date: -1, _id: -1, deadline: -1}, { unique: false, background: true }) + index({approved: -1, is_hidden: 1, is_preview: 1, is_top: -1, sort_number: 1, event_date: 1, postdate: -1, _id: -1, deadline: -1}, { unique: false, background: true }) + end + field :sort_number, type: Integer def get_org_model if self.is_preview org_model = nil diff --git a/app/models/event_news_setting.rb b/app/models/event_news_setting.rb index c92fd71..2aa8002 100644 --- a/app/models/event_news_setting.rb +++ b/app/models/event_news_setting.rb @@ -11,11 +11,32 @@ class EventNewsSetting field :including_day_of_the_week, type: Boolean, default: true field :including_time, type: Boolean, default: true field :hour_clock_24, type: Boolean, default: true + field :enable_manually_sort, type: Boolean, default: false has_many :event_news_status_settings, :autosave => true, :dependent => :destroy accepts_nested_attributes_for :event_news_status_settings, :allow_destroy => true - after_save do + + before_save do + can_update_shared_hash = (defined?(OrbitHelper::SharedHash) && OrbitHelper::SharedHash) if self.including_day_of_the_week_changed? || self.including_time_changed? || self.hour_clock_24_changed? - EventNewsCache.all.destroy + @need_update_event_news = true + end + if self.enable_manually_sort_changed? && self.enable_manually_sort + EventNews.index({approved: -1,is_hidden: 1,is_preview: 1, is_top: -1,sort_number: 1,postdate: -1,_id: -1,deadline: -1}, { unique: false, background: true }) + EventNews.create_indexes + if can_update_shared_hash + OrbitHelper::SharedMutex.synchronize do + OrbitHelper::SharedHash["event_news_mod"][:enable_manually_sort] = self.enable_manually_sort + end + end + @need_update_event_news = true + end + end + after_save do + if @need_update_event_news + @need_update_event_news = false + Thread.new do + EventNewsCache.all.delete + end end end def self.check_limit_for_user(user_id, b_id = nil) diff --git a/app/views/admin/event_news/_edit_sort.html.erb b/app/views/admin/event_news/_edit_sort.html.erb new file mode 100644 index 0000000..cc07acc --- /dev/null +++ b/app/views/admin/event_news/_edit_sort.html.erb @@ -0,0 +1,38 @@ +
<%= th_data.html_safe %> | + <% end %> +|||
---|---|---|---|
+ <%= number_field_tag nil,ev.sort_number,class: 'sort_number',step: 1 %> + | ++ <%= ev.title.html_safe %> + | ++ <%= format_value ev.event_date %> + | ++ <%= format_value ev.postdate %> + | +