Guest User

Untitled

a guest
Jun 23rd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. module CalendarTextBuilder
  2. def get_databinder(events, check_exceptions, &cell_text_builder)
  3. cell_text_builder ||= lambda {|d| nil}
  4. @databinder = lambda do |d|
  5. cell_text = "#{d.mday}<br />"
  6. cell_attrs = {:class => 'day'}
  7. curdate = Time.local(d.year, d.month, d.mday)
  8. events.each do |e|
  9. # Start at the starting day of the event
  10. workdate = Time.local(e.startdate.year, e.startdate.month, e.startdate.mday)
  11. if !e.enddate.nil?
  12. enddate = Time.local(e.enddate.year, e.enddate.month, e.enddate.mday)
  13. else
  14. enddate = Time.gm(Event::FOREVER)
  15. end
  16. # Work forward and figure out if a repeating event falls on this day
  17. case e.repeat
  18. when Event::REP_DAILY
  19. workdate += e.repeat_interval.days while (workdate < curdate) && (workdate < enddate)
  20.  
  21. when Event::REP_WEEKLY
  22. if e.repeats_on_day?(curdate.wday)
  23. # Set the workdate to be the right day of the week, moving forward
  24. workdate += ((curdate.wday - workdate.wday) % 7).days
  25. workdate += e.repeat_interval.weeks while (workdate < curdate) && (workdate < enddate)
  26. end
  27.  
  28. when Event::REP_MONTHLY
  29. # Event repeats by date
  30. if e.repeats_by_date? && (e.startdate.mday == curdate.mday)
  31. workdate = Time.local(workdate.year, workdate.month + e.repeat_interval, workdate.mday) while (workdate < curdate) && (workdate < enddate)
  32. # Event repeats by day
  33. elsif (e.startdate.wday == curdate.wday) && (curdate <= enddate)
  34. # First figure out which of that weekday (first? second?) of the month
  35. # both the event and the current day fall on
  36. c_ordday = get_ordday_of_month(curdate)
  37. e_ordday = get_ordday_of_month(workdate)
  38. # If it's the first through third, and they're the same, we have
  39. # a match for this day
  40. if (c_ordday < 4)
  41. workdate = curdate if c_ordday == e_ordday
  42. # If it's marked as repeating on the fourth day, check for a match
  43. elsif !e.repeats_on_last_day?
  44. workdate = curdate if c_ordday == 4
  45. # If it's marked as repeating on the last day, see if we have
  46. # another of this day in this month; if we don't, it's a match
  47. else
  48. tempdate = curdate + 1.week
  49. workdate = curdate if tempdate.month != curdate.month
  50. end
  51. end
  52.  
  53. when Event::REP_YEARLY
  54. workdate += e.repeat_interval.years while (workdate < curdate) && (workdate < enddate)
  55. end
  56.  
  57. # Does the calculated date falls on the current date?
  58. if (workdate == curdate) && (workdate <= enddate)
  59. found_exc = false
  60. # Check for an exception first, unless told not to
  61. if check_exceptions
  62. e.event_exceptions.each do |exception|
  63. found_exc = true if workdate == Time.local(exception.date.year, exception.date.month, exception.date.mday)
  64. end
  65. end
  66. if !found_exc
  67. # This one is good to go, get the controller to give us some text
  68. addl_cell_text, cell_attrs = cell_text_builder.call(e, workdate)
  69. cell_text << addl_cell_text
  70. end
  71. end
  72. end
  73. [cell_text, cell_attrs]
  74. end
  75. end
  76.  
  77. def get_ordday_of_month(findDate)
  78. c_ordday = 1
  79. tempdate = Time.local(findDate.year, findDate.month, findDate.mday)
  80. while (tempdate.month == findDate.month)
  81. tempdate -= 7.days
  82. c_ordday += 1 if tempdate.month == findDate.month
  83. end
  84. return c_ordday
  85. end
  86. end
Add Comment
Please, Sign In to add comment