annotate .svn/pristine/32/32c52319e33e839952a511178f3d013ae8279f9b.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

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