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 @ 958:352539ac7b43

History | View | Annotate | Download (13.6 KB)

1
# Redmine - project management software
2
# Copyright (C) 2006-2011  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, :move, :perform_move, :destroy]
24
  before_filter :check_project_uniqueness, :only => [:move, :perform_move]
25
  before_filter :find_project, :only => [:new, :create]
26
  before_filter :authorize, :except => [:index]
27
  before_filter :find_optional_project, :only => [:index]
28
  before_filter :check_for_default_issue_status, :only => [:new, :create]
29
  before_filter :build_new_issue_from_params, :only => [:new, :create]
30
  accept_rss_auth :index, :show
31
  accept_api_auth :index, :show, :create, :update, :destroy
32

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

    
35
  helper :journals
36
  helper :projects
37
  include ProjectsHelper
38
  helper :custom_fields
39
  include CustomFieldsHelper
40
  helper :issue_relations
41
  include IssueRelationsHelper
42
  helper :watchers
43
  include WatchersHelper
44
  helper :attachments
45
  include AttachmentsHelper
46
  helper :queries
47
  include QueriesHelper
48
  helper :repositories
49
  include RepositoriesHelper
50
  helper :sort
51
  include SortHelper
52
  include IssuesHelper
53
  helper :timelog
54
  helper :gantt
55
  include Redmine::Export::PDF
56

    
57
  verify :method => [:post, :delete],
58
         :only => :destroy,
59
         :render => { :nothing => true, :status => :method_not_allowed }
60

    
61
  verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
62
  verify :method => :post, :only => :bulk_update, :render => {:nothing => true, :status => :method_not_allowed }
63
  verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
64

    
65
  def index
66
    retrieve_query
67
    sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
68
    sort_update(@query.sortable_columns)
69

    
70
    if @query.valid?
71
      case params[:format]
72
      when 'csv', 'pdf'
73
        @limit = Setting.issues_export_limit.to_i
74
      when 'atom'
75
        @limit = Setting.feeds_limit.to_i
76
      when 'xml', 'json'
77
        @offset, @limit = api_offset_and_limit
78
      else
79
        @limit = per_page_option
80
      end
81

    
82
      @issue_count = @query.issue_count
83
      @issue_pages = Paginator.new self, @issue_count, @limit, params['page']
84
      @offset ||= @issue_pages.current.offset
85
      @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version],
86
                              :order => sort_clause,
87
                              :offset => @offset,
88
                              :limit => @limit)
89
      @issue_count_by_group = @query.issue_count_by_group
90

    
91
      respond_to do |format|
92
        format.html { render :template => 'issues/index', :layout => !request.xhr? }
93
        format.api  {
94
          Issue.load_relations(@issues) if include_in_api_response?('relations')
95
        }
96
        format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") }
97
        format.csv  { send_data(issues_to_csv(@issues, @project, @query, params), :type => 'text/csv; header=present', :filename => 'export.csv') }
98
        format.pdf  { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'export.pdf') }
99
      end
100
    else
101
      respond_to do |format|
102
        format.html { render(:template => 'issues/index', :layout => !request.xhr?) }
103
        format.any(:atom, :csv, :pdf) { render(:nothing => true) }
104
        format.api { render_validation_errors(@query) }
105
      end
106
    end
107
  rescue ActiveRecord::RecordNotFound
108
    render_404
109
  end
110

    
111
  def show
112
    @journals = @issue.journals.find(:all, :include => [:user, :details], :order => "#{Journal.table_name}.created_on ASC")
113
    @journals.each_with_index {|j,i| j.indice = i+1}
114
    @journals.reverse! if User.current.wants_comments_in_reverse_order?
115

    
116
    if User.current.allowed_to?(:view_changesets, @project)
117
      @changesets = @issue.changesets.visible.all
118
      @changesets.reverse! if User.current.wants_comments_in_reverse_order?
119
    end
120

    
121
    @relations = @issue.relations.select {|r| r.other_issue(@issue) && r.other_issue(@issue).visible? }
122
    @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
123
    @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
124
    @priorities = IssuePriority.active
125
    @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
126
    respond_to do |format|
127
      format.html { render :template => 'issues/show' }
128
      format.api
129
      format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' }
130
      format.pdf  { send_data(issue_to_pdf(@issue), :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") }
131
    end
132
  end
133

    
134
  # Add a new issue
135
  # The new issue will be created from an existing one if copy_from parameter is given
136
  def new
137
    respond_to do |format|
138
      format.html { render :action => 'new', :layout => !request.xhr? }
139
      format.js { render :partial => 'attributes' }
140
    end
141
  end
142

    
143
  def create
144
    call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
145
    if @issue.save
146
      attachments = Attachment.attach_files(@issue, params[:attachments])
147
      
148
      call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
149

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

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

    
175
  def edit
176
    update_issue_from_params
177

    
178
    @journal = @issue.current_journal
179

    
180
    respond_to do |format|
181
      format.html { }
182
      format.xml  { }
183
    end
184
  end
185

    
186
  def update
187
    update_issue_from_params
188

    
189
    if @issue.save_issue_with_child_records(params, @time_entry)
190
      render_attachment_warning_if_needed(@issue)
191
      flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
192

    
193
      respond_to do |format|
194
        format.html { redirect_back_or_default({:action => 'show', :id => @issue}) }
195
        format.api  { head :ok }
196
      end
197
    else
198
      render_attachment_warning_if_needed(@issue)
199
      flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
200
      @journal = @issue.current_journal
201

    
202
      respond_to do |format|
203
        format.html { render :action => 'edit' }
204
        format.api  { render_validation_errors(@issue) }
205
      end
206
    end
207
  end
208

    
209
  # Bulk edit a set of issues
210
  def bulk_edit
211
    @issues.sort!
212
    @available_statuses = @projects.map{|p|Workflow.available_statuses(p)}.inject{|memo,w|memo & w}
213
    @custom_fields = @projects.map{|p|p.all_issue_custom_fields}.inject{|memo,c|memo & c}
214
    @assignables = @projects.map(&:assignable_users).inject{|memo,a| memo & a}
215
    @trackers = @projects.map(&:trackers).inject{|memo,t| memo & t}
216
  end
217

    
218
  def bulk_update
219
    @issues.sort!
220
    attributes = parse_params_for_bulk_issue_attributes(params)
221

    
222
    unsaved_issue_ids = []
223
    @issues.each do |issue|
224
      issue.reload
225
      journal = issue.init_journal(User.current, params[:notes])
226
      issue.safe_attributes = attributes
227
      call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue })
228
      unless issue.save
229
        # Keep unsaved issue ids to display them in flash error
230
        unsaved_issue_ids << issue.id
231
      end
232
    end
233
    set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids)
234
    redirect_back_or_default({:controller => 'issues', :action => 'index', :project_id => @project})
235
  end
236

    
237
  def destroy
238
    @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f
239
    if @hours > 0
240
      case params[:todo]
241
      when 'destroy'
242
        # nothing to do
243
      when 'nullify'
244
        TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues])
245
      when 'reassign'
246
        reassign_to = @project.issues.find_by_id(params[:reassign_to_id])
247
        if reassign_to.nil?
248
          flash.now[:error] = l(:error_issue_not_found_in_project)
249
          return
250
        else
251
          TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues])
252
        end
253
      else
254
        # display the destroy form if it's a user request
255
        return unless api_request?
256
      end
257
    end
258
    @issues.each do |issue|
259
      begin
260
        issue.reload.destroy
261
      rescue ::ActiveRecord::RecordNotFound # raised by #reload if issue no longer exists
262
        # nothing to do, issue was already deleted (eg. by a parent)
263
      end
264
    end
265
    respond_to do |format|
266
      format.html { redirect_back_or_default(:action => 'index', :project_id => @project) }
267
      format.api  { head :ok }
268
    end
269
  end
270

    
271
private
272
  def find_issue
273
    # Issue.visible.find(...) can not be used to redirect user to the login form
274
    # if the issue actually exists but requires authentication
275
    @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category])
276
    unless @issue.visible?
277
      deny_access
278
      return
279
    end
280
    @project = @issue.project
281
  rescue ActiveRecord::RecordNotFound
282
    render_404
283
  end
284

    
285
  def find_project
286
    project_id = (params[:issue] && params[:issue][:project_id]) || params[:project_id]
287
    @project = Project.find(project_id)
288
  rescue ActiveRecord::RecordNotFound
289
    render_404
290
  end
291

    
292
  # Used by #edit and #update to set some common instance variables
293
  # from the params
294
  # TODO: Refactor, not everything in here is needed by #edit
295
  def update_issue_from_params
296
    @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
297
    @priorities = IssuePriority.active
298
    @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
299
    @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
300
    @time_entry.attributes = params[:time_entry]
301

    
302
    @notes = params[:notes] || (params[:issue].present? ? params[:issue][:notes] : nil)
303
    @issue.init_journal(User.current, @notes)
304
    @issue.safe_attributes = params[:issue]
305

    
306
    # tests if the the user assigned_to_id
307
    # is in this issues watcher's list
308
    # if not, adds it.
309

    
310
    if params[:issue] && params[:issue][:assigned_to_id] && !params[:issue][:assigned_to_id].empty?:
311
     unless @issue.watched_by?(User.find(params[:issue][:assigned_to_id])):
312
       @issue.add_watcher(User.find(params[:issue][:assigned_to_id]))
313
     end
314
    end
315

    
316

    
317
  end
318

    
319
  # TODO: Refactor, lots of extra code in here
320
  # TODO: Changing tracker on an existing issue should not trigger this
321
  def build_new_issue_from_params
322
    if params[:id].blank?
323
      @issue = Issue.new
324
      @issue.copy_from(params[:copy_from]) if params[:copy_from]
325
      @issue.project = @project
326
    else
327
      @issue = @project.issues.visible.find(params[:id])
328
    end
329

    
330
    @issue.project = @project
331
    @issue.author = User.current
332
    # Tracker must be set before custom field values
333
    @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first)
334
    if @issue.tracker.nil?
335
      render_error l(:error_no_tracker_in_project)
336
      return false
337
    end
338
    @issue.start_date ||= Date.today if Setting.default_issue_start_date_to_creation_date?
339
    if params[:issue].is_a?(Hash)
340
      @issue.safe_attributes = params[:issue]
341
      if User.current.allowed_to?(:add_issue_watchers, @project) && @issue.new_record?
342
        @issue.watcher_user_ids = params[:issue]['watcher_user_ids']
343
      end
344
    end
345
    @priorities = IssuePriority.active
346
    @allowed_statuses = @issue.new_statuses_allowed_to(User.current, true)
347
  end
348

    
349
  def check_for_default_issue_status
350
    if IssueStatus.default.nil?
351
      render_error l(:error_no_default_issue_status)
352
      return false
353
    end
354
  end
355

    
356
  def parse_params_for_bulk_issue_attributes(params)
357
    attributes = (params[:issue] || {}).reject {|k,v| v.blank?}
358
    attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
359
    attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values]
360
    attributes
361
  end
362
end