From e7bcef96a710700c565cfffecc2ddb6aa9c2cf34 Mon Sep 17 00:00:00 2001 From: chiu Date: Sat, 8 Aug 2020 20:33:26 +0800 Subject: [PATCH] fix some error --- app/assets/javascripts/calendar.js | 13 ++-- app/assets/javascripts/fullcalendar.js | 5 +- app/controllers/admin/calendars_controller.rb | 2 +- app/controllers/calendars_controller.rb | 4 +- app/models/event.rb | 77 +++++++++++-------- 5 files changed, 55 insertions(+), 46 deletions(-) diff --git a/app/assets/javascripts/calendar.js b/app/assets/javascripts/calendar.js index 41ac24c..1199d10 100644 --- a/app/assets/javascripts/calendar.js +++ b/app/assets/javascripts/calendar.js @@ -84,12 +84,12 @@ var Calendar = function(dom){ var checked = ($("#all_day_check").is(":checked") ? true : false); var checked_function = function(c){ if(c){ - for(i in pickers){ + for(i=0;i $(window).width()){ x = x - event_quick_view.width(); } diff --git a/app/assets/javascripts/fullcalendar.js b/app/assets/javascripts/fullcalendar.js index e514e6e..2c9989a 100644 --- a/app/assets/javascripts/fullcalendar.js +++ b/app/assets/javascripts/fullcalendar.js @@ -919,7 +919,6 @@ function EventManager(options, _sources) { _fetchEventSource(source, function(events) { if (fetchID == currentFetchID) { if (events) { - if (options.eventDataTransform) { events = $.map(events, options.eventDataTransform); } @@ -1191,8 +1190,8 @@ function EventManager(options, _sources) { } event._start = cloneDate(event.start = parseDate(event.start, ignoreTimezone)); event.end = parseDate(event.end, ignoreTimezone); - if (event.end && event.end <= event.start) { - event.end = null; + if (event.end && event.end < event.start) { + event.end = event.start; } event._end = event.end ? cloneDate(event.end) : null; if (event.allDay === undefined) { diff --git a/app/controllers/admin/calendars_controller.rb b/app/controllers/admin/calendars_controller.rb index d333c31..92971b7 100644 --- a/app/controllers/admin/calendars_controller.rb +++ b/app/controllers/admin/calendars_controller.rb @@ -136,7 +136,7 @@ class Admin::CalendarsController < OrbitAdminController if !bulletin.nil? bulletin.update_attributes(calendar_start_date: p[:start],calendar_end_date: p[:end],calendar_all_day: p[:all_day],calendar_type_id: p[:calendar_type_id]) end - if @event.update_attributes(p) + if @event.update_attributes!(p) e = @event.to_json e = JSON.parse(e) e["can_edit"] = true diff --git a/app/controllers/calendars_controller.rb b/app/controllers/calendars_controller.rb index b820565..4f55b80 100644 --- a/app/controllers/calendars_controller.rb +++ b/app/controllers/calendars_controller.rb @@ -38,9 +38,9 @@ class CalendarsController < ApplicationController if params[:start].present? && params[:end].present? sdt = Time.at(params[:start].to_i) edt = Time.at(params[:end].to_i) - events = Event.monthly_event(sdt,edt) + events = Event.monthly_event(sdt,edt)+Event.recurring_event(sdt,edt) end - end + end respond_to do |format| format.html # index.html.erb format.json { render json: events.to_json } diff --git a/app/models/event.rb b/app/models/event.rb index 22947e4..2276c9d 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -46,8 +46,8 @@ class Event end end ######################################## - validates_presence_of :title, :message => "Please fill the title of the Event", :unless => lambda { self.title_translations.blank? } - validates_presence_of :title_translations, :message => "Please fill the title of the Event", :unless => lambda { self.title.blank? } + validates_presence_of :title, :message => "Please fill the title of the Event", :if => lambda { self['title_translations'].blank? } + validates_presence_of :title_translations, :message => "Please fill the title of the Event", :if => lambda { self['title'].blank? } def title tp = self['title_translations'][I18n.locale] tp.blank? ? self['title'] : tp @@ -112,57 +112,68 @@ class Event 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) + def self.get_diff_month(date1,date2) + (date2.year-date1.year)*12+(date2.month-date1.month) + end + def self.recurring_event(start_date,end_date) @recurring_events = self.where(:recurring => true) @recurring = [] @recurring_events.each do |re| - case re.period + 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) + data = {:id => re.id.to_s, :title=>re.title, :note=>re.note, :allDay => re.all_day, :recurring => re.recurring, :calendar => re.calendar_type.id.to_s, :color => re.calendar_type.color, :edit_url => edit_url, :delete_url => delete_url} + 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| + days = (end_date.to_date-re.start.to_date) + freq = re.frequency.to_i + (1..days).each do |i| + if i%freq==0 @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_date = re.end + i + @recurring << data.merge({:start => @start_date, :end => @end_date}) 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| + (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)} + @recurring << data.merge({:start => @start_date, :end => @end_date}) end when "Monthly" + if !(start_date..end_date).cover?(re.start) && start_date.to_datetime>re.start + sd = re.start + ed = re.end + end_datetime = end_date.to_datetime + start_datetime = start_date.to_datetime + months = self.get_diff_month(sd,start_datetime)..self.get_diff_month(sd,end_datetime) + months.each do |diff_month| + if (diff_month%re.frequency.to_i)==0 + sd_tp = sd + diff_month.month + ed_tp = ed + diff_month.month + @recurring << data.merge({:start => sd_tp, :end => ed_tp}) + end + end + 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_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)} + start_datetime = start_date.to_datetime + end_datetime = end_date.to_datetime + if start_datetime>sd + ((start_datetime.year-sd.year)..(end_datetime.year-sd.year)).each do |year_diff| + if (year_diff%re.frequency.to_i)==0 + sd_tp = sd + year_diff.year + ed_tp = ed + year_diff.year + @recurring << data.merge({:start => sd_tp, :end => ed_tp}) + end + end 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 end @recurring