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