class EventNewsSetting
include Mongoid::Document
include Mongoid::Timestamps
field :carousel_image_width, type: String, :default => "75%"
field :top_limit, type: Integer, :default => 0
field :pro_enabled, type: Boolean, :default => false
field :approvers, type: Array, :default => []
field :email_to, type: Array, :default => ["admins","managers","approvers"]
field :is_display_edit_only, type: Boolean, :default => false
field :only_manager_can_edit_status, type: Boolean, :default => false
field :including_day_of_the_week, type: Boolean, default: true
field :including_time, type: Boolean, default: true
field :hour_clock_24, type: Boolean, default: true
field :enable_manually_sort, type: Boolean, default: false
field :is_postdate_sort_first, type: Boolean, default: false
field :migrate_flag, type: Array, default: []
has_many :event_news_status_settings, :autosave => true, :dependent => :destroy
accepts_nested_attributes_for :event_news_status_settings, :allow_destroy => true
before_save do
can_update_shared_hash = (defined?(OrbitHelper::SharedHash) && OrbitHelper::SharedHash)
if self.including_day_of_the_week_changed? || self.including_time_changed? || self.hour_clock_24_changed?
@need_update_event_news = true
end
if self.enable_manually_sort_changed? && self.enable_manually_sort
EventNews.index({approved: -1,is_hidden: 1,is_preview: 1, is_top: -1,sort_number: 1,postdate: -1,_id: -1,deadline: -1}, { unique: false, background: true })
EventNews.create_indexes
if can_update_shared_hash
OrbitHelper::SharedMutex.synchronize do
OrbitHelper::SharedHash["event_news_mod"][:enable_manually_sort] = self.enable_manually_sort
end
end
@need_update_event_news = true
end
end
after_save do
if @need_update_event_news
@need_update_event_news = false
Thread.new do
EventNewsCache.all.delete
end
end
end
def self.check_limit_for_user(user_id, b_id = nil)
limit = self.first.top_limit rescue 0
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
def self.is_pro?
self.first.pro_enabled rescue false
end
def self.event_date_default_format
setting = self.first
setting.event_date_default_format
end
def event_date_default_format
datetime_format = "%Y-%m-%d"
if self.including_day_of_the_week
datetime_format += " %a"
end
if self.including_time
if self.hour_clock_24
datetime_format += " %H:%M"
else
datetime_format += " %I:%M %P"
end
end
datetime_format
end
def get_weekday(w)
trans = EventNews::Week_day_trans
if trans.keys.include?(I18n.locale)
trans[I18n.locale][w]
else
trans[:en][w]
end
end
def custom_strftime(dt, datetime_format)
if dt
dt.strftime(datetime_format.sub("%a","%%a")).sub("%a",get_weekday(dt.wday))
else
""
end
end
def event_date_format
@event_date_format ||= self.event_date_default_format
end
def self.event_time_formated_for_frontend(st,ed)
self.first.event_time_formated_for_frontend(st,ed)
end
def event_time_formated_for_frontend(st,ed)
st,ed = self.event_time_formated(st,ed).split("~")
if st.blank?
st = "#{st}"
end
if ed.nil?
st
else
ed = "#{ed}"
"#{st} ~ #{ed}"
end
end
def event_time_formated(st,ed)
s = st
e = ed
if s.blank? && e.blank?
""
elsif e.blank?
custom_strftime(s, self.event_date_format)
elsif s.blank?
"~ " + custom_strftime(e, self.event_date_format)
else
parse_format = self.event_date_format
if s.to_date == e.to_date
parse_format_arr = parse_format.split(/(%d %a|%d) /)
parse_format_arr = parse_format_arr[0..-2].join(""), parse_format_arr[-1]
date_str = custom_strftime(s, parse_format_arr[0])
s_time = s.strftime(parse_format_arr[1])
e_time = e.strftime(parse_format_arr[1])
"#{date_str} #{s_time} ~ #{e_time}"
else
custom_strftime(s, self.event_date_format) + " ~ " + custom_strftime(e, self.event_date_format)
end
end
end
end