@@ -1,590 +1,590 | |||
|
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 |
|
20 | 20 | default_search_scope :issues |
|
21 | 21 | |
|
22 | 22 | before_filter :find_issue, :only => [:show, :edit, :update, :reply] |
|
23 | 23 | before_filter :find_issues, :only => [:bulk_edit, :move, :destroy] |
|
24 | 24 | before_filter :find_project, :only => [:new, :update_form, :preview] |
|
25 | 25 | before_filter :authorize, :except => [:index, :changes, :gantt, :calendar, :preview, :context_menu] |
|
26 | 26 | before_filter :find_optional_project, :only => [:index, :changes, :gantt, :calendar] |
|
27 | 27 | accept_key_auth :index, :show, :changes |
|
28 | 28 | |
|
29 | 29 | rescue_from Query::StatementInvalid, :with => :query_statement_invalid |
|
30 | 30 | |
|
31 | 31 | helper :journals |
|
32 | 32 | helper :projects |
|
33 | 33 | include ProjectsHelper |
|
34 | 34 | helper :custom_fields |
|
35 | 35 | include CustomFieldsHelper |
|
36 | 36 | helper :issue_relations |
|
37 | 37 | include IssueRelationsHelper |
|
38 | 38 | helper :watchers |
|
39 | 39 | include WatchersHelper |
|
40 | 40 | helper :attachments |
|
41 | 41 | include AttachmentsHelper |
|
42 | 42 | helper :queries |
|
43 | 43 | include QueriesHelper |
|
44 | 44 | helper :sort |
|
45 | 45 | include SortHelper |
|
46 | 46 | include IssuesHelper |
|
47 | 47 | helper :timelog |
|
48 | 48 | include Redmine::Export::PDF |
|
49 | 49 | |
|
50 | 50 | verify :method => [:post, :delete], |
|
51 | 51 | :only => :destroy, |
|
52 | 52 | :render => { :nothing => true, :status => :method_not_allowed } |
|
53 | 53 | |
|
54 | 54 | verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed } |
|
55 | 55 | |
|
56 | 56 | def index |
|
57 | 57 | retrieve_query |
|
58 | 58 | sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria) |
|
59 | 59 | sort_update({'id' => "#{Issue.table_name}.id"}.merge(@query.available_columns.inject({}) {|h, c| h[c.name.to_s] = c.sortable; h})) |
|
60 | 60 | |
|
61 | 61 | if @query.valid? |
|
62 | 62 | limit = case params[:format] |
|
63 | 63 | when 'csv', 'pdf' |
|
64 | 64 | Setting.issues_export_limit.to_i |
|
65 | 65 | when 'atom' |
|
66 | 66 | Setting.feeds_limit.to_i |
|
67 | 67 | else |
|
68 | 68 | per_page_option |
|
69 | 69 | end |
|
70 | 70 | |
|
71 | 71 | @issue_count = @query.issue_count |
|
72 | 72 | @issue_pages = Paginator.new self, @issue_count, limit, params['page'] |
|
73 | 73 | @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version], |
|
74 | 74 | :order => sort_clause, |
|
75 | 75 | :offset => @issue_pages.current.offset, |
|
76 | 76 | :limit => limit) |
|
77 | 77 | @issue_count_by_group = @query.issue_count_by_group |
|
78 | 78 | |
|
79 | 79 | respond_to do |format| |
|
80 | 80 | format.html { render :template => 'issues/index.rhtml', :layout => !request.xhr? } |
|
81 | 81 | format.xml { render :layout => false } |
|
82 | 82 | format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") } |
|
83 | 83 | format.csv { send_data(issues_to_csv(@issues, @project), :type => 'text/csv; header=present', :filename => 'export.csv') } |
|
84 | 84 | format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'export.pdf') } |
|
85 | 85 | end |
|
86 | 86 | else |
|
87 | 87 | # Send html if the query is not valid |
|
88 | 88 | render(:template => 'issues/index.rhtml', :layout => !request.xhr?) |
|
89 | 89 | end |
|
90 | 90 | rescue ActiveRecord::RecordNotFound |
|
91 | 91 | render_404 |
|
92 | 92 | end |
|
93 | 93 | |
|
94 | 94 | def changes |
|
95 | 95 | retrieve_query |
|
96 | 96 | sort_init 'id', 'desc' |
|
97 | 97 | sort_update({'id' => "#{Issue.table_name}.id"}.merge(@query.available_columns.inject({}) {|h, c| h[c.name.to_s] = c.sortable; h})) |
|
98 | 98 | |
|
99 | 99 | if @query.valid? |
|
100 | 100 | @journals = @query.journals(:order => "#{Journal.table_name}.created_on DESC", |
|
101 | 101 | :limit => 25) |
|
102 | 102 | end |
|
103 | 103 | @title = (@project ? @project.name : Setting.app_title) + ": " + (@query.new_record? ? l(:label_changes_details) : @query.name) |
|
104 | 104 | render :layout => false, :content_type => 'application/atom+xml' |
|
105 | 105 | rescue ActiveRecord::RecordNotFound |
|
106 | 106 | render_404 |
|
107 | 107 | end |
|
108 | 108 | |
|
109 | 109 | def show |
|
110 | 110 | @journals = @issue.journals.find(:all, :include => [:user, :details], :order => "#{Journal.table_name}.created_on ASC") |
|
111 | 111 | @journals.each_with_index {|j,i| j.indice = i+1} |
|
112 | 112 | @journals.reverse! if User.current.wants_comments_in_reverse_order? |
|
113 | 113 | @changesets = @issue.changesets.visible.all |
|
114 | 114 | @changesets.reverse! if User.current.wants_comments_in_reverse_order? |
|
115 | 115 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current) |
|
116 | 116 | @edit_allowed = User.current.allowed_to?(:edit_issues, @project) |
|
117 | 117 | @priorities = IssuePriority.all |
|
118 | 118 | @time_entry = TimeEntry.new |
|
119 | 119 | respond_to do |format| |
|
120 | 120 | format.html { render :template => 'issues/show.rhtml' } |
|
121 | 121 | format.xml { render :layout => false } |
|
122 | 122 | format.atom { render :action => 'changes', :layout => false, :content_type => 'application/atom+xml' } |
|
123 | 123 | format.pdf { send_data(issue_to_pdf(@issue), :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") } |
|
124 | 124 | end |
|
125 | 125 | end |
|
126 | 126 | |
|
127 | 127 | # Add a new issue |
|
128 | 128 | # The new issue will be created from an existing one if copy_from parameter is given |
|
129 | 129 | def new |
|
130 | 130 | @issue = Issue.new |
|
131 | 131 | @issue.copy_from(params[:copy_from]) if params[:copy_from] |
|
132 | 132 | @issue.project = @project |
|
133 | 133 | # Tracker must be set before custom field values |
|
134 | 134 | @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first) |
|
135 | 135 | if @issue.tracker.nil? |
|
136 | 136 | render_error l(:error_no_tracker_in_project) |
|
137 | 137 | return |
|
138 | 138 | end |
|
139 | 139 | if params[:issue].is_a?(Hash) |
|
140 | 140 | @issue.safe_attributes = params[:issue] |
|
141 | 141 | @issue.watcher_user_ids = params[:issue]['watcher_user_ids'] if User.current.allowed_to?(:add_issue_watchers, @project) |
|
142 | 142 | end |
|
143 | 143 | @issue.author = User.current |
|
144 | 144 | |
|
145 | 145 | default_status = IssueStatus.default |
|
146 | 146 | unless default_status |
|
147 | 147 | render_error l(:error_no_default_issue_status) |
|
148 | 148 | return |
|
149 | 149 | end |
|
150 | 150 | @issue.status = default_status |
|
151 | @allowed_statuses = ([default_status] + default_status.find_new_statuses_allowed_to(User.current.roles_for_project(@project), @issue.tracker)).uniq | |
|
151 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current, true) | |
|
152 | 152 | |
|
153 | 153 | if request.get? || request.xhr? |
|
154 | 154 | @issue.start_date ||= Date.today |
|
155 | 155 | else |
|
156 | 156 | requested_status = IssueStatus.find_by_id(params[:issue][:status_id]) |
|
157 | 157 | # Check that the user is allowed to apply the requested status |
|
158 | 158 | @issue.status = (@allowed_statuses.include? requested_status) ? requested_status : default_status |
|
159 | 159 | call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue }) |
|
160 | 160 | if @issue.save |
|
161 | 161 | attachments = Attachment.attach_files(@issue, params[:attachments]) |
|
162 | 162 | render_attachment_warning_if_needed(@issue) |
|
163 | 163 | flash[:notice] = l(:notice_successful_create) |
|
164 | 164 | call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue}) |
|
165 | 165 | respond_to do |format| |
|
166 | 166 | format.html { |
|
167 | 167 | redirect_to(params[:continue] ? { :action => 'new', :tracker_id => @issue.tracker } : |
|
168 | 168 | { :action => 'show', :id => @issue }) |
|
169 | 169 | } |
|
170 | 170 | format.xml { render :action => 'show', :status => :created, :location => url_for(:controller => 'issues', :action => 'show', :id => @issue) } |
|
171 | 171 | end |
|
172 | 172 | return |
|
173 | 173 | else |
|
174 | 174 | respond_to do |format| |
|
175 | 175 | format.html { } |
|
176 | 176 | format.xml { render(:xml => @issue.errors, :status => :unprocessable_entity); return } |
|
177 | 177 | end |
|
178 | 178 | end |
|
179 | 179 | end |
|
180 | 180 | @priorities = IssuePriority.all |
|
181 | 181 | render :layout => !request.xhr? |
|
182 | 182 | end |
|
183 | 183 | |
|
184 | 184 | # Attributes that can be updated on workflow transition (without :edit permission) |
|
185 | 185 | # TODO: make it configurable (at least per role) |
|
186 | 186 | UPDATABLE_ATTRS_ON_TRANSITION = %w(status_id assigned_to_id fixed_version_id done_ratio) unless const_defined?(:UPDATABLE_ATTRS_ON_TRANSITION) |
|
187 | 187 | |
|
188 | 188 | def edit |
|
189 | 189 | update_issue_from_params |
|
190 | 190 | |
|
191 | 191 | respond_to do |format| |
|
192 | 192 | format.html { } |
|
193 | 193 | format.xml { } |
|
194 | 194 | end |
|
195 | 195 | end |
|
196 | 196 | |
|
197 | 197 | def update |
|
198 | 198 | update_issue_from_params |
|
199 | 199 | |
|
200 | 200 | if issue_update |
|
201 | 201 | respond_to do |format| |
|
202 | 202 | format.html { redirect_back_or_default({:action => 'show', :id => @issue}) } |
|
203 | 203 | format.xml { head :ok } |
|
204 | 204 | end |
|
205 | 205 | else |
|
206 | 206 | respond_to do |format| |
|
207 | 207 | format.html { render :action => 'edit' } |
|
208 | 208 | format.xml { render :xml => @issue.errors, :status => :unprocessable_entity } |
|
209 | 209 | end |
|
210 | 210 | end |
|
211 | 211 | |
|
212 | 212 | rescue ActiveRecord::StaleObjectError |
|
213 | 213 | # Optimistic locking exception |
|
214 | 214 | flash.now[:error] = l(:notice_locking_conflict) |
|
215 | 215 | # Remove the previously added attachments if issue was not updated |
|
216 | 216 | attachments[:files].each(&:destroy) if attachments[:files] |
|
217 | 217 | end |
|
218 | 218 | |
|
219 | 219 | def reply |
|
220 | 220 | journal = Journal.find(params[:journal_id]) if params[:journal_id] |
|
221 | 221 | if journal |
|
222 | 222 | user = journal.user |
|
223 | 223 | text = journal.notes |
|
224 | 224 | else |
|
225 | 225 | user = @issue.author |
|
226 | 226 | text = @issue.description |
|
227 | 227 | end |
|
228 | 228 | content = "#{ll(Setting.default_language, :text_user_wrote, user)}\\n> " |
|
229 | 229 | content << text.to_s.strip.gsub(%r{<pre>((.|\s)*?)</pre>}m, '[...]').gsub('"', '\"').gsub(/(\r?\n|\r\n?)/, "\\n> ") + "\\n\\n" |
|
230 | 230 | render(:update) { |page| |
|
231 | 231 | page.<< "$('notes').value = \"#{content}\";" |
|
232 | 232 | page.show 'update' |
|
233 | 233 | page << "Form.Element.focus('notes');" |
|
234 | 234 | page << "Element.scrollTo('update');" |
|
235 | 235 | page << "$('notes').scrollTop = $('notes').scrollHeight - $('notes').clientHeight;" |
|
236 | 236 | } |
|
237 | 237 | end |
|
238 | 238 | |
|
239 | 239 | # Bulk edit a set of issues |
|
240 | 240 | def bulk_edit |
|
241 | 241 | if request.post? |
|
242 | 242 | attributes = (params[:issue] || {}).reject {|k,v| v.blank?} |
|
243 | 243 | attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'} |
|
244 | 244 | attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values] |
|
245 | 245 | |
|
246 | 246 | unsaved_issue_ids = [] |
|
247 | 247 | @issues.each do |issue| |
|
248 | 248 | journal = issue.init_journal(User.current, params[:notes]) |
|
249 | 249 | issue.safe_attributes = attributes |
|
250 | 250 | call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue }) |
|
251 | 251 | unless issue.save |
|
252 | 252 | # Keep unsaved issue ids to display them in flash error |
|
253 | 253 | unsaved_issue_ids << issue.id |
|
254 | 254 | end |
|
255 | 255 | end |
|
256 | 256 | if unsaved_issue_ids.empty? |
|
257 | 257 | flash[:notice] = l(:notice_successful_update) unless @issues.empty? |
|
258 | 258 | else |
|
259 | 259 | flash[:error] = l(:notice_failed_to_save_issues, :count => unsaved_issue_ids.size, |
|
260 | 260 | :total => @issues.size, |
|
261 | 261 | :ids => '#' + unsaved_issue_ids.join(', #')) |
|
262 | 262 | end |
|
263 | 263 | redirect_back_or_default({:controller => 'issues', :action => 'index', :project_id => @project}) |
|
264 | 264 | return |
|
265 | 265 | end |
|
266 | 266 | @available_statuses = Workflow.available_statuses(@project) |
|
267 | 267 | @custom_fields = @project.all_issue_custom_fields |
|
268 | 268 | end |
|
269 | 269 | |
|
270 | 270 | def move |
|
271 | 271 | @copy = params[:copy_options] && params[:copy_options][:copy] |
|
272 | 272 | @allowed_projects = [] |
|
273 | 273 | # find projects to which the user is allowed to move the issue |
|
274 | 274 | if User.current.admin? |
|
275 | 275 | # admin is allowed to move issues to any active (visible) project |
|
276 | 276 | @allowed_projects = Project.find(:all, :conditions => Project.visible_by(User.current)) |
|
277 | 277 | else |
|
278 | 278 | User.current.memberships.each {|m| @allowed_projects << m.project if m.roles.detect {|r| r.allowed_to?(:move_issues)}} |
|
279 | 279 | end |
|
280 | 280 | @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:new_project_id]} if params[:new_project_id] |
|
281 | 281 | @target_project ||= @project |
|
282 | 282 | @trackers = @target_project.trackers |
|
283 | 283 | @available_statuses = Workflow.available_statuses(@project) |
|
284 | 284 | if request.post? |
|
285 | 285 | new_tracker = params[:new_tracker_id].blank? ? nil : @target_project.trackers.find_by_id(params[:new_tracker_id]) |
|
286 | 286 | unsaved_issue_ids = [] |
|
287 | 287 | moved_issues = [] |
|
288 | 288 | @issues.each do |issue| |
|
289 | 289 | changed_attributes = {} |
|
290 | 290 | [:assigned_to_id, :status_id, :start_date, :due_date].each do |valid_attribute| |
|
291 | 291 | unless params[valid_attribute].blank? |
|
292 | 292 | changed_attributes[valid_attribute] = (params[valid_attribute] == 'none' ? nil : params[valid_attribute]) |
|
293 | 293 | end |
|
294 | 294 | end |
|
295 | 295 | issue.init_journal(User.current) |
|
296 | 296 | call_hook(:controller_issues_move_before_save, { :params => params, :issue => issue, :target_project => @target_project, :copy => !!@copy }) |
|
297 | 297 | if r = issue.move_to(@target_project, new_tracker, {:copy => @copy, :attributes => changed_attributes}) |
|
298 | 298 | moved_issues << r |
|
299 | 299 | else |
|
300 | 300 | unsaved_issue_ids << issue.id |
|
301 | 301 | end |
|
302 | 302 | end |
|
303 | 303 | if unsaved_issue_ids.empty? |
|
304 | 304 | flash[:notice] = l(:notice_successful_update) unless @issues.empty? |
|
305 | 305 | else |
|
306 | 306 | flash[:error] = l(:notice_failed_to_save_issues, :count => unsaved_issue_ids.size, |
|
307 | 307 | :total => @issues.size, |
|
308 | 308 | :ids => '#' + unsaved_issue_ids.join(', #')) |
|
309 | 309 | end |
|
310 | 310 | if params[:follow] |
|
311 | 311 | if @issues.size == 1 && moved_issues.size == 1 |
|
312 | 312 | redirect_to :controller => 'issues', :action => 'show', :id => moved_issues.first |
|
313 | 313 | else |
|
314 | 314 | redirect_to :controller => 'issues', :action => 'index', :project_id => (@target_project || @project) |
|
315 | 315 | end |
|
316 | 316 | else |
|
317 | 317 | redirect_to :controller => 'issues', :action => 'index', :project_id => @project |
|
318 | 318 | end |
|
319 | 319 | return |
|
320 | 320 | end |
|
321 | 321 | render :layout => false if request.xhr? |
|
322 | 322 | end |
|
323 | 323 | |
|
324 | 324 | def destroy |
|
325 | 325 | @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f |
|
326 | 326 | if @hours > 0 |
|
327 | 327 | case params[:todo] |
|
328 | 328 | when 'destroy' |
|
329 | 329 | # nothing to do |
|
330 | 330 | when 'nullify' |
|
331 | 331 | TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues]) |
|
332 | 332 | when 'reassign' |
|
333 | 333 | reassign_to = @project.issues.find_by_id(params[:reassign_to_id]) |
|
334 | 334 | if reassign_to.nil? |
|
335 | 335 | flash.now[:error] = l(:error_issue_not_found_in_project) |
|
336 | 336 | return |
|
337 | 337 | else |
|
338 | 338 | TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues]) |
|
339 | 339 | end |
|
340 | 340 | else |
|
341 | 341 | unless params[:format] == 'xml' |
|
342 | 342 | # display the destroy form if it's a user request |
|
343 | 343 | return |
|
344 | 344 | end |
|
345 | 345 | end |
|
346 | 346 | end |
|
347 | 347 | @issues.each(&:destroy) |
|
348 | 348 | respond_to do |format| |
|
349 | 349 | format.html { redirect_to :action => 'index', :project_id => @project } |
|
350 | 350 | format.xml { head :ok } |
|
351 | 351 | end |
|
352 | 352 | end |
|
353 | 353 | |
|
354 | 354 | def gantt |
|
355 | 355 | @gantt = Redmine::Helpers::Gantt.new(params) |
|
356 | 356 | retrieve_query |
|
357 | 357 | @query.group_by = nil |
|
358 | 358 | if @query.valid? |
|
359 | 359 | events = [] |
|
360 | 360 | # Issues that have start and due dates |
|
361 | 361 | events += @query.issues(:include => [:tracker, :assigned_to, :priority], |
|
362 | 362 | :order => "start_date, due_date", |
|
363 | 363 | :conditions => ["(((start_date>=? and start_date<=?) or (due_date>=? and due_date<=?) or (start_date<? and due_date>?)) and start_date is not null and due_date is not null)", @gantt.date_from, @gantt.date_to, @gantt.date_from, @gantt.date_to, @gantt.date_from, @gantt.date_to] |
|
364 | 364 | ) |
|
365 | 365 | # Issues that don't have a due date but that are assigned to a version with a date |
|
366 | 366 | events += @query.issues(:include => [:tracker, :assigned_to, :priority, :fixed_version], |
|
367 | 367 | :order => "start_date, effective_date", |
|
368 | 368 | :conditions => ["(((start_date>=? and start_date<=?) or (effective_date>=? and effective_date<=?) or (start_date<? and effective_date>?)) and start_date is not null and due_date is null and effective_date is not null)", @gantt.date_from, @gantt.date_to, @gantt.date_from, @gantt.date_to, @gantt.date_from, @gantt.date_to] |
|
369 | 369 | ) |
|
370 | 370 | # Versions |
|
371 | 371 | events += @query.versions(:conditions => ["effective_date BETWEEN ? AND ?", @gantt.date_from, @gantt.date_to]) |
|
372 | 372 | |
|
373 | 373 | @gantt.events = events |
|
374 | 374 | end |
|
375 | 375 | |
|
376 | 376 | basename = (@project ? "#{@project.identifier}-" : '') + 'gantt' |
|
377 | 377 | |
|
378 | 378 | respond_to do |format| |
|
379 | 379 | format.html { render :template => "issues/gantt.rhtml", :layout => !request.xhr? } |
|
380 | 380 | format.png { send_data(@gantt.to_image, :disposition => 'inline', :type => 'image/png', :filename => "#{basename}.png") } if @gantt.respond_to?('to_image') |
|
381 | 381 | format.pdf { send_data(gantt_to_pdf(@gantt, @project), :type => 'application/pdf', :filename => "#{basename}.pdf") } |
|
382 | 382 | end |
|
383 | 383 | end |
|
384 | 384 | |
|
385 | 385 | def calendar |
|
386 | 386 | if params[:year] and params[:year].to_i > 1900 |
|
387 | 387 | @year = params[:year].to_i |
|
388 | 388 | if params[:month] and params[:month].to_i > 0 and params[:month].to_i < 13 |
|
389 | 389 | @month = params[:month].to_i |
|
390 | 390 | end |
|
391 | 391 | end |
|
392 | 392 | @year ||= Date.today.year |
|
393 | 393 | @month ||= Date.today.month |
|
394 | 394 | |
|
395 | 395 | @calendar = Redmine::Helpers::Calendar.new(Date.civil(@year, @month, 1), current_language, :month) |
|
396 | 396 | retrieve_query |
|
397 | 397 | @query.group_by = nil |
|
398 | 398 | if @query.valid? |
|
399 | 399 | events = [] |
|
400 | 400 | events += @query.issues(:include => [:tracker, :assigned_to, :priority], |
|
401 | 401 | :conditions => ["((start_date BETWEEN ? AND ?) OR (due_date BETWEEN ? AND ?))", @calendar.startdt, @calendar.enddt, @calendar.startdt, @calendar.enddt] |
|
402 | 402 | ) |
|
403 | 403 | events += @query.versions(:conditions => ["effective_date BETWEEN ? AND ?", @calendar.startdt, @calendar.enddt]) |
|
404 | 404 | |
|
405 | 405 | @calendar.events = events |
|
406 | 406 | end |
|
407 | 407 | |
|
408 | 408 | render :layout => false if request.xhr? |
|
409 | 409 | end |
|
410 | 410 | |
|
411 | 411 | def context_menu |
|
412 | 412 | @issues = Issue.find_all_by_id(params[:ids], :include => :project) |
|
413 | 413 | if (@issues.size == 1) |
|
414 | 414 | @issue = @issues.first |
|
415 | 415 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current) |
|
416 | 416 | end |
|
417 | 417 | projects = @issues.collect(&:project).compact.uniq |
|
418 | 418 | @project = projects.first if projects.size == 1 |
|
419 | 419 | |
|
420 | 420 | @can = {:edit => (@project && User.current.allowed_to?(:edit_issues, @project)), |
|
421 | 421 | :log_time => (@project && User.current.allowed_to?(:log_time, @project)), |
|
422 | 422 | :update => (@project && (User.current.allowed_to?(:edit_issues, @project) || (User.current.allowed_to?(:change_status, @project) && @allowed_statuses && !@allowed_statuses.empty?))), |
|
423 | 423 | :move => (@project && User.current.allowed_to?(:move_issues, @project)), |
|
424 | 424 | :copy => (@issue && @project.trackers.include?(@issue.tracker) && User.current.allowed_to?(:add_issues, @project)), |
|
425 | 425 | :delete => (@project && User.current.allowed_to?(:delete_issues, @project)) |
|
426 | 426 | } |
|
427 | 427 | if @project |
|
428 | 428 | @assignables = @project.assignable_users |
|
429 | 429 | @assignables << @issue.assigned_to if @issue && @issue.assigned_to && !@assignables.include?(@issue.assigned_to) |
|
430 | 430 | @trackers = @project.trackers |
|
431 | 431 | end |
|
432 | 432 | |
|
433 | 433 | @priorities = IssuePriority.all.reverse |
|
434 | 434 | @statuses = IssueStatus.find(:all, :order => 'position') |
|
435 | 435 | @back = params[:back_url] || request.env['HTTP_REFERER'] |
|
436 | 436 | |
|
437 | 437 | render :layout => false |
|
438 | 438 | end |
|
439 | 439 | |
|
440 | 440 | def update_form |
|
441 | 441 | if params[:id].blank? |
|
442 | 442 | @issue = Issue.new |
|
443 | 443 | @issue.project = @project |
|
444 | 444 | else |
|
445 | 445 | @issue = @project.issues.visible.find(params[:id]) |
|
446 | 446 | end |
|
447 | 447 | @issue.attributes = params[:issue] |
|
448 | 448 | @allowed_statuses = ([@issue.status] + @issue.status.find_new_statuses_allowed_to(User.current.roles_for_project(@project), @issue.tracker)).uniq |
|
449 | 449 | @priorities = IssuePriority.all |
|
450 | 450 | |
|
451 | 451 | render :partial => 'attributes' |
|
452 | 452 | end |
|
453 | 453 | |
|
454 | 454 | def preview |
|
455 | 455 | @issue = @project.issues.find_by_id(params[:id]) unless params[:id].blank? |
|
456 | 456 | @attachements = @issue.attachments if @issue |
|
457 | 457 | @text = params[:notes] || (params[:issue] ? params[:issue][:description] : nil) |
|
458 | 458 | render :partial => 'common/preview' |
|
459 | 459 | end |
|
460 | 460 | |
|
461 | 461 | private |
|
462 | 462 | def find_issue |
|
463 | 463 | @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category]) |
|
464 | 464 | @project = @issue.project |
|
465 | 465 | rescue ActiveRecord::RecordNotFound |
|
466 | 466 | render_404 |
|
467 | 467 | end |
|
468 | 468 | |
|
469 | 469 | # Filter for bulk operations |
|
470 | 470 | def find_issues |
|
471 | 471 | @issues = Issue.find_all_by_id(params[:id] || params[:ids]) |
|
472 | 472 | raise ActiveRecord::RecordNotFound if @issues.empty? |
|
473 | 473 | projects = @issues.collect(&:project).compact.uniq |
|
474 | 474 | if projects.size == 1 |
|
475 | 475 | @project = projects.first |
|
476 | 476 | else |
|
477 | 477 | # TODO: let users bulk edit/move/destroy issues from different projects |
|
478 | 478 | render_error 'Can not bulk edit/move/destroy issues from different projects' |
|
479 | 479 | return false |
|
480 | 480 | end |
|
481 | 481 | rescue ActiveRecord::RecordNotFound |
|
482 | 482 | render_404 |
|
483 | 483 | end |
|
484 | 484 | |
|
485 | 485 | def find_project |
|
486 | 486 | project_id = (params[:issue] && params[:issue][:project_id]) || params[:project_id] |
|
487 | 487 | @project = Project.find(project_id) |
|
488 | 488 | rescue ActiveRecord::RecordNotFound |
|
489 | 489 | render_404 |
|
490 | 490 | end |
|
491 | 491 | |
|
492 | 492 | def find_optional_project |
|
493 | 493 | @project = Project.find(params[:project_id]) unless params[:project_id].blank? |
|
494 | 494 | allowed = User.current.allowed_to?({:controller => params[:controller], :action => params[:action]}, @project, :global => true) |
|
495 | 495 | allowed ? true : deny_access |
|
496 | 496 | rescue ActiveRecord::RecordNotFound |
|
497 | 497 | render_404 |
|
498 | 498 | end |
|
499 | 499 | |
|
500 | 500 | # Retrieve query from session or build a new query |
|
501 | 501 | def retrieve_query |
|
502 | 502 | if !params[:query_id].blank? |
|
503 | 503 | cond = "project_id IS NULL" |
|
504 | 504 | cond << " OR project_id = #{@project.id}" if @project |
|
505 | 505 | @query = Query.find(params[:query_id], :conditions => cond) |
|
506 | 506 | @query.project = @project |
|
507 | 507 | session[:query] = {:id => @query.id, :project_id => @query.project_id} |
|
508 | 508 | sort_clear |
|
509 | 509 | else |
|
510 | 510 | if api_request? || params[:set_filter] || session[:query].nil? || session[:query][:project_id] != (@project ? @project.id : nil) |
|
511 | 511 | # Give it a name, required to be valid |
|
512 | 512 | @query = Query.new(:name => "_") |
|
513 | 513 | @query.project = @project |
|
514 | 514 | if params[:fields] and params[:fields].is_a? Array |
|
515 | 515 | params[:fields].each do |field| |
|
516 | 516 | @query.add_filter(field, params[:operators][field], params[:values][field]) |
|
517 | 517 | end |
|
518 | 518 | else |
|
519 | 519 | @query.available_filters.keys.each do |field| |
|
520 | 520 | @query.add_short_filter(field, params[field]) if params[field] |
|
521 | 521 | end |
|
522 | 522 | end |
|
523 | 523 | @query.group_by = params[:group_by] |
|
524 | 524 | @query.column_names = params[:query] && params[:query][:column_names] |
|
525 | 525 | session[:query] = {:project_id => @query.project_id, :filters => @query.filters, :group_by => @query.group_by, :column_names => @query.column_names} |
|
526 | 526 | else |
|
527 | 527 | @query = Query.find_by_id(session[:query][:id]) if session[:query][:id] |
|
528 | 528 | @query ||= Query.new(:name => "_", :project => @project, :filters => session[:query][:filters], :group_by => session[:query][:group_by], :column_names => session[:query][:column_names]) |
|
529 | 529 | @query.project = @project |
|
530 | 530 | end |
|
531 | 531 | end |
|
532 | 532 | end |
|
533 | 533 | |
|
534 | 534 | # Rescues an invalid query statement. Just in case... |
|
535 | 535 | def query_statement_invalid(exception) |
|
536 | 536 | logger.error "Query::StatementInvalid: #{exception.message}" if logger |
|
537 | 537 | session.delete(:query) |
|
538 | 538 | sort_clear |
|
539 | 539 | render_error "An error occurred while executing the query and has been logged. Please report this error to your Redmine administrator." |
|
540 | 540 | end |
|
541 | 541 | |
|
542 | 542 | # Used by #edit and #update to set some common instance variables |
|
543 | 543 | # from the params |
|
544 | 544 | # TODO: Refactor, not everything in here is needed by #edit |
|
545 | 545 | def update_issue_from_params |
|
546 | 546 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current) |
|
547 | 547 | @priorities = IssuePriority.all |
|
548 | 548 | @edit_allowed = User.current.allowed_to?(:edit_issues, @project) |
|
549 | 549 | @time_entry = TimeEntry.new |
|
550 | 550 | |
|
551 | 551 | @notes = params[:notes] |
|
552 | 552 | @journal = @issue.init_journal(User.current, @notes) |
|
553 | 553 | # User can change issue attributes only if he has :edit permission or if a workflow transition is allowed |
|
554 | 554 | if (@edit_allowed || !@allowed_statuses.empty?) && params[:issue] |
|
555 | 555 | attrs = params[:issue].dup |
|
556 | 556 | attrs.delete_if {|k,v| !UPDATABLE_ATTRS_ON_TRANSITION.include?(k) } unless @edit_allowed |
|
557 | 557 | attrs.delete(:status_id) unless @allowed_statuses.detect {|s| s.id.to_s == attrs[:status_id].to_s} |
|
558 | 558 | @issue.safe_attributes = attrs |
|
559 | 559 | end |
|
560 | 560 | |
|
561 | 561 | end |
|
562 | 562 | |
|
563 | 563 | # TODO: Temporary utility method for #update. Should be split off |
|
564 | 564 | # and moved to the Issue model (accepts_nested_attributes_for maybe?) |
|
565 | 565 | def issue_update |
|
566 | 566 | if params[:time_entry] && params[:time_entry][:hours].present? && User.current.allowed_to?(:log_time, @project) |
|
567 | 567 | @time_entry = TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => Date.today) |
|
568 | 568 | @time_entry.attributes = params[:time_entry] |
|
569 | 569 | @issue.time_entries << @time_entry |
|
570 | 570 | end |
|
571 | 571 | |
|
572 | 572 | if @issue.valid? |
|
573 | 573 | attachments = Attachment.attach_files(@issue, params[:attachments]) |
|
574 | 574 | render_attachment_warning_if_needed(@issue) |
|
575 | 575 | |
|
576 | 576 | attachments[:files].each {|a| @journal.details << JournalDetail.new(:property => 'attachment', :prop_key => a.id, :value => a.filename)} |
|
577 | 577 | call_hook(:controller_issues_edit_before_save, { :params => params, :issue => @issue, :time_entry => @time_entry, :journal => @journal}) |
|
578 | 578 | if @issue.save |
|
579 | 579 | if !@journal.new_record? |
|
580 | 580 | # Only send notification if something was actually changed |
|
581 | 581 | flash[:notice] = l(:notice_successful_update) |
|
582 | 582 | end |
|
583 | 583 | call_hook(:controller_issues_edit_after_save, { :params => params, :issue => @issue, :time_entry => @time_entry, :journal => @journal}) |
|
584 | 584 | return true |
|
585 | 585 | end |
|
586 | 586 | end |
|
587 | 587 | # failure, returns false |
|
588 | 588 | |
|
589 | 589 | end |
|
590 | 590 | end |
@@ -1,577 +1,578 | |||
|
1 | 1 | # redMine - project management software |
|
2 | 2 | # Copyright (C) 2006-2007 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 Issue < ActiveRecord::Base |
|
19 | 19 | belongs_to :project |
|
20 | 20 | belongs_to :tracker |
|
21 | 21 | belongs_to :status, :class_name => 'IssueStatus', :foreign_key => 'status_id' |
|
22 | 22 | belongs_to :author, :class_name => 'User', :foreign_key => 'author_id' |
|
23 | 23 | belongs_to :assigned_to, :class_name => 'User', :foreign_key => 'assigned_to_id' |
|
24 | 24 | belongs_to :fixed_version, :class_name => 'Version', :foreign_key => 'fixed_version_id' |
|
25 | 25 | belongs_to :priority, :class_name => 'IssuePriority', :foreign_key => 'priority_id' |
|
26 | 26 | belongs_to :category, :class_name => 'IssueCategory', :foreign_key => 'category_id' |
|
27 | 27 | |
|
28 | 28 | has_many :journals, :as => :journalized, :dependent => :destroy |
|
29 | 29 | has_many :time_entries, :dependent => :delete_all |
|
30 | 30 | has_and_belongs_to_many :changesets, :order => "#{Changeset.table_name}.committed_on ASC, #{Changeset.table_name}.id ASC" |
|
31 | 31 | |
|
32 | 32 | has_many :relations_from, :class_name => 'IssueRelation', :foreign_key => 'issue_from_id', :dependent => :delete_all |
|
33 | 33 | has_many :relations_to, :class_name => 'IssueRelation', :foreign_key => 'issue_to_id', :dependent => :delete_all |
|
34 | 34 | |
|
35 | 35 | acts_as_attachable :after_remove => :attachment_removed |
|
36 | 36 | acts_as_customizable |
|
37 | 37 | acts_as_watchable |
|
38 | 38 | acts_as_searchable :columns => ['subject', "#{table_name}.description", "#{Journal.table_name}.notes"], |
|
39 | 39 | :include => [:project, :journals], |
|
40 | 40 | # sort by id so that limited eager loading doesn't break with postgresql |
|
41 | 41 | :order_column => "#{table_name}.id" |
|
42 | 42 | acts_as_event :title => Proc.new {|o| "#{o.tracker.name} ##{o.id} (#{o.status}): #{o.subject}"}, |
|
43 | 43 | :url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.id}}, |
|
44 | 44 | :type => Proc.new {|o| 'issue' + (o.closed? ? ' closed' : '') } |
|
45 | 45 | |
|
46 | 46 | acts_as_activity_provider :find_options => {:include => [:project, :author, :tracker]}, |
|
47 | 47 | :author_key => :author_id |
|
48 | 48 | |
|
49 | 49 | DONE_RATIO_OPTIONS = %w(issue_field issue_status) |
|
50 | 50 | |
|
51 | 51 | validates_presence_of :subject, :priority, :project, :tracker, :author, :status |
|
52 | 52 | validates_length_of :subject, :maximum => 255 |
|
53 | 53 | validates_inclusion_of :done_ratio, :in => 0..100 |
|
54 | 54 | validates_numericality_of :estimated_hours, :allow_nil => true |
|
55 | 55 | |
|
56 | 56 | named_scope :visible, lambda {|*args| { :include => :project, |
|
57 | 57 | :conditions => Project.allowed_to_condition(args.first || User.current, :view_issues) } } |
|
58 | 58 | |
|
59 | 59 | named_scope :open, :conditions => ["#{IssueStatus.table_name}.is_closed = ?", false], :include => :status |
|
60 | 60 | |
|
61 | 61 | before_create :default_assign |
|
62 | 62 | before_save :reschedule_following_issues, :close_duplicates, :update_done_ratio_from_issue_status |
|
63 | 63 | after_save :create_journal |
|
64 | 64 | |
|
65 | 65 | # Returns true if usr or current user is allowed to view the issue |
|
66 | 66 | def visible?(usr=nil) |
|
67 | 67 | (usr || User.current).allowed_to?(:view_issues, self.project) |
|
68 | 68 | end |
|
69 | 69 | |
|
70 | 70 | def after_initialize |
|
71 | 71 | if new_record? |
|
72 | 72 | # set default values for new records only |
|
73 | 73 | self.status ||= IssueStatus.default |
|
74 | 74 | self.priority ||= IssuePriority.default |
|
75 | 75 | end |
|
76 | 76 | end |
|
77 | 77 | |
|
78 | 78 | # Overrides Redmine::Acts::Customizable::InstanceMethods#available_custom_fields |
|
79 | 79 | def available_custom_fields |
|
80 | 80 | (project && tracker) ? project.all_issue_custom_fields.select {|c| tracker.custom_fields.include? c } : [] |
|
81 | 81 | end |
|
82 | 82 | |
|
83 | 83 | def copy_from(arg) |
|
84 | 84 | issue = arg.is_a?(Issue) ? arg : Issue.find(arg) |
|
85 | 85 | self.attributes = issue.attributes.dup.except("id", "created_on", "updated_on") |
|
86 | 86 | self.custom_values = issue.custom_values.collect {|v| v.clone} |
|
87 | 87 | self.status = issue.status |
|
88 | 88 | self |
|
89 | 89 | end |
|
90 | 90 | |
|
91 | 91 | # Moves/copies an issue to a new project and tracker |
|
92 | 92 | # Returns the moved/copied issue on success, false on failure |
|
93 | 93 | def move_to(new_project, new_tracker = nil, options = {}) |
|
94 | 94 | options ||= {} |
|
95 | 95 | issue = options[:copy] ? self.clone : self |
|
96 | 96 | transaction do |
|
97 | 97 | if new_project && issue.project_id != new_project.id |
|
98 | 98 | # delete issue relations |
|
99 | 99 | unless Setting.cross_project_issue_relations? |
|
100 | 100 | issue.relations_from.clear |
|
101 | 101 | issue.relations_to.clear |
|
102 | 102 | end |
|
103 | 103 | # issue is moved to another project |
|
104 | 104 | # reassign to the category with same name if any |
|
105 | 105 | new_category = issue.category.nil? ? nil : new_project.issue_categories.find_by_name(issue.category.name) |
|
106 | 106 | issue.category = new_category |
|
107 | 107 | # Keep the fixed_version if it's still valid in the new_project |
|
108 | 108 | unless new_project.shared_versions.include?(issue.fixed_version) |
|
109 | 109 | issue.fixed_version = nil |
|
110 | 110 | end |
|
111 | 111 | issue.project = new_project |
|
112 | 112 | end |
|
113 | 113 | if new_tracker |
|
114 | 114 | issue.tracker = new_tracker |
|
115 | 115 | end |
|
116 | 116 | if options[:copy] |
|
117 | 117 | issue.custom_field_values = self.custom_field_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h} |
|
118 | 118 | issue.status = if options[:attributes] && options[:attributes][:status_id] |
|
119 | 119 | IssueStatus.find_by_id(options[:attributes][:status_id]) |
|
120 | 120 | else |
|
121 | 121 | self.status |
|
122 | 122 | end |
|
123 | 123 | end |
|
124 | 124 | # Allow bulk setting of attributes on the issue |
|
125 | 125 | if options[:attributes] |
|
126 | 126 | issue.attributes = options[:attributes] |
|
127 | 127 | end |
|
128 | 128 | if issue.save |
|
129 | 129 | unless options[:copy] |
|
130 | 130 | # Manually update project_id on related time entries |
|
131 | 131 | TimeEntry.update_all("project_id = #{new_project.id}", {:issue_id => id}) |
|
132 | 132 | end |
|
133 | 133 | else |
|
134 | 134 | Issue.connection.rollback_db_transaction |
|
135 | 135 | return false |
|
136 | 136 | end |
|
137 | 137 | end |
|
138 | 138 | return issue |
|
139 | 139 | end |
|
140 | 140 | |
|
141 | 141 | def priority_id=(pid) |
|
142 | 142 | self.priority = nil |
|
143 | 143 | write_attribute(:priority_id, pid) |
|
144 | 144 | end |
|
145 | 145 | |
|
146 | 146 | def tracker_id=(tid) |
|
147 | 147 | self.tracker = nil |
|
148 | 148 | result = write_attribute(:tracker_id, tid) |
|
149 | 149 | @custom_field_values = nil |
|
150 | 150 | result |
|
151 | 151 | end |
|
152 | 152 | |
|
153 | 153 | # Overrides attributes= so that tracker_id gets assigned first |
|
154 | 154 | def attributes_with_tracker_first=(new_attributes, *args) |
|
155 | 155 | return if new_attributes.nil? |
|
156 | 156 | new_tracker_id = new_attributes['tracker_id'] || new_attributes[:tracker_id] |
|
157 | 157 | if new_tracker_id |
|
158 | 158 | self.tracker_id = new_tracker_id |
|
159 | 159 | end |
|
160 | 160 | send :attributes_without_tracker_first=, new_attributes, *args |
|
161 | 161 | end |
|
162 | 162 | # Do not redefine alias chain on reload (see #4838) |
|
163 | 163 | alias_method_chain(:attributes=, :tracker_first) unless method_defined?(:attributes_without_tracker_first=) |
|
164 | 164 | |
|
165 | 165 | def estimated_hours=(h) |
|
166 | 166 | write_attribute :estimated_hours, (h.is_a?(String) ? h.to_hours : h) |
|
167 | 167 | end |
|
168 | 168 | |
|
169 | 169 | SAFE_ATTRIBUTES = %w( |
|
170 | 170 | tracker_id |
|
171 | 171 | status_id |
|
172 | 172 | category_id |
|
173 | 173 | assigned_to_id |
|
174 | 174 | priority_id |
|
175 | 175 | fixed_version_id |
|
176 | 176 | subject |
|
177 | 177 | description |
|
178 | 178 | start_date |
|
179 | 179 | due_date |
|
180 | 180 | done_ratio |
|
181 | 181 | estimated_hours |
|
182 | 182 | custom_field_values |
|
183 | 183 | ) unless const_defined?(:SAFE_ATTRIBUTES) |
|
184 | 184 | |
|
185 | 185 | # Safely sets attributes |
|
186 | 186 | # Should be called from controllers instead of #attributes= |
|
187 | 187 | # attr_accessible is too rough because we still want things like |
|
188 | 188 | # Issue.new(:project => foo) to work |
|
189 | 189 | # TODO: move workflow/permission checks from controllers to here |
|
190 | 190 | def safe_attributes=(attrs, user=User.current) |
|
191 | 191 | return if attrs.nil? |
|
192 | 192 | attrs = attrs.reject {|k,v| !SAFE_ATTRIBUTES.include?(k)} |
|
193 | 193 | if attrs['status_id'] |
|
194 | 194 | unless new_statuses_allowed_to(user).collect(&:id).include?(attrs['status_id'].to_i) |
|
195 | 195 | attrs.delete('status_id') |
|
196 | 196 | end |
|
197 | 197 | end |
|
198 | 198 | self.attributes = attrs |
|
199 | 199 | end |
|
200 | 200 | |
|
201 | 201 | def done_ratio |
|
202 | 202 | if Issue.use_status_for_done_ratio? && status && status.default_done_ratio? |
|
203 | 203 | status.default_done_ratio |
|
204 | 204 | else |
|
205 | 205 | read_attribute(:done_ratio) |
|
206 | 206 | end |
|
207 | 207 | end |
|
208 | 208 | |
|
209 | 209 | def self.use_status_for_done_ratio? |
|
210 | 210 | Setting.issue_done_ratio == 'issue_status' |
|
211 | 211 | end |
|
212 | 212 | |
|
213 | 213 | def self.use_field_for_done_ratio? |
|
214 | 214 | Setting.issue_done_ratio == 'issue_field' |
|
215 | 215 | end |
|
216 | 216 | |
|
217 | 217 | def validate |
|
218 | 218 | if self.due_date.nil? && @attributes['due_date'] && !@attributes['due_date'].empty? |
|
219 | 219 | errors.add :due_date, :not_a_date |
|
220 | 220 | end |
|
221 | 221 | |
|
222 | 222 | if self.due_date and self.start_date and self.due_date < self.start_date |
|
223 | 223 | errors.add :due_date, :greater_than_start_date |
|
224 | 224 | end |
|
225 | 225 | |
|
226 | 226 | if start_date && soonest_start && start_date < soonest_start |
|
227 | 227 | errors.add :start_date, :invalid |
|
228 | 228 | end |
|
229 | 229 | |
|
230 | 230 | if fixed_version |
|
231 | 231 | if !assignable_versions.include?(fixed_version) |
|
232 | 232 | errors.add :fixed_version_id, :inclusion |
|
233 | 233 | elsif reopened? && fixed_version.closed? |
|
234 | 234 | errors.add_to_base I18n.t(:error_can_not_reopen_issue_on_closed_version) |
|
235 | 235 | end |
|
236 | 236 | end |
|
237 | 237 | |
|
238 | 238 | # Checks that the issue can not be added/moved to a disabled tracker |
|
239 | 239 | if project && (tracker_id_changed? || project_id_changed?) |
|
240 | 240 | unless project.trackers.include?(tracker) |
|
241 | 241 | errors.add :tracker_id, :inclusion |
|
242 | 242 | end |
|
243 | 243 | end |
|
244 | 244 | end |
|
245 | 245 | |
|
246 | 246 | # Set the done_ratio using the status if that setting is set. This will keep the done_ratios |
|
247 | 247 | # even if the user turns off the setting later |
|
248 | 248 | def update_done_ratio_from_issue_status |
|
249 | 249 | if Issue.use_status_for_done_ratio? && status && status.default_done_ratio? |
|
250 | 250 | self.done_ratio = status.default_done_ratio |
|
251 | 251 | end |
|
252 | 252 | end |
|
253 | 253 | |
|
254 | 254 | def init_journal(user, notes = "") |
|
255 | 255 | @current_journal ||= Journal.new(:journalized => self, :user => user, :notes => notes) |
|
256 | 256 | @issue_before_change = self.clone |
|
257 | 257 | @issue_before_change.status = self.status |
|
258 | 258 | @custom_values_before_change = {} |
|
259 | 259 | self.custom_values.each {|c| @custom_values_before_change.store c.custom_field_id, c.value } |
|
260 | 260 | # Make sure updated_on is updated when adding a note. |
|
261 | 261 | updated_on_will_change! |
|
262 | 262 | @current_journal |
|
263 | 263 | end |
|
264 | 264 | |
|
265 | 265 | # Return true if the issue is closed, otherwise false |
|
266 | 266 | def closed? |
|
267 | 267 | self.status.is_closed? |
|
268 | 268 | end |
|
269 | 269 | |
|
270 | 270 | # Return true if the issue is being reopened |
|
271 | 271 | def reopened? |
|
272 | 272 | if !new_record? && status_id_changed? |
|
273 | 273 | status_was = IssueStatus.find_by_id(status_id_was) |
|
274 | 274 | status_new = IssueStatus.find_by_id(status_id) |
|
275 | 275 | if status_was && status_new && status_was.is_closed? && !status_new.is_closed? |
|
276 | 276 | return true |
|
277 | 277 | end |
|
278 | 278 | end |
|
279 | 279 | false |
|
280 | 280 | end |
|
281 | 281 | |
|
282 | 282 | # Return true if the issue is being closed |
|
283 | 283 | def closing? |
|
284 | 284 | if !new_record? && status_id_changed? |
|
285 | 285 | status_was = IssueStatus.find_by_id(status_id_was) |
|
286 | 286 | status_new = IssueStatus.find_by_id(status_id) |
|
287 | 287 | if status_was && status_new && !status_was.is_closed? && status_new.is_closed? |
|
288 | 288 | return true |
|
289 | 289 | end |
|
290 | 290 | end |
|
291 | 291 | false |
|
292 | 292 | end |
|
293 | 293 | |
|
294 | 294 | # Returns true if the issue is overdue |
|
295 | 295 | def overdue? |
|
296 | 296 | !due_date.nil? && (due_date < Date.today) && !status.is_closed? |
|
297 | 297 | end |
|
298 | 298 | |
|
299 | 299 | # Users the issue can be assigned to |
|
300 | 300 | def assignable_users |
|
301 | 301 | project.assignable_users |
|
302 | 302 | end |
|
303 | 303 | |
|
304 | 304 | # Versions that the issue can be assigned to |
|
305 | 305 | def assignable_versions |
|
306 | 306 | @assignable_versions ||= (project.shared_versions.open + [Version.find_by_id(fixed_version_id_was)]).compact.uniq.sort |
|
307 | 307 | end |
|
308 | 308 | |
|
309 | 309 | # Returns true if this issue is blocked by another issue that is still open |
|
310 | 310 | def blocked? |
|
311 | 311 | !relations_to.detect {|ir| ir.relation_type == 'blocks' && !ir.issue_from.closed?}.nil? |
|
312 | 312 | end |
|
313 | 313 | |
|
314 | 314 | # Returns an array of status that user is able to apply |
|
315 | def new_statuses_allowed_to(user) | |
|
315 | def new_statuses_allowed_to(user, include_default=false) | |
|
316 | 316 | statuses = status.find_new_statuses_allowed_to(user.roles_for_project(project), tracker) |
|
317 | 317 | statuses << status unless statuses.empty? |
|
318 | statuses << IssueStatus.default if include_default | |
|
318 | 319 | statuses = statuses.uniq.sort |
|
319 | 320 | blocked? ? statuses.reject {|s| s.is_closed?} : statuses |
|
320 | 321 | end |
|
321 | 322 | |
|
322 | 323 | # Returns the mail adresses of users that should be notified |
|
323 | 324 | def recipients |
|
324 | 325 | notified = project.notified_users |
|
325 | 326 | # Author and assignee are always notified unless they have been locked |
|
326 | 327 | notified << author if author && author.active? |
|
327 | 328 | notified << assigned_to if assigned_to && assigned_to.active? |
|
328 | 329 | notified.uniq! |
|
329 | 330 | # Remove users that can not view the issue |
|
330 | 331 | notified.reject! {|user| !visible?(user)} |
|
331 | 332 | notified.collect(&:mail) |
|
332 | 333 | end |
|
333 | 334 | |
|
334 | 335 | # Returns the total number of hours spent on this issue. |
|
335 | 336 | # |
|
336 | 337 | # Example: |
|
337 | 338 | # spent_hours => 0 |
|
338 | 339 | # spent_hours => 50 |
|
339 | 340 | def spent_hours |
|
340 | 341 | @spent_hours ||= time_entries.sum(:hours) || 0 |
|
341 | 342 | end |
|
342 | 343 | |
|
343 | 344 | def relations |
|
344 | 345 | (relations_from + relations_to).sort |
|
345 | 346 | end |
|
346 | 347 | |
|
347 | 348 | def all_dependent_issues |
|
348 | 349 | dependencies = [] |
|
349 | 350 | relations_from.each do |relation| |
|
350 | 351 | dependencies << relation.issue_to |
|
351 | 352 | dependencies += relation.issue_to.all_dependent_issues |
|
352 | 353 | end |
|
353 | 354 | dependencies |
|
354 | 355 | end |
|
355 | 356 | |
|
356 | 357 | # Returns an array of issues that duplicate this one |
|
357 | 358 | def duplicates |
|
358 | 359 | relations_to.select {|r| r.relation_type == IssueRelation::TYPE_DUPLICATES}.collect {|r| r.issue_from} |
|
359 | 360 | end |
|
360 | 361 | |
|
361 | 362 | # Returns the due date or the target due date if any |
|
362 | 363 | # Used on gantt chart |
|
363 | 364 | def due_before |
|
364 | 365 | due_date || (fixed_version ? fixed_version.effective_date : nil) |
|
365 | 366 | end |
|
366 | 367 | |
|
367 | 368 | # Returns the time scheduled for this issue. |
|
368 | 369 | # |
|
369 | 370 | # Example: |
|
370 | 371 | # Start Date: 2/26/09, End Date: 3/04/09 |
|
371 | 372 | # duration => 6 |
|
372 | 373 | def duration |
|
373 | 374 | (start_date && due_date) ? due_date - start_date : 0 |
|
374 | 375 | end |
|
375 | 376 | |
|
376 | 377 | def soonest_start |
|
377 | 378 | @soonest_start ||= relations_to.collect{|relation| relation.successor_soonest_start}.compact.min |
|
378 | 379 | end |
|
379 | 380 | |
|
380 | 381 | def to_s |
|
381 | 382 | "#{tracker} ##{id}: #{subject}" |
|
382 | 383 | end |
|
383 | 384 | |
|
384 | 385 | # Returns a string of css classes that apply to the issue |
|
385 | 386 | def css_classes |
|
386 | 387 | s = "issue status-#{status.position} priority-#{priority.position}" |
|
387 | 388 | s << ' closed' if closed? |
|
388 | 389 | s << ' overdue' if overdue? |
|
389 | 390 | s << ' created-by-me' if User.current.logged? && author_id == User.current.id |
|
390 | 391 | s << ' assigned-to-me' if User.current.logged? && assigned_to_id == User.current.id |
|
391 | 392 | s |
|
392 | 393 | end |
|
393 | 394 | |
|
394 | 395 | # Unassigns issues from +version+ if it's no longer shared with issue's project |
|
395 | 396 | def self.update_versions_from_sharing_change(version) |
|
396 | 397 | # Update issues assigned to the version |
|
397 | 398 | update_versions(["#{Issue.table_name}.fixed_version_id = ?", version.id]) |
|
398 | 399 | end |
|
399 | 400 | |
|
400 | 401 | # Unassigns issues from versions that are no longer shared |
|
401 | 402 | # after +project+ was moved |
|
402 | 403 | def self.update_versions_from_hierarchy_change(project) |
|
403 | 404 | moved_project_ids = project.self_and_descendants.reload.collect(&:id) |
|
404 | 405 | # Update issues of the moved projects and issues assigned to a version of a moved project |
|
405 | 406 | Issue.update_versions(["#{Version.table_name}.project_id IN (?) OR #{Issue.table_name}.project_id IN (?)", moved_project_ids, moved_project_ids]) |
|
406 | 407 | end |
|
407 | 408 | |
|
408 | 409 | # Extracted from the ReportsController. |
|
409 | 410 | def self.by_tracker(project) |
|
410 | 411 | count_and_group_by(:project => project, |
|
411 | 412 | :field => 'tracker_id', |
|
412 | 413 | :joins => Tracker.table_name) |
|
413 | 414 | end |
|
414 | 415 | |
|
415 | 416 | def self.by_version(project) |
|
416 | 417 | count_and_group_by(:project => project, |
|
417 | 418 | :field => 'fixed_version_id', |
|
418 | 419 | :joins => Version.table_name) |
|
419 | 420 | end |
|
420 | 421 | |
|
421 | 422 | def self.by_priority(project) |
|
422 | 423 | count_and_group_by(:project => project, |
|
423 | 424 | :field => 'priority_id', |
|
424 | 425 | :joins => IssuePriority.table_name) |
|
425 | 426 | end |
|
426 | 427 | |
|
427 | 428 | def self.by_category(project) |
|
428 | 429 | count_and_group_by(:project => project, |
|
429 | 430 | :field => 'category_id', |
|
430 | 431 | :joins => IssueCategory.table_name) |
|
431 | 432 | end |
|
432 | 433 | |
|
433 | 434 | def self.by_assigned_to(project) |
|
434 | 435 | count_and_group_by(:project => project, |
|
435 | 436 | :field => 'assigned_to_id', |
|
436 | 437 | :joins => User.table_name) |
|
437 | 438 | end |
|
438 | 439 | |
|
439 | 440 | def self.by_author(project) |
|
440 | 441 | count_and_group_by(:project => project, |
|
441 | 442 | :field => 'author_id', |
|
442 | 443 | :joins => User.table_name) |
|
443 | 444 | end |
|
444 | 445 | |
|
445 | 446 | def self.by_subproject(project) |
|
446 | 447 | ActiveRecord::Base.connection.select_all("select s.id as status_id, |
|
447 | 448 | s.is_closed as closed, |
|
448 | 449 | i.project_id as project_id, |
|
449 | 450 | count(i.id) as total |
|
450 | 451 | from |
|
451 | 452 | #{Issue.table_name} i, #{IssueStatus.table_name} s |
|
452 | 453 | where |
|
453 | 454 | i.status_id=s.id |
|
454 | 455 | and i.project_id IN (#{project.descendants.active.collect{|p| p.id}.join(',')}) |
|
455 | 456 | group by s.id, s.is_closed, i.project_id") if project.descendants.active.any? |
|
456 | 457 | end |
|
457 | 458 | # End ReportsController extraction |
|
458 | 459 | |
|
459 | 460 | private |
|
460 | 461 | |
|
461 | 462 | # Update issues so their versions are not pointing to a |
|
462 | 463 | # fixed_version that is not shared with the issue's project |
|
463 | 464 | def self.update_versions(conditions=nil) |
|
464 | 465 | # Only need to update issues with a fixed_version from |
|
465 | 466 | # a different project and that is not systemwide shared |
|
466 | 467 | Issue.all(:conditions => merge_conditions("#{Issue.table_name}.fixed_version_id IS NOT NULL" + |
|
467 | 468 | " AND #{Issue.table_name}.project_id <> #{Version.table_name}.project_id" + |
|
468 | 469 | " AND #{Version.table_name}.sharing <> 'system'", |
|
469 | 470 | conditions), |
|
470 | 471 | :include => [:project, :fixed_version] |
|
471 | 472 | ).each do |issue| |
|
472 | 473 | next if issue.project.nil? || issue.fixed_version.nil? |
|
473 | 474 | unless issue.project.shared_versions.include?(issue.fixed_version) |
|
474 | 475 | issue.init_journal(User.current) |
|
475 | 476 | issue.fixed_version = nil |
|
476 | 477 | issue.save |
|
477 | 478 | end |
|
478 | 479 | end |
|
479 | 480 | end |
|
480 | 481 | |
|
481 | 482 | # Callback on attachment deletion |
|
482 | 483 | def attachment_removed(obj) |
|
483 | 484 | journal = init_journal(User.current) |
|
484 | 485 | journal.details << JournalDetail.new(:property => 'attachment', |
|
485 | 486 | :prop_key => obj.id, |
|
486 | 487 | :old_value => obj.filename) |
|
487 | 488 | journal.save |
|
488 | 489 | end |
|
489 | 490 | |
|
490 | 491 | # Default assignment based on category |
|
491 | 492 | def default_assign |
|
492 | 493 | if assigned_to.nil? && category && category.assigned_to |
|
493 | 494 | self.assigned_to = category.assigned_to |
|
494 | 495 | end |
|
495 | 496 | end |
|
496 | 497 | |
|
497 | 498 | # Updates start/due dates of following issues |
|
498 | 499 | def reschedule_following_issues |
|
499 | 500 | if start_date_changed? || due_date_changed? |
|
500 | 501 | relations_from.each do |relation| |
|
501 | 502 | relation.set_issue_to_dates |
|
502 | 503 | end |
|
503 | 504 | end |
|
504 | 505 | end |
|
505 | 506 | |
|
506 | 507 | # Closes duplicates if the issue is being closed |
|
507 | 508 | def close_duplicates |
|
508 | 509 | if closing? |
|
509 | 510 | duplicates.each do |duplicate| |
|
510 | 511 | # Reload is need in case the duplicate was updated by a previous duplicate |
|
511 | 512 | duplicate.reload |
|
512 | 513 | # Don't re-close it if it's already closed |
|
513 | 514 | next if duplicate.closed? |
|
514 | 515 | # Same user and notes |
|
515 | 516 | if @current_journal |
|
516 | 517 | duplicate.init_journal(@current_journal.user, @current_journal.notes) |
|
517 | 518 | end |
|
518 | 519 | duplicate.update_attribute :status, self.status |
|
519 | 520 | end |
|
520 | 521 | end |
|
521 | 522 | end |
|
522 | 523 | |
|
523 | 524 | # Saves the changes in a Journal |
|
524 | 525 | # Called after_save |
|
525 | 526 | def create_journal |
|
526 | 527 | if @current_journal |
|
527 | 528 | # attributes changes |
|
528 | 529 | (Issue.column_names - %w(id description lock_version created_on updated_on)).each {|c| |
|
529 | 530 | @current_journal.details << JournalDetail.new(:property => 'attr', |
|
530 | 531 | :prop_key => c, |
|
531 | 532 | :old_value => @issue_before_change.send(c), |
|
532 | 533 | :value => send(c)) unless send(c)==@issue_before_change.send(c) |
|
533 | 534 | } |
|
534 | 535 | # custom fields changes |
|
535 | 536 | custom_values.each {|c| |
|
536 | 537 | next if (@custom_values_before_change[c.custom_field_id]==c.value || |
|
537 | 538 | (@custom_values_before_change[c.custom_field_id].blank? && c.value.blank?)) |
|
538 | 539 | @current_journal.details << JournalDetail.new(:property => 'cf', |
|
539 | 540 | :prop_key => c.custom_field_id, |
|
540 | 541 | :old_value => @custom_values_before_change[c.custom_field_id], |
|
541 | 542 | :value => c.value) |
|
542 | 543 | } |
|
543 | 544 | @current_journal.save |
|
544 | 545 | # reset current journal |
|
545 | 546 | init_journal @current_journal.user, @current_journal.notes |
|
546 | 547 | end |
|
547 | 548 | end |
|
548 | 549 | |
|
549 | 550 | # Query generator for selecting groups of issue counts for a project |
|
550 | 551 | # based on specific criteria |
|
551 | 552 | # |
|
552 | 553 | # Options |
|
553 | 554 | # * project - Project to search in. |
|
554 | 555 | # * field - String. Issue field to key off of in the grouping. |
|
555 | 556 | # * joins - String. The table name to join against. |
|
556 | 557 | def self.count_and_group_by(options) |
|
557 | 558 | project = options.delete(:project) |
|
558 | 559 | select_field = options.delete(:field) |
|
559 | 560 | joins = options.delete(:joins) |
|
560 | 561 | |
|
561 | 562 | where = "i.#{select_field}=j.id" |
|
562 | 563 | |
|
563 | 564 | ActiveRecord::Base.connection.select_all("select s.id as status_id, |
|
564 | 565 | s.is_closed as closed, |
|
565 | 566 | j.id as #{select_field}, |
|
566 | 567 | count(i.id) as total |
|
567 | 568 | from |
|
568 | 569 | #{Issue.table_name} i, #{IssueStatus.table_name} s, #{joins} as j |
|
569 | 570 | where |
|
570 | 571 | i.status_id=s.id |
|
571 | 572 | and #{where} |
|
572 | 573 | and i.project_id=#{project.id} |
|
573 | 574 | group by s.id, s.is_closed, j.id") |
|
574 | 575 | end |
|
575 | 576 | |
|
576 | 577 | |
|
577 | 578 | end |
General Comments 0
You need to be logged in to leave comments.
Login now