From 6a5712500d05ea13a0300eca7e28b1c2c17c86aa Mon Sep 17 00:00:00 2001 From: chiu Date: Mon, 2 Mar 2020 18:09:55 +0800 Subject: [PATCH] fix some error and add new feature to let user can set role's is_top limit and is_hot limit --- .../admin/announcements_controller.rb | 12 +- app/controllers/announcements_controller.rb | 5 +- app/helpers/announcements_helper.rb | 30 +- app/models/announcement_setting.rb | 3 +- app/models/anns_status_setting.rb | 7 + app/models/bulletin.rb | 37 ++- app/views/admin/announcements/_form.html.erb | 36 ++- app/views/admin/announcements/_index.html.erb | 2 +- .../admin/announcements/settings.html.erb | 304 ++++++++++++------ 9 files changed, 304 insertions(+), 132 deletions(-) create mode 100644 app/models/anns_status_setting.rb 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 @@
-
- - + <% if !(@reach_limit.include?('is_top') && @bulletin.is_top != true) || current_user.is_admin? %> + + <% else %> + + <% end %> + <% if !(@reach_limit.include?('is_hot') && @bulletin.is_hot != true) || current_user.is_admin? %> + + <% else %> + + <% end %> diff --git a/app/views/admin/announcements/_index.html.erb b/app/views/admin/announcements/_index.html.erb index b354fec..753ea39 100644 --- a/app/views/admin/announcements/_index.html.erb +++ b/app/views/admin/announcements/_index.html.erb @@ -24,7 +24,7 @@ <% if b.expired? || (b.category.disable rescue false)%> - <%= b.title %> + <%= b.title.html_safe %> <% else %> <%= b.title.html_safe %> <% end %> diff --git a/app/views/admin/announcements/settings.html.erb b/app/views/admin/announcements/settings.html.erb index 43db706..cbe030c 100644 --- a/app/views/admin/announcements/settings.html.erb +++ b/app/views/admin/announcements/settings.html.erb @@ -1,94 +1,203 @@ <% - sub_managers = @module_app.sub_managers - sub_managers.delete(nil) + sub_managers = @module_app.sub_managers + sub_managers.delete(nil) + all_statuses = [[t('top'),'is_top'],[t('hot'),'is_hot']] + tp1 = select_tag("announcement_setting[anns_status_settings][-1][status]",options_for_select(all_statuses)) + tp2 = select_tag("announcement_setting[anns_status_settings][-1][role_id]",options_for_select(Role.all.map{|r| [r.title,r.id]})) + tp3 = number_field_tag("announcement_setting[anns_status_settings][-1][top_limit]",nil, min: 0,required: true) + tp4 = "" + all_tp = "
#{tp1}
#{tp2}
#{tp3}
#{tp4}
" %> +
<%= t("announcement.click_on_submit") %>
<%= form_for @setting, url: (@setting.new_record? ? admin_announcement_createsettings_path : admin_announcement_updatesettings_path), html: {class: "form-horizontal main-forms"} do |f| %>
-
+
<%= f.label :top_limit, t("announcement.top_limit"), :class => "control-label muted" %>
<%= f.number_field :top_limit, :min => "0" %> <%= t("announcement.for_unlimited") %>
+
+
+
+
+ <%= t('status') %> +
+
+ <%= t('role') %> +
+
+ <%= t('announcement.top_limit') %> +
+
+ <% (AnnouncementSetting.first.anns_status_settings rescue []).each_with_index do |v,i| %> +
+ <%= hidden_field_tag("announcement_setting[anns_status_settings][#{i}][_id]",v.id) %> +
+ <%= select_tag("announcement_setting[anns_status_settings][#{i}][status]",options_for_select(all_statuses,:selected => v['status'])) %> +
+
+ <%= select_tag("announcement_setting[anns_status_settings][#{i}][role_id]",options_for_select(Role.all.map{|r| [r.title,r.id]},:selected => v['role_id'])) %> +
+
+ <%= number_field_tag("announcement_setting[anns_status_settings][#{i}][top_limit]",v['top_limit'], min: 0,required: true) %> +
+
+ +
+
+ <% end %> +
+
+ +
+
+
+
<% if AnnouncementSetting.is_pro? %> - <% if !sub_managers.blank? %> -
- <%= f.label "Approver Setting", :class => "control-label muted" %> -
- <%= t("announcement.approvers_list") %> - <%= @setting.approvers.count %> -
-
- <% else %> - - <% end %> -
- <%= f.label "Send emails to", :class => "control-label muted" %> -
- > <%= t("admin") %> - > <%= t("manager") %> - > <%= t("announcement.approver") %> -
-
+ <% if !sub_managers.blank? %> +
+ <%= f.label "Approver Setting", :class => "control-label muted" %> +
+ <%= t("announcement.approvers_list") %> + <%= @setting.approvers.count %> +
+
+ <% else %> + + <% end %> +
+ <%= f.label "Send emails to", :class => "control-label muted" %> +
+ > <%= t("admin") %> + > <%= t("manager") %> + > <%= t("announcement.approver") %> +
+
- <% sub_managers.each do |sm| %> - <% if @setting.approvers.include?(sm.id.to_s) %> - - <% end %> - <% end %> + <% sub_managers.each do |sm| %> + <% if @setting.approvers.include?(sm.id.to_s) %> + + <% end %> + <% end %>
<% end %>
@@ -102,41 +211,28 @@

<%= t("sub_manager") %>

<% end %>