Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module CalendarTextBuilder
- def get_databinder(events, check_exceptions, &cell_text_builder)
- cell_text_builder ||= lambda {|d| nil}
- @databinder = lambda do |d|
- cell_text = "#{d.mday}<br />"
- cell_attrs = {:class => 'day'}
- curdate = Time.local(d.year, d.month, d.mday)
- events.each do |e|
- # Start at the starting day of the event
- workdate = Time.local(e.startdate.year, e.startdate.month, e.startdate.mday)
- if !e.enddate.nil?
- enddate = Time.local(e.enddate.year, e.enddate.month, e.enddate.mday)
- else
- enddate = Time.gm(Event::FOREVER)
- end
- # Work forward and figure out if a repeating event falls on this day
- case e.repeat
- when Event::REP_DAILY
- workdate += e.repeat_interval.days while (workdate < curdate) && (workdate < enddate)
- when Event::REP_WEEKLY
- if e.repeats_on_day?(curdate.wday)
- # Set the workdate to be the right day of the week, moving forward
- workdate += ((curdate.wday - workdate.wday) % 7).days
- workdate += e.repeat_interval.weeks while (workdate < curdate) && (workdate < enddate)
- end
- when Event::REP_MONTHLY
- # Event repeats by date
- if e.repeats_by_date? && (e.startdate.mday == curdate.mday)
- workdate = Time.local(workdate.year, workdate.month + e.repeat_interval, workdate.mday) while (workdate < curdate) && (workdate < enddate)
- # Event repeats by day
- elsif (e.startdate.wday == curdate.wday) && (curdate <= enddate)
- # First figure out which of that weekday (first? second?) of the month
- # both the event and the current day fall on
- c_ordday = get_ordday_of_month(curdate)
- e_ordday = get_ordday_of_month(workdate)
- # If it's the first through third, and they're the same, we have
- # a match for this day
- if (c_ordday < 4)
- workdate = curdate if c_ordday == e_ordday
- # If it's marked as repeating on the fourth day, check for a match
- elsif !e.repeats_on_last_day?
- workdate = curdate if c_ordday == 4
- # If it's marked as repeating on the last day, see if we have
- # another of this day in this month; if we don't, it's a match
- else
- tempdate = curdate + 1.week
- workdate = curdate if tempdate.month != curdate.month
- end
- end
- when Event::REP_YEARLY
- workdate += e.repeat_interval.years while (workdate < curdate) && (workdate < enddate)
- end
- # Does the calculated date falls on the current date?
- if (workdate == curdate) && (workdate <= enddate)
- found_exc = false
- # Check for an exception first, unless told not to
- if check_exceptions
- e.event_exceptions.each do |exception|
- found_exc = true if workdate == Time.local(exception.date.year, exception.date.month, exception.date.mday)
- end
- end
- if !found_exc
- # This one is good to go, get the controller to give us some text
- addl_cell_text, cell_attrs = cell_text_builder.call(e, workdate)
- cell_text << addl_cell_text
- end
- end
- end
- [cell_text, cell_attrs]
- end
- end
- def get_ordday_of_month(findDate)
- c_ordday = 1
- tempdate = Time.local(findDate.year, findDate.month, findDate.mday)
- while (tempdate.month == findDate.month)
- tempdate -= 7.days
- c_ordday += 1 if tempdate.month == findDate.month
- end
- return c_ordday
- end
- end
Add Comment
Please, Sign In to add comment