Class: IssuesController

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

Overview

Redmine - project management software Copyright (C) 2006-2016 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::HEADING_RE, ApplicationHelper::MACROS_RE, ApplicationHelper::MACRO_SUB_RE, ApplicationHelper::TOC_RE

Instance Method Summary collapse

Methods included from SortHelper

#sort_clause, #sort_clear, #sort_criteria, #sort_css_classes, #sort_header_tag, #sort_init, #sort_link, #sort_name, #sort_update

Methods included from QueriesHelper

#available_block_columns_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, #query_as_hidden_field_tags, #query_available_inline_columns_options, #query_columns_hidden_tags, #query_filters_hidden_tags, #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, #anchor, #api_meta, #authoring, #authorize_for, #avatar, #avatar_edit_link, #back_url, #back_url_hidden_field_tag, #body_css_classes, #breadcrumb, #calendar_for, #catch_macros, #check_all_links, #checked_image, #context_menu, #delete_link, #due_date_distance_in_words, #email_delivery_enabled?, #error_messages_for, #favicon, #favicon_path, #favicon_url, #format_activity_day, #format_activity_description, #format_activity_title, #format_changeset_comments, #format_object, #format_version_name, #generate_csv, #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_function, #link_to_if_authorized, #link_to_issue, #link_to_message, #link_to_project, #link_to_project_settings, #link_to_revision, #link_to_user, #link_to_version, #option_tag, #other_formats_links, #page_header_title, #parse_headings, #parse_inline_attachments, #parse_non_pre_blocks, #parse_redmine_links, #parse_sections, #parse_wiki_links, #preview_link, #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_page_hierarchy, #render_project_jump_box, #render_project_nested_lists, #render_projects_for_jump_box, #render_tabs, #reorder_handle, #reorder_links, #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 GravatarHelper::PublicMethods

#gravatar, #gravatar_api_url, #gravatar_for, #gravatar_url

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_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, #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_project_menu_item

Methods included from Redmine::Search::Controller

#default_search_scope, #default_search_scopes, included

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



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
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
# File 'app/controllers/issues_controller.rb', line 206

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

  @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

  @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?
  end

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

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

#bulk_updateObject



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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'app/controllers/issues_controller.rb', line 269

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')

  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
  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,
        :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



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

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 {
        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
      }
      format.api  { render :action => 'show', :status => :created, :location => issue_url(@issue) }
    end
    return
  else
    respond_to do |format|
      format.html {
        if @issue.project.nil?
          render_error :status => 422
        else
          render :action => 'new'
        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



389
390
391
392
393
394
395
# File 'app/controllers/issues_controller.rb', line 389

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:



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

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'
      time_entries.update_all(:issue_id => nil)
    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 { redirect_back_or_default _project_issues_path(@project) }
    format.api  { render_api_ok }
  end
end

#editObject



166
167
168
169
170
171
172
173
# File 'app/controllers/issues_controller.rb', line 166

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
88
89
90
91
92
93
# File 'app/controllers/issues_controller.rb', line 44

def index
  retrieve_query
  sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
  sort_update(@query.sortable_columns)
  @query.sort_criteria = sort_criteria.to_a

  if @query.valid?
    case params[:format]
    when 'csv', 'pdf'
      @limit = Setting.issues_export_limit.to_i
      if params[:columns] == 'all'
        @query.column_names = @query.available_inline_columns.map(&:name)
      end
    when 'atom'
      @limit = Setting.feeds_limit.to_i
    when 'xml', 'json'
      @offset, @limit = api_offset_and_limit
      @query.column_names = %w(author)
    else
      @limit = per_page_option
    end

    @issue_count = @query.issue_count
    @issue_pages = Paginator.new @issue_count, @limit, params['page']
    @offset ||= @issue_pages.offset
    @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version],
                            :order => sort_clause,
                            :offset => @offset,
                            :limit => @limit)
    @issue_count_by_group = @query.issue_count_by_group

    respond_to do |format|
      format.html { render :template => 'issues/index', :layout => !request.xhr? }
      format.api  {
        Issue.load_visible_relations(@issues) if include_in_api_response?('relations')
      }
      format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") }
      format.csv  { send_data(query_to_csv(@issues, @query, params[:csv]), :type => 'text/csv; header=present', :filename => 'issues.csv') }
      format.pdf  { send_file_headers! :type => 'application/pdf', :filename => 'issues.pdf' }
    end
  else
    respond_to do |format|
      format.html { render(:template => 'issues/index', :layout => !request.xhr?) }
      format.any(:atom, :csv, :pdf) { head 422 }
      format.api { render_validation_errors(@query) }
    end
  end
rescue ActiveRecord::RecordNotFound
  render_404
end

#newObject



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

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

#showObject



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

def show
  @journals = @issue.journals.
                preload(:details).
                preload(:user => :email_address).
                reorder(:created_on, :id).to_a
  @journals.each_with_index {|j,i| j.indice = i+1}
  @journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project)
  Journal.preload_journals_details_custom_fields(@journals)
  @journals.select! {|journal| journal.notes? || journal.visible_details.any?}
  @journals.reverse! if User.current.wants_comments_in_reverse_order?

  @changesets = @issue.changesets.visible.preload(:repository, :user).to_a
  @changesets.reverse! if User.current.wants_comments_in_reverse_order?

  @relations = @issue.relations.select {|r| r.other_issue(@issue) && r.other_issue(@issue).visible? }
  @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
  @priorities = IssuePriority.active
  @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
  @relation = IssueRelation.new

  respond_to do |format|
    format.html {
      retrieve_previous_and_next_issue_ids
      render :template => 'issues/show'
    }
    format.api
    format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' }
    format.pdf  {
      send_file_headers! :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf"
    }
  end
end

#updateObject



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'app/controllers/issues_controller.rb', line 175

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
      @conflict_journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project)
    end
  end

  if saved
    render_attachment_warning_if_needed(@issue)
    flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?

    respond_to do |format|
      format.html { redirect_back_or_default issue_path(@issue, previous_and_next_issue_ids_params) }
      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