annotate lib/redmine/helpers/calendar.rb @ 1600:ed9c467ef922 dockerise

Add hggit extension
author Chris Cannam
date Wed, 23 Aug 2017 11:32:50 +0100
parents e248c7af89ec
children
rev   line source
Chris@909 1 # Redmine - project management software
Chris@1494 2 # Copyright (C) 2006-2014 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@909 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@909 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@909 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@909 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@909 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@909 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@909 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@909 59
Chris@0 60 # Calendar current month
Chris@0 61 def month
Chris@0 62 @date.month
Chris@0 63 end
Chris@909 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@441 71 when 6
Chris@441 72 @first_dow ||= (6 - 1)%7 + 1
Chris@0 73 when 7
Chris@0 74 @first_dow ||= (7 - 1)%7 + 1
Chris@0 75 else
Chris@0 76 @first_dow ||= (l(:general_first_day_of_week).to_i - 1)%7 + 1
Chris@0 77 end
Chris@0 78 end
Chris@909 79
Chris@0 80 def last_wday
Chris@0 81 @last_dow ||= (first_wday + 5)%7 + 1
Chris@0 82 end
Chris@909 83 end
Chris@0 84 end
Chris@0 85 end