To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / app / controllers / issues_controller.rb @ 1298:4f746d8966dd

History | View | Annotate | Download (16.3 KB)

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