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 @ 418:a9921f3e9088

History | View | Annotate | Download (13.2 KB)

1
# Redmine - project management software
2
# Copyright (C) 2006-2008  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 :sort
48
  include SortHelper
49
  include IssuesHelper
50
  helper :timelog
51
  helper :gantt
52
  include Redmine::Export::PDF
53

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

    
58
  verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
59
  verify :method => :post, :only => :bulk_update, :render => {:nothing => true, :status => :method_not_allowed }
60
  verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
61
  
62
  def index
63
    retrieve_query
64
    sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
65
    sort_update(@query.sortable_columns)
66
    
67
    if @query.valid?
68
      limit = case params[:format]
69
      when 'csv', 'pdf'
70
        Setting.issues_export_limit.to_i
71
      when 'atom'
72
        Setting.feeds_limit.to_i
73
      else
74
        per_page_option
75
      end
76
      
77
      @issue_count = @query.issue_count
78
      @issue_pages = Paginator.new self, @issue_count, limit, params['page']
79
      @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version],
80
                              :order => sort_clause, 
81
                              :offset => @issue_pages.current.offset, 
82
                              :limit => limit)
83
      @issue_count_by_group = @query.issue_count_by_group
84
      
85
      respond_to do |format|
86
        format.html { render :template => 'issues/index.rhtml', :layout => !request.xhr? }
87
        format.xml  { render :layout => false }
88
        format.json { render :text => @issues.to_json, :layout => false }
89
        format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") }
90
        format.csv  { send_data(issues_to_csv(@issues, @project), :type => 'text/csv; header=present', :filename => 'export.csv') }
91
        format.pdf  { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'export.pdf') }
92
      end
93
    else
94
      # Send html if the query is not valid
95
      render(:template => 'issues/index.rhtml', :layout => !request.xhr?)
96
    end
97
  rescue ActiveRecord::RecordNotFound
98
    render_404
99
  end
100
  
101
  def show
102
    @journals = @issue.journals.find(:all, :include => [:user, :details], :order => "#{Journal.table_name}.created_on ASC")
103
    @journals.each_with_index {|j,i| j.indice = i+1}
104
    @journals.reverse! if User.current.wants_comments_in_reverse_order?
105
    @changesets = @issue.changesets.visible.all
106
    @changesets.reverse! if User.current.wants_comments_in_reverse_order?
107
    @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
108
    @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
109
    @priorities = IssuePriority.all
110
    @time_entry = TimeEntry.new
111
    respond_to do |format|
112
      format.html { render :template => 'issues/show.rhtml' }
113
      format.xml  { render :layout => false }
114
      format.json { render :text => @issue.to_json, :layout => false }
115
      format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' }
116
      format.pdf  { send_data(issue_to_pdf(@issue), :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") }
117
    end
118
  end
119

    
120
  # Add a new issue
121
  # The new issue will be created from an existing one if copy_from parameter is given
122
  def new
123
    respond_to do |format|
124
      format.html { render :action => 'new', :layout => !request.xhr? }
125
      format.js { render :partial => 'attributes' }
126
    end
127
  end
128

    
129
  def create
130
    call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
131
    if @issue.save
132
      attachments = Attachment.attach_files(@issue, params[:attachments])
133
      render_attachment_warning_if_needed(@issue)
134
      flash[:notice] = l(:notice_successful_create)
135
      
136
      call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
137

    
138
      # Also adds the assignee to the watcher's list
139
      if params[:issue][:assigned_to_id] && !params[:issue][:assigned_to_id].empty?:
140
       unless @issue.watcher_ids.include?(params[:issue][:assigned_to_id]):
141
         @issue.add_watcher(User.find(params[:issue][:assigned_to_id]))
142
       end
143
      end
144

    
145
      respond_to do |format|
146
        format.html {
147
          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?} } :
148
                      { :action => 'show', :id => @issue })
149
        }
150
        format.xml  { render :action => 'show', :status => :created, :location => url_for(:controller => 'issues', :action => 'show', :id => @issue) }
151
        format.json { render :text => @issue.to_json, :status => :created, :location => url_for(:controller => 'issues', :action => 'show'), :layout => false }
152
      end
153
      return
154
    else
155
      respond_to do |format|
156
        format.html { render :action => 'new' }
157
        format.xml  { render(:xml => @issue.errors, :status => :unprocessable_entity); return }
158
        format.json { render :text => object_errors_to_json(@issue), :status => :unprocessable_entity, :layout => false }
159
      end
160
    end
161
  end
162
    
163
  def edit
164
    update_issue_from_params
165

    
166
    @journal = @issue.current_journal
167

    
168
    respond_to do |format|
169
      format.html { }
170
      format.xml  { }
171
    end
172
  end
173

    
174
  def update
175
    update_issue_from_params
176

    
177
    if @issue.save_issue_with_child_records(params, @time_entry)
178
      render_attachment_warning_if_needed(@issue)
179
      flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
180

    
181
      respond_to do |format|
182
        format.html { redirect_back_or_default({:action => 'show', :id => @issue}) }
183
        format.xml  { head :ok }
184
        format.json  { head :ok }
185
      end
186
    else
187
      render_attachment_warning_if_needed(@issue)
188
      flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
189
      @journal = @issue.current_journal
190

    
191
      respond_to do |format|
192
        format.html { render :action => 'edit' }
193
        format.xml  { render :xml => @issue.errors, :status => :unprocessable_entity }
194
        format.json { render :text => object_errors_to_json(@issue), :status => :unprocessable_entity, :layout => false }
195
      end
196
    end
197
  end
198

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

    
208
  def bulk_update
209
    @issues.sort!
210
    attributes = parse_params_for_bulk_issue_attributes(params)
211

    
212
    unsaved_issue_ids = []
213
    @issues.each do |issue|
214
      issue.reload
215
      journal = issue.init_journal(User.current, params[:notes])
216
      issue.safe_attributes = attributes
217
      call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue })
218
      unless issue.save
219
        # Keep unsaved issue ids to display them in flash error
220
        unsaved_issue_ids << issue.id
221
      end
222
    end
223
    set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids)
224
    redirect_back_or_default({:controller => 'issues', :action => 'index', :project_id => @project})
225
  end
226
  
227
  def destroy
228
    @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f
229
    if @hours > 0
230
      case params[:todo]
231
      when 'destroy'
232
        # nothing to do
233
      when 'nullify'
234
        TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues])
235
      when 'reassign'
236
        reassign_to = @project.issues.find_by_id(params[:reassign_to_id])
237
        if reassign_to.nil?
238
          flash.now[:error] = l(:error_issue_not_found_in_project)
239
          return
240
        else
241
          TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues])
242
        end
243
      else
244
        unless params[:format] == 'xml' || params[:format] == 'json'
245
          # display the destroy form if it's a user request
246
          return
247
        end
248
      end
249
    end
250
    @issues.each(&:destroy)
251
    respond_to do |format|
252
      format.html { redirect_back_or_default(:action => 'index', :project_id => @project) }
253
      format.xml  { head :ok }
254
      format.json  { head :ok }
255
    end
256
  end
257

    
258
private
259
  def find_issue
260
    @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category])
261
    @project = @issue.project
262
  rescue ActiveRecord::RecordNotFound
263
    render_404
264
  end
265
  
266
  def find_project
267
    project_id = (params[:issue] && params[:issue][:project_id]) || params[:project_id]
268
    @project = Project.find(project_id)
269
  rescue ActiveRecord::RecordNotFound
270
    render_404
271
  end
272
  
273
  # Used by #edit and #update to set some common instance variables
274
  # from the params
275
  # TODO: Refactor, not everything in here is needed by #edit
276
  def update_issue_from_params
277
    @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
278
    @priorities = IssuePriority.all
279
    @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
280
    @time_entry = TimeEntry.new
281
    @time_entry.attributes = params[:time_entry]
282
    
283
    @notes = params[:notes] || (params[:issue].present? ? params[:issue][:notes] : nil)
284
    @issue.init_journal(User.current, @notes)
285
    @issue.safe_attributes = params[:issue]
286

    
287
    # tests if the the user assigned_to_id
288
    # is in this issues watcher's list
289
    # if not, adds it.
290

    
291
    if params[:issue][:assigned_to_id] && !params[:issue][:assigned_to_id].empty?:
292
     unless @issue.watched_by?(User.find(params[:issue][:assigned_to_id])):
293
       @issue.add_watcher(User.find(params[:issue][:assigned_to_id]))
294
     end
295
    end
296

    
297

    
298
  end
299

    
300
  # TODO: Refactor, lots of extra code in here
301
  # TODO: Changing tracker on an existing issue should not trigger this
302
  def build_new_issue_from_params
303
    if params[:id].blank?
304
      @issue = Issue.new
305
      @issue.copy_from(params[:copy_from]) if params[:copy_from]
306
      @issue.project = @project
307
    else
308
      @issue = @project.issues.visible.find(params[:id])
309
    end
310
    
311
    @issue.project = @project
312
    # Tracker must be set before custom field values
313
    @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first)
314
    if @issue.tracker.nil?
315
      render_error l(:error_no_tracker_in_project)
316
      return false
317
    end
318
    @issue.start_date ||= Date.today
319
    if params[:issue].is_a?(Hash)
320
      @issue.safe_attributes = params[:issue]
321
      if User.current.allowed_to?(:add_issue_watchers, @project) && @issue.new_record?
322
        @issue.watcher_user_ids = params[:issue]['watcher_user_ids']
323
      end
324
    end
325
    @issue.author = User.current
326
    @priorities = IssuePriority.all
327
    @allowed_statuses = @issue.new_statuses_allowed_to(User.current, true)
328
  end
329

    
330
  def check_for_default_issue_status
331
    if IssueStatus.default.nil?
332
      render_error l(:error_no_default_issue_status)
333
      return false
334
    end
335
  end
336

    
337
  def parse_params_for_bulk_issue_attributes(params)
338
    attributes = (params[:issue] || {}).reject {|k,v| v.blank?}
339
    attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
340
    attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values]
341
    attributes
342
  end
343
end