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 @@
-
+
+ <% if !@bulletin.is_top? && !AnnouncementSetting.check_limit_for_user((@bulletin.new_record? ? current_user.id : @bulletin.create_user_id)) %> + Top limit has been reached. The bulletin wont be marked as top even if you click on it. + <% end %> +
+
+
" data-for="is_top"> + +
+ <%= f.datetime_picker :top_end_date, :no_label => true, :new_record => @bulletin.new_record? %>
- + <%# end %> @@ -409,6 +420,17 @@ return false; }); + $("#bulletin_is_top").parent().on("click",function(){ + setTimeout(function(){ + if($("#bulletin_is_top").parent().hasClass("active")){ + $("div[data-for=is_top]").removeClass("hide"); + }else{ + $("div[data-for=is_top]").addClass("hide"); + $("div[data-for=is_top]").find("input[type=text]").val(""); + } + },100) + }) + }); <% end %> \ No newline at end of file diff --git a/app/views/admin/announcements/settings.html.erb b/app/views/admin/announcements/settings.html.erb new file mode 100644 index 0000000..5572298 --- /dev/null +++ b/app/views/admin/announcements/settings.html.erb @@ -0,0 +1,14 @@ +<%= 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, :class => "control-label muted" %> +
+ <%= f.number_field :top_limit, :min => "0" %> + Put 0 for unlimited. +
+
+
+
+ <%= f.submit t('submit'), class: 'btn btn-primary' %> +
+<% end %> \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index 0b69832..40e9764 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -17,6 +17,7 @@ en: feed_list: Feed List approve: Approve all_articles: All Articles + settings: Settings announcement: Announcement approval_setting: Approval Setting approve_bulletin_fail: Approval Fail diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index c4b450e..e2de266 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -14,6 +14,7 @@ zh_tw: add_new: 新建 approve: 通過 feed_name: Feed 標題 + settings: Settings rssfeed: Rss Feed Link feed_list: 訂閱清單 all_articles: 文章列表 diff --git a/config/routes.rb b/config/routes.rb index 07c8638..985ff12 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -12,6 +12,9 @@ Rails.application.routes.draw do get 'announcement/approve_bulletin', to: 'announcements#approve_bulletin' get 'announcement/feed', to: 'announcements#feed' get 'announcements/feedform', to: 'announcements#feedform' + get 'announcement/settings', to: 'announcements#settings' + post 'announcement/createsettings', to: 'announcements#createsettings' + patch 'announcement/updatesettings', to: 'announcements#updatesettings' resources :announcements end diff --git a/lib/announcement/engine.rb b/lib/announcement/engine.rb index a26cada..5dd7f50 100644 --- a/lib/announcement/engine.rb +++ b/lib/announcement/engine.rb @@ -49,6 +49,11 @@ module Announcement :priority=>5, :active_for_action=>{'admin/announcements'=>'feed'}, :available_for => 'managers' + context_link 'announcement.settings', + :link_path=>"admin_announcement_settings_path" , + :priority=>6, + :active_for_action=>{'admin/announcements'=>'settings'}, + :available_for => 'managers' end end