comparison lib/redmine/helpers/calendar.rb @ 909:cbb26bc654de redmine-1.3

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