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 @ 780:31b3aa308568

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

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

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

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

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

    
138
  def create
139
    call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
140
    if @issue.save
141
      attachments = Attachment.attach_files(@issue, params[:attachments])
142
      render_attachment_warning_if_needed(@issue)
143
      flash[:notice] = l(:notice_successful_create)
144
      
145
      call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
146

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

    
154
      respond_to do |format|
155
        format.html {
156
          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?} } :
157
                      { :action => 'show', :id => @issue })
158
        }
159
        format.api  { render :action => 'show', :status => :created, :location => issue_url(@issue) }
160
      end
161
      return
162
    else
163
      respond_to do |format|
164
        format.html { render :action => 'new' }
165
        format.api  { render_validation_errors(@issue) }
166
      end
167
    end
168
  end
169

    
170
  def edit
171
    update_issue_from_params
172

    
173
    @journal = @issue.current_journal
174

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

    
181
  def update
182
    update_issue_from_params
183

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

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

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

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

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

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

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

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

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

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

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

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

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

    
311

    
312
  end
313

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

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

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

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