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 @ 1362:8633c9040b3b

History | View | Annotate | Download (16.4 KB)

1
# Redmine - project management software
2
# Copyright (C) 2006-2012  Jean-Philippe Lang
3
#
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
#
9
# 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
#
14
# 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

    
22
  before_filter :find_issue, :only => [:show, :edit, :update]
23
  before_filter :find_issues, :only => [:bulk_edit, :bulk_update, :destroy]
24
  before_filter :find_project, :only => [:new, :create]
25
  before_filter :authorize, :except => [:index]
26
  before_filter :find_optional_project, :only => [:index]
27
  before_filter :check_for_default_issue_status, :only => [:new, :create]
28
  before_filter :build_new_issue_from_params, :only => [:new, :create]
29
  accept_rss_auth :index, :show
30
  accept_api_auth :index, :show, :create, :update, :destroy
31

    
32
  rescue_from Query::StatementInvalid, :with => :query_statement_invalid
33

    
34
  helper :journals
35
  helper :projects
36
  include ProjectsHelper
37
  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
  helper :repositories
48
  include RepositoriesHelper
49
  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
    @query.sort_criteria = sort_criteria.to_a
60

    
61
    if @query.valid?
62
      case params[:format]
63
      when 'csv', 'pdf'
64
        @limit = Setting.issues_export_limit.to_i
65
      when 'atom'
66
        @limit = Setting.feeds_limit.to_i
67
      when 'xml', 'json'
68
        @offset, @limit = api_offset_and_limit
69
      else
70
        @limit = per_page_option
71
      end
72

    
73
      @issue_count = @query.issue_count
74
      @issue_pages = Paginator.new self, @issue_count, @limit, params['page']
75
      @offset ||= @issue_pages.current.offset
76
      @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version],
77
                              :order => sort_clause,
78
                              :offset => @offset,
79
                              :limit => @limit)
80
      @issue_count_by_group = @query.issue_count_by_group
81

    
82
      respond_to do |format|
83
        format.html { render :template => 'issues/index', :layout => !request.xhr? }
84
        format.api  {
85
          Issue.load_visible_relations(@issues) if include_in_api_response?('relations')
86
        }
87
        format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") }
88
        format.csv  { send_data(issues_to_csv(@issues, @project, @query, params), :type => 'text/csv; header=present', :filename => 'export.csv') }
89
        format.pdf  { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'export.pdf') }
90
      end
91
    else
92
      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
    end
98
  rescue ActiveRecord::RecordNotFound
99
    render_404
100
  end
101

    
102
  def show
103
    @journals = @issue.journals.includes(:user, :details).reorder("#{Journal.table_name}.id ASC").all
104
    @journals.each_with_index {|j,i| j.indice = i+1}
105
    @journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project)
106
    @journals.reverse! if User.current.wants_comments_in_reverse_order?
107

    
108
    @changesets = @issue.changesets.visible.all
109
    @changesets.reverse! if User.current.wants_comments_in_reverse_order?
110

    
111
    @relations = @issue.relations.select {|r| r.other_issue(@issue) && r.other_issue(@issue).visible? }
112
    @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
113
    @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
114
    @priorities = IssuePriority.active
115
    @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
116
    respond_to do |format|
117
      format.html {
118
        retrieve_previous_and_next_issue_ids
119
        render :template => 'issues/show'
120
      }
121
      format.api
122
      format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' }
123
      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
    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
    respond_to do |format|
134
      format.html { render :action => 'new', :layout => !request.xhr? }
135
      format.js { render :partial => 'update_form' }
136
    end
137
  end
138

    
139
  def create
140
    call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
141
    @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
142
    if @issue.save
143
      
144
      call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
145

    
146
      # Also adds the assignee to the watcher's list
147
      if params[:issue][:assigned_to_id] && !params[:issue][:assigned_to_id].empty?
148
       unless @issue.watcher_ids.include?(params[:issue][:assigned_to_id])
149
         @issue.add_watcher(User.find(params[:issue][:assigned_to_id]))
150
       end
151
      end
152

    
153
      respond_to do |format|
154
        format.html {
155
          render_attachment_warning_if_needed(@issue)
156
          flash[:notice] = l(:notice_issue_successful_create, :id => view_context.link_to("##{@issue.id}", issue_path(@issue), :title => @issue.subject))
157
          redirect_to(params[:continue] ?  { :action => 'new', :project_id => @issue.project, :issue => {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?} } :
158
                      { :action => 'show', :id => @issue })
159
        }
160
        format.api  { render :action => 'show', :status => :created, :location => issue_url(@issue) }
161
      end
162
      return
163
    else
164
      respond_to do |format|
165
        format.html { render :action => 'new' }
166
        format.api  { render_validation_errors(@issue) }
167
      end
168
    end
169
  end
170

    
171
  def edit
172
    return unless update_issue_from_params
173

    
174
    respond_to do |format|
175
      format.html { }
176
      format.xml  { }
177
    end
178
  end
179

    
180
  def update
181
    return unless update_issue_from_params
182
    @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
183
    saved = false
184
    begin
185
      saved = @issue.save_issue_with_child_records(params, @time_entry)
186
    rescue ActiveRecord::StaleObjectError
187
      @conflict = true
188
      if params[:last_journal_id]
189
        @conflict_journals = @issue.journals_after(params[:last_journal_id]).all
190
        @conflict_journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project)
191
      end
192
    end
193

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

    
198
      respond_to do |format|
199
        format.html { redirect_back_or_default({:action => 'show', :id => @issue}) }
200
        format.api  { render_api_ok }
201
      end
202
    else
203
      respond_to do |format|
204
        format.html { render :action => 'edit' }
205
        format.api  { render_validation_errors(@issue) }
206
      end
207
    end
208
  end
209

    
210
  # Bulk edit/copy a set of issues
211
  def bulk_edit
212
    @issues.sort!
213
    @copy = params[:copy].present?
214
    @notes = params[:notes]
215

    
216
    if User.current.allowed_to?(:move_issues, @projects)
217
      @allowed_projects = Issue.allowed_target_projects_on_move
218
      if params[:issue]
219
        @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:issue][:project_id].to_s}
220
        if @target_project
221
          target_projects = [@target_project]
222
        end
223
      end
224
    end
225
    target_projects ||= @projects
226

    
227
    if @copy
228
      @available_statuses = [IssueStatus.default]
229
    else
230
      @available_statuses = @issues.map(&:new_statuses_allowed_to).reduce(:&)
231
    end
232
    @custom_fields = target_projects.map{|p|p.all_issue_custom_fields}.reduce(:&)
233
    @assignables = target_projects.map(&:assignable_users).reduce(:&)
234
    @trackers = target_projects.map(&:trackers).reduce(:&)
235
    @versions = target_projects.map {|p| p.shared_versions.open}.reduce(:&)
236
    @categories = target_projects.map {|p| p.issue_categories}.reduce(:&)
237
    if @copy
238
      @attachments_present = @issues.detect {|i| i.attachments.any?}.present?
239
      @subtasks_present = @issues.detect {|i| !i.leaf?}.present?
240
    end
241

    
242
    @safe_attributes = @issues.map(&:safe_attribute_names).reduce(:&)
243
    render :layout => false if request.xhr?
244
  end
245

    
246
  def bulk_update
247
    @issues.sort!
248
    @copy = params[:copy].present?
249
    attributes = parse_params_for_bulk_issue_attributes(params)
250

    
251
    unsaved_issue_ids = []
252
    moved_issues = []
253

    
254
    if @copy && params[:copy_subtasks].present?
255
      # Descendant issues will be copied with the parent task
256
      # Don't copy them twice
257
      @issues.reject! {|issue| @issues.detect {|other| issue.is_descendant_of?(other)}}
258
    end
259

    
260
    @issues.each do |issue|
261
      issue.reload
262
      if @copy
263
        issue = issue.copy({},
264
          :attachments => params[:copy_attachments].present?,
265
          :subtasks => params[:copy_subtasks].present?
266
        )
267
      end
268
      journal = issue.init_journal(User.current, params[:notes])
269
      issue.safe_attributes = attributes
270
      call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue })
271
      if issue.save
272
        moved_issues << issue
273
      else
274
        # Keep unsaved issue ids to display them in flash error
275
        unsaved_issue_ids << issue.id
276
      end
277
    end
278
    set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids)
279

    
280
    if params[:follow]
281
      if @issues.size == 1 && moved_issues.size == 1
282
        redirect_to :controller => 'issues', :action => 'show', :id => moved_issues.first
283
      elsif moved_issues.map(&:project).uniq.size == 1
284
        redirect_to :controller => 'issues', :action => 'index', :project_id => moved_issues.map(&:project).first
285
      end
286
    else
287
      redirect_back_or_default({:controller => 'issues', :action => 'index', :project_id => @project})
288
    end
289
  end
290

    
291
  def destroy
292
    @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f
293
    if @hours > 0
294
      case params[:todo]
295
      when 'destroy'
296
        # nothing to do
297
      when 'nullify'
298
        TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues])
299
      when 'reassign'
300
        reassign_to = @project.issues.find_by_id(params[:reassign_to_id])
301
        if reassign_to.nil?
302
          flash.now[:error] = l(:error_issue_not_found_in_project)
303
          return
304
        else
305
          TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues])
306
        end
307
      else
308
        # display the destroy form if it's a user request
309
        return unless api_request?
310
      end
311
    end
312
    @issues.each do |issue|
313
      begin
314
        issue.reload.destroy
315
      rescue ::ActiveRecord::RecordNotFound # raised by #reload if issue no longer exists
316
        # nothing to do, issue was already deleted (eg. by a parent)
317
      end
318
    end
319
    respond_to do |format|
320
      format.html { redirect_back_or_default(:action => 'index', :project_id => @project) }
321
      format.api  { render_api_ok }
322
    end
323
  end
324

    
325
  private
326

    
327
  def find_project
328
    project_id = params[:project_id] || (params[:issue] && params[:issue][:project_id])
329
    @project = Project.find(project_id)
330
  rescue ActiveRecord::RecordNotFound
331
    render_404
332
  end
333

    
334
  def retrieve_previous_and_next_issue_ids
335
    retrieve_query_from_session
336
    if @query
337
      sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
338
      sort_update(@query.sortable_columns, 'issues_index_sort')
339
      limit = 500
340
      issue_ids = @query.issue_ids(:order => sort_clause, :limit => (limit + 1), :include => [:assigned_to, :tracker, :priority, :category, :fixed_version])
341
      if (idx = issue_ids.index(@issue.id)) && idx < limit
342
        if issue_ids.size < 500
343
          @issue_position = idx + 1
344
          @issue_count = issue_ids.size
345
        end
346
        @prev_issue_id = issue_ids[idx - 1] if idx > 0
347
        @next_issue_id = issue_ids[idx + 1] if idx < (issue_ids.size - 1)
348
      end
349
    end
350
  end
351

    
352
  # Used by #edit and #update to set some common instance variables
353
  # from the params
354
  # TODO: Refactor, not everything in here is needed by #edit
355
  def update_issue_from_params
356
    @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
357
    @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
358
    @time_entry.attributes = params[:time_entry]
359

    
360
    @issue.init_journal(User.current)
361

    
362
    issue_attributes = params[:issue]
363
    if issue_attributes && params[:conflict_resolution]
364
      case params[:conflict_resolution]
365
      when 'overwrite'
366
        issue_attributes = issue_attributes.dup
367
        issue_attributes.delete(:lock_version)
368
      when 'add_notes'
369
        issue_attributes = issue_attributes.slice(:notes)
370
      when 'cancel'
371
        redirect_to issue_path(@issue)
372
        return false
373
      end
374
    end
375

    
376
    # tests if the the user assigned_to_id
377
    # is in this issues watcher's list
378
    # if not, adds it.
379

    
380
    if params[:issue] && params[:issue][:assigned_to_id] && !params[:issue][:assigned_to_id].empty?
381
     unless @issue.watched_by?(User.find(params[:issue][:assigned_to_id]))
382
       @issue.add_watcher(User.find(params[:issue][:assigned_to_id]))
383
     end
384
    end
385

    
386
    @issue.safe_attributes = issue_attributes
387
    @priorities = IssuePriority.active
388
    @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
389
    true
390

    
391
  end
392

    
393
  # TODO: Refactor, lots of extra code in here
394
  # TODO: Changing tracker on an existing issue should not trigger this
395
  def build_new_issue_from_params
396
    if params[:id].blank?
397
      @issue = Issue.new
398
      if params[:copy_from]
399
        begin
400
          @copy_from = Issue.visible.find(params[:copy_from])
401
          @copy_attachments = params[:copy_attachments].present? || request.get?
402
          @copy_subtasks = params[:copy_subtasks].present? || request.get?
403
          @issue.copy_from(@copy_from, :attachments => @copy_attachments, :subtasks => @copy_subtasks)
404
        rescue ActiveRecord::RecordNotFound
405
          render_404
406
          return
407
        end
408
      end
409
      @issue.project = @project
410
    else
411
      @issue = @project.issues.visible.find(params[:id])
412
    end
413

    
414
    @issue.project = @project
415
    @issue.author ||= User.current
416
    # Tracker must be set before custom field values
417
    @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first)
418
    if @issue.tracker.nil?
419
      render_error l(:error_no_tracker_in_project)
420
      return false
421
    end
422
    @issue.start_date ||= Date.today if Setting.default_issue_start_date_to_creation_date?
423
    @issue.safe_attributes = params[:issue]
424

    
425
    @priorities = IssuePriority.active
426
    @allowed_statuses = @issue.new_statuses_allowed_to(User.current, true)
427
    @available_watchers = (@issue.project.users.sort + @issue.watcher_users).uniq
428
  end
429

    
430
  def check_for_default_issue_status
431
    if IssueStatus.default.nil?
432
      render_error l(:error_no_default_issue_status)
433
      return false
434
    end
435
  end
436

    
437
  def parse_params_for_bulk_issue_attributes(params)
438
    attributes = (params[:issue] || {}).reject {|k,v| v.blank?}
439
    attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
440
    if custom = attributes[:custom_field_values]
441
      custom.reject! {|k,v| v.blank?}
442
      custom.keys.each do |k|
443
        if custom[k].is_a?(Array)
444
          custom[k] << '' if custom[k].delete('__none__')
445
        else
446
          custom[k] = '' if custom[k] == '__none__'
447
        end
448
      end
449
    end
450
    attributes
451
  end
452
end