/
i_calendar_controller.rb
117 lines (105 loc) · 4.03 KB
/
i_calendar_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# redmics - redmine icalendar export plugin
# Copyright (c) 2010 Frank Schwarz, frank.schwarz@buschmais.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
require 'icalendar'
class ICalendarController < ApplicationController
before_filter :find_optional_project
accept_key_auth :index
def index
# project
project_condition = @project ? ["#{Project.table_name}.id IN (?)", ([@project.id] + @project.descendants.collect(&:id))] : nil
# issue status
case params[:status]
when 'all'
status_condition = []
when 'open'
status_condition = ["#{IssueStatus.table_name}.is_closed = ?", false]
else
status_condition = nil
end
# assignment
case params[:assigned_to]
when 'me'
assigned_to_condition = ["assigned_to_id = #{User.current.id}"]
when '+'
assigned_to_condition = ["assigned_to_id is not null"]
when '*'
assigned_to_condition = []
else
end
events = []
# queries
unless status_condition.nil? || assigned_to_condition.nil?
c = ARCondition.new(project_condition)
c << status_condition unless status_condition.empty?
c << assigned_to_condition unless assigned_to_condition.empty?
events += Issue.find(
:all,
:include => [:tracker, :assigned_to, :priority, :project, :status, :fixed_version, :author],
:conditions => c.conditions
)
end
events += Version.find(
:all,
:include => [:project],
:conditions => project_condition
)
@cal_string = create_calendar(events).to_ical
send_data @cal_string, :type => Mime::ICS, :filename => 'issues.ics'
end
private
def find_optional_project
@project = Project.find_by_identifier(params[:project_id])
end
def create_calendar(events)
cal = Icalendar::Calendar.new
events.each { |i|
if (i.due_date == nil) && (i.is_a? Issue) && (i.fixed_version != nil)
i.due_date = i.fixed_version.due_date
end
if i.due_date == nil
next
end
event = Icalendar::Event.new
event.dtstart i.due_date, {'VALUE' => 'DATE'}
event.dtend i.due_date + 1, {'VALUE' => 'DATE'}
project_prefix = @project.nil? ? "#{i.project.name}: " : "" # add project name if this is a global feed
if i.is_a? Issue
event.summary "#{project_prefix}#{i.subject} (#{i.status.name})"
event.url url_for(:controller => 'issues', :action => 'show', :id => i.id, :project_id => i.project.identifier)
unless i.fixed_version.nil?
event.categories [i.fixed_version.name]
end
contacts = [i.author, i.assigned_to] + i.watcher_users
contacts.compact.uniq.each do |contact|
event.add_contact contact.name, {"ALTREP" => contact.mail}
end
event.organizer "mailto:#{i.author.mail}", {"CN" => i.author.name}
event.status i.assigned_to == nil ? "TENTATIVE" : "CONFIRMED"
event.created i.created_on.to_date, {'VALUE' => 'DATE'}
elsif i.is_a? Version
event.summary "#{project_prefix}%s '#{i.name}'" % l(:label_calendar_deadline)
event.url url_for(:controller => 'versions', :action => 'show', :id => i.id)
else
end
unless i.description.nil?
event.description = i.description
end
cal.add_event(event)
}
return cal
end
end