diff --git a/app/controllers/admin/announcements_controller.rb b/app/controllers/admin/announcements_controller.rb index 41ddc11..737677e 100644 --- a/app/controllers/admin/announcements_controller.rb +++ b/app/controllers/admin/announcements_controller.rb @@ -11,6 +11,7 @@ class Admin::AnnouncementsController < OrbitAdminController end def index + Bulletin.remove_expired_status @tags = @module_app.tags @categories = @module_app.categories.enabled @filter_fields = filter_fields(@categories, @tags) @@ -35,6 +36,26 @@ class Admin::AnnouncementsController < OrbitAdminController end + def settings + @setting = AnnouncementSetting.first rescue nil + if @setting.nil? + @setting = AnnouncementSetting.new + end + end + + def createsettings + setting = AnnouncementSetting.new(settings_params) + setting.save + redirect_to admin_announcement_settings_path + end + + def updatesettings + setting = AnnouncementSetting.first + setting.update_attributes(settings_params) + setting.save + redirect_to admin_announcement_settings_path + end + def feedform if params[:type] == "new" @announcement_feed = BulletinFeed.new @@ -75,18 +96,24 @@ class Admin::AnnouncementsController < OrbitAdminController end def create - if !bulletin_params['bulletin_links_attributes'].nil? - bulletin_params['bulletin_links_attributes'].each do |idx,link| - bulletin_params['bulletin_links_attributes'].delete(idx.to_s) if link['url'].blank? + bps = bulletin_params + if !bps['bulletin_links_attributes'].nil? + bps['bulletin_links_attributes'].each do |idx,link| + bps['bulletin_links_attributes'].delete(idx.to_s) if link['url'].blank? end end - - bulletin = Bulletin.new(bulletin_params) + if bps[:is_top] == "1" && !AnnouncementSetting.check_limit_for_user(current_user.id) + bps[:is_top] = "0" + bps[:top_end_date] = nil + end + + bulletin = Bulletin.new(bps) bulletin.create_user_id = current_user.id bulletin.update_user_id = current_user.id # if user_can_approve? bulletin.approved = true # end + bulletin.save build_email(bulletin) redirect_to params['referer_url'] @@ -114,16 +141,22 @@ class Admin::AnnouncementsController < OrbitAdminController def update uid = params[:id].split('-').last bulletin = Bulletin.find_by(:uid=>uid) - bulletin_params[:tags] = bulletin_params[:tags].blank? ? [] : bulletin_params[:tags] - bulletin_params[:email_member_ids] = bulletin_params[:email_member_ids].blank? ? [] : bulletin_params[:email_member_ids] + bps = bulletin_params + bps[:tags] = bps[:tags].blank? ? [] : bps[:tags] + bps[:email_member_ids] = bps[:email_member_ids].blank? ? [] : bps[:email_member_ids] - if !bulletin_params['bulletin_links_attributes'].nil? - bulletin_params['bulletin_links_attributes'].each do |idx,link| - bulletin_params['bulletin_links_attributes'].delete(idx.to_s) if link['url'].blank? + if !bps['bulletin_links_attributes'].nil? + bps['bulletin_links_attributes'].each do |idx,link| + bps['bulletin_links_attributes'].delete(idx.to_s) if link['url'].blank? end end - bulletin.update_attributes(bulletin_params) + if bps[:is_top] == "1" && !AnnouncementSetting.check_limit_for_user(bulletin.create_user_id, bulletin.id) + bps[:is_top] = "0" + bps[:top_end_date] = nil + end + + bulletin.update_attributes(bps) bulletin.save build_email(bulletin) redirect_to params['referer_url'] @@ -261,4 +294,8 @@ class Admin::AnnouncementsController < OrbitAdminController def feed_params params.require(:bulletin_feed).permit! end + + def settings_params + params.require(:announcement_setting).permit! + end end diff --git a/app/controllers/announcements_controller.rb b/app/controllers/announcements_controller.rb index 64fb224..b63fb61 100644 --- a/app/controllers/announcements_controller.rb +++ b/app/controllers/announcements_controller.rb @@ -1,6 +1,7 @@ class AnnouncementsController < ApplicationController def index + Bulletin.remove_expired_status params = OrbitHelper.params feeds_anns = [] if !params['tags'].blank? @@ -109,6 +110,7 @@ class AnnouncementsController < ApplicationController end def widget + Bulletin.remove_expired_status uid = OrbitHelper.params[:uid] rescue "" tags = OrbitHelper.widget_tags announcements = Bulletin.where(:title.ne => "",:is_preview.in=>[false,nil],:uid.ne => uid).and(:title.ne => nil).can_display.is_approved.filter_by_widget_categories.filter_by_tags(tags) @@ -279,4 +281,5 @@ class AnnouncementsController < ApplicationController end end + end diff --git a/app/models/announcement_setting.rb b/app/models/announcement_setting.rb new file mode 100644 index 0000000..59b7e08 --- /dev/null +++ b/app/models/announcement_setting.rb @@ -0,0 +1,13 @@ +class AnnouncementSetting + include Mongoid::Document + include Mongoid::Timestamps + + field :top_limit, type: Integer, :default => 0 + + def self.check_limit_for_user(user_id, b_id = nil) + limit = self.first.top_limit + return true if limit == 0 + count = Bulletin.where(:is_top => true, :create_user_id => user_id, :id.ne => b_id).count + return count < limit + end +end \ No newline at end of file diff --git a/app/models/bulletin.rb b/app/models/bulletin.rb index fcfee99..cbabf3f 100644 --- a/app/models/bulletin.rb +++ b/app/models/bulletin.rb @@ -28,6 +28,7 @@ class Bulletin field :email_member_ids field :other_mailaddress field :image_description, localize: true + field :top_end_date, :type => DateTime mount_uploader :image, ImageUploader @@ -43,6 +44,7 @@ class Bulletin scope :is_approved, ->{where(:approved => true)} before_create :set_expire + def set_expire self.expirable_created_at = Time.now if self.is_preview return true @@ -79,4 +81,12 @@ class Bulletin mail.destroy if !mail.nil? end + def self.remove_expired_status + self.where(:is_top => true, :top_end_date.ne => nil, :top_end_date.lt => Time.now).each do |b| + b.is_top = false + b.top_end_date = nil + b.save + end + end + end diff --git a/app/views/admin/announcements/_form.html.erb b/app/views/admin/announcements/_form.html.erb index b52b3a7..7bd7fd2 100644 --- a/app/views/admin/announcements/_form.html.erb +++ b/app/views/admin/announcements/_form.html.erb @@ -61,7 +61,7 @@