##// END OF EJS Templates
Always instanciate a relation....
Jean-Philippe Lang -
r11380:83d776199826
parent child
Show More
@@ -1,439 +1,441
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class IssuesController < ApplicationController
19 19 menu_item :new_issue, :only => [:new, :create]
20 20 default_search_scope :issues
21 21
22 22 before_filter :find_issue, :only => [:show, :edit, :update]
23 23 before_filter :find_issues, :only => [:bulk_edit, :bulk_update, :destroy]
24 24 before_filter :find_project, :only => [:new, :create, :update_form]
25 25 before_filter :authorize, :except => [:index]
26 26 before_filter :find_optional_project, :only => [:index]
27 27 before_filter :check_for_default_issue_status, :only => [:new, :create]
28 28 before_filter :build_new_issue_from_params, :only => [:new, :create, :update_form]
29 29 accept_rss_auth :index, :show
30 30 accept_api_auth :index, :show, :create, :update, :destroy
31 31
32 32 rescue_from Query::StatementInvalid, :with => :query_statement_invalid
33 33
34 34 helper :journals
35 35 helper :projects
36 36 include ProjectsHelper
37 37 helper :custom_fields
38 38 include CustomFieldsHelper
39 39 helper :issue_relations
40 40 include IssueRelationsHelper
41 41 helper :watchers
42 42 include WatchersHelper
43 43 helper :attachments
44 44 include AttachmentsHelper
45 45 helper :queries
46 46 include QueriesHelper
47 47 helper :repositories
48 48 include RepositoriesHelper
49 49 helper :sort
50 50 include SortHelper
51 51 include IssuesHelper
52 52 helper :timelog
53 53 include Redmine::Export::PDF
54 54
55 55 def index
56 56 retrieve_query
57 57 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
58 58 sort_update(@query.sortable_columns)
59 59 @query.sort_criteria = sort_criteria.to_a
60 60
61 61 if @query.valid?
62 62 case params[:format]
63 63 when 'csv', 'pdf'
64 64 @limit = Setting.issues_export_limit.to_i
65 65 when 'atom'
66 66 @limit = Setting.feeds_limit.to_i
67 67 when 'xml', 'json'
68 68 @offset, @limit = api_offset_and_limit
69 69 else
70 70 @limit = per_page_option
71 71 end
72 72
73 73 @issue_count = @query.issue_count
74 74 @issue_pages = Paginator.new @issue_count, @limit, params['page']
75 75 @offset ||= @issue_pages.offset
76 76 @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version],
77 77 :order => sort_clause,
78 78 :offset => @offset,
79 79 :limit => @limit)
80 80 @issue_count_by_group = @query.issue_count_by_group
81 81
82 82 respond_to do |format|
83 83 format.html { render :template => 'issues/index', :layout => !request.xhr? }
84 84 format.api {
85 85 Issue.load_visible_relations(@issues) if include_in_api_response?('relations')
86 86 }
87 87 format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") }
88 88 format.csv { send_data(query_to_csv(@issues, @query, params), :type => 'text/csv; header=present', :filename => 'issues.csv') }
89 89 format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'issues.pdf') }
90 90 end
91 91 else
92 92 respond_to do |format|
93 93 format.html { render(:template => 'issues/index', :layout => !request.xhr?) }
94 94 format.any(:atom, :csv, :pdf) { render(:nothing => true) }
95 95 format.api { render_validation_errors(@query) }
96 96 end
97 97 end
98 98 rescue ActiveRecord::RecordNotFound
99 99 render_404
100 100 end
101 101
102 102 def show
103 103 @journals = @issue.journals.includes(:user, :details).reorder("#{Journal.table_name}.id ASC").all
104 104 @journals.each_with_index {|j,i| j.indice = i+1}
105 105 @journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project)
106 106 @journals.reverse! if User.current.wants_comments_in_reverse_order?
107 107
108 108 @changesets = @issue.changesets.visible.all
109 109 @changesets.reverse! if User.current.wants_comments_in_reverse_order?
110 110
111 111 @relations = @issue.relations.select {|r| r.other_issue(@issue) && r.other_issue(@issue).visible? }
112 112 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
113 113 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
114 114 @priorities = IssuePriority.active
115 115 @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
116 @relation = IssueRelation.new
117
116 118 respond_to do |format|
117 119 format.html {
118 120 retrieve_previous_and_next_issue_ids
119 121 render :template => 'issues/show'
120 122 }
121 123 format.api
122 124 format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' }
123 125 format.pdf {
124 126 pdf = issue_to_pdf(@issue, :journals => @journals)
125 127 send_data(pdf, :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf")
126 128 }
127 129 end
128 130 end
129 131
130 132 # Add a new issue
131 133 # The new issue will be created from an existing one if copy_from parameter is given
132 134 def new
133 135 respond_to do |format|
134 136 format.html { render :action => 'new', :layout => !request.xhr? }
135 137 end
136 138 end
137 139
138 140 def create
139 141 call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
140 142 @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
141 143 if @issue.save
142 144 call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
143 145 respond_to do |format|
144 146 format.html {
145 147 render_attachment_warning_if_needed(@issue)
146 148 flash[:notice] = l(:notice_issue_successful_create, :id => view_context.link_to("##{@issue.id}", issue_path(@issue), :title => @issue.subject))
147 149 if params[:continue]
148 150 attrs = {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?}
149 151 redirect_to new_project_issue_path(@issue.project, :issue => attrs)
150 152 else
151 153 redirect_to issue_path(@issue)
152 154 end
153 155 }
154 156 format.api { render :action => 'show', :status => :created, :location => issue_url(@issue) }
155 157 end
156 158 return
157 159 else
158 160 respond_to do |format|
159 161 format.html { render :action => 'new' }
160 162 format.api { render_validation_errors(@issue) }
161 163 end
162 164 end
163 165 end
164 166
165 167 def edit
166 168 return unless update_issue_from_params
167 169
168 170 respond_to do |format|
169 171 format.html { }
170 172 format.xml { }
171 173 end
172 174 end
173 175
174 176 def update
175 177 return unless update_issue_from_params
176 178 @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
177 179 saved = false
178 180 begin
179 181 saved = @issue.save_issue_with_child_records(params, @time_entry)
180 182 rescue ActiveRecord::StaleObjectError
181 183 @conflict = true
182 184 if params[:last_journal_id]
183 185 @conflict_journals = @issue.journals_after(params[:last_journal_id]).all
184 186 @conflict_journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project)
185 187 end
186 188 end
187 189
188 190 if saved
189 191 render_attachment_warning_if_needed(@issue)
190 192 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
191 193
192 194 respond_to do |format|
193 195 format.html { redirect_back_or_default issue_path(@issue) }
194 196 format.api { render_api_ok }
195 197 end
196 198 else
197 199 respond_to do |format|
198 200 format.html { render :action => 'edit' }
199 201 format.api { render_validation_errors(@issue) }
200 202 end
201 203 end
202 204 end
203 205
204 206 # Updates the issue form when changing the project, status or tracker
205 207 # on issue creation/update
206 208 def update_form
207 209 end
208 210
209 211 # Bulk edit/copy a set of issues
210 212 def bulk_edit
211 213 @issues.sort!
212 214 @copy = params[:copy].present?
213 215 @notes = params[:notes]
214 216
215 217 if User.current.allowed_to?(:move_issues, @projects)
216 218 @allowed_projects = Issue.allowed_target_projects_on_move
217 219 if params[:issue]
218 220 @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:issue][:project_id].to_s}
219 221 if @target_project
220 222 target_projects = [@target_project]
221 223 end
222 224 end
223 225 end
224 226 target_projects ||= @projects
225 227
226 228 if @copy
227 229 @available_statuses = [IssueStatus.default]
228 230 else
229 231 @available_statuses = @issues.map(&:new_statuses_allowed_to).reduce(:&)
230 232 end
231 233 @custom_fields = target_projects.map{|p|p.all_issue_custom_fields}.reduce(:&)
232 234 @assignables = target_projects.map(&:assignable_users).reduce(:&)
233 235 @trackers = target_projects.map(&:trackers).reduce(:&)
234 236 @versions = target_projects.map {|p| p.shared_versions.open}.reduce(:&)
235 237 @categories = target_projects.map {|p| p.issue_categories}.reduce(:&)
236 238 if @copy
237 239 @attachments_present = @issues.detect {|i| i.attachments.any?}.present?
238 240 @subtasks_present = @issues.detect {|i| !i.leaf?}.present?
239 241 end
240 242
241 243 @safe_attributes = @issues.map(&:safe_attribute_names).reduce(:&)
242 244 render :layout => false if request.xhr?
243 245 end
244 246
245 247 def bulk_update
246 248 @issues.sort!
247 249 @copy = params[:copy].present?
248 250 attributes = parse_params_for_bulk_issue_attributes(params)
249 251
250 252 unsaved_issue_ids = []
251 253 moved_issues = []
252 254
253 255 if @copy && params[:copy_subtasks].present?
254 256 # Descendant issues will be copied with the parent task
255 257 # Don't copy them twice
256 258 @issues.reject! {|issue| @issues.detect {|other| issue.is_descendant_of?(other)}}
257 259 end
258 260
259 261 @issues.each do |issue|
260 262 issue.reload
261 263 if @copy
262 264 issue = issue.copy({},
263 265 :attachments => params[:copy_attachments].present?,
264 266 :subtasks => params[:copy_subtasks].present?
265 267 )
266 268 end
267 269 journal = issue.init_journal(User.current, params[:notes])
268 270 issue.safe_attributes = attributes
269 271 call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue })
270 272 if issue.save
271 273 moved_issues << issue
272 274 else
273 275 # Keep unsaved issue ids to display them in flash error
274 276 unsaved_issue_ids << issue.id
275 277 end
276 278 end
277 279 set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids)
278 280
279 281 if params[:follow]
280 282 if @issues.size == 1 && moved_issues.size == 1
281 283 redirect_to issue_path(moved_issues.first)
282 284 elsif moved_issues.map(&:project).uniq.size == 1
283 285 redirect_to project_issues_path(moved_issues.map(&:project).first)
284 286 end
285 287 else
286 288 redirect_back_or_default _project_issues_path(@project)
287 289 end
288 290 end
289 291
290 292 def destroy
291 293 @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f
292 294 if @hours > 0
293 295 case params[:todo]
294 296 when 'destroy'
295 297 # nothing to do
296 298 when 'nullify'
297 299 TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues])
298 300 when 'reassign'
299 301 reassign_to = @project.issues.find_by_id(params[:reassign_to_id])
300 302 if reassign_to.nil?
301 303 flash.now[:error] = l(:error_issue_not_found_in_project)
302 304 return
303 305 else
304 306 TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues])
305 307 end
306 308 else
307 309 # display the destroy form if it's a user request
308 310 return unless api_request?
309 311 end
310 312 end
311 313 @issues.each do |issue|
312 314 begin
313 315 issue.reload.destroy
314 316 rescue ::ActiveRecord::RecordNotFound # raised by #reload if issue no longer exists
315 317 # nothing to do, issue was already deleted (eg. by a parent)
316 318 end
317 319 end
318 320 respond_to do |format|
319 321 format.html { redirect_back_or_default _project_issues_path(@project) }
320 322 format.api { render_api_ok }
321 323 end
322 324 end
323 325
324 326 private
325 327
326 328 def find_project
327 329 project_id = params[:project_id] || (params[:issue] && params[:issue][:project_id])
328 330 @project = Project.find(project_id)
329 331 rescue ActiveRecord::RecordNotFound
330 332 render_404
331 333 end
332 334
333 335 def retrieve_previous_and_next_issue_ids
334 336 retrieve_query_from_session
335 337 if @query
336 338 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
337 339 sort_update(@query.sortable_columns, 'issues_index_sort')
338 340 limit = 500
339 341 issue_ids = @query.issue_ids(:order => sort_clause, :limit => (limit + 1), :include => [:assigned_to, :tracker, :priority, :category, :fixed_version])
340 342 if (idx = issue_ids.index(@issue.id)) && idx < limit
341 343 if issue_ids.size < 500
342 344 @issue_position = idx + 1
343 345 @issue_count = issue_ids.size
344 346 end
345 347 @prev_issue_id = issue_ids[idx - 1] if idx > 0
346 348 @next_issue_id = issue_ids[idx + 1] if idx < (issue_ids.size - 1)
347 349 end
348 350 end
349 351 end
350 352
351 353 # Used by #edit and #update to set some common instance variables
352 354 # from the params
353 355 # TODO: Refactor, not everything in here is needed by #edit
354 356 def update_issue_from_params
355 357 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
356 358 @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
357 359 @time_entry.attributes = params[:time_entry]
358 360
359 361 @issue.init_journal(User.current)
360 362
361 363 issue_attributes = params[:issue]
362 364 if issue_attributes && params[:conflict_resolution]
363 365 case params[:conflict_resolution]
364 366 when 'overwrite'
365 367 issue_attributes = issue_attributes.dup
366 368 issue_attributes.delete(:lock_version)
367 369 when 'add_notes'
368 370 issue_attributes = issue_attributes.slice(:notes)
369 371 when 'cancel'
370 372 redirect_to issue_path(@issue)
371 373 return false
372 374 end
373 375 end
374 376 @issue.safe_attributes = issue_attributes
375 377 @priorities = IssuePriority.active
376 378 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
377 379 true
378 380 end
379 381
380 382 # TODO: Refactor, lots of extra code in here
381 383 # TODO: Changing tracker on an existing issue should not trigger this
382 384 def build_new_issue_from_params
383 385 if params[:id].blank?
384 386 @issue = Issue.new
385 387 if params[:copy_from]
386 388 begin
387 389 @copy_from = Issue.visible.find(params[:copy_from])
388 390 @copy_attachments = params[:copy_attachments].present? || request.get?
389 391 @copy_subtasks = params[:copy_subtasks].present? || request.get?
390 392 @issue.copy_from(@copy_from, :attachments => @copy_attachments, :subtasks => @copy_subtasks)
391 393 rescue ActiveRecord::RecordNotFound
392 394 render_404
393 395 return
394 396 end
395 397 end
396 398 @issue.project = @project
397 399 else
398 400 @issue = @project.issues.visible.find(params[:id])
399 401 end
400 402
401 403 @issue.project = @project
402 404 @issue.author ||= User.current
403 405 # Tracker must be set before custom field values
404 406 @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first)
405 407 if @issue.tracker.nil?
406 408 render_error l(:error_no_tracker_in_project)
407 409 return false
408 410 end
409 411 @issue.start_date ||= Date.today if Setting.default_issue_start_date_to_creation_date?
410 412 @issue.safe_attributes = params[:issue]
411 413
412 414 @priorities = IssuePriority.active
413 415 @allowed_statuses = @issue.new_statuses_allowed_to(User.current, true)
414 416 @available_watchers = (@issue.project.users.sort + @issue.watcher_users).uniq
415 417 end
416 418
417 419 def check_for_default_issue_status
418 420 if IssueStatus.default.nil?
419 421 render_error l(:error_no_default_issue_status)
420 422 return false
421 423 end
422 424 end
423 425
424 426 def parse_params_for_bulk_issue_attributes(params)
425 427 attributes = (params[:issue] || {}).reject {|k,v| v.blank?}
426 428 attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
427 429 if custom = attributes[:custom_field_values]
428 430 custom.reject! {|k,v| v.blank?}
429 431 custom.keys.each do |k|
430 432 if custom[k].is_a?(Array)
431 433 custom[k] << '' if custom[k].delete('__none__')
432 434 else
433 435 custom[k] = '' if custom[k] == '__none__'
434 436 end
435 437 end
436 438 end
437 439 attributes
438 440 end
439 441 end
@@ -1,6 +1,7
1 1 $('#relations').html('<%= escape_javascript(render :partial => 'issues/relations') %>');
2 2 <% if @relation.errors.empty? %>
3 3 $('#relation_delay').val('');
4 4 $('#relation_issue_to_id').val('');
5 5 $('#relation_issue_to_id').focus();
6 6 <% end %>
7 $('#new-relation-form').show();
@@ -1,44 +1,44
1 1 <div class="contextual">
2 2 <% if User.current.allowed_to?(:manage_issue_relations, @project) %>
3 3 <%= toggle_link l(:button_add), 'new-relation-form', {:focus => 'relation_issue_to_id'} %>
4 4 <% end %>
5 5 </div>
6 6
7 7 <p><strong><%=l(:label_related_issues)%></strong></p>
8 8
9 9 <% if @relations.present? %>
10 10 <form>
11 11 <table class="list issues">
12 12 <% @relations.each do |relation| %>
13 13 <% other_issue = relation.other_issue(@issue) -%>
14 14 <tr class="issue hascontextmenu" id="relation-<%= relation.id %>">
15 15 <td class="checkbox"><%= check_box_tag("ids[]", other_issue.id, false, :id => nil) %></td>
16 16 <td class="subject">
17 17 <%= l(relation.label_for(@issue)) %>
18 18 <%= "(#{l('datetime.distance_in_words.x_days', :count => relation.delay)})" if relation.delay && relation.delay != 0 %>
19 19 <%= h(other_issue.project) + ' - ' if Setting.cross_project_issue_relations? %>
20 20 <%= link_to_issue(other_issue, :truncate => 60) %>
21 21 </td>
22 22 <td class="status"><%=h other_issue.status.name %></td>
23 23 <td class="start_date"><%= format_date(other_issue.start_date) %></td>
24 24 <td class="due_date"><%= format_date(other_issue.due_date) %></td>
25 25 <td class="buttons"><%= link_to image_tag('link_break.png'),
26 26 relation_path(relation),
27 27 :remote => true,
28 28 :method => :delete,
29 29 :data => {:confirm => l(:text_are_you_sure)},
30 30 :title => l(:label_relation_delete) if User.current.allowed_to?(:manage_issue_relations, @project) %></td>
31 31 </tr>
32 32 <% end %>
33 33 </table>
34 34 </form>
35 35 <% end %>
36 36
37 37 <%= form_for @relation, {
38 38 :as => :relation, :remote => true,
39 39 :url => issue_relations_path(@issue),
40 40 :method => :post,
41 :html => {:id => 'new-relation-form', :style => (@relation ? '' : 'display: none;')}
41 :html => {:id => 'new-relation-form', :style => 'display: none;'}
42 42 } do |f| %>
43 43 <%= render :partial => 'issue_relations/form', :locals => {:f => f}%>
44 44 <% end %>
General Comments 0
You need to be logged in to leave comments. Login now