1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 # Simple class to compute the start and end dates of a calendar
24 attr_reader :startdt, :enddt
26 def initialize(date, lang = current_language, period = :month)
29 @ending_events_by_days = {}
30 @starting_events_by_days = {}
31 set_language_if_valid lang
34 @startdt = Date.civil(date.year, date.month, 1)
35 @enddt = (@startdt >> 1)-1
36 # starts from the first day of the week
37 @startdt = @startdt - (@startdt.cwday - first_wday)%7
38 # ends on the last day of the week
39 @enddt = @enddt + (last_wday - @enddt.cwday)%7
41 @startdt = date - (date.cwday - first_wday)%7
42 @enddt = date + (last_wday - date.cwday)%7
44 raise 'Invalid period'
48 # Sets calendar events
51 @ending_events_by_days = @events.group_by {|event| event.due_date}
52 @starting_events_by_days = @events.group_by {|event| event.start_date}
55 # Returns events for the given day
57 ((@ending_events_by_days[day] || []) + (@starting_events_by_days[day] || [])).uniq
60 # Calendar current month
65 # Return the first day of week
66 # 1 = Monday ... 7 = Sunday
68 case Setting.start_of_week.to_i
70 @first_dow ||= (1 - 1)%7 + 1
72 @first_dow ||= (7 - 1)%7 + 1
74 @first_dow ||= (l(:general_first_day_of_week).to_i - 1)%7 + 1
79 @last_dow ||= (first_wday + 5)%7 + 1