annotate lib/redmine/helpers/calendar.rb @ 879:8fb3bed996c3 feature_115

Close obsolete branch feature_115
author Chris Cannam
date Sat, 26 Mar 2011 15:07:00 +0000
parents 513646585e45
children cbce1fd3b1b7
rev   line source
Chris@0 1 # redMine - project management software
Chris@0 2 # Copyright (C) 2006-2007 Jean-Philippe Lang
Chris@0 3 #
Chris@0 4 # This program is free software; you can redistribute it and/or
Chris@0 5 # modify it under the terms of the GNU General Public License
Chris@0 6 # as published by the Free Software Foundation; either version 2
Chris@0 7 # of the License, or (at your option) any later version.
Chris@0 8 #
Chris@0 9 # This program is distributed in the hope that it will be useful,
Chris@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 # GNU General Public License for more details.
Chris@0 13 #
Chris@0 14 # You should have received a copy of the GNU General Public License
Chris@0 15 # along with this program; if not, write to the Free Software
Chris@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 17
Chris@0 18 module Redmine
Chris@0 19 module Helpers
Chris@0 20
Chris@0 21 # Simple class to compute the start and end dates of a calendar
Chris@0 22 class Calendar
Chris@0 23 include Redmine::I18n
Chris@0 24 attr_reader :startdt, :enddt
Chris@0 25
Chris@0 26 def initialize(date, lang = current_language, period = :month)
Chris@0 27 @date = date
Chris@0 28 @events = []
Chris@0 29 @ending_events_by_days = {}
Chris@0 30 @starting_events_by_days = {}
Chris@0 31 set_language_if_valid lang
Chris@0 32 case period
Chris@0 33 when :month
Chris@0 34 @startdt = Date.civil(date.year, date.month, 1)
Chris@0 35 @enddt = (@startdt >> 1)-1
Chris@0 36 # starts from the first day of the week
Chris@0 37 @startdt = @startdt - (@startdt.cwday - first_wday)%7
Chris@0 38 # ends on the last day of the week
Chris@0 39 @enddt = @enddt + (last_wday - @enddt.cwday)%7
Chris@0 40 when :week
Chris@0 41 @startdt = date - (date.cwday - first_wday)%7
Chris@0 42 @enddt = date + (last_wday - date.cwday)%7
Chris@0 43 else
Chris@0 44 raise 'Invalid period'
Chris@0 45 end
Chris@0 46 end
Chris@0 47
Chris@0 48 # Sets calendar events
Chris@0 49 def events=(events)
Chris@0 50 @events = events
Chris@0 51 @ending_events_by_days = @events.group_by {|event| event.due_date}
Chris@0 52 @starting_events_by_days = @events.group_by {|event| event.start_date}
Chris@0 53 end
Chris@0 54
Chris@0 55 # Returns events for the given day
Chris@0 56 def events_on(day)
Chris@0 57 ((@ending_events_by_days[day] || []) + (@starting_events_by_days[day] || [])).uniq
Chris@0 58 end
Chris@0 59
Chris@0 60 # Calendar current month
Chris@0 61 def month
Chris@0 62 @date.month
Chris@0 63 end
Chris@0 64
Chris@0 65 # Return the first day of week
Chris@0 66 # 1 = Monday ... 7 = Sunday
Chris@0 67 def first_wday
Chris@0 68 case Setting.start_of_week.to_i
Chris@0 69 when 1
Chris@0 70 @first_dow ||= (1 - 1)%7 + 1
Chris@0 71 when 7
Chris@0 72 @first_dow ||= (7 - 1)%7 + 1
Chris@0 73 else
Chris@0 74 @first_dow ||= (l(:general_first_day_of_week).to_i - 1)%7 + 1
Chris@0 75 end
Chris@0 76 end
Chris@0 77
Chris@0 78 def last_wday
Chris@0 79 @last_dow ||= (first_wday + 5)%7 + 1
Chris@0 80 end
Chris@0 81 end
Chris@0 82 end
Chris@0 83 end