Chris@909: # Redmine - project management software Chris@909: # Copyright (C) 2006-2011 Jean-Philippe Lang Chris@909: # Chris@909: # This program is free software; you can redistribute it and/or Chris@909: # modify it under the terms of the GNU General Public License Chris@909: # as published by the Free Software Foundation; either version 2 Chris@909: # of the License, or (at your option) any later version. Chris@909: # Chris@909: # This program is distributed in the hope that it will be useful, Chris@909: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@909: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@909: # GNU General Public License for more details. Chris@909: # Chris@909: # You should have received a copy of the GNU General Public License Chris@909: # along with this program; if not, write to the Free Software Chris@909: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@909: Chris@909: module Redmine Chris@909: module Helpers Chris@909: Chris@909: # Simple class to compute the start and end dates of a calendar Chris@909: class Calendar Chris@909: include Redmine::I18n Chris@909: attr_reader :startdt, :enddt Chris@909: Chris@909: def initialize(date, lang = current_language, period = :month) Chris@909: @date = date Chris@909: @events = [] Chris@909: @ending_events_by_days = {} Chris@909: @starting_events_by_days = {} Chris@909: set_language_if_valid lang Chris@909: case period Chris@909: when :month Chris@909: @startdt = Date.civil(date.year, date.month, 1) Chris@909: @enddt = (@startdt >> 1)-1 Chris@909: # starts from the first day of the week Chris@909: @startdt = @startdt - (@startdt.cwday - first_wday)%7 Chris@909: # ends on the last day of the week Chris@909: @enddt = @enddt + (last_wday - @enddt.cwday)%7 Chris@909: when :week Chris@909: @startdt = date - (date.cwday - first_wday)%7 Chris@909: @enddt = date + (last_wday - date.cwday)%7 Chris@909: else Chris@909: raise 'Invalid period' Chris@909: end Chris@909: end Chris@909: Chris@909: # Sets calendar events Chris@909: def events=(events) Chris@909: @events = events Chris@909: @ending_events_by_days = @events.group_by {|event| event.due_date} Chris@909: @starting_events_by_days = @events.group_by {|event| event.start_date} Chris@909: end Chris@909: Chris@909: # Returns events for the given day Chris@909: def events_on(day) Chris@909: ((@ending_events_by_days[day] || []) + (@starting_events_by_days[day] || [])).uniq Chris@909: end Chris@909: Chris@909: # Calendar current month Chris@909: def month Chris@909: @date.month Chris@909: end Chris@909: Chris@909: # Return the first day of week Chris@909: # 1 = Monday ... 7 = Sunday Chris@909: def first_wday Chris@909: case Setting.start_of_week.to_i Chris@909: when 1 Chris@909: @first_dow ||= (1 - 1)%7 + 1 Chris@909: when 6 Chris@909: @first_dow ||= (6 - 1)%7 + 1 Chris@909: when 7 Chris@909: @first_dow ||= (7 - 1)%7 + 1 Chris@909: else Chris@909: @first_dow ||= (l(:general_first_day_of_week).to_i - 1)%7 + 1 Chris@909: end Chris@909: end Chris@909: Chris@909: def last_wday Chris@909: @last_dow ||= (first_wday + 5)%7 + 1 Chris@909: end Chris@909: end Chris@909: end Chris@909: end