2014-04-01 07:12:43 +00:00
|
|
|
class Bulletin
|
|
|
|
include Mongoid::Document
|
|
|
|
include Mongoid::Timestamps
|
2014-05-01 08:41:00 +00:00
|
|
|
|
|
|
|
include OrbitModel::Status
|
2014-05-15 11:27:05 +00:00
|
|
|
include OrbitModel::Impression
|
2014-06-23 08:36:04 +00:00
|
|
|
# encoding: utf-8
|
2014-04-20 12:23:52 +00:00
|
|
|
include OrbitTag::Taggable
|
|
|
|
include OrbitCategory::Categorizable
|
2014-04-03 03:18:02 +00:00
|
|
|
include Slug
|
2020-03-01 07:49:01 +00:00
|
|
|
require 'bulletin_model/cache'
|
2021-04-07 08:12:42 +00:00
|
|
|
include ::BulletinModel::Cache
|
2020-09-01 06:56:25 +00:00
|
|
|
attr_accessor :org_tag_ids
|
|
|
|
def tags=(ids)
|
|
|
|
self.org_tag_ids = self.tag_ids
|
|
|
|
super(ids)
|
|
|
|
end
|
|
|
|
def []=(index,value)
|
|
|
|
if index.to_s=='tags'
|
|
|
|
self.org_tag_ids = self.tag_ids
|
|
|
|
end
|
|
|
|
super(index,value)
|
|
|
|
end
|
2020-03-01 07:49:01 +00:00
|
|
|
SubPart.class_eval { include BulletinModel::Cache }
|
2020-05-06 11:30:09 +00:00
|
|
|
Page.class_eval { include BulletinModel::Cache }
|
2020-04-10 02:13:23 +00:00
|
|
|
before_destroy do
|
|
|
|
AnnsCache.all.destroy
|
|
|
|
end
|
2020-05-28 11:16:00 +00:00
|
|
|
field :add_to_calendar,type: Boolean,default: false
|
|
|
|
field :calendar_start_date, :type => DateTime
|
|
|
|
field :calendar_end_date, :type => DateTime
|
|
|
|
field :calendar_all_day,type: Boolean,default: false
|
|
|
|
field :calendar_type_id
|
|
|
|
field :event_id
|
2020-08-12 13:28:57 +00:00
|
|
|
field :page_id
|
2020-02-06 04:46:38 +00:00
|
|
|
field :title, type: String, localize: true
|
2014-05-01 08:41:00 +00:00
|
|
|
field :subtitle, localize: true
|
|
|
|
field :text, localize: true
|
|
|
|
field :create_user_id
|
|
|
|
field :update_user_id
|
|
|
|
field :public, :type => Boolean, :default => true
|
|
|
|
field :postdate , :type => DateTime, :default => Time.now
|
|
|
|
field :deadline , :type => DateTime
|
2014-06-06 04:05:33 +00:00
|
|
|
field :rss2_sn
|
2014-09-22 08:32:30 +00:00
|
|
|
field :approved, :type => Boolean, :default => false
|
2014-06-17 10:46:52 +00:00
|
|
|
field :is_preview, :type => Boolean, :default => false
|
2015-03-05 11:48:38 +00:00
|
|
|
field :expirable_created_at, type: DateTime
|
2016-07-20 12:35:23 +00:00
|
|
|
field :rejected, :type => Boolean, :default => false
|
|
|
|
field :reapproval, :type => Boolean, :default => false
|
|
|
|
field :rejection_reason
|
2017-06-20 07:04:05 +00:00
|
|
|
field :is_external_link, :type => Boolean, :default => false
|
|
|
|
field :external_link
|
2020-01-13 04:55:14 +00:00
|
|
|
field :display_subtitle, :type => Boolean, :default => false
|
|
|
|
field :display_img, :type => Boolean, :default => false
|
2014-05-01 08:41:00 +00:00
|
|
|
|
2014-05-28 07:37:56 +00:00
|
|
|
field :email_id
|
2014-05-01 08:41:00 +00:00
|
|
|
field :email_sent, :type => Boolean, :default => false
|
2014-06-24 09:23:51 +00:00
|
|
|
field :email_sentdate , :type => DateTime
|
2014-05-28 07:37:56 +00:00
|
|
|
field :email_member_ids
|
2014-05-01 08:41:00 +00:00
|
|
|
field :other_mailaddress
|
2014-10-14 06:28:51 +00:00
|
|
|
field :image_description, localize: true
|
2015-12-22 11:04:13 +00:00
|
|
|
field :top_end_date, :type => DateTime
|
2020-07-30 14:12:21 +00:00
|
|
|
field :open_comment, :type => Boolean, :default => false
|
|
|
|
field :comment_end_time, :type => DateTime
|
|
|
|
field :comment_role, :type => Array, :default => []
|
|
|
|
|
2014-05-01 08:41:00 +00:00
|
|
|
mount_uploader :image, ImageUploader
|
|
|
|
|
|
|
|
has_many :bulletin_links, :autosave => true, :dependent => :destroy
|
|
|
|
has_many :bulletin_files, :autosave => true, :dependent => :destroy
|
2020-07-30 14:12:21 +00:00
|
|
|
has_many :bulletin_comments, :autosave => true, :dependent => :destroy
|
2021-04-10 16:28:00 +00:00
|
|
|
has_many :bulletin_carousel_images, :autosave => true, :dependent => :destroy
|
2014-05-01 08:41:00 +00:00
|
|
|
accepts_nested_attributes_for :bulletin_files, :allow_destroy => true
|
|
|
|
accepts_nested_attributes_for :bulletin_links, :allow_destroy => true
|
2021-04-10 16:28:00 +00:00
|
|
|
accepts_nested_attributes_for :bulletin_carousel_images, :allow_destroy => true
|
2014-05-01 08:41:00 +00:00
|
|
|
|
2014-05-28 07:37:56 +00:00
|
|
|
before_destroy :destroy_email
|
|
|
|
|
2021-04-11 14:08:27 +00:00
|
|
|
scope :can_display_and_sorted, ->{where(:is_hidden=>false,:is_preview => false).any_of({:postdate.lte=>Time.now, :deadline.gte=>Time.now},{:postdate.lte=>Time.now, :deadline=>nil},{:postdate=>nil}).order(is_top: :desc,postdate: :desc,id: :desc)}
|
2014-09-22 08:32:30 +00:00
|
|
|
scope :is_approved, ->{where(:approved => true)}
|
2014-06-06 11:17:58 +00:00
|
|
|
|
2015-03-05 11:48:38 +00:00
|
|
|
before_create :set_expire
|
2020-03-02 10:09:55 +00:00
|
|
|
before_save :check_limit
|
2020-08-15 13:41:45 +00:00
|
|
|
def to_calendar_param
|
|
|
|
self.to_param
|
|
|
|
end
|
2020-05-28 11:16:00 +00:00
|
|
|
def calendar_type
|
|
|
|
CalendarType.where(:category_id.in => self.calendar_type_id)
|
|
|
|
end
|
|
|
|
def event
|
|
|
|
if !self.event_id.nil?
|
|
|
|
Event.where(:id => self.event_id).first
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
2020-03-02 10:09:55 +00:00
|
|
|
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
|
2020-02-06 04:46:38 +00:00
|
|
|
def slug_title
|
|
|
|
doc = Nokogiri::HTML(self.title)
|
|
|
|
title = doc.text.gsub('/','-')
|
|
|
|
end
|
2015-03-05 11:48:38 +00:00
|
|
|
def set_expire
|
|
|
|
self.expirable_created_at = Time.now if self.is_preview
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2014-05-01 08:41:00 +00:00
|
|
|
def update_user
|
|
|
|
User.find(update_user_id) rescue nil
|
|
|
|
end
|
2014-04-01 07:12:43 +00:00
|
|
|
|
2014-05-01 08:41:00 +00:00
|
|
|
def update_user=(user)
|
|
|
|
self.update_user_id = user.id
|
2014-04-01 07:12:43 +00:00
|
|
|
end
|
2014-04-21 07:39:13 +00:00
|
|
|
|
2014-05-28 07:37:56 +00:00
|
|
|
def email_members
|
|
|
|
MemberProfile.find(self.email_member_ids) rescue []
|
|
|
|
end
|
|
|
|
|
|
|
|
def email_addresses
|
|
|
|
addresses = self.email_members.collect{|member| member.email} rescue []
|
|
|
|
addresses = addresses +[self.other_mailaddress] if !self.other_mailaddress.blank?
|
|
|
|
addresses.flatten
|
|
|
|
end
|
|
|
|
|
|
|
|
def email
|
|
|
|
mail = Email.find(self.email_id) rescue nil
|
|
|
|
end
|
|
|
|
|
2014-08-08 03:45:33 +00:00
|
|
|
def expired?
|
|
|
|
(self.deadline < Time.now) rescue false
|
|
|
|
end
|
|
|
|
|
2014-05-28 07:37:56 +00:00
|
|
|
def destroy_email
|
|
|
|
mail = Email.find(self.email_id) rescue nil
|
|
|
|
mail.destroy if !mail.nil?
|
|
|
|
end
|
|
|
|
|
2015-12-22 11:04:13 +00:00
|
|
|
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
|
|
|
|
|
2020-01-13 04:55:14 +00:00
|
|
|
def display_subtitle?
|
|
|
|
self.display_subtitle rescue false
|
|
|
|
end
|
|
|
|
|
|
|
|
def display_img?
|
|
|
|
self.display_img rescue false
|
|
|
|
end
|
2020-07-30 14:12:21 +00:00
|
|
|
def comments
|
|
|
|
self.bulletin_comments.select{|v| !v.is_hidden}
|
|
|
|
end
|
|
|
|
def open_comment_for_user(user)
|
|
|
|
role_ids = user.member_profile.roles.collect{|v| v.id.to_s} rescue ['visitor']
|
2020-07-31 03:03:38 +00:00
|
|
|
self.open_comment && (self.comment_end_time.blank? || self.comment_end_time > Time.now) && (self.comment_role.any?{|v| role_ids.include?(v)} || self.comment_role.include?('visitor') || (self.comment_role.include?('all_member') && role_ids[0] != 'visitor'))
|
2020-07-30 14:12:21 +00:00
|
|
|
end
|
2020-11-11 08:22:50 +00:00
|
|
|
def statuses
|
|
|
|
statuses = []
|
|
|
|
statuses << top_text if is_top?
|
|
|
|
statuses << hot_text if is_hot?
|
|
|
|
statuses << hidden_text if is_hidden?
|
|
|
|
statuses
|
|
|
|
end
|
|
|
|
def statuses_with_classname
|
|
|
|
statuses = []
|
|
|
|
statuses << {"name" => top_text, "classname" => "top"} if is_top?
|
|
|
|
statuses << {"name" => hot_text, "classname" => "hot"} if is_hot?
|
|
|
|
statuses << {"name" => hidden_text, "classname" => "hidden"} if is_hidden?
|
|
|
|
statuses
|
|
|
|
end
|
|
|
|
def status_for_table
|
|
|
|
status = ""
|
|
|
|
status << "<span class='label label-success'>#{top_text}</span> " if self.is_top
|
|
|
|
status << "<span class='label label-important'>#{hot_text}</span> " if self.is_hot
|
|
|
|
status << "<span class='label'>#{hidden_text}</span>"if self.is_hidden
|
|
|
|
status.html_safe
|
|
|
|
end
|
|
|
|
def top_text
|
|
|
|
I18n.t("announcement.status.top")
|
|
|
|
end
|
|
|
|
def hot_text
|
|
|
|
I18n.t("announcement.status.hot")
|
|
|
|
end
|
|
|
|
def hidden_text
|
|
|
|
I18n.t("announcement.status.hidden")
|
|
|
|
end
|
2014-04-01 07:12:43 +00:00
|
|
|
end
|