event_news/app/models/event_news.rb

284 lines
9.9 KiB
Ruby
Raw Normal View History

2020-07-22 02:23:36 +00:00
class EventNews
include Mongoid::Document
include Mongoid::Timestamps
include OrbitModel::Status
include OrbitModel::Impression
# encoding: utf-8
include OrbitTag::Taggable
include OrbitCategory::Categorizable
include Slug
require 'event_news_model/cache'
include EventNewsModel::Cache
2021-09-16 09:16:20 +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-07-22 02:23:36 +00:00
SubPart.class_eval { include EventNewsModel::Cache }
Page.class_eval { include EventNewsModel::Cache }
before_destroy do
EventNewsCache.all.destroy
end
2021-09-16 09:16:20 +00:00
field :is_edit, type: Boolean, default: false #use to check whether the preview record changed
field :copy_id
field :custom_carousel_image_width, type: String, default: ""
field :image_display_class, type: String, default: "full-size-img" #3 choices: full-size-img , pull-left , pull-right
2020-07-22 02:23:36 +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
2021-10-21 07:03:35 +00:00
field :all_day,type: Boolean,default: false
2020-07-22 02:23:36 +00:00
field :calendar_type_id
field :event_id
2021-07-14 04:56:02 +00:00
field :place, type: String, localize: true
2021-09-16 09:16:20 +00:00
field :title, as: :slug_title, type: String, localize: true
2020-07-22 02:23:36 +00:00
field :speaker, type: String, localize: true
field :host, type: String, localize: true
field :subtitle, localize: true
2021-09-05 10:56:57 +00:00
field :text, localize: true, default: ''
2020-07-22 02:23:36 +00:00
field :notes, localize: true
field :create_user_id
field :update_user_id
field :public, :type => Boolean, :default => true
field :event_date , :type => DateTime, :default => Time.now
2021-08-09 04:22:45 +00:00
field :event_end_date , :type => DateTime
2020-07-22 02:23:36 +00:00
field :postdate , :type => DateTime, :default => Time.now
field :deadline , :type => DateTime
field :rss2_sn
field :approved, :type => Boolean, :default => false
field :is_preview, :type => Boolean, :default => false
field :expirable_created_at, type: DateTime
field :rejected, :type => Boolean, :default => false
field :reapproval, :type => Boolean, :default => false
field :rejection_reason
field :is_external_link, :type => Boolean, :default => false
field :external_link
field :display_subtitle, :type => Boolean, :default => false
field :display_img, :type => Boolean, :default => false
field :email_id
field :email_sent, :type => Boolean, :default => false
field :email_sentdate , :type => DateTime
field :email_member_ids
field :other_mailaddress
field :image_description, localize: true
field :top_end_date, :type => DateTime
mount_uploader :image, ImageUploader
has_many :event_news_links, :autosave => true, :dependent => :destroy
has_many :event_news_files, :autosave => true, :dependent => :destroy
has_many :event_carousel_images, :autosave => true, :dependent => :destroy
2020-07-22 02:23:36 +00:00
accepts_nested_attributes_for :event_news_files, :allow_destroy => true
accepts_nested_attributes_for :event_news_links, :allow_destroy => true
accepts_nested_attributes_for :event_carousel_images, :allow_destroy => true
2020-07-22 02:23:36 +00:00
before_destroy :destroy_email
2021-09-16 09:16:20 +00:00
scope :open_in_future, ->{where(:is_hidden.ne=>true,:is_preview.ne => true,:postdate.gt=>Time.now).order(postdate: :asc)}
2021-10-18 01:41:50 +00:00
scope :can_display_and_sorted, ->{where(:is_hidden.ne=>true,:is_preview.ne => true).any_of({:postdate.lte=>Time.now, :deadline.gte=>Time.now},{:postdate.lte=>Time.now, :deadline=>nil},{:postdate=>nil}).order(is_top: :desc, event_date: :desc,postdate: :desc,id: :desc)}
2021-09-19 14:21:19 +00:00
scope :can_display_and_sorted_according_today, ->{where(:is_hidden.ne=>true,:is_preview.ne => true).any_of({:postdate.lte=>Time.now, :deadline.gte=>Time.now},{:postdate.lte=>Time.now, :deadline=>nil},{:postdate=>nil}).order(is_top: :desc, event_date: :asc,postdate: :desc,id: :desc).where(:event_date.gte => Date.today.to_time)}
2020-07-22 02:23:36 +00:00
scope :is_approved, ->{where(:approved => true)}
2021-09-16 09:16:20 +00:00
scope :is_approved_and_show, ->{where(:approved => true,:is_hidden.ne=>true,:is_preview.ne => true)}
scope :filter_cats_and_tags, ->(cats,tags) {filter_by_widget_categories(cats,false).filter_by_tags(tags)}
2020-07-22 02:23:36 +00:00
before_create :set_expire
before_save :check_limit
2021-09-16 09:16:20 +00:00
index({postdate: 1}, { unique: false, background: true })
2021-09-19 14:21:19 +00:00
index({is_top: -1, postdate: -1, event_date: -1, _id: -1}, { unique: false, background: true })
index({is_top: -1, event_date: 1, postdate: -1, _id: -1}, { unique: false, background: true })
2021-09-16 09:16:20 +00:00
def get_org_model
if self.is_preview
org_model = nil
if self.copy_id
org_model = self.class.find(self.copy_id) rescue nil
else
org_model = self.class.where(:title=>self.title,:is_preview.ne=>true).desc(:updated_at).first
end
org_model.nil? ? self : org_model
else
self
end
end
2021-10-21 07:03:35 +00:00
def date_parse_format
all_day ? '%Y-%m-%d' : '%Y-%m-%d %H:%M'
end
2021-10-21 04:54:14 +00:00
def event_time_formated
s = self.event_date
2021-10-21 05:41:51 +00:00
e = self.event_end_date
2021-10-21 04:54:14 +00:00
if s.blank? && e.blank?
""
elsif e.blank?
2021-10-21 07:03:35 +00:00
s.strftime(self.date_parse_format)
2021-10-21 05:46:34 +00:00
elsif s.blank?
2021-10-21 07:03:35 +00:00
"~ " + e.strftime(self.date_parse_format)
2021-10-21 04:54:14 +00:00
else
if s.to_date == e.to_date
date_str = s.strftime('%Y-%m-%d')
s_time = s.strftime('%H:%M')
e_time = e.strftime('%H:%M')
"#{date_str} #{s_time} ~ #{e_time}"
else
2021-10-21 07:03:35 +00:00
s.strftime(self.date_parse_format) + " ~ " + e.strftime(self.date_parse_format)
2021-10-21 04:54:14 +00:00
end
end
end
2021-09-16 09:16:20 +00:00
def to_calendar_param
self.to_param
end
2020-07-22 02:23:36 +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
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|
EventNewsSetting.first.event_news_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
2021-09-16 09:16:20 +00:00
if status_setting.top_limit.to_i <= self.class.where(:is_preview.ne=>true,:update_user_id.in => Role.find(status_setting.role_id).member_profiles.collect(&:user).flatten.uniq.map{|v| v.id},status => true).count
2020-07-22 02:23:36 +00:00
if !check_only
if self[status] && !self.class.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 set_expire
self.expirable_created_at = Time.now if self.is_preview
return true
end
def update_user
User.find(update_user_id) rescue nil
end
def update_user=(user)
self.update_user_id = user.id
end
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
def expired?
(self.deadline < Time.now) rescue false
end
def destroy_email
mail = Email.find(self.email_id) rescue nil
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
def display_subtitle?
self.display_subtitle rescue false
end
def display_img?
self.display_img rescue false
end
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
def carousel_image_width
(self.custom_carousel_image_width.blank? ? AnnouncementSetting.last.carousel_image_width : self.custom_carousel_image_width)
end
2021-09-05 10:56:57 +00:00
2021-09-06 05:44:20 +00:00
def self.agenda_events(agenda_start, agenda_end,read_more_url)
events = self.monthly_event(agenda_start, agenda_end).convert_front(read_more_url)
2021-09-05 10:56:57 +00:00
end
def self.monthly_event(start_date,end_date)
self.any_of({:event_date.lte => start_date,:event_end_date.gte => start_date},{:event_date.gte => start_date,:event_end_date.lte => end_date},{:event_date.lte => end_date,:event_end_date.gte => end_date}).asc(:event_date)
end
2021-09-06 05:44:20 +00:00
def self.convert_front(read_more_url)
2021-09-05 10:56:57 +00:00
self.all.collect do |re|
{:id => re.id.to_s,
:title=>re.title,
:note=>re.subtitle || "",
:allDay => false,
:color => nil,
2021-09-16 09:16:20 +00:00
:url_linked => (re.is_external_link ? re.external_link : "#{read_more_url}/#{re.to_param}" rescue ""),
2021-09-05 10:56:57 +00:00
:start => re.event_date,
:end => re.event_end_date}
end
end
2020-07-22 02:23:36 +00:00
end