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 @@ +
+ + + + <% @table_fields.each do |field| %> + <% + field_text = field.to_s.include?('.') ? t(field.to_s) : field.to_s + sort = field.to_s.split('.')[-1] + active = params[:sort].eql? sort + order = active ? (["asc", "desc"]-[params[:order]]).first : "asc" + arrow = (order.eql? "desc") ? "" : "" + klass = sort.eql?(:title) ? "span5" : "span2" + th_data = "#{field_text} #{active ? arrow : ""}" + %> + + <% end %> + + + + <% @event_news.each do |ev| %> + + + + + + + <% end %> + +
<%= th_data.html_safe %>
+ <%= 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 %> +
+
\ No newline at end of file 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..248eceb --- /dev/null +++ b/app/views/admin/event_news/edit_sort.html.erb @@ -0,0 +1,86 @@ +<% content_for :page_specific_css do %> + <%= stylesheet_link_tag "lib/main-forms" %> + <%= stylesheet_link_tag "lib/fileupload" %> + <%= stylesheet_link_tag "lib/main-list" %> +<% end %> +<%= form_for @setting, url: update_sort_setting_admin_event_news_index_path, html: {class: "form-horizontal main-forms"}, method: 'post' do |f| %> +
+
+ <%= f.label :maull, t("event_news.enable_manually_sort"), :class => "control-label muted" %> +
+ <%= f.check_box :enable_manually_sort,:id=>'enable_manually_sort' %> +
+
+
+<% end %> +
+ +
+<%= render partial: 'edit_sort' %> + \ No newline at end of file diff --git a/app/views/admin/event_news/update_sort.html.erb b/app/views/admin/event_news/update_sort.html.erb new file mode 100644 index 0000000..79513a3 --- /dev/null +++ b/app/views/admin/event_news/update_sort.html.erb @@ -0,0 +1 @@ +<%= render partial: 'edit_sort' %> \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index d654b10..997ec1d 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -4,6 +4,9 @@ en: event_news_mod: event_news_mod: Event News event_news: + manually_sort: Manually Sort + enable_manually_sort: Enable Manually Sort + manual_update_sort: Manually Update Sort category: Category event_date_setting: "Event date setting" event_date_use_default_setting: "Event date use default setting" @@ -78,6 +81,7 @@ en: file: File view_count: View Count department: Department + sort_number: Sort Number add_new: Add New export_to_excel: Export to Excel export_all_anns: Export all Event News diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index 0faf9b3..bffacff 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -4,6 +4,9 @@ zh_tw: event_news_mod: event_news_mod: 活動公告 event_news: + manually_sort: 手動排序 + enable_manually_sort: 開啟手動排序 + manual_update_sort: 手動更新排序 category: 類別 event_date_setting: "事件日期設定" event_date_use_default_setting: "使用預設的事件日期設定" @@ -78,6 +81,7 @@ zh_tw: file: 檔案下載 view_count: 瀏覽人次 department: 單位 + sort_number: 排序值 add_new: 新建 import: 匯入 export_to_excel: 匯出至Excel檔 diff --git a/config/routes.rb b/config/routes.rb index 0b8ffe2..fb50bb0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -30,6 +30,9 @@ Rails.application.routes.draw do collection do get 'custom_fields_title' post 'update_custom_title' + get "edit_sort" + post "update_sort_setting", to: 'event_news#update_sort_setting' + post "update_sort", to: 'event_news#update_sort' end end end diff --git a/event_news_mod.gemspec b/event_news_mod.gemspec index 22ec4cb..792c54a 100644 --- a/event_news_mod.gemspec +++ b/event_news_mod.gemspec @@ -153,6 +153,10 @@ Gem::Specification.new do |s| s.summary = "EventNews for Orbit" s.description = "EventNews for Orbit" s.license = "MIT" + s.metadata = { + "_require" => "#{File.expand_path("../app/models/event_news_setting", __FILE__)}", + "global_hash" => "{enable_manually_sort: (EventNewsSetting.first.enable_manually_sort rescue false)}" + } s.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.rdoc"] s.test_files = Dir["test/**/*"] diff --git a/lib/event_news_mod/engine.rb b/lib/event_news_mod/engine.rb index 751acd8..95e4f81 100644 --- a/lib/event_news_mod/engine.rb +++ b/lib/event_news_mod/engine.rb @@ -144,6 +144,11 @@ module EventNewsMod :priority=>1, :active_for_action=>{'admin/event_news'=>'index'}, :available_for => 'users' + context_link 'event_news.manually_sort', + :link_path=>"edit_sort_admin_event_news_index_path" , + :priority=>2, + :active_for_action=>{'admin/event_news'=>'edit_sort'}, + :available_for => 'managers' context_link 'new_', :link_path=>"new_admin_event_news_path" , :priority=>2,