diff --git a/announcement.gemspec b/announcement.gemspec index 24c77d8..957d0c8 100644 --- a/announcement.gemspec +++ b/announcement.gemspec @@ -1,6 +1,6 @@ # encoding: UTF-8 $:.push File.expand_path("../lib", __FILE__) - +$:.push File.expand_path("../app/models", __FILE__) # Maintain your gem's version: require "announcement/version" require 'json' @@ -293,7 +293,10 @@ Gem::Specification.new do |s| s.summary = "Announcements for Orbit" s.description = "Announcements for Orbit" s.license = "MIT" - + s.metadata = { + "_require" => "announcement_setting", + "global_hash" => "{enable_manually_sort: (AnnouncementSetting.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/app/controllers/admin/announcements_controller.rb b/app/controllers/admin/announcements_controller.rb index 7280693..4ab867b 100644 --- a/app/controllers/admin/announcements_controller.rb +++ b/app/controllers/admin/announcements_controller.rb @@ -13,6 +13,32 @@ class Admin::AnnouncementsController < OrbitAdminController super @app_title = "announcement" end + + def update_sort + ids = params[:ids] + ids.each_with_index do |id,i| + Bulletin.where(id: id).update(sort_number: i) + end + edit_sort + render 'update_sort',layout: false + + end + + def update_sort_setting + setting = @announcement_setting + setting.update_attributes(settings_params) + if defined?(OrbitHelper::SharedHash) + OrbitHelper::SharedHash['announcement'][:enable_manually_sort] = setting.enable_manually_sort + end + redirect_to edit_sort_admin_announcement_path + end + + def edit_sort + @setting = AnnouncementSetting.first + @announcements = Bulletin.where(is_top: true).order_by({is_top: -1,sort_number: 1,postdate: -1, _id: -1}) + @table_fields = ['announcement.table.sort_number','announcement.table.title','announcement.default_widget.postdate'] + end + def comment_hidden b = BulletinComment.find(params[:id]) rescue nil if !b.nil? diff --git a/app/controllers/announcements_controller.rb b/app/controllers/announcements_controller.rb index 8da40ad..f828850 100644 --- a/app/controllers/announcements_controller.rb +++ b/app/controllers/announcements_controller.rb @@ -1,5 +1,20 @@ class AnnouncementsController < ApplicationController include AnnouncementsHelper + def initialize + super + @app_title = 'announcement' + @manually_sort = manually_sort + Bulletin.instance_variable_set('@manually_sort',@manually_sort) + #self.request = OrbitHelper.request + end + + def manually_sort + if defined?(OrbitHelper::SharedHash) + OrbitHelper::SharedHash['announcement'][:enable_manually_sort] + else + AnnouncementSetting.first.enable_manually_sort rescue false + end + end def comment @bulletin = Bulletin.where(:uid=>params[:uid]).first comment_val = params['comment'] @@ -207,7 +222,11 @@ class AnnouncementsController < ApplicationController else cats = ["all"] + cats end - anns = anns.sort_by { |a| [ (a["is_top"] ? 1 : 0) , a["postdate"].blank? ? nil : a["postdate"].to_time] }.reverse + 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 end cats = cats.uniq tags = tags.uniq @@ -559,7 +578,11 @@ class AnnouncementsController < ApplicationController 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} - top_anns = top_anns.sort_by { |a| tmp=a["postdate"].blank?;[tmp ? 0 : 1, tmp ? nil : a["postdate"].to_time] }.reverse + 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 rest_count = widget_data_count - top_anns.count if rest_count <= 0 anns = top_anns.take(widget_data_count) diff --git a/app/helpers/announcements_helper.rb b/app/helpers/announcements_helper.rb index 2f60a21..d5140da 100644 --- a/app/helpers/announcements_helper.rb +++ b/app/helpers/announcements_helper.rb @@ -48,6 +48,7 @@ module AnnouncementsHelper "is_top" => a.is_top, "bulletin_links" => links, "bulletin_files" => files, + "sort_number" => a.sort_number, "title" => a.title, "source-site" => "", "source-site-title" => "", @@ -194,6 +195,7 @@ module AnnouncementsHelper .can_display_and_sorted.is_approved .filter_by_categories(categories,false).filter_by_tags(tags).to_a end + print('announcements',announcements.collect(&:title)) if !(defined? SiteFeed).nil? if @type != "show_widget" feeds_anns = get_feed_announcements("index",nil,categories,page_number*page_data_count) @@ -214,7 +216,17 @@ module AnnouncementsHelper 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 - all_sorted = top_anns.sort_by { |a| tmp=a["postdate"].blank?;[tmp ? 0 : 1, tmp ? nil : a["postdate"].to_time] }.reverse + rest_anns + 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 diff --git a/app/models/announcement_setting.rb b/app/models/announcement_setting.rb index f2d4bd3..0cceb59 100644 --- a/app/models/announcement_setting.rb +++ b/app/models/announcement_setting.rb @@ -10,9 +10,10 @@ class AnnouncementSetting field :email_to, type: Array, :default => ["admins","managers","approvers"] field :is_display_edit_only, type: Boolean, :default => false field :only_manager_can_edit_status, type: Boolean, :default => false - field :top_text , type: String , localize: true - field :hot_text , type: String , localize: true - field :hidden_text , type: String , localize: true + field :top_text , type: String , localize: true + field :hot_text , type: String , localize: true + field :hidden_text , type: String , localize: true + field :enable_manually_sort, type: Boolean, default: false has_many :anns_status_settings, :autosave => true, :dependent => :destroy accepts_nested_attributes_for :anns_status_settings, :allow_destroy => true def self.check_limit_for_user(user_id, b_id = nil) @@ -31,4 +32,12 @@ class AnnouncementSetting self.sub_annc_title_trans.blank? ? I18n.t("announcement.table.title") : self.sub_annc_title_trans end end + + before_save do + if self.enable_manually_sort_changed? && self.enable_manually_sort + AnnsCache.all.delete + Bulletin.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 }) + Bulletin.create_indexes + end + end end \ No newline at end of file diff --git a/app/models/bulletin.rb b/app/models/bulletin.rb index af4886c..eb3328f 100644 --- a/app/models/bulletin.rb +++ b/app/models/bulletin.rb @@ -100,7 +100,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, ->{any_of({:postdate.lte=>Time.now, :deadline.gte=>Time.now},{:postdate.lte=>Time.now, :deadline=>nil},{:postdate=>nil,:deadline.gte=>Time.now},{:postdate=>nil,:deadline=>nil}).order(is_top: :desc,postdate: :desc,id: :desc)} + scope :valid_time_range, ->{any_of({:postdate.lte=>Time.now, :deadline.gte=>Time.now},{:postdate.lte=>Time.now, :deadline=>nil},{:postdate=>nil,:deadline.gte=>Time.now},{:postdate=>nil,: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)} @@ -109,6 +109,7 @@ class Bulletin index({postdate: 1}, { unique: false, background: true }) index({is_top: -1,postdate: -1, _id: -1}, { unique: false, background: true }) index({approved: -1,is_hidden: 1,is_preview: 1, is_top: -1,postdate: -1,_id: -1,deadline: -1}, { unique: false, background: true }) + field :sort_number, type: Integer def get_org_model if self.is_preview org_model = nil diff --git a/app/views/admin/announcements/_edit_sort.html.erb b/app/views/admin/announcements/_edit_sort.html.erb new file mode 100644 index 0000000..2bc9766 --- /dev/null +++ b/app/views/admin/announcements/_edit_sort.html.erb @@ -0,0 +1,35 @@ +
+ + + + <% @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 %> + + + + <% @announcements.each do |annc| %> + + + + + + <% end %> + +
<%= th_data.html_safe %>
+ <%= number_field_tag nil,annc.sort_number,class: 'sort_number',step: 1 %> + + <%= annc.title %> + + <%= format_value annc.postdate %> +
+
\ No newline at end of file diff --git a/app/views/admin/announcements/edit_sort.html.erb b/app/views/admin/announcements/edit_sort.html.erb new file mode 100644 index 0000000..2764a76 --- /dev/null +++ b/app/views/admin/announcements/edit_sort.html.erb @@ -0,0 +1,117 @@ +<% 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_announcement_path, html: {class: "form-horizontal main-forms"}, method: 'post' do |f| %> +
+
+ <%= f.label :maull, t("announcement.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/announcements/update_sort.html.erb b/app/views/admin/announcements/update_sort.html.erb new file mode 100644 index 0000000..79513a3 --- /dev/null +++ b/app/views/admin/announcements/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 9130a34..5f9f049 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -3,6 +3,9 @@ en: feed: Feed import: Import announcement: + manually_sort: Manually Sort + enable_manually_sort: Enable Manually Sort + manual_update_sort: Manually Update Sort carousel_image_types: default: Default carousel: Carousel diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index ca4f096..0caca81 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -4,6 +4,9 @@ zh_tw: import: 匯入 get_all_anncs_without_subannc: "選擇相關公告" announcement: + manually_sort: 手動排序 + enable_manually_sort: 開啟手動排序 + manual_update_sort: 手動更新排序 carousel_image_types: default: 預設 carousel: 輪播 diff --git a/config/routes.rb b/config/routes.rb index 8db2b20..bbfbbb9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -35,21 +35,27 @@ Rails.application.routes.draw do resources :announcements get 'announcements/:id/comment'=> 'announcements#comment' get 'annc-comment-hidden/:id' => 'announcements#comment_hidden' - end - - resources :announcements do - collection do - get ':slug_title-:uid', to: 'announcements#show', as: :display + resource :announcement,:only => [] do + collection do + get "edit_sort" + post "update_sort_setting", to: 'announcements#update_sort_setting' + post "update_sort", to: 'announcements#update_sort' + end + end end - end - get "/xhr/announcements/feed/:uid" => "announcement_feeds#feed" - get "/xhr/announcements/rssfeed/:uid" => "announcement_feeds#rssfeed" - get "/xhr/announcements/feeds" => "announcement_feeds#feeds" - get '/xhr/announcements/announcement.json', to: 'bulletins#get_bulletins' - get '/xhr/panel/announcement/widget/sync_data' => 'announcements#show_widget' - get '/xhr/announcements/:slug_title-:uid/comment', to: 'announcements#comment' - get '/xhr/announcements/file/:id/*f_name', to: 'announcements#get_file', format: false + resources :announcements do + collection do + get ':slug_title-:uid', to: 'announcements#show', as: :display + end + end + + get "/xhr/announcements/feed/:uid" => "announcement_feeds#feed" + get "/xhr/announcements/rssfeed/:uid" => "announcement_feeds#rssfeed" + get "/xhr/announcements/feeds" => "announcement_feeds#feeds" + get '/xhr/announcements/announcement.json', to: 'bulletins#get_bulletins' + get '/xhr/panel/announcement/widget/sync_data' => 'announcements#show_widget' + get '/xhr/announcements/:slug_title-:uid/comment', to: 'announcements#comment' + get '/xhr/announcements/file/:id/*f_name', to: 'announcements#get_file', format: false end - end diff --git a/lib/announcement/engine.rb b/lib/announcement/engine.rb index b8b0f87..a3f7b16 100644 --- a/lib/announcement/engine.rb +++ b/lib/announcement/engine.rb @@ -75,7 +75,7 @@ module Announcement if ENV['worker_num']=='0' && File.basename($0) != 'rake' && !Rails.const_defined?('Console') require File.expand_path('../../../app/models/anns_cache', __FILE__) if defined?(AnnsCache) - AnnsCache.destroy_all + AnnsCache.all.delete end end rescue => e @@ -109,38 +109,43 @@ module Announcement :priority=>1, :active_for_action=>{'admin/announcements'=>'index'}, :available_for => 'users' + context_link 'announcement.manually_sort', + :link_path=>"edit_sort_admin_announcement_path" , + :priority=>2, + :active_for_action=>{'admin/announcement'=>'edit_sort'}, + :available_for => 'sub_managers' context_link 'new_', :link_path=>"new_admin_announcement_path" , - :priority=>2, + :priority=>3, :active_for_action=>{'admin/announcements'=>'new'}, :available_for => 'sub_managers' context_link 'categories', :link_path=>"admin_module_app_categories_path" , :link_arg=>"{:module_app_id=>ModuleApp.find_by(:key=>'announcement').id}", - :priority=>3, + :priority=>4, :active_for_action=>{'admin/announcements'=>'categories'}, :active_for_category => 'Announcement', :available_for => 'managers' context_link 'tags', :link_path=>"admin_module_app_tags_path" , :link_arg=>"{:module_app_id=>ModuleApp.find_by(:key=>'announcement').id}", - :priority=>4, + :priority=>5, :active_for_action=>{'admin/announcements'=>'tags'}, :active_for_tag => 'Announcement', :available_for => 'managers' context_link 'announcement.feed_list', :link_path=>"admin_announcement_feed_path" , - :priority=>5, + :priority=>6, :active_for_action=>{'admin/announcements'=>'feed'}, :available_for => 'managers' context_link 'announcement.import', :link_path=>"admin_announcement_import_path" , - :priority=>6, + :priority=>7, :active_for_action=>{'admin/announcements'=>'import'}, :available_for => 'managers' context_link 'announcement.settings', :link_path=>"admin_announcement_settings_path" , - :priority=>6, + :priority=>8, :active_for_action=>{'admin/announcements'=>'settings'}, :available_for => 'managers' end