To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / lib / redmine / helpers / calendar.rb @ 1298:4f746d8966dd
History | View | Annotate | Download (2.67 KB)
| 1 | 909:cbb26bc654de | Chris | # Redmine - project management software
|
|---|---|---|---|
| 2 | 1295:622f24f53b42 | Chris | # Copyright (C) 2006-2013 Jean-Philippe Lang
|
| 3 | 0:513646585e45 | Chris | #
|
| 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 | 909:cbb26bc654de | Chris | #
|
| 9 | 0:513646585e45 | Chris | # 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 | 909:cbb26bc654de | Chris | #
|
| 14 | 0:513646585e45 | Chris | # 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 | 909:cbb26bc654de | Chris | |
| 21 | 0:513646585e45 | Chris | # 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 | 909:cbb26bc654de | Chris | |
| 26 | 0:513646585e45 | Chris | def initialize(date, lang = current_language, period = :month) |
| 27 | @date = date
|
||
| 28 | @events = []
|
||
| 29 | @ending_events_by_days = {}
|
||
| 30 | @starting_events_by_days = {}
|
||
| 31 | 909:cbb26bc654de | Chris | set_language_if_valid lang |
| 32 | 0:513646585e45 | Chris | 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 | 909:cbb26bc654de | Chris | |
| 48 | 0:513646585e45 | Chris | # 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 | 909:cbb26bc654de | Chris | |
| 55 | 0:513646585e45 | Chris | # 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 | 909:cbb26bc654de | Chris | |
| 60 | 0:513646585e45 | Chris | # Calendar current month
|
| 61 | def month |
||
| 62 | @date.month
|
||
| 63 | end
|
||
| 64 | 909:cbb26bc654de | Chris | |
| 65 | 0:513646585e45 | Chris | # 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 | 441:cbce1fd3b1b7 | Chris | when 6 |
| 72 | @first_dow ||= (6 - 1)%7 + 1 |
||
| 73 | 0:513646585e45 | Chris | 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 | 909:cbb26bc654de | Chris | |
| 80 | 0:513646585e45 | Chris | def last_wday |
| 81 | @last_dow ||= (first_wday + 5)%7 + 1 |
||
| 82 | end
|
||
| 83 | 909:cbb26bc654de | Chris | end
|
| 84 | 0:513646585e45 | Chris | end
|
| 85 | end |