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