require 'time' require 'date' class Event include Mongoid::Document include Mongoid::Timestamps include OrbitTag::Taggable # include Mongoid::MultiParameterAttributes field :title field :note field :start, type: DateTime field :end, type: DateTime field :all_day, type: Boolean,default: false field :recurring, type: Boolean,default: false field :frequency field :period field :create_user_id field :update_user_id field :bulletin_id belongs_to :calendar_type attr_accessor :agenda_start, :agenda_end, :get_agenda_events validates_presence_of :title, :message => "Please fill the title of the Event" def self.with_categories(cat_ids=[]) if cat_ids.blank? self.all else type_ids = CalendarType.where(:category_id.in=>cat_ids).pluck(:id) self.where(:calendar_type_id.in => type_ids) end end def bulletin Bulletin.find(self.bulletin_id) rescue nil end def self.format_date(date_time) Time.at(date_time.to_i).to_formatted_s(:db) end REPEATS = [ "Daily" , "Weekly" , "Monthly" , "Yearly" ] def as_json(options = {}) if options["frontend"] { :id => self.id.to_s, :title => self.title, :note => self.note || "", :start => self.start.rfc822, :end => self.end.rfc822, :allDay => self.all_day, :recurring => self.recurring, :calendar => self.calendar_type_id.to_s, :color => (self.calendar_type.color rescue nil) } else { :id => self.id.to_s, :title => self.title, :note => self.note || "", :start => self.start.rfc822, :end => self.end.rfc822, :allDay => self.all_day, :recurring => self.recurring, :calendar => self.calendar_type_id.to_s, :color => (self.calendar_type.color rescue nil), :edit_url => Rails.application.routes.url_helpers.edit_admin_calendar_path(:locale=>I18n.locale, :id=>self.id), :delete_url => Rails.application.routes.url_helpers.admin_calendar_path(:locale=>I18n.locale, :id=>self.id) } end end def self.monthly_event(start_date,end_date) self.any_of(:start.gte => start_date, :end.gte => start_date).and(:start.lte => end_date).asc(:start) end def self.recurring_event(start_date,end_date) @recurring_events = self.where(:recurring => true) @recurring = [] @recurring_events.each do |re| case re.period when 'Daily' if (start_date..end_date).cover?(re.start) @i = TimeDifference.between(re.start,end_date).in_days.to_i (1..@i).each do |i| @start_date = re.start + i @recurring << {:id => re.id.to_s, :title=>re.title, :note=>re.note, :start=>@start_date, :end => @start_date, :allDay => re.all_day, :recurring => re.recurring, :calendar => re.calendar_type.id.to_s, :color => re.calendar_type.color, :edit_url => Rails.application.routes.url_helpers.edit_admin_calendar_path(:locale=>I18n.locale, :id=>re.id), :delete_url => Rails.application.routes.url_helpers.admin_calendar_path(:locale=>I18n.locale, :id=>re.id)} end elsif re.start < start_date @i = TimeDifference.between(start_date,end_date).in_days.to_i (0..@i-1).each do |i| @start_date = start_date.to_date + i @recurring << {:id => re.id.to_s, :title=>re.title, :note=>re.note, :start=>@start_date, :end => @start_date, :allDay => re.all_day, :recurring => re.recurring, :calendar => re.calendar_type.id.to_s, :color => re.calendar_type.color, :edit_url => Rails.application.routes.url_helpers.edit_admin_calendar_path(:locale=>I18n.locale, :id=>re.id), :delete_url => Rails.application.routes.url_helpers.admin_calendar_path(:locale=>I18n.locale, :id=>re.id)} end end when "Weekly" @start_date = re.start @end_date = re.end @i = TimeDifference.between(re.start,end_date).in_weeks.to_i (1..@i).each do |i| @start_date += (7*re.frequency.to_i) @end_date += (7*re.frequency.to_i) @recurring << {:id => re.id.to_s, :title=>re.title, :note=>re.note, :start=>@start_date, :end => @end_date, :allDay => re.all_day, :recurring => re.recurring, :calendar => re.calendar_type.id.to_s, :color => re.calendar_type.color, :edit_url => Rails.application.routes.url_helpers.edit_admin_calendar_path(:locale=>I18n.locale, :id=>re.id), :delete_url => Rails.application.routes.url_helpers.admin_calendar_path(:locale=>I18n.locale, :id=>re.id)} end when "Monthly" if !(start_date..end_date).cover?(re.start) sd = re.start ed = re.end @i = TimeDifference.between(re.start,end_date).in_months.to_i @start_date = sd sd = sd >> @i*re.frequency.to_i ed = ed >> @i*re.frequency.to_i @recurring << {:id => re.id.to_s, :title=>re.title, :note=>re.note, :start=>sd, :end => ed, :allDay => re.all_day, :recurring => re.recurring, :calendar => re.calendar_type.id.to_s, :color => re.calendar_type.color, :edit_url => Rails.application.routes.url_helpers.edit_admin_calendar_path(:locale=>I18n.locale, :id=>re.id), :delete_url => Rails.application.routes.url_helpers.admin_calendar_path(:locale=>I18n.locale, :id=>re.id)} end when "Yearly" if !(start_date..end_date).cover?(re.start) sd = re.start ed = re.end @i = TimeDifference.between(re.start,end_date).in_years.to_i @start_date = sd sd = sd >> 12 * @i*re.frequency.to_i ed = ed >> 12 * @i*re.frequency.to_i @recurring << {:id => re.id.to_s, :title=>re.title, :note=>re.note, :start=>sd, :end => ed, :allDay => re.all_day, :recurring => re.recurring, :calendar => re.calendar_type.id.to_s, :color => re.calendar_type.color, :edit_url => Rails.application.routes.url_helpers.edit_admin_calendar_path(:locale=>I18n.locale, :id=>re.id), :delete_url => Rails.application.routes.url_helpers.admin_calendar_path(:locale=>I18n.locale, :id=>re.id)} end end end @recurring end def self.agenda_events(agenda_start, agenda_end) recurring = self.recurring_event(agenda_start,agenda_end) events = self.any_of(:start.gte => agenda_start).or(:end.gte => agenda_start).and(:start.lte => agenda_end).asc(:start) all_events = recurring.concat(events) recurring end end