File: lib/redmine/helpers/calendar.rb

Overview
Module Structure
Class Hierarchy
Code

Overview

Module Structure

  module: <Toplevel Module>
  module: Redmine#18
  module: Helpers#19
  class: Calendar#22
includes
  I18n ( Redmine )
inherits from
  Object ( Builtin-Module )
has properties
attribute: startdt [R] #24
attribute: enddt [R] #24
method: initialize / 3 #26
method: events= / 1 #49
method: events_on / 1 #56
method: month #61
method: first_wday #67
method: last_wday #80

Class Hierarchy

Code

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