Class: IssuesController

Inherits:
ApplicationController show all
Includes:
QueriesHelper
Defined in:
app/controllers/issues_controller.rb

Overview

Redmine - project management software Copyright (C) 2006-2020 Jean-Philippe Lang

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.

Constant Summary

Constants included from ApplicationHelper

ApplicationHelper::ATTACHMENT_CONTAINER_LINK, ApplicationHelper::HEADING_RE, ApplicationHelper::LINKS_RE, ApplicationHelper::MACROS_RE, ApplicationHelper::MACRO_SUB_RE, ApplicationHelper::RECORD_LINK, ApplicationHelper::TOC_RE

Instance Method Summary collapse

Methods included from QueriesHelper

#available_block_columns_tags, #available_display_types_tags, #available_totalable_columns_tags, #column_content, #column_header, #column_value, #csv_content, #csv_value, #filters_options_for_select, #group_by_column_select_tag, #grouped_query_results, #link_to_clear_query, #query_as_hidden_field_tags, #query_available_inline_columns_options, #query_columns_hidden_tags, #query_filters_hidden_tags, #query_hidden_sort_tag, #query_hidden_tags, #query_links, #query_selected_inline_columns_options, #query_to_csv, #render_query_columns_selection, #render_query_totals, #render_sidebar_queries, #retrieve_query, #retrieve_query_from_session, #sidebar_queries, #total_tag

Methods included from ApplicationHelper

#accesskey, #actions_dropdown, #anchor, #api_meta, #authoring, #authorize_for, #back_url_hidden_field_tag, #body_css_classes, #breadcrumb, #bulk_edit_error_messages, #calendar_for, #cancel_button_tag, #catch_macros, #check_all_links, #checked_image, #context_menu, #default_search_project_scope, #delete_link, #due_date_distance_in_words, #email_delivery_enabled?, #error_messages_for, #export_csv_encoding_select_tag, #favicon, #favicon_path, #favicon_url, #form_tag_html, #format_activity_day, #format_activity_description, #format_activity_title, #format_changeset_comments, #format_object, #format_version_name, #get_tab_action, #html_hours, #html_title, #image_tag, #include_calendar_headers_tags, #include_in_api_response?, #inject_macros, #javascript_heads, #javascript_include_tag, #labelled_fields_for, #labelled_form_for, #lang_options_for_select, #link_to_attachment, #link_to_attachment_container, #link_to_context_menu, #link_to_function, #link_to_group, #link_to_if_authorized, #link_to_issue, #link_to_message, #link_to_month, #link_to_next_month, #link_to_previous_month, #link_to_project, #link_to_project_settings, #link_to_record, #link_to_revision, #link_to_user, #link_to_version, #option_tag, #other_formats_links, #page_header_title, #parse_headings, #parse_hires_images, #parse_inline_attachments, #parse_non_pre_blocks, #parse_redmine_links, #parse_sections, #parse_wiki_links, #principals_check_box_tags, #principals_options_for_select, #progress_bar, #project_tree, #project_tree_options_for_select, #projects_for_jump_box, #raw_json, #render_error_messages, #render_flash_messages, #render_if_exist, #render_page_hierarchy, #render_project_jump_box, #render_project_nested_lists, #render_projects_for_jump_box, #render_tabs, #reorder_handle, #replace_toc, #robot_exclusion_tag, #sanitize_anchor_name, #sidebar_content?, #simple_format_without_paragraph, #stylesheet_link_tag, #syntax_highlight, #syntax_highlight_lines, #textilizable, #thumbnail_tag, #time_tag, #title, #to_path_param, #toggle_checkboxes_link, #toggle_link, #truncate_lines, #truncate_single_line_raw, #view_layouts_base_sidebar_hook_response, #wiki_page_path

Methods included from Redmine::Helpers::URL

#uri_with_safe_scheme?

Methods included from Redmine::Hook::Helper

#call_hook

Methods included from Redmine::Themes::Helper

#current_theme, #heads_for_theme

Methods included from Redmine::SudoMode::Helper

#hash_to_hidden_fields

Methods included from Redmine::Pagination::Helper

#pagination_links_each, #pagination_links_full, #per_page_links, #per_page_options

Methods included from Redmine::I18n

#current_language, #day_letter, #day_name, #find_language, #format_date, #format_hours, #format_time, included, #l, #l_hours, #l_hours_short, #l_or_humanize, #languages_options, #ll, #lu, #month_name, #set_language_if_valid, #valid_languages

Methods included from Redmine::WikiFormatting::Macros::Definitions

#exec_macro, #extract_macro_options, #macro_exists?

Methods inherited from ApplicationController

#_include_layout?, accept_api_auth, #accept_api_auth?, accept_rss_auth, #accept_rss_auth?, #api_key_from_request, #api_offset_and_limit, #api_request?, #api_switch_user_from_request, #authorize, #authorize_global, #autologin_cookie_name, #back_url, #check_if_login_required, #check_password_change, #check_project_privacy, #deny_access, #filename_for_content_disposition, #find_attachments, #find_current_user, #find_issue, #find_issues, #find_model_object, #find_optional_project, #find_optional_project_by_id, #find_project, #find_project_by_project_id, #find_project_from_association, #handle_unverified_request, #logged_user=, #logout_user, #missing_template, model_object, #parse_params_for_bulk_update, #parse_qvalues, #per_page_option, #query_statement_invalid, #record_project_usage, #redirect_back_or_default, #redirect_to_referer_or, #render_403, #render_404, #render_api_errors, #render_api_head, #render_api_ok, #render_attachment_warning_if_needed, #render_error, #render_feed, #render_validation_errors, #require_admin, #require_admin_or_api_request, #require_login, #session_expiration, #session_expired?, #set_localization, #start_user_session, #try_to_autologin, #use_layout, #user_setup, #verify_authenticity_token

Methods included from Redmine::SudoMode::Controller

#process_sudo_form, #render_sudo_form, #require_sudo_mode, #sudo_mode, #sudo_timestamp_valid?, #update_sudo_timestamp!

Methods included from Redmine::MenuManager::MenuController

#current_menu, included, #menu_items, #redirect_to_menu_item, #redirect_to_project_menu_item

Methods included from Redmine::Search::Controller

#default_search_scope, #default_search_scopes, included

Methods included from AvatarsHelper

#assignee_avatar, #author_avatar, #avatar, #avatar_edit_link

Methods included from GravatarHelper::PublicMethods

#gravatar, #gravatar_api_url, #gravatar_for, #gravatar_url

Methods included from RoutesHelper

#_new_project_issue_path, #_new_time_entry_path, #_project_calendar_path, #_project_gantt_path, #_project_issues_path, #_project_news_path, #_report_time_entries_path, #_time_entries_path, #board_path

Methods included from Redmine::Pagination

#paginate, #paginator

Instance Method Details

#bulk_editObject

Bulk edit/copy a set of issues


239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'app/controllers/issues_controller.rb', line 239

def bulk_edit
  @issues.sort!
  @copy = params[:copy].present?
  @notes = params[:notes]

  if @copy
    unless User.current.allowed_to?(:copy_issues, @projects)
      raise ::Unauthorized
    end
  else
    unless @issues.all?(&:attributes_editable?)
      raise ::Unauthorized
    end
  end

  edited_issues = Issue.where(:id => @issues.map(&:id)).to_a

  @values_by_custom_field = {}
  edited_issues.each do |issue|
    issue.custom_field_values.each do |c|
      if c.value_present?
        @values_by_custom_field[c.custom_field] ||= []
        @values_by_custom_field[c.custom_field] << issue.id
      end
    end
  end

  @allowed_projects = Issue.allowed_target_projects
  if params[:issue]
    @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:issue][:project_id].to_s}
    if @target_project
      target_projects = [@target_project]
      edited_issues.each {|issue| issue.project = @target_project}
    end
  end
  target_projects ||= @projects

  @trackers = target_projects.map {|p| Issue.allowed_target_trackers(p)}.reduce(:&)
  if params[:issue]
    @target_tracker = @trackers.detect {|t| t.id.to_s == params[:issue][:tracker_id].to_s}
    if @target_tracker
      edited_issues.each {|issue| issue.tracker = @target_tracker}
    end
  end

  if @copy
    # Copied issues will get their default statuses
    @available_statuses = []
  else
    @available_statuses = edited_issues.map(&:new_statuses_allowed_to).reduce(:&)
  end
  if params[:issue]
    @target_status = @available_statuses.detect {|t| t.id.to_s == params[:issue][:status_id].to_s}
    if @target_status
      edited_issues.each {|issue| issue.status = @target_status}
    end
  end

  edited_issues.each do |issue|
    issue.custom_field_values.each do |c|
      if c.value_present? && @values_by_custom_field[c.custom_field]
        @values_by_custom_field[c.custom_field].delete(issue.id)
      end
    end
  end
  @values_by_custom_field.delete_if {|k, v| v.blank?}
  @custom_fields =
    edited_issues.map{|i| i.editable_custom_fields}.
      reduce(:&).select {|field| field.format.bulk_edit_supported}
  @assignables = target_projects.map(&:assignable_users).reduce(:&)
  @versions = target_projects.map {|p| p.shared_versions.open}.reduce(:&)
  @categories = target_projects.map {|p| p.issue_categories}.reduce(:&)
  if @copy
    @attachments_present = @issues.detect {|i| i.attachments.any?}.present?
    @subtasks_present = @issues.detect {|i| !i.leaf?}.present?
    @watchers_present = User.current.allowed_to?(:add_issue_watchers, @projects) &&
                          Watcher.where(:watchable_type => 'Issue',
                                        :watchable_id => @issues.map(&:id)).exists?
  end

  @safe_attributes = edited_issues.map(&:safe_attribute_names).reduce(:&)

  @issue_params = params[:issue] || {}
  @issue_params[:custom_field_values] ||= {}
end

#bulk_updateObject


325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'app/controllers/issues_controller.rb', line 325

def bulk_update
  @issues.sort!
  @copy = params[:copy].present?

  attributes = parse_params_for_bulk_update(params[:issue])
  copy_subtasks = (params[:copy_subtasks] == '1')
  copy_attachments = (params[:copy_attachments] == '1')
  copy_watchers = (params[:copy_watchers] == '1')

  if @copy
    unless User.current.allowed_to?(:copy_issues, @projects)
      raise ::Unauthorized
    end

    target_projects = @projects
    if attributes['project_id'].present?
      target_projects = Project.where(:id => attributes['project_id']).to_a
    end
    unless User.current.allowed_to?(:add_issues, target_projects)
      raise ::Unauthorized
    end

    unless User.current.allowed_to?(:add_issue_watchers, @projects)
      copy_watchers = false
    end
  else
    unless @issues.all?(&:attributes_editable?)
      raise ::Unauthorized
    end
  end

  unsaved_issues = []
  saved_issues = []

  if @copy && copy_subtasks
    # Descendant issues will be copied with the parent task
    # Don't copy them twice
    @issues.reject! {|issue| @issues.detect {|other| issue.is_descendant_of?(other)}}
  end

  @issues.each do |orig_issue|
    orig_issue.reload
    if @copy
      issue = orig_issue.copy(
        {},
        :attachments => copy_attachments,
        :subtasks => copy_subtasks,
        :watchers => copy_watchers,
        :link => link_copy?(params[:link_copy])
      )
    else
      issue = orig_issue
    end
    journal = issue.init_journal(User.current, params[:notes])
    issue.safe_attributes = attributes
    call_hook(:controller_issues_bulk_edit_before_save, {:params => params, :issue => issue})
    if issue.save
      saved_issues << issue
    else
      unsaved_issues << orig_issue
    end
  end

  if unsaved_issues.empty?
    flash[:notice] = l(:notice_successful_update) unless saved_issues.empty?
    if params[:follow]
      if @issues.size == 1 && saved_issues.size == 1
        redirect_to issue_path(saved_issues.first)
      elsif saved_issues.map(&:project).uniq.size == 1
        redirect_to project_issues_path(saved_issues.map(&:project).first)
      end
    else
      redirect_back_or_default _project_issues_path(@project)
    end
  else
    @saved_issues = @issues
    @unsaved_issues = unsaved_issues
    @issues = Issue.visible.where(:id => @unsaved_issues.map(&:id)).to_a
    bulk_edit
    render :action => 'bulk_edit'
  end
end

#createObject


136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'app/controllers/issues_controller.rb', line 136

def create
  unless User.current.allowed_to?(:add_issues, @issue.project, :global => true)
    raise ::Unauthorized
  end

  call_hook(:controller_issues_new_before_save, {:params => params, :issue => @issue})
  @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
  if @issue.save
    call_hook(:controller_issues_new_after_save, {:params => params, :issue => @issue})
    respond_to do |format|
      format.html do
        render_attachment_warning_if_needed(@issue)
        flash[:notice] =
          l(:notice_issue_successful_create,
            :id => view_context.link_to("##{@issue.id}", issue_path(@issue),
                                        :title => @issue.subject))
        redirect_after_create
      end
      format.api do
        render :action => 'show', :status => :created,
        :location => issue_url(@issue)
      end
    end
    return
  else
    respond_to do |format|
      format.html do
        if @issue.project.nil?
          render_error :status => 422
        else
          render :action => 'new'
        end
      end
      format.api  {render_validation_errors(@issue)}
    end
  end
end

#current_menu_itemObject

Overrides Redmine::MenuManager::MenuController::ClassMethods for when the “New issue” tab is enabled


461
462
463
464
465
466
467
# File 'app/controllers/issues_controller.rb', line 461

def current_menu_item
  if Setting.new_item_menu_tab == '1' && [:new, :create].include?(action_name.to_sym)
    :new_issue
  else
    super
  end
end

#destroyObject

Raises:


408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
# File 'app/controllers/issues_controller.rb', line 408

def destroy
  raise Unauthorized unless @issues.all?(&:deletable?)

  # all issues and their descendants are about to be deleted
  issues_and_descendants_ids = Issue.self_and_descendants(@issues).pluck(:id)
  time_entries = TimeEntry.where(:issue_id => issues_and_descendants_ids)
  @hours = time_entries.sum(:hours).to_f

  if @hours > 0
    case params[:todo]
    when 'destroy'
      # nothing to do
    when 'nullify'
      if Setting.timelog_required_fields.include?('issue_id')
        flash.now[:error] = l(:field_issue) + " " + ::I18n.t('activerecord.errors.messages.blank')
        return
      else
        time_entries.update_all(:issue_id => nil)
      end
    when 'reassign'
      reassign_to = @project && @project.issues.find_by_id(params[:reassign_to_id])
      if reassign_to.nil?
        flash.now[:error] = l(:error_issue_not_found_in_project)
        return
      elsif issues_and_descendants_ids.include?(reassign_to.id)
        flash.now[:error] = l(:error_cannot_reassign_time_entries_to_an_issue_about_to_be_deleted)
        return
      else
        time_entries.update_all(:issue_id => reassign_to.id, :project_id => reassign_to.project_id)
      end
    else
      # display the destroy form if it's a user request
      return unless api_request?
    end
  end
  @issues.each do |issue|
    begin
      issue.reload.destroy
    rescue ::ActiveRecord::RecordNotFound # raised by #reload if issue no longer exists
      # nothing to do, issue was already deleted (eg. by a parent)
    end
  end
  respond_to do |format|
    format.html {
      flash[:notice] = l(:notice_successful_delete)
      redirect_back_or_default _project_issues_path(@project)
    }
    format.api  {render_api_ok}
  end
end

#editObject


174
175
176
177
178
179
180
181
# File 'app/controllers/issues_controller.rb', line 174

def edit
  return unless update_issue_from_params

  respond_to do |format|
    format.html {}
    format.js
  end
end

#indexObject


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
# File 'app/controllers/issues_controller.rb', line 44

def index
  use_session = !request.format.csv?
  retrieve_query(IssueQuery, use_session)

  if @query.valid?
    respond_to do |format|
      format.html do
        @issue_count = @query.issue_count
        @issue_pages = Paginator.new @issue_count, per_page_option, params['page']
        @issues = @query.issues(:offset => @issue_pages.offset, :limit => @issue_pages.per_page)
        render :layout => !request.xhr?
      end
      format.api do
        @offset, @limit = api_offset_and_limit
        @query.column_names = %w(author)
        @issue_count = @query.issue_count
        @issues = @query.issues(:offset => @offset, :limit => @limit)
        Issue.load_visible_relations(@issues) if include_in_api_response?('relations')
      end
      format.atom do
        @issues = @query.issues(:limit => Setting.feeds_limit.to_i)
        render_feed(@issues,
                    :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}")
      end
      format.csv do
        @issues = @query.issues(:limit => Setting.issues_export_limit.to_i)
        send_data(query_to_csv(@issues, @query, params[:csv]),
                  :type => 'text/csv; header=present', :filename => 'issues.csv')
      end
      format.pdf do
        @issues = @query.issues(:limit => Setting.issues_export_limit.to_i)
        send_file_headers! :type => 'application/pdf', :filename => 'issues.pdf'
      end
    end
  else
    respond_to do |format|
      format.html {render :layout => !request.xhr?}
      format.any(:atom, :csv, :pdf) {head 422}
      format.api {render_validation_errors(@query)}
    end
  end
rescue ActiveRecord::RecordNotFound
  render_404
end

#issue_tabObject


222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'app/controllers/issues_controller.rb', line 222

def issue_tab
  return render_error :status => 422 unless request.xhr?

  tab = params[:name]

  case tab
  when 'time_entries'
    @time_entries = @issue.time_entries.visible.preload(:activity, :user).to_a
    render :partial => 'issues/tabs/time_entries', :locals => {:time_entries => @time_entries}
  when 'changesets'
    @changesets = @issue.changesets.visible.preload(:repository, :user).to_a
    @changesets.reverse! if User.current.wants_comments_in_reverse_order?
    render :partial => 'issues/tabs/changesets', :locals => {:changesets => @changesets}
  end
end

#newObject


129
130
131
132
133
134
# File 'app/controllers/issues_controller.rb', line 129

def new
  respond_to do |format|
    format.html {render :action => 'new', :layout => !request.xhr?}
    format.js
  end
end

#showObject


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
117
118
119
120
121
122
123
124
125
126
127
# File 'app/controllers/issues_controller.rb', line 89

def show
  @journals = @issue.visible_journals_with_index
  @has_changesets = @issue.changesets.visible.preload(:repository, :user).exists?
  @relations =
    @issue.relations.
      select do |r|
        r.other_issue(@issue) && r.other_issue(@issue).visible?
      end
  @journals.reverse! if User.current.wants_comments_in_reverse_order?

  if User.current.allowed_to?(:view_time_entries, @project)
    Issue.load_visible_spent_hours([@issue])
    Issue.load_visible_total_spent_hours([@issue])
  end

  respond_to do |format|
    format.html do
      @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
      @priorities = IssuePriority.active
      @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
      @time_entries = @issue.time_entries.visible.preload(:activity, :user)
      @relation = IssueRelation.new
      retrieve_previous_and_next_issue_ids
      render :template => 'issues/show'
    end
    format.api do
      @changesets = @issue.changesets.visible.preload(:repository, :user).to_a
      @changesets.reverse! if User.current.wants_comments_in_reverse_order?
    end
    format.atom do
      render :template => 'journals/index', :layout => false,
      :content_type => 'application/atom+xml'
    end
    format.pdf do
      send_file_headers!(:type => 'application/pdf',
                         :filename => "#{@project.identifier}-#{@issue.id}.pdf")
    end
  end
end

#updateObject


183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'app/controllers/issues_controller.rb', line 183

def update
  return unless update_issue_from_params

  @issue.save_attachments(params[:attachments] ||
                           (params[:issue] && params[:issue][:uploads]))
  saved = false
  begin
    saved = save_issue_with_child_records
  rescue ActiveRecord::StaleObjectError
    @conflict = true
    if params[:last_journal_id]
      @conflict_journals = @issue.journals_after(params[:last_journal_id]).to_a
      unless User.current.allowed_to?(:view_private_notes, @issue.project)
        @conflict_journals.reject!(&:private_notes?)
      end
    end
  end

  if saved
    render_attachment_warning_if_needed(@issue)
    unless @issue.current_journal.new_record? || params[:no_flash]
      flash[:notice] = l(:notice_successful_update)
    end
    respond_to do |format|
      format.html do
        redirect_back_or_default(
          issue_path(@issue, previous_and_next_issue_ids_params)
        )
      end
      format.api  {render_api_ok}
    end
  else
    respond_to do |format|
      format.html {render :action => 'edit'}
      format.api  {render_validation_errors(@issue)}
    end
  end
end