diff --git a/app/controllers/admin/announcements_controller.rb b/app/controllers/admin/announcements_controller.rb index fea6e28..cdabadb 100644 --- a/app/controllers/admin/announcements_controller.rb +++ b/app/controllers/admin/announcements_controller.rb @@ -102,6 +102,10 @@ class Admin::AnnouncementsController < OrbitAdminController def updatesettings setting = @announcement_setting + ids = params['announcement_setting']['anns_status_settings'].collect do |i,v| + v['_id'] + end.compact + AnnsStatusSetting.where(:id.nin=>ids).destroy setting.update_attributes(settings_params) setting.save redirect_to admin_announcement_settings_path @@ -144,6 +148,7 @@ class Admin::AnnouncementsController < OrbitAdminController @statuses = [] @bulletin = Bulletin.new @bulletin.email_sentdate = Time.now + @reach_limit = @bulletin.check_status_limit(current_user,true) end def create @@ -195,6 +200,7 @@ class Admin::AnnouncementsController < OrbitAdminController def edit if can_edit_or_delete?(@bulletin) + @reach_limit = @bulletin.check_status_limit(current_user,true) @tags = @module_app.tags @categories = @module_app.categories.enabled @statuses = [] @@ -223,6 +229,7 @@ class Admin::AnnouncementsController < OrbitAdminController end bulletin.update_attributes(bps) + bulletin.update_user_id = current_user.id if bulletin.rejected bulletin.reapproval = true bulletin.save @@ -231,7 +238,10 @@ class Admin::AnnouncementsController < OrbitAdminController bulletin.save end build_email(bulletin) - redirect_to params['referer_url'] + now_bulletin_page = Bulletin.where(:title.ne => "",:is_preview.in=>[false,nil]) + .order_by(sort).map(&:id).map.with_index.select{|v,i| v==bulletin.id}[0][1] rescue nil + now_bulletin_page = now_bulletin_page.nil? ? 0 : ((now_bulletin_page+1).to_f/10).ceil + redirect_to "/zh_tw/admin/announcements?page=#{now_bulletin_page}" end def destroy diff --git a/app/controllers/announcements_controller.rb b/app/controllers/announcements_controller.rb index 1797267..ea083f7 100644 --- a/app/controllers/announcements_controller.rb +++ b/app/controllers/announcements_controller.rb @@ -133,8 +133,9 @@ class AnnouncementsController < ApplicationController sorted_anns = sorted_anns.sample(widget_data_count) end else - sorted_anns = Bulletin.find(anns_cache.first.filter_result) - sorted_anns = sorted_anns.sort_by{|v| [(v.is_top==true ? 1 : 0),v['postdate']]}.reverse + sorted_ann_ids = anns_cache.first.filter_result + sorted_anns = Bulletin.find(sorted_ann_ids) + sorted_anns = sorted_ann_ids.map{|v1| sorted_anns.find{|v2| v2.id==v1}} end if (defined? SiteFeed).nil? || is_random anns = sorted_anns.map{|v| data_to_human_type(v)} diff --git a/app/helpers/announcements_helper.rb b/app/helpers/announcements_helper.rb index 2f00342..2cfe056 100644 --- a/app/helpers/announcements_helper.rb +++ b/app/helpers/announcements_helper.rb @@ -100,19 +100,19 @@ module AnnouncementsHelper end fans end - def filter_by_keywords(sorted,keywords,postdate) - sorted.select{|anns| - flag1 = true - if !keywords.nil? - if anns["source-site"].present? - title = Nokogiri::HTML(anns["title"].to_s).text - else - title = Nokogiri::HTML(anns.title.to_s).text - end - flag1 = title.include?(keywords.to_s) - end - flag1 - } + def filter_by_keywords(sorted,keywords) + if !keywords.nil? + sorted.select{|anns| + if anns["source-site"].present? + title = Nokogiri::HTML(anns["title"].to_s).text + else + title = Nokogiri::HTML(anns.title.to_s).text + end + title.include?(keywords.to_s) + } + else + sorted + end end def get_sorted_annc params = OrbitHelper.params @@ -145,9 +145,9 @@ module AnnouncementsHelper else all_sorted = feeds_anns.sort_by{|v| v["postdate"] } end - all_filter = filter_by_keywords(all_sorted,params[:keywords],params[:postdate]) + all_filter = filter_by_keywords(all_sorted,params[:keywords]) else - all_filter = filter_by_keywords(announcements,params[:keywords],params[:postdate]) + all_filter = filter_by_keywords(announcements,params[:keywords]) end if page_data_count != 0 sorted = all_filter[(page_number-1)*page_data_count...page_number*page_data_count] diff --git a/app/models/announcement_setting.rb b/app/models/announcement_setting.rb index 112c486..5e76839 100644 --- a/app/models/announcement_setting.rb +++ b/app/models/announcement_setting.rb @@ -6,7 +6,8 @@ class AnnouncementSetting field :pro_enabled, type: Boolean, :default => false field :approvers, type: Array, :default => [] field :email_to, type: Array, :default => ["admins","managers","approvers"] - + 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) limit = self.first.top_limit rescue 0 return true if limit == 0 diff --git a/app/models/anns_status_setting.rb b/app/models/anns_status_setting.rb new file mode 100644 index 0000000..75413a0 --- /dev/null +++ b/app/models/anns_status_setting.rb @@ -0,0 +1,7 @@ +class AnnsStatusSetting + include Mongoid::Document + field :role_id + field :status + field :top_limit + belongs_to :announcement_setting +end \ No newline at end of file diff --git a/app/models/bulletin.rb b/app/models/bulletin.rb index 77cbb89..82610ef 100644 --- a/app/models/bulletin.rb +++ b/app/models/bulletin.rb @@ -54,7 +54,42 @@ class Bulletin scope :is_approved, ->{where(:approved => true)} before_create :set_expire - + before_save :check_limit + def check_limit + check_status_limit(update_user) + end + def check_status_limit(user,check_only=false) + role_ids = user.member_profile.roles.map(&:id) rescue [] + status_settings = (role_ids.collect do |role_id| + AnnouncementSetting.first.anns_status_settings.select{|v| v.role_id.to_s == role_id.to_s} + end.flatten rescue []) + reach_limit = [] + if status_settings.count != 0 + reach_limit = status_settings.collect do |status_setting| + status = status_setting.status + if status_setting.top_limit.to_i <= Bulletin.where(:update_user_id.in => Role.find(status_setting.role_id).member_profiles.collect(&:user).flatten.uniq.map{|v| v.id},status => true).count + if !check_only + if self[status] && !Bulletin.where(id:self.id).first[status] + self[status] = false + nil + end + else + status + end + else + nil + end + end.compact + reach_limit = reach_limit.group_by{|v| v}.collect do |k,v| + if v.count >= user.member_profile.roles.count + k + else + nil + end + end.compact + end + reach_limit + end def slug_title doc = Nokogiri::HTML(self.title) title = doc.text.gsub('/','-') diff --git a/app/views/admin/announcements/_form.html.erb b/app/views/admin/announcements/_form.html.erb index 32f79f2..402cfba 100644 --- a/app/views/admin/announcements/_form.html.erb +++ b/app/views/admin/announcements/_form.html.erb @@ -3,6 +3,17 @@ <%= stylesheet_link_tag "lib/fileupload" %> <%= stylesheet_link_tag "lib/main-list" %> <% end %> + <% content_for :page_specific_javascript do %> <%= javascript_include_tag "lib/bootstrap-fileupload" %> <%= javascript_include_tag "lib/bootstrap-datetimepicker" %> @@ -87,17 +98,28 @@