##// END OF EJS Templates
Initialize TimeEntry with issue and project for the issue edit form....
Jean-Philippe Lang -
r5154:c6e34b0c1135
parent child
Show More
@@ -1,319 +1,319
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2008 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class IssuesController < ApplicationController
19 19 menu_item :new_issue, :only => [:new, :create]
20 20 default_search_scope :issues
21 21
22 22 before_filter :find_issue, :only => [:show, :edit, :update]
23 23 before_filter :find_issues, :only => [:bulk_edit, :bulk_update, :move, :perform_move, :destroy]
24 24 before_filter :check_project_uniqueness, :only => [:move, :perform_move]
25 25 before_filter :find_project, :only => [:new, :create]
26 26 before_filter :authorize, :except => [:index]
27 27 before_filter :find_optional_project, :only => [:index]
28 28 before_filter :check_for_default_issue_status, :only => [:new, :create]
29 29 before_filter :build_new_issue_from_params, :only => [:new, :create]
30 30 accept_key_auth :index, :show, :create, :update, :destroy
31 31
32 32 rescue_from Query::StatementInvalid, :with => :query_statement_invalid
33 33
34 34 helper :journals
35 35 helper :projects
36 36 include ProjectsHelper
37 37 helper :custom_fields
38 38 include CustomFieldsHelper
39 39 helper :issue_relations
40 40 include IssueRelationsHelper
41 41 helper :watchers
42 42 include WatchersHelper
43 43 helper :attachments
44 44 include AttachmentsHelper
45 45 helper :queries
46 46 include QueriesHelper
47 47 helper :repositories
48 48 include RepositoriesHelper
49 49 helper :sort
50 50 include SortHelper
51 51 include IssuesHelper
52 52 helper :timelog
53 53 helper :gantt
54 54 include Redmine::Export::PDF
55 55
56 56 verify :method => [:post, :delete],
57 57 :only => :destroy,
58 58 :render => { :nothing => true, :status => :method_not_allowed }
59 59
60 60 verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
61 61 verify :method => :post, :only => :bulk_update, :render => {:nothing => true, :status => :method_not_allowed }
62 62 verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
63 63
64 64 def index
65 65 retrieve_query
66 66 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
67 67 sort_update(@query.sortable_columns)
68 68
69 69 if @query.valid?
70 70 case params[:format]
71 71 when 'csv', 'pdf'
72 72 @limit = Setting.issues_export_limit.to_i
73 73 when 'atom'
74 74 @limit = Setting.feeds_limit.to_i
75 75 when 'xml', 'json'
76 76 @offset, @limit = api_offset_and_limit
77 77 else
78 78 @limit = per_page_option
79 79 end
80 80
81 81 @issue_count = @query.issue_count
82 82 @issue_pages = Paginator.new self, @issue_count, @limit, params['page']
83 83 @offset ||= @issue_pages.current.offset
84 84 @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version],
85 85 :order => sort_clause,
86 86 :offset => @offset,
87 87 :limit => @limit)
88 88 @issue_count_by_group = @query.issue_count_by_group
89 89
90 90 respond_to do |format|
91 91 format.html { render :template => 'issues/index.rhtml', :layout => !request.xhr? }
92 92 format.api
93 93 format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") }
94 94 format.csv { send_data(issues_to_csv(@issues, @project), :type => 'text/csv; header=present', :filename => 'export.csv') }
95 95 format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'export.pdf') }
96 96 end
97 97 else
98 98 # Send html if the query is not valid
99 99 render(:template => 'issues/index.rhtml', :layout => !request.xhr?)
100 100 end
101 101 rescue ActiveRecord::RecordNotFound
102 102 render_404
103 103 end
104 104
105 105 def show
106 106 @journals = @issue.journals.find(:all, :include => [:user, :details], :order => "#{Journal.table_name}.created_on ASC")
107 107 @journals.each_with_index {|j,i| j.indice = i+1}
108 108 @journals.reverse! if User.current.wants_comments_in_reverse_order?
109 109 @changesets = @issue.changesets.visible.all
110 110 @changesets.reverse! if User.current.wants_comments_in_reverse_order?
111 111 @relations = @issue.relations.select {|r| r.other_issue(@issue) && r.other_issue(@issue).visible? }
112 112 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
113 113 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
114 114 @priorities = IssuePriority.all
115 @time_entry = TimeEntry.new
115 @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
116 116 respond_to do |format|
117 117 format.html { render :template => 'issues/show.rhtml' }
118 118 format.api
119 119 format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' }
120 120 format.pdf { send_data(issue_to_pdf(@issue), :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") }
121 121 end
122 122 end
123 123
124 124 # Add a new issue
125 125 # The new issue will be created from an existing one if copy_from parameter is given
126 126 def new
127 127 respond_to do |format|
128 128 format.html { render :action => 'new', :layout => !request.xhr? }
129 129 format.js { render :partial => 'attributes' }
130 130 end
131 131 end
132 132
133 133 def create
134 134 call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
135 135 if @issue.save
136 136 attachments = Attachment.attach_files(@issue, params[:attachments])
137 137 render_attachment_warning_if_needed(@issue)
138 138 flash[:notice] = l(:notice_successful_create)
139 139 call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
140 140 respond_to do |format|
141 141 format.html {
142 142 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?} } :
143 143 { :action => 'show', :id => @issue })
144 144 }
145 145 format.api { render :action => 'show', :status => :created, :location => issue_url(@issue) }
146 146 end
147 147 return
148 148 else
149 149 respond_to do |format|
150 150 format.html { render :action => 'new' }
151 151 format.api { render_validation_errors(@issue) }
152 152 end
153 153 end
154 154 end
155 155
156 156 def edit
157 157 update_issue_from_params
158 158
159 159 @journal = @issue.current_journal
160 160
161 161 respond_to do |format|
162 162 format.html { }
163 163 format.xml { }
164 164 end
165 165 end
166 166
167 167 def update
168 168 update_issue_from_params
169 169
170 170 if @issue.save_issue_with_child_records(params, @time_entry)
171 171 render_attachment_warning_if_needed(@issue)
172 172 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
173 173
174 174 respond_to do |format|
175 175 format.html { redirect_back_or_default({:action => 'show', :id => @issue}) }
176 176 format.api { head :ok }
177 177 end
178 178 else
179 179 render_attachment_warning_if_needed(@issue)
180 180 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
181 181 @journal = @issue.current_journal
182 182
183 183 respond_to do |format|
184 184 format.html { render :action => 'edit' }
185 185 format.api { render_validation_errors(@issue) }
186 186 end
187 187 end
188 188 end
189 189
190 190 # Bulk edit a set of issues
191 191 def bulk_edit
192 192 @issues.sort!
193 193 @available_statuses = @projects.map{|p|Workflow.available_statuses(p)}.inject{|memo,w|memo & w}
194 194 @custom_fields = @projects.map{|p|p.all_issue_custom_fields}.inject{|memo,c|memo & c}
195 195 @assignables = @projects.map(&:assignable_users).inject{|memo,a| memo & a}
196 196 @trackers = @projects.map(&:trackers).inject{|memo,t| memo & t}
197 197 end
198 198
199 199 def bulk_update
200 200 @issues.sort!
201 201 attributes = parse_params_for_bulk_issue_attributes(params)
202 202
203 203 unsaved_issue_ids = []
204 204 @issues.each do |issue|
205 205 issue.reload
206 206 journal = issue.init_journal(User.current, params[:notes])
207 207 issue.safe_attributes = attributes
208 208 call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue })
209 209 unless issue.save
210 210 # Keep unsaved issue ids to display them in flash error
211 211 unsaved_issue_ids << issue.id
212 212 end
213 213 end
214 214 set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids)
215 215 redirect_back_or_default({:controller => 'issues', :action => 'index', :project_id => @project})
216 216 end
217 217
218 218 def destroy
219 219 @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f
220 220 if @hours > 0
221 221 case params[:todo]
222 222 when 'destroy'
223 223 # nothing to do
224 224 when 'nullify'
225 225 TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues])
226 226 when 'reassign'
227 227 reassign_to = @project.issues.find_by_id(params[:reassign_to_id])
228 228 if reassign_to.nil?
229 229 flash.now[:error] = l(:error_issue_not_found_in_project)
230 230 return
231 231 else
232 232 TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues])
233 233 end
234 234 else
235 235 # display the destroy form if it's a user request
236 236 return unless api_request?
237 237 end
238 238 end
239 239 @issues.each(&:destroy)
240 240 respond_to do |format|
241 241 format.html { redirect_back_or_default(:action => 'index', :project_id => @project) }
242 242 format.api { head :ok }
243 243 end
244 244 end
245 245
246 246 private
247 247 def find_issue
248 248 @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category])
249 249 @project = @issue.project
250 250 rescue ActiveRecord::RecordNotFound
251 251 render_404
252 252 end
253 253
254 254 def find_project
255 255 project_id = (params[:issue] && params[:issue][:project_id]) || params[:project_id]
256 256 @project = Project.find(project_id)
257 257 rescue ActiveRecord::RecordNotFound
258 258 render_404
259 259 end
260 260
261 261 # Used by #edit and #update to set some common instance variables
262 262 # from the params
263 263 # TODO: Refactor, not everything in here is needed by #edit
264 264 def update_issue_from_params
265 265 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
266 266 @priorities = IssuePriority.all
267 267 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
268 @time_entry = TimeEntry.new
268 @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
269 269 @time_entry.attributes = params[:time_entry]
270 270
271 271 @notes = params[:notes] || (params[:issue].present? ? params[:issue][:notes] : nil)
272 272 @issue.init_journal(User.current, @notes)
273 273 @issue.safe_attributes = params[:issue]
274 274 end
275 275
276 276 # TODO: Refactor, lots of extra code in here
277 277 # TODO: Changing tracker on an existing issue should not trigger this
278 278 def build_new_issue_from_params
279 279 if params[:id].blank?
280 280 @issue = Issue.new
281 281 @issue.copy_from(params[:copy_from]) if params[:copy_from]
282 282 @issue.project = @project
283 283 else
284 284 @issue = @project.issues.visible.find(params[:id])
285 285 end
286 286
287 287 @issue.project = @project
288 288 # Tracker must be set before custom field values
289 289 @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first)
290 290 if @issue.tracker.nil?
291 291 render_error l(:error_no_tracker_in_project)
292 292 return false
293 293 end
294 294 @issue.start_date ||= Date.today
295 295 if params[:issue].is_a?(Hash)
296 296 @issue.safe_attributes = params[:issue]
297 297 if User.current.allowed_to?(:add_issue_watchers, @project) && @issue.new_record?
298 298 @issue.watcher_user_ids = params[:issue]['watcher_user_ids']
299 299 end
300 300 end
301 301 @issue.author = User.current
302 302 @priorities = IssuePriority.all
303 303 @allowed_statuses = @issue.new_statuses_allowed_to(User.current, true)
304 304 end
305 305
306 306 def check_for_default_issue_status
307 307 if IssueStatus.default.nil?
308 308 render_error l(:error_no_default_issue_status)
309 309 return false
310 310 end
311 311 end
312 312
313 313 def parse_params_for_bulk_issue_attributes(params)
314 314 attributes = (params[:issue] || {}).reject {|k,v| v.blank?}
315 315 attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
316 316 attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values]
317 317 attributes
318 318 end
319 319 end
General Comments 0
You need to be logged in to leave comments. Login now