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 @ 443:350acce374a2

History | View | Annotate | Download (13.2 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_key_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
  helper :gantt
54
  include Redmine::Export::PDF
55

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

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

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

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

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

    
90
      respond_to do |format|
91
        format.html { render :template => 'issues/index.rhtml', :layout => !request.xhr? }
92
        format.api
93
        format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") }
94
        format.csv  { send_data(issues_to_csv(@issues, @project), :type => 'text/csv; header=present', :filename => 'export.csv') }
95
        format.pdf  { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'export.pdf') }
96
      end
97
    else
98
      # Send html if the query is not valid
99
      render(:template => 'issues/index.rhtml', :layout => !request.xhr?)
100
    end
101
  rescue ActiveRecord::RecordNotFound
102
    render_404
103
  end
104

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

    
110
    if User.current.allowed_to?(:view_changesets, @project)
111
      @changesets = @issue.changesets.visible.all
112
      @changesets.reverse! if User.current.wants_comments_in_reverse_order?
113
    end
114

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

    
128
  # Add a new issue
129
  # The new issue will be created from an existing one if copy_from parameter is given
130
  def new
131
    respond_to do |format|
132
      format.html { render :action => 'new', :layout => !request.xhr? }
133
      format.js { render :partial => 'attributes' }
134
    end
135
  end
136

    
137
  def create
138
    call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
139
    if @issue.save
140
      attachments = Attachment.attach_files(@issue, params[:attachments])
141
      render_attachment_warning_if_needed(@issue)
142
      flash[:notice] = l(:notice_successful_create)
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
          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?} } :
156
                      { :action => 'show', :id => @issue })
157
        }
158
        format.api  { render :action => 'show', :status => :created, :location => issue_url(@issue) }
159
      end
160
      return
161
    else
162
      respond_to do |format|
163
        format.html { render :action => 'new' }
164
        format.api  { render_validation_errors(@issue) }
165
      end
166
    end
167
  end
168

    
169
  def edit
170
    update_issue_from_params
171

    
172
    @journal = @issue.current_journal
173

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

    
180
  def update
181
    update_issue_from_params
182

    
183
    if @issue.save_issue_with_child_records(params, @time_entry)
184
      render_attachment_warning_if_needed(@issue)
185
      flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
186

    
187
      respond_to do |format|
188
        format.html { redirect_back_or_default({:action => 'show', :id => @issue}) }
189
        format.api  { head :ok }
190
      end
191
    else
192
      render_attachment_warning_if_needed(@issue)
193
      flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
194
      @journal = @issue.current_journal
195

    
196
      respond_to do |format|
197
        format.html { render :action => 'edit' }
198
        format.api  { render_validation_errors(@issue) }
199
      end
200
    end
201
  end
202

    
203
  # Bulk edit a set of issues
204
  def bulk_edit
205
    @issues.sort!
206
    @available_statuses = @projects.map{|p|Workflow.available_statuses(p)}.inject{|memo,w|memo & w}
207
    @custom_fields = @projects.map{|p|p.all_issue_custom_fields}.inject{|memo,c|memo & c}
208
    @assignables = @projects.map(&:assignable_users).inject{|memo,a| memo & a}
209
    @trackers = @projects.map(&:trackers).inject{|memo,t| memo & t}
210
  end
211

    
212
  def bulk_update
213
    @issues.sort!
214
    attributes = parse_params_for_bulk_issue_attributes(params)
215

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

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

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

    
279
  def find_project
280
    project_id = (params[:issue] && params[:issue][:project_id]) || params[:project_id]
281
    @project = Project.find(project_id)
282
  rescue ActiveRecord::RecordNotFound
283
    render_404
284
  end
285

    
286
  # Used by #edit and #update to set some common instance variables
287
  # from the params
288
  # TODO: Refactor, not everything in here is needed by #edit
289
  def update_issue_from_params
290
    @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
291
    @priorities = IssuePriority.all
292
    @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
293
    @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
294
    @time_entry.attributes = params[:time_entry]
295

    
296
    @notes = params[:notes] || (params[:issue].present? ? params[:issue][:notes] : nil)
297
    @issue.init_journal(User.current, @notes)
298
    @issue.safe_attributes = params[:issue]
299

    
300
    # tests if the the user assigned_to_id
301
    # is in this issues watcher's list
302
    # if not, adds it.
303

    
304
    if params[:issue][:assigned_to_id] && !params[:issue][:assigned_to_id].empty?:
305
     unless @issue.watched_by?(User.find(params[:issue][:assigned_to_id])):
306
       @issue.add_watcher(User.find(params[:issue][:assigned_to_id]))
307
     end
308
    end
309

    
310

    
311
  end
312

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

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

    
343
  def check_for_default_issue_status
344
    if IssueStatus.default.nil?
345
      render_error l(:error_no_default_issue_status)
346
      return false
347
    end
348
  end
349

    
350
  def parse_params_for_bulk_issue_attributes(params)
351
    attributes = (params[:issue] || {}).reject {|k,v| v.blank?}
352
    attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
353
    attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values]
354
    attributes
355
  end
356
end