@@ -1,441 +1,442 | |||
|
1 | 1 | # redMine - project management software |
|
2 | 2 | # Copyright (C) 2006-2007 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | class IssuesController < ApplicationController |
|
19 | 19 | layout 'base' |
|
20 | 20 | menu_item :new_issue, :only => :new |
|
21 | 21 | |
|
22 | 22 | before_filter :find_issue, :only => [:show, :edit, :reply, :destroy_attachment] |
|
23 | 23 | before_filter :find_issues, :only => [:bulk_edit, :move, :destroy] |
|
24 | 24 | before_filter :find_project, :only => [:new, :update_form, :preview] |
|
25 | 25 | before_filter :authorize, :except => [:index, :changes, :preview, :update_form, :context_menu] |
|
26 | 26 | before_filter :find_optional_project, :only => [:index, :changes] |
|
27 | 27 | accept_key_auth :index, :changes |
|
28 | 28 | |
|
29 | 29 | helper :journals |
|
30 | 30 | helper :projects |
|
31 | 31 | include ProjectsHelper |
|
32 | 32 | helper :custom_fields |
|
33 | 33 | include CustomFieldsHelper |
|
34 | 34 | helper :ifpdf |
|
35 | 35 | include IfpdfHelper |
|
36 | 36 | helper :issue_relations |
|
37 | 37 | include IssueRelationsHelper |
|
38 | 38 | helper :watchers |
|
39 | 39 | include WatchersHelper |
|
40 | 40 | helper :attachments |
|
41 | 41 | include AttachmentsHelper |
|
42 | 42 | helper :queries |
|
43 | 43 | helper :sort |
|
44 | 44 | include SortHelper |
|
45 | 45 | include IssuesHelper |
|
46 | 46 | |
|
47 | 47 | def index |
|
48 | 48 | sort_init "#{Issue.table_name}.id", "desc" |
|
49 | 49 | sort_update |
|
50 | 50 | retrieve_query |
|
51 | 51 | if @query.valid? |
|
52 | 52 | limit = per_page_option |
|
53 | 53 | respond_to do |format| |
|
54 | 54 | format.html { } |
|
55 | 55 | format.atom { } |
|
56 | 56 | format.csv { limit = Setting.issues_export_limit.to_i } |
|
57 | 57 | format.pdf { limit = Setting.issues_export_limit.to_i } |
|
58 | 58 | end |
|
59 | 59 | @issue_count = Issue.count(:include => [:status, :project], :conditions => @query.statement) |
|
60 | 60 | @issue_pages = Paginator.new self, @issue_count, limit, params['page'] |
|
61 | 61 | @issues = Issue.find :all, :order => sort_clause, |
|
62 | 62 | :include => [ :assigned_to, :status, :tracker, :project, :priority, :category, :fixed_version ], |
|
63 | 63 | :conditions => @query.statement, |
|
64 | 64 | :limit => limit, |
|
65 | 65 | :offset => @issue_pages.current.offset |
|
66 | 66 | respond_to do |format| |
|
67 | 67 | format.html { render :template => 'issues/index.rhtml', :layout => !request.xhr? } |
|
68 | 68 | format.atom { render_feed(@issues, :title => l(:label_issue_plural)) } |
|
69 | 69 | format.csv { send_data(issues_to_csv(@issues, @project).read, :type => 'text/csv; header=present', :filename => 'export.csv') } |
|
70 | 70 | format.pdf { send_data(render(:template => 'issues/index.rfpdf', :layout => false), :type => 'application/pdf', :filename => 'export.pdf') } |
|
71 | 71 | end |
|
72 | 72 | else |
|
73 | 73 | # Send html if the query is not valid |
|
74 | 74 | render(:template => 'issues/index.rhtml', :layout => !request.xhr?) |
|
75 | 75 | end |
|
76 | 76 | rescue ActiveRecord::RecordNotFound |
|
77 | 77 | render_404 |
|
78 | 78 | end |
|
79 | 79 | |
|
80 | 80 | def changes |
|
81 | 81 | sort_init "#{Issue.table_name}.id", "desc" |
|
82 | 82 | sort_update |
|
83 | 83 | retrieve_query |
|
84 | 84 | if @query.valid? |
|
85 | 85 | @journals = Journal.find :all, :include => [ :details, :user, {:issue => [:project, :author, :tracker, :status]} ], |
|
86 | 86 | :conditions => @query.statement, |
|
87 | 87 | :limit => 25, |
|
88 | 88 | :order => "#{Journal.table_name}.created_on DESC" |
|
89 | 89 | end |
|
90 | 90 | @title = (@project ? @project.name : Setting.app_title) + ": " + (@query.new_record? ? l(:label_changes_details) : @query.name) |
|
91 | 91 | render :layout => false, :content_type => 'application/atom+xml' |
|
92 | 92 | rescue ActiveRecord::RecordNotFound |
|
93 | 93 | render_404 |
|
94 | 94 | end |
|
95 | 95 | |
|
96 | 96 | def show |
|
97 | 97 | @custom_values = @project.custom_fields_for_issues(@issue.tracker).collect { |x| @issue.custom_values.find_by_custom_field_id(x.id) || CustomValue.new(:custom_field => x, :customized => @issue) } |
|
98 | 98 | @journals = @issue.journals.find(:all, :include => [:user, :details], :order => "#{Journal.table_name}.created_on ASC") |
|
99 | 99 | @journals.each_with_index {|j,i| j.indice = i+1} |
|
100 | 100 | @journals.reverse! if User.current.wants_comments_in_reverse_order? |
|
101 | 101 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current) |
|
102 | 102 | @edit_allowed = User.current.allowed_to?(:edit_issues, @project) |
|
103 | 103 | @activities = Enumeration::get_values('ACTI') |
|
104 | 104 | @priorities = Enumeration::get_values('IPRI') |
|
105 | 105 | @time_entry = TimeEntry.new |
|
106 | 106 | respond_to do |format| |
|
107 | 107 | format.html { render :template => 'issues/show.rhtml' } |
|
108 | 108 | format.atom { render :action => 'changes', :layout => false, :content_type => 'application/atom+xml' } |
|
109 | 109 | format.pdf { send_data(render(:template => 'issues/show.rfpdf', :layout => false), :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") } |
|
110 | 110 | end |
|
111 | 111 | end |
|
112 | 112 | |
|
113 | 113 | # Add a new issue |
|
114 | 114 | # The new issue will be created from an existing one if copy_from parameter is given |
|
115 | 115 | def new |
|
116 | 116 | @issue = params[:copy_from] ? Issue.new.copy_from(params[:copy_from]) : Issue.new(params[:issue]) |
|
117 | 117 | @issue.project = @project |
|
118 | 118 | @issue.author = User.current |
|
119 | 119 | @issue.tracker ||= @project.trackers.find(params[:tracker_id] ? params[:tracker_id] : :first) |
|
120 | 120 | if @issue.tracker.nil? |
|
121 | 121 | flash.now[:error] = 'No tracker is associated to this project. Please check the Project settings.' |
|
122 | 122 | render :nothing => true, :layout => true |
|
123 | 123 | return |
|
124 | 124 | end |
|
125 | 125 | |
|
126 | 126 | default_status = IssueStatus.default |
|
127 | 127 | unless default_status |
|
128 | 128 | flash.now[:error] = 'No default issue status is defined. Please check your configuration (Go to "Administration -> Issue statuses").' |
|
129 | 129 | render :nothing => true, :layout => true |
|
130 | 130 | return |
|
131 | 131 | end |
|
132 | 132 | @issue.status = default_status |
|
133 | 133 | @allowed_statuses = ([default_status] + default_status.find_new_statuses_allowed_to(User.current.role_for_project(@project), @issue.tracker)).uniq |
|
134 | 134 | |
|
135 | 135 | if request.get? || request.xhr? |
|
136 | 136 | @issue.start_date ||= Date.today |
|
137 | 137 | @custom_values = @issue.custom_values.empty? ? |
|
138 | 138 | @project.custom_fields_for_issues(@issue.tracker).collect { |x| CustomValue.new(:custom_field => x, :customized => @issue) } : |
|
139 | 139 | @issue.custom_values |
|
140 | 140 | else |
|
141 | 141 | requested_status = IssueStatus.find_by_id(params[:issue][:status_id]) |
|
142 | 142 | # Check that the user is allowed to apply the requested status |
|
143 | 143 | @issue.status = (@allowed_statuses.include? requested_status) ? requested_status : default_status |
|
144 | 144 | @custom_values = @project.custom_fields_for_issues(@issue.tracker).collect { |x| CustomValue.new(:custom_field => x, |
|
145 | 145 | :customized => @issue, |
|
146 | 146 | :value => (params[:custom_fields] ? params[:custom_fields][x.id.to_s] : nil)) } |
|
147 | 147 | @issue.custom_values = @custom_values |
|
148 | 148 | if @issue.save |
|
149 | 149 | attach_files(@issue, params[:attachments]) |
|
150 | 150 | flash[:notice] = l(:notice_successful_create) |
|
151 | 151 | Mailer.deliver_issue_add(@issue) if Setting.notified_events.include?('issue_added') |
|
152 | 152 | redirect_to :controller => 'issues', :action => 'show', :id => @issue |
|
153 | 153 | return |
|
154 | 154 | end |
|
155 | 155 | end |
|
156 | 156 | @priorities = Enumeration::get_values('IPRI') |
|
157 | 157 | render :layout => !request.xhr? |
|
158 | 158 | end |
|
159 | 159 | |
|
160 | 160 | # Attributes that can be updated on workflow transition (without :edit permission) |
|
161 | 161 | # TODO: make it configurable (at least per role) |
|
162 | 162 | UPDATABLE_ATTRS_ON_TRANSITION = %w(status_id assigned_to_id fixed_version_id done_ratio) unless const_defined?(:UPDATABLE_ATTRS_ON_TRANSITION) |
|
163 | 163 | |
|
164 | 164 | def edit |
|
165 | 165 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current) |
|
166 | 166 | @activities = Enumeration::get_values('ACTI') |
|
167 | 167 | @priorities = Enumeration::get_values('IPRI') |
|
168 | 168 | @custom_values = [] |
|
169 | 169 | @edit_allowed = User.current.allowed_to?(:edit_issues, @project) |
|
170 | 170 | |
|
171 | 171 | @notes = params[:notes] |
|
172 | 172 | journal = @issue.init_journal(User.current, @notes) |
|
173 | 173 | # User can change issue attributes only if he has :edit permission or if a workflow transition is allowed |
|
174 | 174 | if (@edit_allowed || !@allowed_statuses.empty?) && params[:issue] |
|
175 | 175 | attrs = params[:issue].dup |
|
176 | 176 | attrs.delete_if {|k,v| !UPDATABLE_ATTRS_ON_TRANSITION.include?(k) } unless @edit_allowed |
|
177 | 177 | attrs.delete(:status_id) unless @allowed_statuses.detect {|s| s.id.to_s == attrs[:status_id].to_s} |
|
178 | 178 | @issue.attributes = attrs |
|
179 | 179 | end |
|
180 | 180 | |
|
181 | 181 | if request.get? |
|
182 | 182 | @custom_values = @project.custom_fields_for_issues(@issue.tracker).collect { |x| @issue.custom_values.find_by_custom_field_id(x.id) || CustomValue.new(:custom_field => x, :customized => @issue) } |
|
183 | 183 | else |
|
184 | 184 | # Update custom fields if user has :edit permission |
|
185 | 185 | if @edit_allowed && params[:custom_fields] |
|
186 | 186 | @custom_values = @project.custom_fields_for_issues(@issue.tracker).collect { |x| CustomValue.new(:custom_field => x, :customized => @issue, :value => params["custom_fields"][x.id.to_s]) } |
|
187 | 187 | @issue.custom_values = @custom_values |
|
188 | 188 | end |
|
189 | 189 | @time_entry = TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => Date.today) |
|
190 | 190 | @time_entry.attributes = params[:time_entry] |
|
191 | 191 | attachments = attach_files(@issue, params[:attachments]) |
|
192 | 192 | attachments.each {|a| journal.details << JournalDetail.new(:property => 'attachment', :prop_key => a.id, :value => a.filename)} |
|
193 | 193 | if @issue.save |
|
194 | 194 | # Log spend time |
|
195 | 195 | if current_role.allowed_to?(:log_time) |
|
196 | 196 | @time_entry.save |
|
197 | 197 | end |
|
198 | 198 | if !journal.new_record? |
|
199 | 199 | # Only send notification if something was actually changed |
|
200 | 200 | flash[:notice] = l(:notice_successful_update) |
|
201 | 201 | Mailer.deliver_issue_edit(journal) if Setting.notified_events.include?('issue_updated') |
|
202 | 202 | end |
|
203 | 203 | redirect_to(params[:back_to] || {:action => 'show', :id => @issue}) |
|
204 | 204 | end |
|
205 | 205 | end |
|
206 | 206 | rescue ActiveRecord::StaleObjectError |
|
207 | 207 | # Optimistic locking exception |
|
208 | 208 | flash.now[:error] = l(:notice_locking_conflict) |
|
209 | 209 | end |
|
210 | 210 | |
|
211 | 211 | def reply |
|
212 | 212 | journal = Journal.find(params[:journal_id]) if params[:journal_id] |
|
213 | 213 | if journal |
|
214 | 214 | user = journal.user |
|
215 | 215 | text = journal.notes |
|
216 | 216 | else |
|
217 | 217 | user = @issue.author |
|
218 | 218 | text = @issue.description |
|
219 | 219 | end |
|
220 | 220 | content = "#{ll(Setting.default_language, :text_user_wrote, user)}\\n> " |
|
221 | 221 | content << text.to_s.strip.gsub(%r{<pre>((.|\s)*?)</pre>}m, '[...]').gsub('"', '\"').gsub(/(\r?\n|\r\n?)/, "\\n> ") + "\\n\\n" |
|
222 | 222 | render(:update) { |page| |
|
223 | 223 | page.<< "$('notes').value = \"#{content}\";" |
|
224 | 224 | page.show 'update' |
|
225 | 225 | page << "Form.Element.focus('notes');" |
|
226 | 226 | page << "Element.scrollTo('update');" |
|
227 | 227 | page << "$('notes').scrollTop = $('notes').scrollHeight - $('notes').clientHeight;" |
|
228 | 228 | } |
|
229 | 229 | end |
|
230 | 230 | |
|
231 | 231 | # Bulk edit a set of issues |
|
232 | 232 | def bulk_edit |
|
233 | 233 | if request.post? |
|
234 | 234 | status = params[:status_id].blank? ? nil : IssueStatus.find_by_id(params[:status_id]) |
|
235 | 235 | priority = params[:priority_id].blank? ? nil : Enumeration.find_by_id(params[:priority_id]) |
|
236 | 236 | assigned_to = (params[:assigned_to_id].blank? || params[:assigned_to_id] == 'none') ? nil : User.find_by_id(params[:assigned_to_id]) |
|
237 | 237 | category = (params[:category_id].blank? || params[:category_id] == 'none') ? nil : @project.issue_categories.find_by_id(params[:category_id]) |
|
238 | 238 | fixed_version = (params[:fixed_version_id].blank? || params[:fixed_version_id] == 'none') ? nil : @project.versions.find_by_id(params[:fixed_version_id]) |
|
239 | 239 | |
|
240 | 240 | unsaved_issue_ids = [] |
|
241 | 241 | @issues.each do |issue| |
|
242 | 242 | journal = issue.init_journal(User.current, params[:notes]) |
|
243 | 243 | issue.priority = priority if priority |
|
244 | 244 | issue.assigned_to = assigned_to if assigned_to || params[:assigned_to_id] == 'none' |
|
245 | 245 | issue.category = category if category || params[:category_id] == 'none' |
|
246 | 246 | issue.fixed_version = fixed_version if fixed_version || params[:fixed_version_id] == 'none' |
|
247 | 247 | issue.start_date = params[:start_date] unless params[:start_date].blank? |
|
248 | 248 | issue.due_date = params[:due_date] unless params[:due_date].blank? |
|
249 | 249 | issue.done_ratio = params[:done_ratio] unless params[:done_ratio].blank? |
|
250 | 250 | # Don't save any change to the issue if the user is not authorized to apply the requested status |
|
251 | 251 | if (status.nil? || (issue.status.new_status_allowed_to?(status, current_role, issue.tracker) && issue.status = status)) && issue.save |
|
252 | 252 | # Send notification for each issue (if changed) |
|
253 | 253 | Mailer.deliver_issue_edit(journal) if journal.details.any? && Setting.notified_events.include?('issue_updated') |
|
254 | 254 | else |
|
255 | 255 | # Keep unsaved issue ids to display them in flash error |
|
256 | 256 | unsaved_issue_ids << issue.id |
|
257 | 257 | end |
|
258 | 258 | end |
|
259 | 259 | if unsaved_issue_ids.empty? |
|
260 | 260 | flash[:notice] = l(:notice_successful_update) unless @issues.empty? |
|
261 | 261 | else |
|
262 | 262 | flash[:error] = l(:notice_failed_to_save_issues, unsaved_issue_ids.size, @issues.size, '#' + unsaved_issue_ids.join(', #')) |
|
263 | 263 | end |
|
264 | 264 | redirect_to :controller => 'issues', :action => 'index', :project_id => @project |
|
265 | 265 | return |
|
266 | 266 | end |
|
267 | 267 | # Find potential statuses the user could be allowed to switch issues to |
|
268 | 268 | @available_statuses = Workflow.find(:all, :include => :new_status, |
|
269 | 269 | :conditions => {:role_id => current_role.id}).collect(&:new_status).compact.uniq |
|
270 | 270 | end |
|
271 | 271 | |
|
272 | 272 | def move |
|
273 | 273 | @allowed_projects = [] |
|
274 | 274 | # find projects to which the user is allowed to move the issue |
|
275 | 275 | if User.current.admin? |
|
276 | 276 | # admin is allowed to move issues to any active (visible) project |
|
277 | 277 | @allowed_projects = Project.find(:all, :conditions => Project.visible_by(User.current), :order => 'name') |
|
278 | 278 | else |
|
279 | 279 | User.current.memberships.each {|m| @allowed_projects << m.project if m.role.allowed_to?(:move_issues)} |
|
280 | 280 | end |
|
281 | 281 | @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:new_project_id]} if params[:new_project_id] |
|
282 | 282 | @target_project ||= @project |
|
283 | 283 | @trackers = @target_project.trackers |
|
284 | 284 | if request.post? |
|
285 | 285 | new_tracker = params[:new_tracker_id].blank? ? nil : @target_project.trackers.find_by_id(params[:new_tracker_id]) |
|
286 | 286 | unsaved_issue_ids = [] |
|
287 | 287 | @issues.each do |issue| |
|
288 | issue.init_journal(User.current) | |
|
288 | 289 | unsaved_issue_ids << issue.id unless issue.move_to(@target_project, new_tracker) |
|
289 | 290 | end |
|
290 | 291 | if unsaved_issue_ids.empty? |
|
291 | 292 | flash[:notice] = l(:notice_successful_update) unless @issues.empty? |
|
292 | 293 | else |
|
293 | 294 | flash[:error] = l(:notice_failed_to_save_issues, unsaved_issue_ids.size, @issues.size, '#' + unsaved_issue_ids.join(', #')) |
|
294 | 295 | end |
|
295 | 296 | redirect_to :controller => 'issues', :action => 'index', :project_id => @project |
|
296 | 297 | return |
|
297 | 298 | end |
|
298 | 299 | render :layout => false if request.xhr? |
|
299 | 300 | end |
|
300 | 301 | |
|
301 | 302 | def destroy |
|
302 | 303 | @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f |
|
303 | 304 | if @hours > 0 |
|
304 | 305 | case params[:todo] |
|
305 | 306 | when 'destroy' |
|
306 | 307 | # nothing to do |
|
307 | 308 | when 'nullify' |
|
308 | 309 | TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues]) |
|
309 | 310 | when 'reassign' |
|
310 | 311 | reassign_to = @project.issues.find_by_id(params[:reassign_to_id]) |
|
311 | 312 | if reassign_to.nil? |
|
312 | 313 | flash.now[:error] = l(:error_issue_not_found_in_project) |
|
313 | 314 | return |
|
314 | 315 | else |
|
315 | 316 | TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues]) |
|
316 | 317 | end |
|
317 | 318 | else |
|
318 | 319 | # display the destroy form |
|
319 | 320 | return |
|
320 | 321 | end |
|
321 | 322 | end |
|
322 | 323 | @issues.each(&:destroy) |
|
323 | 324 | redirect_to :action => 'index', :project_id => @project |
|
324 | 325 | end |
|
325 | 326 | |
|
326 | 327 | def destroy_attachment |
|
327 | 328 | a = @issue.attachments.find(params[:attachment_id]) |
|
328 | 329 | a.destroy |
|
329 | 330 | journal = @issue.init_journal(User.current) |
|
330 | 331 | journal.details << JournalDetail.new(:property => 'attachment', |
|
331 | 332 | :prop_key => a.id, |
|
332 | 333 | :old_value => a.filename) |
|
333 | 334 | journal.save |
|
334 | 335 | redirect_to :action => 'show', :id => @issue |
|
335 | 336 | end |
|
336 | 337 | |
|
337 | 338 | def context_menu |
|
338 | 339 | @issues = Issue.find_all_by_id(params[:ids], :include => :project) |
|
339 | 340 | if (@issues.size == 1) |
|
340 | 341 | @issue = @issues.first |
|
341 | 342 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current) |
|
342 | 343 | @assignables = @issue.assignable_users |
|
343 | 344 | @assignables << @issue.assigned_to if @issue.assigned_to && !@assignables.include?(@issue.assigned_to) |
|
344 | 345 | end |
|
345 | 346 | projects = @issues.collect(&:project).compact.uniq |
|
346 | 347 | @project = projects.first if projects.size == 1 |
|
347 | 348 | |
|
348 | 349 | @can = {:edit => (@project && User.current.allowed_to?(:edit_issues, @project)), |
|
349 | 350 | :update => (@issue && (User.current.allowed_to?(:edit_issues, @project) || (User.current.allowed_to?(:change_status, @project) && !@allowed_statuses.empty?))), |
|
350 | 351 | :move => (@project && User.current.allowed_to?(:move_issues, @project)), |
|
351 | 352 | :copy => (@issue && @project.trackers.include?(@issue.tracker) && User.current.allowed_to?(:add_issues, @project)), |
|
352 | 353 | :delete => (@project && User.current.allowed_to?(:delete_issues, @project)) |
|
353 | 354 | } |
|
354 | 355 | |
|
355 | 356 | @priorities = Enumeration.get_values('IPRI').reverse |
|
356 | 357 | @statuses = IssueStatus.find(:all, :order => 'position') |
|
357 | 358 | @back = request.env['HTTP_REFERER'] |
|
358 | 359 | |
|
359 | 360 | render :layout => false |
|
360 | 361 | end |
|
361 | 362 | |
|
362 | 363 | def update_form |
|
363 | 364 | @issue = Issue.new(params[:issue]) |
|
364 | 365 | render :action => :new, :layout => false |
|
365 | 366 | end |
|
366 | 367 | |
|
367 | 368 | def preview |
|
368 | 369 | @issue = @project.issues.find_by_id(params[:id]) unless params[:id].blank? |
|
369 | 370 | @attachements = @issue.attachments if @issue |
|
370 | 371 | @text = params[:notes] || (params[:issue] ? params[:issue][:description] : nil) |
|
371 | 372 | render :partial => 'common/preview' |
|
372 | 373 | end |
|
373 | 374 | |
|
374 | 375 | private |
|
375 | 376 | def find_issue |
|
376 | 377 | @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category]) |
|
377 | 378 | @project = @issue.project |
|
378 | 379 | rescue ActiveRecord::RecordNotFound |
|
379 | 380 | render_404 |
|
380 | 381 | end |
|
381 | 382 | |
|
382 | 383 | # Filter for bulk operations |
|
383 | 384 | def find_issues |
|
384 | 385 | @issues = Issue.find_all_by_id(params[:id] || params[:ids]) |
|
385 | 386 | raise ActiveRecord::RecordNotFound if @issues.empty? |
|
386 | 387 | projects = @issues.collect(&:project).compact.uniq |
|
387 | 388 | if projects.size == 1 |
|
388 | 389 | @project = projects.first |
|
389 | 390 | else |
|
390 | 391 | # TODO: let users bulk edit/move/destroy issues from different projects |
|
391 | 392 | render_error 'Can not bulk edit/move/destroy issues from different projects' and return false |
|
392 | 393 | end |
|
393 | 394 | rescue ActiveRecord::RecordNotFound |
|
394 | 395 | render_404 |
|
395 | 396 | end |
|
396 | 397 | |
|
397 | 398 | def find_project |
|
398 | 399 | @project = Project.find(params[:project_id]) |
|
399 | 400 | rescue ActiveRecord::RecordNotFound |
|
400 | 401 | render_404 |
|
401 | 402 | end |
|
402 | 403 | |
|
403 | 404 | def find_optional_project |
|
404 | 405 | return true unless params[:project_id] |
|
405 | 406 | @project = Project.find(params[:project_id]) |
|
406 | 407 | authorize |
|
407 | 408 | rescue ActiveRecord::RecordNotFound |
|
408 | 409 | render_404 |
|
409 | 410 | end |
|
410 | 411 | |
|
411 | 412 | # Retrieve query from session or build a new query |
|
412 | 413 | def retrieve_query |
|
413 | 414 | if !params[:query_id].blank? |
|
414 | 415 | cond = "project_id IS NULL" |
|
415 | 416 | cond << " OR project_id = #{@project.id}" if @project |
|
416 | 417 | @query = Query.find(params[:query_id], :conditions => cond) |
|
417 | 418 | @query.project = @project |
|
418 | 419 | session[:query] = {:id => @query.id, :project_id => @query.project_id} |
|
419 | 420 | else |
|
420 | 421 | if params[:set_filter] || session[:query].nil? || session[:query][:project_id] != (@project ? @project.id : nil) |
|
421 | 422 | # Give it a name, required to be valid |
|
422 | 423 | @query = Query.new(:name => "_") |
|
423 | 424 | @query.project = @project |
|
424 | 425 | if params[:fields] and params[:fields].is_a? Array |
|
425 | 426 | params[:fields].each do |field| |
|
426 | 427 | @query.add_filter(field, params[:operators][field], params[:values][field]) |
|
427 | 428 | end |
|
428 | 429 | else |
|
429 | 430 | @query.available_filters.keys.each do |field| |
|
430 | 431 | @query.add_short_filter(field, params[field]) if params[field] |
|
431 | 432 | end |
|
432 | 433 | end |
|
433 | 434 | session[:query] = {:project_id => @query.project_id, :filters => @query.filters} |
|
434 | 435 | else |
|
435 | 436 | @query = Query.find_by_id(session[:query][:id]) if session[:query][:id] |
|
436 | 437 | @query ||= Query.new(:name => "_", :project => @project, :filters => session[:query][:filters]) |
|
437 | 438 | @query.project = @project |
|
438 | 439 | end |
|
439 | 440 | end |
|
440 | 441 | end |
|
441 | 442 | end |
@@ -1,177 +1,183 | |||
|
1 | 1 | # redMine - project management software |
|
2 | 2 | # Copyright (C) 2006 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | require 'csv' |
|
19 | 19 | |
|
20 | 20 | module IssuesHelper |
|
21 | 21 | include ApplicationHelper |
|
22 | 22 | |
|
23 | 23 | def render_issue_tooltip(issue) |
|
24 | 24 | @cached_label_start_date ||= l(:field_start_date) |
|
25 | 25 | @cached_label_due_date ||= l(:field_due_date) |
|
26 | 26 | @cached_label_assigned_to ||= l(:field_assigned_to) |
|
27 | 27 | @cached_label_priority ||= l(:field_priority) |
|
28 | 28 | |
|
29 | 29 | link_to_issue(issue) + ": #{h(issue.subject)}<br /><br />" + |
|
30 | 30 | "<strong>#{@cached_label_start_date}</strong>: #{format_date(issue.start_date)}<br />" + |
|
31 | 31 | "<strong>#{@cached_label_due_date}</strong>: #{format_date(issue.due_date)}<br />" + |
|
32 | 32 | "<strong>#{@cached_label_assigned_to}</strong>: #{issue.assigned_to}<br />" + |
|
33 | 33 | "<strong>#{@cached_label_priority}</strong>: #{issue.priority.name}" |
|
34 | 34 | end |
|
35 | 35 | |
|
36 | 36 | def sidebar_queries |
|
37 | 37 | unless @sidebar_queries |
|
38 | 38 | # User can see public queries and his own queries |
|
39 | 39 | visible = ARCondition.new(["is_public = ? OR user_id = ?", true, (User.current.logged? ? User.current.id : 0)]) |
|
40 | 40 | # Project specific queries and global queries |
|
41 | 41 | visible << (@project.nil? ? ["project_id IS NULL"] : ["project_id IS NULL OR project_id = ?", @project.id]) |
|
42 | 42 | @sidebar_queries = Query.find(:all, |
|
43 | 43 | :order => "name ASC", |
|
44 | 44 | :conditions => visible.conditions) |
|
45 | 45 | end |
|
46 | 46 | @sidebar_queries |
|
47 | 47 | end |
|
48 | 48 | |
|
49 | 49 | def show_detail(detail, no_html=false) |
|
50 | 50 | case detail.property |
|
51 | 51 | when 'attr' |
|
52 | 52 | label = l(("field_" + detail.prop_key.to_s.gsub(/\_id$/, "")).to_sym) |
|
53 | 53 | case detail.prop_key |
|
54 | 54 | when 'due_date', 'start_date' |
|
55 | 55 | value = format_date(detail.value.to_date) if detail.value |
|
56 | 56 | old_value = format_date(detail.old_value.to_date) if detail.old_value |
|
57 | when 'project_id' | |
|
58 | p = Project.find_by_id(detail.value) and value = p.name if detail.value | |
|
59 | p = Project.find_by_id(detail.old_value) and old_value = p.name if detail.old_value | |
|
57 | 60 | when 'status_id' |
|
58 | 61 | s = IssueStatus.find_by_id(detail.value) and value = s.name if detail.value |
|
59 | 62 | s = IssueStatus.find_by_id(detail.old_value) and old_value = s.name if detail.old_value |
|
63 | when 'tracker_id' | |
|
64 | t = Tracker.find_by_id(detail.value) and value = t.name if detail.value | |
|
65 | t = Tracker.find_by_id(detail.old_value) and old_value = t.name if detail.old_value | |
|
60 | 66 | when 'assigned_to_id' |
|
61 | 67 | u = User.find_by_id(detail.value) and value = u.name if detail.value |
|
62 | 68 | u = User.find_by_id(detail.old_value) and old_value = u.name if detail.old_value |
|
63 | 69 | when 'priority_id' |
|
64 | 70 | e = Enumeration.find_by_id(detail.value) and value = e.name if detail.value |
|
65 | 71 | e = Enumeration.find_by_id(detail.old_value) and old_value = e.name if detail.old_value |
|
66 | 72 | when 'category_id' |
|
67 | 73 | c = IssueCategory.find_by_id(detail.value) and value = c.name if detail.value |
|
68 | 74 | c = IssueCategory.find_by_id(detail.old_value) and old_value = c.name if detail.old_value |
|
69 | 75 | when 'fixed_version_id' |
|
70 | 76 | v = Version.find_by_id(detail.value) and value = v.name if detail.value |
|
71 | 77 | v = Version.find_by_id(detail.old_value) and old_value = v.name if detail.old_value |
|
72 | 78 | end |
|
73 | 79 | when 'cf' |
|
74 | 80 | custom_field = CustomField.find_by_id(detail.prop_key) |
|
75 | 81 | if custom_field |
|
76 | 82 | label = custom_field.name |
|
77 | 83 | value = format_value(detail.value, custom_field.field_format) if detail.value |
|
78 | 84 | old_value = format_value(detail.old_value, custom_field.field_format) if detail.old_value |
|
79 | 85 | end |
|
80 | 86 | when 'attachment' |
|
81 | 87 | label = l(:label_attachment) |
|
82 | 88 | end |
|
83 | 89 | |
|
84 | 90 | label ||= detail.prop_key |
|
85 | 91 | value ||= detail.value |
|
86 | 92 | old_value ||= detail.old_value |
|
87 | 93 | |
|
88 | 94 | unless no_html |
|
89 | 95 | label = content_tag('strong', label) |
|
90 | 96 | old_value = content_tag("i", h(old_value)) if detail.old_value |
|
91 | 97 | old_value = content_tag("strike", old_value) if detail.old_value and (!detail.value or detail.value.empty?) |
|
92 | 98 | if detail.property == 'attachment' && !value.blank? && Attachment.find_by_id(detail.prop_key) |
|
93 | 99 | # Link to the attachment if it has not been removed |
|
94 | 100 | value = link_to(value, :controller => 'attachments', :action => 'show', :id => detail.prop_key) |
|
95 | 101 | else |
|
96 | 102 | value = content_tag("i", h(value)) if value |
|
97 | 103 | end |
|
98 | 104 | end |
|
99 | 105 | |
|
100 | 106 | if !detail.value.blank? |
|
101 | 107 | case detail.property |
|
102 | 108 | when 'attr', 'cf' |
|
103 | 109 | if !detail.old_value.blank? |
|
104 | 110 | label + " " + l(:text_journal_changed, old_value, value) |
|
105 | 111 | else |
|
106 | 112 | label + " " + l(:text_journal_set_to, value) |
|
107 | 113 | end |
|
108 | 114 | when 'attachment' |
|
109 | 115 | "#{label} #{value} #{l(:label_added)}" |
|
110 | 116 | end |
|
111 | 117 | else |
|
112 | 118 | case detail.property |
|
113 | 119 | when 'attr', 'cf' |
|
114 | 120 | label + " " + l(:text_journal_deleted) + " (#{old_value})" |
|
115 | 121 | when 'attachment' |
|
116 | 122 | "#{label} #{old_value} #{l(:label_deleted)}" |
|
117 | 123 | end |
|
118 | 124 | end |
|
119 | 125 | end |
|
120 | 126 | |
|
121 | 127 | def issues_to_csv(issues, project = nil) |
|
122 | 128 | ic = Iconv.new(l(:general_csv_encoding), 'UTF-8') |
|
123 | 129 | export = StringIO.new |
|
124 | 130 | CSV::Writer.generate(export, l(:general_csv_separator)) do |csv| |
|
125 | 131 | # csv header fields |
|
126 | 132 | headers = [ "#", |
|
127 | 133 | l(:field_status), |
|
128 | 134 | l(:field_project), |
|
129 | 135 | l(:field_tracker), |
|
130 | 136 | l(:field_priority), |
|
131 | 137 | l(:field_subject), |
|
132 | 138 | l(:field_assigned_to), |
|
133 | 139 | l(:field_category), |
|
134 | 140 | l(:field_fixed_version), |
|
135 | 141 | l(:field_author), |
|
136 | 142 | l(:field_start_date), |
|
137 | 143 | l(:field_due_date), |
|
138 | 144 | l(:field_done_ratio), |
|
139 | 145 | l(:field_estimated_hours), |
|
140 | 146 | l(:field_created_on), |
|
141 | 147 | l(:field_updated_on) |
|
142 | 148 | ] |
|
143 | 149 | # Export project custom fields if project is given |
|
144 | 150 | # otherwise export custom fields marked as "For all projects" |
|
145 | 151 | custom_fields = project.nil? ? IssueCustomField.for_all : project.all_custom_fields |
|
146 | 152 | custom_fields.each {|f| headers << f.name} |
|
147 | 153 | # Description in the last column |
|
148 | 154 | headers << l(:field_description) |
|
149 | 155 | csv << headers.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end } |
|
150 | 156 | # csv lines |
|
151 | 157 | issues.each do |issue| |
|
152 | 158 | fields = [issue.id, |
|
153 | 159 | issue.status.name, |
|
154 | 160 | issue.project.name, |
|
155 | 161 | issue.tracker.name, |
|
156 | 162 | issue.priority.name, |
|
157 | 163 | issue.subject, |
|
158 | 164 | issue.assigned_to, |
|
159 | 165 | issue.category, |
|
160 | 166 | issue.fixed_version, |
|
161 | 167 | issue.author.name, |
|
162 | 168 | format_date(issue.start_date), |
|
163 | 169 | format_date(issue.due_date), |
|
164 | 170 | issue.done_ratio, |
|
165 | 171 | issue.estimated_hours, |
|
166 | 172 | format_time(issue.created_on), |
|
167 | 173 | format_time(issue.updated_on) |
|
168 | 174 | ] |
|
169 | 175 | custom_fields.each {|f| fields << show_value(issue.custom_value_for(f)) } |
|
170 | 176 | fields << issue.description |
|
171 | 177 | csv << fields.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end } |
|
172 | 178 | end |
|
173 | 179 | end |
|
174 | 180 | export.rewind |
|
175 | 181 | export |
|
176 | 182 | end |
|
177 | 183 | end |
General Comments 0
You need to be logged in to leave comments.
Login now