##// END OF EJS Templates
Preload issue relations when exporting issues to CSV with all columns (#16091)....
Jean-Philippe Lang -
r12635:15c85ededd94
parent child
Show More
@@ -1,481 +1,484
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2014 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 menu_item :new_issue, :only => [:new, :create]
20 20 default_search_scope :issues
21 21
22 22 before_filter :find_issue, :only => [:show, :edit, :update]
23 23 before_filter :find_issues, :only => [:bulk_edit, :bulk_update, :destroy]
24 24 before_filter :find_project, :only => [:new, :create, :update_form]
25 25 before_filter :authorize, :except => [:index]
26 26 before_filter :find_optional_project, :only => [:index]
27 27 before_filter :check_for_default_issue_status, :only => [:new, :create]
28 28 before_filter :build_new_issue_from_params, :only => [:new, :create, :update_form]
29 29 accept_rss_auth :index, :show
30 30 accept_api_auth :index, :show, :create, :update, :destroy
31 31
32 32 rescue_from Query::StatementInvalid, :with => :query_statement_invalid
33 33
34 34 helper :journals
35 35 helper :projects
36 36 include ProjectsHelper
37 37 helper :custom_fields
38 38 include CustomFieldsHelper
39 39 helper :issue_relations
40 40 include IssueRelationsHelper
41 41 helper :watchers
42 42 include WatchersHelper
43 43 helper :attachments
44 44 include AttachmentsHelper
45 45 helper :queries
46 46 include QueriesHelper
47 47 helper :repositories
48 48 include RepositoriesHelper
49 49 helper :sort
50 50 include SortHelper
51 51 include IssuesHelper
52 52 helper :timelog
53 53 include Redmine::Export::PDF
54 54
55 55 def index
56 56 retrieve_query
57 57 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
58 58 sort_update(@query.sortable_columns)
59 59 @query.sort_criteria = sort_criteria.to_a
60 60
61 61 if @query.valid?
62 62 case params[:format]
63 63 when 'csv', 'pdf'
64 64 @limit = Setting.issues_export_limit.to_i
65 if params[:columns] == 'all'
66 @query.column_names = @query.available_inline_columns.map(&:name)
67 end
65 68 when 'atom'
66 69 @limit = Setting.feeds_limit.to_i
67 70 when 'xml', 'json'
68 71 @offset, @limit = api_offset_and_limit
69 72 else
70 73 @limit = per_page_option
71 74 end
72 75
73 76 @issue_count = @query.issue_count
74 77 @issue_pages = Paginator.new @issue_count, @limit, params['page']
75 78 @offset ||= @issue_pages.offset
76 79 @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version],
77 80 :order => sort_clause,
78 81 :offset => @offset,
79 82 :limit => @limit)
80 83 @issue_count_by_group = @query.issue_count_by_group
81 84
82 85 respond_to do |format|
83 86 format.html { render :template => 'issues/index', :layout => !request.xhr? }
84 87 format.api {
85 88 Issue.load_visible_relations(@issues) if include_in_api_response?('relations')
86 89 }
87 90 format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") }
88 91 format.csv { send_data(query_to_csv(@issues, @query, params), :type => 'text/csv; header=present', :filename => 'issues.csv') }
89 92 format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'issues.pdf') }
90 93 end
91 94 else
92 95 respond_to do |format|
93 96 format.html { render(:template => 'issues/index', :layout => !request.xhr?) }
94 97 format.any(:atom, :csv, :pdf) { render(:nothing => true) }
95 98 format.api { render_validation_errors(@query) }
96 99 end
97 100 end
98 101 rescue ActiveRecord::RecordNotFound
99 102 render_404
100 103 end
101 104
102 105 def show
103 106 @journals = @issue.journals.includes(:user, :details).reorder("#{Journal.table_name}.id ASC").all
104 107 @journals.each_with_index {|j,i| j.indice = i+1}
105 108 @journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project)
106 109 Journal.preload_journals_details_custom_fields(@journals)
107 110 # TODO: use #select! when ruby1.8 support is dropped
108 111 @journals.reject! {|journal| !journal.notes? && journal.visible_details.empty?}
109 112 @journals.reverse! if User.current.wants_comments_in_reverse_order?
110 113
111 114 @changesets = @issue.changesets.visible.all
112 115 @changesets.reverse! if User.current.wants_comments_in_reverse_order?
113 116
114 117 @relations = @issue.relations.select {|r| r.other_issue(@issue) && r.other_issue(@issue).visible? }
115 118 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
116 119 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
117 120 @priorities = IssuePriority.active
118 121 @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
119 122 @relation = IssueRelation.new
120 123
121 124 respond_to do |format|
122 125 format.html {
123 126 retrieve_previous_and_next_issue_ids
124 127 render :template => 'issues/show'
125 128 }
126 129 format.api
127 130 format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' }
128 131 format.pdf {
129 132 pdf = issue_to_pdf(@issue, :journals => @journals)
130 133 send_data(pdf, :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf")
131 134 }
132 135 end
133 136 end
134 137
135 138 # Add a new issue
136 139 # The new issue will be created from an existing one if copy_from parameter is given
137 140 def new
138 141 respond_to do |format|
139 142 format.html { render :action => 'new', :layout => !request.xhr? }
140 143 end
141 144 end
142 145
143 146 def create
144 147 call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
145 148 @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
146 149 if @issue.save
147 150 call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
148 151 respond_to do |format|
149 152 format.html {
150 153 render_attachment_warning_if_needed(@issue)
151 154 flash[:notice] = l(:notice_issue_successful_create, :id => view_context.link_to("##{@issue.id}", issue_path(@issue), :title => @issue.subject))
152 155 if params[:continue]
153 156 attrs = {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?}
154 157 redirect_to new_project_issue_path(@issue.project, :issue => attrs)
155 158 else
156 159 redirect_to issue_path(@issue)
157 160 end
158 161 }
159 162 format.api { render :action => 'show', :status => :created, :location => issue_url(@issue) }
160 163 end
161 164 return
162 165 else
163 166 respond_to do |format|
164 167 format.html { render :action => 'new' }
165 168 format.api { render_validation_errors(@issue) }
166 169 end
167 170 end
168 171 end
169 172
170 173 def edit
171 174 return unless update_issue_from_params
172 175
173 176 respond_to do |format|
174 177 format.html { }
175 178 format.xml { }
176 179 end
177 180 end
178 181
179 182 def update
180 183 return unless update_issue_from_params
181 184 @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
182 185 saved = false
183 186 begin
184 187 saved = save_issue_with_child_records
185 188 rescue ActiveRecord::StaleObjectError
186 189 @conflict = true
187 190 if params[:last_journal_id]
188 191 @conflict_journals = @issue.journals_after(params[:last_journal_id]).all
189 192 @conflict_journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project)
190 193 end
191 194 end
192 195
193 196 if saved
194 197 render_attachment_warning_if_needed(@issue)
195 198 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
196 199
197 200 respond_to do |format|
198 201 format.html { redirect_back_or_default issue_path(@issue) }
199 202 format.api { render_api_ok }
200 203 end
201 204 else
202 205 respond_to do |format|
203 206 format.html { render :action => 'edit' }
204 207 format.api { render_validation_errors(@issue) }
205 208 end
206 209 end
207 210 end
208 211
209 212 # Updates the issue form when changing the project, status or tracker
210 213 # on issue creation/update
211 214 def update_form
212 215 end
213 216
214 217 # Bulk edit/copy a set of issues
215 218 def bulk_edit
216 219 @issues.sort!
217 220 @copy = params[:copy].present?
218 221 @notes = params[:notes]
219 222
220 223 if User.current.allowed_to?(:move_issues, @projects)
221 224 @allowed_projects = Issue.allowed_target_projects_on_move
222 225 if params[:issue]
223 226 @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:issue][:project_id].to_s}
224 227 if @target_project
225 228 target_projects = [@target_project]
226 229 end
227 230 end
228 231 end
229 232 target_projects ||= @projects
230 233
231 234 if @copy
232 235 @available_statuses = [IssueStatus.default]
233 236 else
234 237 @available_statuses = @issues.map(&:new_statuses_allowed_to).reduce(:&)
235 238 end
236 239 @custom_fields = target_projects.map{|p|p.all_issue_custom_fields.visible}.reduce(:&)
237 240 @assignables = target_projects.map(&:assignable_users).reduce(:&)
238 241 @trackers = target_projects.map(&:trackers).reduce(:&)
239 242 @versions = target_projects.map {|p| p.shared_versions.open}.reduce(:&)
240 243 @categories = target_projects.map {|p| p.issue_categories}.reduce(:&)
241 244 if @copy
242 245 @attachments_present = @issues.detect {|i| i.attachments.any?}.present?
243 246 @subtasks_present = @issues.detect {|i| !i.leaf?}.present?
244 247 end
245 248
246 249 @safe_attributes = @issues.map(&:safe_attribute_names).reduce(:&)
247 250
248 251 @issue_params = params[:issue] || {}
249 252 @issue_params[:custom_field_values] ||= {}
250 253 end
251 254
252 255 def bulk_update
253 256 @issues.sort!
254 257 @copy = params[:copy].present?
255 258 attributes = parse_params_for_bulk_issue_attributes(params)
256 259
257 260 unsaved_issues = []
258 261 saved_issues = []
259 262
260 263 if @copy && params[:copy_subtasks].present?
261 264 # Descendant issues will be copied with the parent task
262 265 # Don't copy them twice
263 266 @issues.reject! {|issue| @issues.detect {|other| issue.is_descendant_of?(other)}}
264 267 end
265 268
266 269 @issues.each do |orig_issue|
267 270 orig_issue.reload
268 271 if @copy
269 272 issue = orig_issue.copy({},
270 273 :attachments => params[:copy_attachments].present?,
271 274 :subtasks => params[:copy_subtasks].present?
272 275 )
273 276 else
274 277 issue = orig_issue
275 278 end
276 279 journal = issue.init_journal(User.current, params[:notes])
277 280 issue.safe_attributes = attributes
278 281 call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue })
279 282 if issue.save
280 283 saved_issues << issue
281 284 else
282 285 unsaved_issues << orig_issue
283 286 end
284 287 end
285 288
286 289 if unsaved_issues.empty?
287 290 flash[:notice] = l(:notice_successful_update) unless saved_issues.empty?
288 291 if params[:follow]
289 292 if @issues.size == 1 && saved_issues.size == 1
290 293 redirect_to issue_path(saved_issues.first)
291 294 elsif saved_issues.map(&:project).uniq.size == 1
292 295 redirect_to project_issues_path(saved_issues.map(&:project).first)
293 296 end
294 297 else
295 298 redirect_back_or_default _project_issues_path(@project)
296 299 end
297 300 else
298 301 @saved_issues = @issues
299 302 @unsaved_issues = unsaved_issues
300 303 @issues = Issue.visible.where(:id => @unsaved_issues.map(&:id)).all
301 304 bulk_edit
302 305 render :action => 'bulk_edit'
303 306 end
304 307 end
305 308
306 309 def destroy
307 310 @hours = TimeEntry.where(:issue_id => @issues.map(&:id)).sum(:hours).to_f
308 311 if @hours > 0
309 312 case params[:todo]
310 313 when 'destroy'
311 314 # nothing to do
312 315 when 'nullify'
313 316 TimeEntry.where(['issue_id IN (?)', @issues]).update_all('issue_id = NULL')
314 317 when 'reassign'
315 318 reassign_to = @project.issues.find_by_id(params[:reassign_to_id])
316 319 if reassign_to.nil?
317 320 flash.now[:error] = l(:error_issue_not_found_in_project)
318 321 return
319 322 else
320 323 TimeEntry.where(['issue_id IN (?)', @issues]).
321 324 update_all("issue_id = #{reassign_to.id}")
322 325 end
323 326 else
324 327 # display the destroy form if it's a user request
325 328 return unless api_request?
326 329 end
327 330 end
328 331 @issues.each do |issue|
329 332 begin
330 333 issue.reload.destroy
331 334 rescue ::ActiveRecord::RecordNotFound # raised by #reload if issue no longer exists
332 335 # nothing to do, issue was already deleted (eg. by a parent)
333 336 end
334 337 end
335 338 respond_to do |format|
336 339 format.html { redirect_back_or_default _project_issues_path(@project) }
337 340 format.api { render_api_ok }
338 341 end
339 342 end
340 343
341 344 private
342 345
343 346 def find_project
344 347 project_id = params[:project_id] || (params[:issue] && params[:issue][:project_id])
345 348 @project = Project.find(project_id)
346 349 rescue ActiveRecord::RecordNotFound
347 350 render_404
348 351 end
349 352
350 353 def retrieve_previous_and_next_issue_ids
351 354 retrieve_query_from_session
352 355 if @query
353 356 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
354 357 sort_update(@query.sortable_columns, 'issues_index_sort')
355 358 limit = 500
356 359 issue_ids = @query.issue_ids(:order => sort_clause, :limit => (limit + 1), :include => [:assigned_to, :tracker, :priority, :category, :fixed_version])
357 360 if (idx = issue_ids.index(@issue.id)) && idx < limit
358 361 if issue_ids.size < 500
359 362 @issue_position = idx + 1
360 363 @issue_count = issue_ids.size
361 364 end
362 365 @prev_issue_id = issue_ids[idx - 1] if idx > 0
363 366 @next_issue_id = issue_ids[idx + 1] if idx < (issue_ids.size - 1)
364 367 end
365 368 end
366 369 end
367 370
368 371 # Used by #edit and #update to set some common instance variables
369 372 # from the params
370 373 # TODO: Refactor, not everything in here is needed by #edit
371 374 def update_issue_from_params
372 375 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
373 376 @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
374 377 @time_entry.attributes = params[:time_entry]
375 378
376 379 @issue.init_journal(User.current)
377 380
378 381 issue_attributes = params[:issue]
379 382 if issue_attributes && params[:conflict_resolution]
380 383 case params[:conflict_resolution]
381 384 when 'overwrite'
382 385 issue_attributes = issue_attributes.dup
383 386 issue_attributes.delete(:lock_version)
384 387 when 'add_notes'
385 388 issue_attributes = issue_attributes.slice(:notes)
386 389 when 'cancel'
387 390 redirect_to issue_path(@issue)
388 391 return false
389 392 end
390 393 end
391 394 @issue.safe_attributes = issue_attributes
392 395 @priorities = IssuePriority.active
393 396 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
394 397 true
395 398 end
396 399
397 400 # TODO: Refactor, lots of extra code in here
398 401 # TODO: Changing tracker on an existing issue should not trigger this
399 402 def build_new_issue_from_params
400 403 if params[:id].blank?
401 404 @issue = Issue.new
402 405 if params[:copy_from]
403 406 begin
404 407 @copy_from = Issue.visible.find(params[:copy_from])
405 408 @copy_attachments = params[:copy_attachments].present? || request.get?
406 409 @copy_subtasks = params[:copy_subtasks].present? || request.get?
407 410 @issue.copy_from(@copy_from, :attachments => @copy_attachments, :subtasks => @copy_subtasks)
408 411 rescue ActiveRecord::RecordNotFound
409 412 render_404
410 413 return
411 414 end
412 415 end
413 416 @issue.project = @project
414 417 else
415 418 @issue = @project.issues.visible.find(params[:id])
416 419 end
417 420
418 421 @issue.project = @project
419 422 @issue.author ||= User.current
420 423 # Tracker must be set before custom field values
421 424 @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first)
422 425 if @issue.tracker.nil?
423 426 render_error l(:error_no_tracker_in_project)
424 427 return false
425 428 end
426 429 @issue.start_date ||= Date.today if Setting.default_issue_start_date_to_creation_date?
427 430 @issue.safe_attributes = params[:issue]
428 431
429 432 @priorities = IssuePriority.active
430 433 @allowed_statuses = @issue.new_statuses_allowed_to(User.current, @issue.new_record?)
431 434 @available_watchers = @issue.watcher_users
432 435 if @issue.project.users.count <= 20
433 436 @available_watchers = (@available_watchers + @issue.project.users.sort).uniq
434 437 end
435 438 end
436 439
437 440 def check_for_default_issue_status
438 441 if IssueStatus.default.nil?
439 442 render_error l(:error_no_default_issue_status)
440 443 return false
441 444 end
442 445 end
443 446
444 447 def parse_params_for_bulk_issue_attributes(params)
445 448 attributes = (params[:issue] || {}).reject {|k,v| v.blank?}
446 449 attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
447 450 if custom = attributes[:custom_field_values]
448 451 custom.reject! {|k,v| v.blank?}
449 452 custom.keys.each do |k|
450 453 if custom[k].is_a?(Array)
451 454 custom[k] << '' if custom[k].delete('__none__')
452 455 else
453 456 custom[k] = '' if custom[k] == '__none__'
454 457 end
455 458 end
456 459 end
457 460 attributes
458 461 end
459 462
460 463 # Saves @issue and a time_entry from the parameters
461 464 def save_issue_with_child_records
462 465 Issue.transaction do
463 466 if params[:time_entry] && (params[:time_entry][:hours].present? || params[:time_entry][:comments].present?) && User.current.allowed_to?(:log_time, @issue.project)
464 467 time_entry = @time_entry || TimeEntry.new
465 468 time_entry.project = @issue.project
466 469 time_entry.issue = @issue
467 470 time_entry.user = User.current
468 471 time_entry.spent_on = User.current.today
469 472 time_entry.attributes = params[:time_entry]
470 473 @issue.time_entries << time_entry
471 474 end
472 475
473 476 call_hook(:controller_issues_edit_before_save, { :params => params, :issue => @issue, :time_entry => time_entry, :journal => @issue.current_journal})
474 477 if @issue.save
475 478 call_hook(:controller_issues_edit_after_save, { :params => params, :issue => @issue, :time_entry => time_entry, :journal => @issue.current_journal})
476 479 else
477 480 raise ActiveRecord::Rollback
478 481 end
479 482 end
480 483 end
481 484 end
General Comments 0
You need to be logged in to leave comments. Login now