@@ -1,481 +1,484 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2014 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 | if params[:columns] == 'all' | |
|
66 | @query.column_names = @query.available_inline_columns.map(&:name) | |
|
67 | end | |
|
65 | 68 | when 'atom' |
|
66 | 69 | @limit = Setting.feeds_limit.to_i |
|
67 | 70 | when 'xml', 'json' |
|
68 | 71 | @offset, @limit = api_offset_and_limit |
|
69 | 72 | else |
|
70 | 73 | @limit = per_page_option |
|
71 | 74 | end |
|
72 | 75 | |
|
73 | 76 | @issue_count = @query.issue_count |
|
74 | 77 | @issue_pages = Paginator.new @issue_count, @limit, params['page'] |
|
75 | 78 | @offset ||= @issue_pages.offset |
|
76 | 79 | @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version], |
|
77 | 80 | :order => sort_clause, |
|
78 | 81 | :offset => @offset, |
|
79 | 82 | :limit => @limit) |
|
80 | 83 | @issue_count_by_group = @query.issue_count_by_group |
|
81 | 84 | |
|
82 | 85 | respond_to do |format| |
|
83 | 86 | format.html { render :template => 'issues/index', :layout => !request.xhr? } |
|
84 | 87 | format.api { |
|
85 | 88 | Issue.load_visible_relations(@issues) if include_in_api_response?('relations') |
|
86 | 89 | } |
|
87 | 90 | format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") } |
|
88 | 91 | format.csv { send_data(query_to_csv(@issues, @query, params), :type => 'text/csv; header=present', :filename => 'issues.csv') } |
|
89 | 92 | format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'issues.pdf') } |
|
90 | 93 | end |
|
91 | 94 | else |
|
92 | 95 | respond_to do |format| |
|
93 | 96 | format.html { render(:template => 'issues/index', :layout => !request.xhr?) } |
|
94 | 97 | format.any(:atom, :csv, :pdf) { render(:nothing => true) } |
|
95 | 98 | format.api { render_validation_errors(@query) } |
|
96 | 99 | end |
|
97 | 100 | end |
|
98 | 101 | rescue ActiveRecord::RecordNotFound |
|
99 | 102 | render_404 |
|
100 | 103 | end |
|
101 | 104 | |
|
102 | 105 | def show |
|
103 | 106 | @journals = @issue.journals.includes(:user, :details).reorder("#{Journal.table_name}.id ASC").all |
|
104 | 107 | @journals.each_with_index {|j,i| j.indice = i+1} |
|
105 | 108 | @journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project) |
|
106 | 109 | Journal.preload_journals_details_custom_fields(@journals) |
|
107 | 110 | # TODO: use #select! when ruby1.8 support is dropped |
|
108 | 111 | @journals.reject! {|journal| !journal.notes? && journal.visible_details.empty?} |
|
109 | 112 | @journals.reverse! if User.current.wants_comments_in_reverse_order? |
|
110 | 113 | |
|
111 | 114 | @changesets = @issue.changesets.visible.all |
|
112 | 115 | @changesets.reverse! if User.current.wants_comments_in_reverse_order? |
|
113 | 116 | |
|
114 | 117 | @relations = @issue.relations.select {|r| r.other_issue(@issue) && r.other_issue(@issue).visible? } |
|
115 | 118 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current) |
|
116 | 119 | @edit_allowed = User.current.allowed_to?(:edit_issues, @project) |
|
117 | 120 | @priorities = IssuePriority.active |
|
118 | 121 | @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project) |
|
119 | 122 | @relation = IssueRelation.new |
|
120 | 123 | |
|
121 | 124 | respond_to do |format| |
|
122 | 125 | format.html { |
|
123 | 126 | retrieve_previous_and_next_issue_ids |
|
124 | 127 | render :template => 'issues/show' |
|
125 | 128 | } |
|
126 | 129 | format.api |
|
127 | 130 | format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' } |
|
128 | 131 | format.pdf { |
|
129 | 132 | pdf = issue_to_pdf(@issue, :journals => @journals) |
|
130 | 133 | send_data(pdf, :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") |
|
131 | 134 | } |
|
132 | 135 | end |
|
133 | 136 | end |
|
134 | 137 | |
|
135 | 138 | # Add a new issue |
|
136 | 139 | # The new issue will be created from an existing one if copy_from parameter is given |
|
137 | 140 | def new |
|
138 | 141 | respond_to do |format| |
|
139 | 142 | format.html { render :action => 'new', :layout => !request.xhr? } |
|
140 | 143 | end |
|
141 | 144 | end |
|
142 | 145 | |
|
143 | 146 | def create |
|
144 | 147 | call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue }) |
|
145 | 148 | @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads])) |
|
146 | 149 | if @issue.save |
|
147 | 150 | call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue}) |
|
148 | 151 | respond_to do |format| |
|
149 | 152 | format.html { |
|
150 | 153 | render_attachment_warning_if_needed(@issue) |
|
151 | 154 | flash[:notice] = l(:notice_issue_successful_create, :id => view_context.link_to("##{@issue.id}", issue_path(@issue), :title => @issue.subject)) |
|
152 | 155 | if params[:continue] |
|
153 | 156 | attrs = {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?} |
|
154 | 157 | redirect_to new_project_issue_path(@issue.project, :issue => attrs) |
|
155 | 158 | else |
|
156 | 159 | redirect_to issue_path(@issue) |
|
157 | 160 | end |
|
158 | 161 | } |
|
159 | 162 | format.api { render :action => 'show', :status => :created, :location => issue_url(@issue) } |
|
160 | 163 | end |
|
161 | 164 | return |
|
162 | 165 | else |
|
163 | 166 | respond_to do |format| |
|
164 | 167 | format.html { render :action => 'new' } |
|
165 | 168 | format.api { render_validation_errors(@issue) } |
|
166 | 169 | end |
|
167 | 170 | end |
|
168 | 171 | end |
|
169 | 172 | |
|
170 | 173 | def edit |
|
171 | 174 | return unless update_issue_from_params |
|
172 | 175 | |
|
173 | 176 | respond_to do |format| |
|
174 | 177 | format.html { } |
|
175 | 178 | format.xml { } |
|
176 | 179 | end |
|
177 | 180 | end |
|
178 | 181 | |
|
179 | 182 | def update |
|
180 | 183 | return unless update_issue_from_params |
|
181 | 184 | @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads])) |
|
182 | 185 | saved = false |
|
183 | 186 | begin |
|
184 | 187 | saved = save_issue_with_child_records |
|
185 | 188 | rescue ActiveRecord::StaleObjectError |
|
186 | 189 | @conflict = true |
|
187 | 190 | if params[:last_journal_id] |
|
188 | 191 | @conflict_journals = @issue.journals_after(params[:last_journal_id]).all |
|
189 | 192 | @conflict_journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project) |
|
190 | 193 | end |
|
191 | 194 | end |
|
192 | 195 | |
|
193 | 196 | if saved |
|
194 | 197 | render_attachment_warning_if_needed(@issue) |
|
195 | 198 | flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record? |
|
196 | 199 | |
|
197 | 200 | respond_to do |format| |
|
198 | 201 | format.html { redirect_back_or_default issue_path(@issue) } |
|
199 | 202 | format.api { render_api_ok } |
|
200 | 203 | end |
|
201 | 204 | else |
|
202 | 205 | respond_to do |format| |
|
203 | 206 | format.html { render :action => 'edit' } |
|
204 | 207 | format.api { render_validation_errors(@issue) } |
|
205 | 208 | end |
|
206 | 209 | end |
|
207 | 210 | end |
|
208 | 211 | |
|
209 | 212 | # Updates the issue form when changing the project, status or tracker |
|
210 | 213 | # on issue creation/update |
|
211 | 214 | def update_form |
|
212 | 215 | end |
|
213 | 216 | |
|
214 | 217 | # Bulk edit/copy a set of issues |
|
215 | 218 | def bulk_edit |
|
216 | 219 | @issues.sort! |
|
217 | 220 | @copy = params[:copy].present? |
|
218 | 221 | @notes = params[:notes] |
|
219 | 222 | |
|
220 | 223 | if User.current.allowed_to?(:move_issues, @projects) |
|
221 | 224 | @allowed_projects = Issue.allowed_target_projects_on_move |
|
222 | 225 | if params[:issue] |
|
223 | 226 | @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:issue][:project_id].to_s} |
|
224 | 227 | if @target_project |
|
225 | 228 | target_projects = [@target_project] |
|
226 | 229 | end |
|
227 | 230 | end |
|
228 | 231 | end |
|
229 | 232 | target_projects ||= @projects |
|
230 | 233 | |
|
231 | 234 | if @copy |
|
232 | 235 | @available_statuses = [IssueStatus.default] |
|
233 | 236 | else |
|
234 | 237 | @available_statuses = @issues.map(&:new_statuses_allowed_to).reduce(:&) |
|
235 | 238 | end |
|
236 | 239 | @custom_fields = target_projects.map{|p|p.all_issue_custom_fields.visible}.reduce(:&) |
|
237 | 240 | @assignables = target_projects.map(&:assignable_users).reduce(:&) |
|
238 | 241 | @trackers = target_projects.map(&:trackers).reduce(:&) |
|
239 | 242 | @versions = target_projects.map {|p| p.shared_versions.open}.reduce(:&) |
|
240 | 243 | @categories = target_projects.map {|p| p.issue_categories}.reduce(:&) |
|
241 | 244 | if @copy |
|
242 | 245 | @attachments_present = @issues.detect {|i| i.attachments.any?}.present? |
|
243 | 246 | @subtasks_present = @issues.detect {|i| !i.leaf?}.present? |
|
244 | 247 | end |
|
245 | 248 | |
|
246 | 249 | @safe_attributes = @issues.map(&:safe_attribute_names).reduce(:&) |
|
247 | 250 | |
|
248 | 251 | @issue_params = params[:issue] || {} |
|
249 | 252 | @issue_params[:custom_field_values] ||= {} |
|
250 | 253 | end |
|
251 | 254 | |
|
252 | 255 | def bulk_update |
|
253 | 256 | @issues.sort! |
|
254 | 257 | @copy = params[:copy].present? |
|
255 | 258 | attributes = parse_params_for_bulk_issue_attributes(params) |
|
256 | 259 | |
|
257 | 260 | unsaved_issues = [] |
|
258 | 261 | saved_issues = [] |
|
259 | 262 | |
|
260 | 263 | if @copy && params[:copy_subtasks].present? |
|
261 | 264 | # Descendant issues will be copied with the parent task |
|
262 | 265 | # Don't copy them twice |
|
263 | 266 | @issues.reject! {|issue| @issues.detect {|other| issue.is_descendant_of?(other)}} |
|
264 | 267 | end |
|
265 | 268 | |
|
266 | 269 | @issues.each do |orig_issue| |
|
267 | 270 | orig_issue.reload |
|
268 | 271 | if @copy |
|
269 | 272 | issue = orig_issue.copy({}, |
|
270 | 273 | :attachments => params[:copy_attachments].present?, |
|
271 | 274 | :subtasks => params[:copy_subtasks].present? |
|
272 | 275 | ) |
|
273 | 276 | else |
|
274 | 277 | issue = orig_issue |
|
275 | 278 | end |
|
276 | 279 | journal = issue.init_journal(User.current, params[:notes]) |
|
277 | 280 | issue.safe_attributes = attributes |
|
278 | 281 | call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue }) |
|
279 | 282 | if issue.save |
|
280 | 283 | saved_issues << issue |
|
281 | 284 | else |
|
282 | 285 | unsaved_issues << orig_issue |
|
283 | 286 | end |
|
284 | 287 | end |
|
285 | 288 | |
|
286 | 289 | if unsaved_issues.empty? |
|
287 | 290 | flash[:notice] = l(:notice_successful_update) unless saved_issues.empty? |
|
288 | 291 | if params[:follow] |
|
289 | 292 | if @issues.size == 1 && saved_issues.size == 1 |
|
290 | 293 | redirect_to issue_path(saved_issues.first) |
|
291 | 294 | elsif saved_issues.map(&:project).uniq.size == 1 |
|
292 | 295 | redirect_to project_issues_path(saved_issues.map(&:project).first) |
|
293 | 296 | end |
|
294 | 297 | else |
|
295 | 298 | redirect_back_or_default _project_issues_path(@project) |
|
296 | 299 | end |
|
297 | 300 | else |
|
298 | 301 | @saved_issues = @issues |
|
299 | 302 | @unsaved_issues = unsaved_issues |
|
300 | 303 | @issues = Issue.visible.where(:id => @unsaved_issues.map(&:id)).all |
|
301 | 304 | bulk_edit |
|
302 | 305 | render :action => 'bulk_edit' |
|
303 | 306 | end |
|
304 | 307 | end |
|
305 | 308 | |
|
306 | 309 | def destroy |
|
307 | 310 | @hours = TimeEntry.where(:issue_id => @issues.map(&:id)).sum(:hours).to_f |
|
308 | 311 | if @hours > 0 |
|
309 | 312 | case params[:todo] |
|
310 | 313 | when 'destroy' |
|
311 | 314 | # nothing to do |
|
312 | 315 | when 'nullify' |
|
313 | 316 | TimeEntry.where(['issue_id IN (?)', @issues]).update_all('issue_id = NULL') |
|
314 | 317 | when 'reassign' |
|
315 | 318 | reassign_to = @project.issues.find_by_id(params[:reassign_to_id]) |
|
316 | 319 | if reassign_to.nil? |
|
317 | 320 | flash.now[:error] = l(:error_issue_not_found_in_project) |
|
318 | 321 | return |
|
319 | 322 | else |
|
320 | 323 | TimeEntry.where(['issue_id IN (?)', @issues]). |
|
321 | 324 | update_all("issue_id = #{reassign_to.id}") |
|
322 | 325 | end |
|
323 | 326 | else |
|
324 | 327 | # display the destroy form if it's a user request |
|
325 | 328 | return unless api_request? |
|
326 | 329 | end |
|
327 | 330 | end |
|
328 | 331 | @issues.each do |issue| |
|
329 | 332 | begin |
|
330 | 333 | issue.reload.destroy |
|
331 | 334 | rescue ::ActiveRecord::RecordNotFound # raised by #reload if issue no longer exists |
|
332 | 335 | # nothing to do, issue was already deleted (eg. by a parent) |
|
333 | 336 | end |
|
334 | 337 | end |
|
335 | 338 | respond_to do |format| |
|
336 | 339 | format.html { redirect_back_or_default _project_issues_path(@project) } |
|
337 | 340 | format.api { render_api_ok } |
|
338 | 341 | end |
|
339 | 342 | end |
|
340 | 343 | |
|
341 | 344 | private |
|
342 | 345 | |
|
343 | 346 | def find_project |
|
344 | 347 | project_id = params[:project_id] || (params[:issue] && params[:issue][:project_id]) |
|
345 | 348 | @project = Project.find(project_id) |
|
346 | 349 | rescue ActiveRecord::RecordNotFound |
|
347 | 350 | render_404 |
|
348 | 351 | end |
|
349 | 352 | |
|
350 | 353 | def retrieve_previous_and_next_issue_ids |
|
351 | 354 | retrieve_query_from_session |
|
352 | 355 | if @query |
|
353 | 356 | sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria) |
|
354 | 357 | sort_update(@query.sortable_columns, 'issues_index_sort') |
|
355 | 358 | limit = 500 |
|
356 | 359 | issue_ids = @query.issue_ids(:order => sort_clause, :limit => (limit + 1), :include => [:assigned_to, :tracker, :priority, :category, :fixed_version]) |
|
357 | 360 | if (idx = issue_ids.index(@issue.id)) && idx < limit |
|
358 | 361 | if issue_ids.size < 500 |
|
359 | 362 | @issue_position = idx + 1 |
|
360 | 363 | @issue_count = issue_ids.size |
|
361 | 364 | end |
|
362 | 365 | @prev_issue_id = issue_ids[idx - 1] if idx > 0 |
|
363 | 366 | @next_issue_id = issue_ids[idx + 1] if idx < (issue_ids.size - 1) |
|
364 | 367 | end |
|
365 | 368 | end |
|
366 | 369 | end |
|
367 | 370 | |
|
368 | 371 | # Used by #edit and #update to set some common instance variables |
|
369 | 372 | # from the params |
|
370 | 373 | # TODO: Refactor, not everything in here is needed by #edit |
|
371 | 374 | def update_issue_from_params |
|
372 | 375 | @edit_allowed = User.current.allowed_to?(:edit_issues, @project) |
|
373 | 376 | @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project) |
|
374 | 377 | @time_entry.attributes = params[:time_entry] |
|
375 | 378 | |
|
376 | 379 | @issue.init_journal(User.current) |
|
377 | 380 | |
|
378 | 381 | issue_attributes = params[:issue] |
|
379 | 382 | if issue_attributes && params[:conflict_resolution] |
|
380 | 383 | case params[:conflict_resolution] |
|
381 | 384 | when 'overwrite' |
|
382 | 385 | issue_attributes = issue_attributes.dup |
|
383 | 386 | issue_attributes.delete(:lock_version) |
|
384 | 387 | when 'add_notes' |
|
385 | 388 | issue_attributes = issue_attributes.slice(:notes) |
|
386 | 389 | when 'cancel' |
|
387 | 390 | redirect_to issue_path(@issue) |
|
388 | 391 | return false |
|
389 | 392 | end |
|
390 | 393 | end |
|
391 | 394 | @issue.safe_attributes = issue_attributes |
|
392 | 395 | @priorities = IssuePriority.active |
|
393 | 396 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current) |
|
394 | 397 | true |
|
395 | 398 | end |
|
396 | 399 | |
|
397 | 400 | # TODO: Refactor, lots of extra code in here |
|
398 | 401 | # TODO: Changing tracker on an existing issue should not trigger this |
|
399 | 402 | def build_new_issue_from_params |
|
400 | 403 | if params[:id].blank? |
|
401 | 404 | @issue = Issue.new |
|
402 | 405 | if params[:copy_from] |
|
403 | 406 | begin |
|
404 | 407 | @copy_from = Issue.visible.find(params[:copy_from]) |
|
405 | 408 | @copy_attachments = params[:copy_attachments].present? || request.get? |
|
406 | 409 | @copy_subtasks = params[:copy_subtasks].present? || request.get? |
|
407 | 410 | @issue.copy_from(@copy_from, :attachments => @copy_attachments, :subtasks => @copy_subtasks) |
|
408 | 411 | rescue ActiveRecord::RecordNotFound |
|
409 | 412 | render_404 |
|
410 | 413 | return |
|
411 | 414 | end |
|
412 | 415 | end |
|
413 | 416 | @issue.project = @project |
|
414 | 417 | else |
|
415 | 418 | @issue = @project.issues.visible.find(params[:id]) |
|
416 | 419 | end |
|
417 | 420 | |
|
418 | 421 | @issue.project = @project |
|
419 | 422 | @issue.author ||= User.current |
|
420 | 423 | # Tracker must be set before custom field values |
|
421 | 424 | @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first) |
|
422 | 425 | if @issue.tracker.nil? |
|
423 | 426 | render_error l(:error_no_tracker_in_project) |
|
424 | 427 | return false |
|
425 | 428 | end |
|
426 | 429 | @issue.start_date ||= Date.today if Setting.default_issue_start_date_to_creation_date? |
|
427 | 430 | @issue.safe_attributes = params[:issue] |
|
428 | 431 | |
|
429 | 432 | @priorities = IssuePriority.active |
|
430 | 433 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current, @issue.new_record?) |
|
431 | 434 | @available_watchers = @issue.watcher_users |
|
432 | 435 | if @issue.project.users.count <= 20 |
|
433 | 436 | @available_watchers = (@available_watchers + @issue.project.users.sort).uniq |
|
434 | 437 | end |
|
435 | 438 | end |
|
436 | 439 | |
|
437 | 440 | def check_for_default_issue_status |
|
438 | 441 | if IssueStatus.default.nil? |
|
439 | 442 | render_error l(:error_no_default_issue_status) |
|
440 | 443 | return false |
|
441 | 444 | end |
|
442 | 445 | end |
|
443 | 446 | |
|
444 | 447 | def parse_params_for_bulk_issue_attributes(params) |
|
445 | 448 | attributes = (params[:issue] || {}).reject {|k,v| v.blank?} |
|
446 | 449 | attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'} |
|
447 | 450 | if custom = attributes[:custom_field_values] |
|
448 | 451 | custom.reject! {|k,v| v.blank?} |
|
449 | 452 | custom.keys.each do |k| |
|
450 | 453 | if custom[k].is_a?(Array) |
|
451 | 454 | custom[k] << '' if custom[k].delete('__none__') |
|
452 | 455 | else |
|
453 | 456 | custom[k] = '' if custom[k] == '__none__' |
|
454 | 457 | end |
|
455 | 458 | end |
|
456 | 459 | end |
|
457 | 460 | attributes |
|
458 | 461 | end |
|
459 | 462 | |
|
460 | 463 | # Saves @issue and a time_entry from the parameters |
|
461 | 464 | def save_issue_with_child_records |
|
462 | 465 | Issue.transaction do |
|
463 | 466 | if params[:time_entry] && (params[:time_entry][:hours].present? || params[:time_entry][:comments].present?) && User.current.allowed_to?(:log_time, @issue.project) |
|
464 | 467 | time_entry = @time_entry || TimeEntry.new |
|
465 | 468 | time_entry.project = @issue.project |
|
466 | 469 | time_entry.issue = @issue |
|
467 | 470 | time_entry.user = User.current |
|
468 | 471 | time_entry.spent_on = User.current.today |
|
469 | 472 | time_entry.attributes = params[:time_entry] |
|
470 | 473 | @issue.time_entries << time_entry |
|
471 | 474 | end |
|
472 | 475 | |
|
473 | 476 | call_hook(:controller_issues_edit_before_save, { :params => params, :issue => @issue, :time_entry => time_entry, :journal => @issue.current_journal}) |
|
474 | 477 | if @issue.save |
|
475 | 478 | call_hook(:controller_issues_edit_after_save, { :params => params, :issue => @issue, :time_entry => time_entry, :journal => @issue.current_journal}) |
|
476 | 479 | else |
|
477 | 480 | raise ActiveRecord::Rollback |
|
478 | 481 | end |
|
479 | 482 | end |
|
480 | 483 | end |
|
481 | 484 | end |
@@ -1,481 +1,484 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2014 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 IssueQuery < Query |
|
19 | 19 | |
|
20 | 20 | self.queried_class = Issue |
|
21 | 21 | |
|
22 | 22 | self.available_columns = [ |
|
23 | 23 | QueryColumn.new(:id, :sortable => "#{Issue.table_name}.id", :default_order => 'desc', :caption => '#', :frozen => true), |
|
24 | 24 | QueryColumn.new(:project, :sortable => "#{Project.table_name}.name", :groupable => true), |
|
25 | 25 | QueryColumn.new(:tracker, :sortable => "#{Tracker.table_name}.position", :groupable => true), |
|
26 | 26 | QueryColumn.new(:parent, :sortable => ["#{Issue.table_name}.root_id", "#{Issue.table_name}.lft ASC"], :default_order => 'desc', :caption => :field_parent_issue), |
|
27 | 27 | QueryColumn.new(:status, :sortable => "#{IssueStatus.table_name}.position", :groupable => true), |
|
28 | 28 | QueryColumn.new(:priority, :sortable => "#{IssuePriority.table_name}.position", :default_order => 'desc', :groupable => true), |
|
29 | 29 | QueryColumn.new(:subject, :sortable => "#{Issue.table_name}.subject"), |
|
30 | 30 | QueryColumn.new(:author, :sortable => lambda {User.fields_for_order_statement("authors")}, :groupable => true), |
|
31 | 31 | QueryColumn.new(:assigned_to, :sortable => lambda {User.fields_for_order_statement}, :groupable => true), |
|
32 | 32 | QueryColumn.new(:updated_on, :sortable => "#{Issue.table_name}.updated_on", :default_order => 'desc'), |
|
33 | 33 | QueryColumn.new(:category, :sortable => "#{IssueCategory.table_name}.name", :groupable => true), |
|
34 | 34 | QueryColumn.new(:fixed_version, :sortable => lambda {Version.fields_for_order_statement}, :groupable => true), |
|
35 | 35 | QueryColumn.new(:start_date, :sortable => "#{Issue.table_name}.start_date"), |
|
36 | 36 | QueryColumn.new(:due_date, :sortable => "#{Issue.table_name}.due_date"), |
|
37 | 37 | QueryColumn.new(:estimated_hours, :sortable => "#{Issue.table_name}.estimated_hours"), |
|
38 | 38 | QueryColumn.new(:done_ratio, :sortable => "#{Issue.table_name}.done_ratio", :groupable => true), |
|
39 | 39 | QueryColumn.new(:created_on, :sortable => "#{Issue.table_name}.created_on", :default_order => 'desc'), |
|
40 | 40 | QueryColumn.new(:closed_on, :sortable => "#{Issue.table_name}.closed_on", :default_order => 'desc'), |
|
41 | 41 | QueryColumn.new(:relations, :caption => :label_related_issues), |
|
42 | 42 | QueryColumn.new(:description, :inline => false) |
|
43 | 43 | ] |
|
44 | 44 | |
|
45 | 45 | scope :visible, lambda {|*args| |
|
46 | 46 | user = args.shift || User.current |
|
47 | 47 | base = Project.allowed_to_condition(user, :view_issues, *args) |
|
48 | 48 | scope = includes(:project).where("#{table_name}.project_id IS NULL OR (#{base})") |
|
49 | 49 | |
|
50 | 50 | if user.admin? |
|
51 | 51 | scope.where("#{table_name}.visibility <> ? OR #{table_name}.user_id = ?", VISIBILITY_PRIVATE, user.id) |
|
52 | 52 | elsif user.memberships.any? |
|
53 | 53 | scope.where("#{table_name}.visibility = ?" + |
|
54 | 54 | " OR (#{table_name}.visibility = ? AND #{table_name}.id IN (" + |
|
55 | 55 | "SELECT DISTINCT q.id FROM #{table_name} q" + |
|
56 | 56 | " INNER JOIN #{table_name_prefix}queries_roles#{table_name_suffix} qr on qr.query_id = q.id" + |
|
57 | 57 | " INNER JOIN #{MemberRole.table_name} mr ON mr.role_id = qr.role_id" + |
|
58 | 58 | " INNER JOIN #{Member.table_name} m ON m.id = mr.member_id AND m.user_id = ?" + |
|
59 | 59 | " WHERE q.project_id IS NULL OR q.project_id = m.project_id))" + |
|
60 | 60 | " OR #{table_name}.user_id = ?", |
|
61 | 61 | VISIBILITY_PUBLIC, VISIBILITY_ROLES, user.id, user.id) |
|
62 | 62 | elsif user.logged? |
|
63 | 63 | scope.where("#{table_name}.visibility = ? OR #{table_name}.user_id = ?", VISIBILITY_PUBLIC, user.id) |
|
64 | 64 | else |
|
65 | 65 | scope.where("#{table_name}.visibility = ?", VISIBILITY_PUBLIC) |
|
66 | 66 | end |
|
67 | 67 | } |
|
68 | 68 | |
|
69 | 69 | def initialize(attributes=nil, *args) |
|
70 | 70 | super attributes |
|
71 | 71 | self.filters ||= { 'status_id' => {:operator => "o", :values => [""]} } |
|
72 | 72 | end |
|
73 | 73 | |
|
74 | 74 | # Returns true if the query is visible to +user+ or the current user. |
|
75 | 75 | def visible?(user=User.current) |
|
76 | 76 | return true if user.admin? |
|
77 | 77 | return false unless project.nil? || user.allowed_to?(:view_issues, project) |
|
78 | 78 | case visibility |
|
79 | 79 | when VISIBILITY_PUBLIC |
|
80 | 80 | true |
|
81 | 81 | when VISIBILITY_ROLES |
|
82 | 82 | if project |
|
83 | 83 | (user.roles_for_project(project) & roles).any? |
|
84 | 84 | else |
|
85 | 85 | Member.where(:user_id => user.id).joins(:roles).where(:member_roles => {:role_id => roles.map(&:id)}).any? |
|
86 | 86 | end |
|
87 | 87 | else |
|
88 | 88 | user == self.user |
|
89 | 89 | end |
|
90 | 90 | end |
|
91 | 91 | |
|
92 | 92 | def is_private? |
|
93 | 93 | visibility == VISIBILITY_PRIVATE |
|
94 | 94 | end |
|
95 | 95 | |
|
96 | 96 | def is_public? |
|
97 | 97 | !is_private? |
|
98 | 98 | end |
|
99 | 99 | |
|
100 | 100 | def draw_relations |
|
101 | 101 | r = options[:draw_relations] |
|
102 | 102 | r.nil? || r == '1' |
|
103 | 103 | end |
|
104 | 104 | |
|
105 | 105 | def draw_relations=(arg) |
|
106 | 106 | options[:draw_relations] = (arg == '0' ? '0' : nil) |
|
107 | 107 | end |
|
108 | 108 | |
|
109 | 109 | def draw_progress_line |
|
110 | 110 | r = options[:draw_progress_line] |
|
111 | 111 | r == '1' |
|
112 | 112 | end |
|
113 | 113 | |
|
114 | 114 | def draw_progress_line=(arg) |
|
115 | 115 | options[:draw_progress_line] = (arg == '1' ? '1' : nil) |
|
116 | 116 | end |
|
117 | 117 | |
|
118 | 118 | def build_from_params(params) |
|
119 | 119 | super |
|
120 | 120 | self.draw_relations = params[:draw_relations] || (params[:query] && params[:query][:draw_relations]) |
|
121 | 121 | self.draw_progress_line = params[:draw_progress_line] || (params[:query] && params[:query][:draw_progress_line]) |
|
122 | 122 | self |
|
123 | 123 | end |
|
124 | 124 | |
|
125 | 125 | def initialize_available_filters |
|
126 | 126 | principals = [] |
|
127 | 127 | subprojects = [] |
|
128 | 128 | versions = [] |
|
129 | 129 | categories = [] |
|
130 | 130 | issue_custom_fields = [] |
|
131 | 131 | |
|
132 | 132 | if project |
|
133 | 133 | principals += project.principals.sort |
|
134 | 134 | unless project.leaf? |
|
135 | 135 | subprojects = project.descendants.visible.all |
|
136 | 136 | principals += Principal.member_of(subprojects) |
|
137 | 137 | end |
|
138 | 138 | versions = project.shared_versions.all |
|
139 | 139 | categories = project.issue_categories.all |
|
140 | 140 | issue_custom_fields = project.all_issue_custom_fields |
|
141 | 141 | else |
|
142 | 142 | if all_projects.any? |
|
143 | 143 | principals += Principal.member_of(all_projects) |
|
144 | 144 | end |
|
145 | 145 | versions = Version.visible.where(:sharing => 'system').all |
|
146 | 146 | issue_custom_fields = IssueCustomField.where(:is_for_all => true) |
|
147 | 147 | end |
|
148 | 148 | principals.uniq! |
|
149 | 149 | principals.sort! |
|
150 | 150 | users = principals.select {|p| p.is_a?(User)} |
|
151 | 151 | |
|
152 | 152 | add_available_filter "status_id", |
|
153 | 153 | :type => :list_status, :values => IssueStatus.sorted.collect{|s| [s.name, s.id.to_s] } |
|
154 | 154 | |
|
155 | 155 | if project.nil? |
|
156 | 156 | project_values = [] |
|
157 | 157 | if User.current.logged? && User.current.memberships.any? |
|
158 | 158 | project_values << ["<< #{l(:label_my_projects).downcase} >>", "mine"] |
|
159 | 159 | end |
|
160 | 160 | project_values += all_projects_values |
|
161 | 161 | add_available_filter("project_id", |
|
162 | 162 | :type => :list, :values => project_values |
|
163 | 163 | ) unless project_values.empty? |
|
164 | 164 | end |
|
165 | 165 | |
|
166 | 166 | add_available_filter "tracker_id", |
|
167 | 167 | :type => :list, :values => trackers.collect{|s| [s.name, s.id.to_s] } |
|
168 | 168 | add_available_filter "priority_id", |
|
169 | 169 | :type => :list, :values => IssuePriority.all.collect{|s| [s.name, s.id.to_s] } |
|
170 | 170 | |
|
171 | 171 | author_values = [] |
|
172 | 172 | author_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged? |
|
173 | 173 | author_values += users.collect{|s| [s.name, s.id.to_s] } |
|
174 | 174 | add_available_filter("author_id", |
|
175 | 175 | :type => :list, :values => author_values |
|
176 | 176 | ) unless author_values.empty? |
|
177 | 177 | |
|
178 | 178 | assigned_to_values = [] |
|
179 | 179 | assigned_to_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged? |
|
180 | 180 | assigned_to_values += (Setting.issue_group_assignment? ? |
|
181 | 181 | principals : users).collect{|s| [s.name, s.id.to_s] } |
|
182 | 182 | add_available_filter("assigned_to_id", |
|
183 | 183 | :type => :list_optional, :values => assigned_to_values |
|
184 | 184 | ) unless assigned_to_values.empty? |
|
185 | 185 | |
|
186 | 186 | group_values = Group.all.collect {|g| [g.name, g.id.to_s] } |
|
187 | 187 | add_available_filter("member_of_group", |
|
188 | 188 | :type => :list_optional, :values => group_values |
|
189 | 189 | ) unless group_values.empty? |
|
190 | 190 | |
|
191 | 191 | role_values = Role.givable.collect {|r| [r.name, r.id.to_s] } |
|
192 | 192 | add_available_filter("assigned_to_role", |
|
193 | 193 | :type => :list_optional, :values => role_values |
|
194 | 194 | ) unless role_values.empty? |
|
195 | 195 | |
|
196 | 196 | if versions.any? |
|
197 | 197 | add_available_filter "fixed_version_id", |
|
198 | 198 | :type => :list_optional, |
|
199 | 199 | :values => versions.sort.collect{|s| ["#{s.project.name} - #{s.name}", s.id.to_s] } |
|
200 | 200 | end |
|
201 | 201 | |
|
202 | 202 | if categories.any? |
|
203 | 203 | add_available_filter "category_id", |
|
204 | 204 | :type => :list_optional, |
|
205 | 205 | :values => categories.collect{|s| [s.name, s.id.to_s] } |
|
206 | 206 | end |
|
207 | 207 | |
|
208 | 208 | add_available_filter "subject", :type => :text |
|
209 | 209 | add_available_filter "created_on", :type => :date_past |
|
210 | 210 | add_available_filter "updated_on", :type => :date_past |
|
211 | 211 | add_available_filter "closed_on", :type => :date_past |
|
212 | 212 | add_available_filter "start_date", :type => :date |
|
213 | 213 | add_available_filter "due_date", :type => :date |
|
214 | 214 | add_available_filter "estimated_hours", :type => :float |
|
215 | 215 | add_available_filter "done_ratio", :type => :integer |
|
216 | 216 | |
|
217 | 217 | if User.current.allowed_to?(:set_issues_private, nil, :global => true) || |
|
218 | 218 | User.current.allowed_to?(:set_own_issues_private, nil, :global => true) |
|
219 | 219 | add_available_filter "is_private", |
|
220 | 220 | :type => :list, |
|
221 | 221 | :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]] |
|
222 | 222 | end |
|
223 | 223 | |
|
224 | 224 | if User.current.logged? |
|
225 | 225 | add_available_filter "watcher_id", |
|
226 | 226 | :type => :list, :values => [["<< #{l(:label_me)} >>", "me"]] |
|
227 | 227 | end |
|
228 | 228 | |
|
229 | 229 | if subprojects.any? |
|
230 | 230 | add_available_filter "subproject_id", |
|
231 | 231 | :type => :list_subprojects, |
|
232 | 232 | :values => subprojects.collect{|s| [s.name, s.id.to_s] } |
|
233 | 233 | end |
|
234 | 234 | |
|
235 | 235 | add_custom_fields_filters(issue_custom_fields) |
|
236 | 236 | |
|
237 | 237 | add_associations_custom_fields_filters :project, :author, :assigned_to, :fixed_version |
|
238 | 238 | |
|
239 | 239 | IssueRelation::TYPES.each do |relation_type, options| |
|
240 | 240 | add_available_filter relation_type, :type => :relation, :label => options[:name] |
|
241 | 241 | end |
|
242 | 242 | |
|
243 | 243 | Tracker.disabled_core_fields(trackers).each {|field| |
|
244 | 244 | delete_available_filter field |
|
245 | 245 | } |
|
246 | 246 | end |
|
247 | 247 | |
|
248 | 248 | def available_columns |
|
249 | 249 | return @available_columns if @available_columns |
|
250 | 250 | @available_columns = self.class.available_columns.dup |
|
251 | 251 | @available_columns += (project ? |
|
252 | 252 | project.all_issue_custom_fields : |
|
253 | 253 | IssueCustomField |
|
254 | 254 | ).visible.collect {|cf| QueryCustomFieldColumn.new(cf) } |
|
255 | 255 | |
|
256 | 256 | if User.current.allowed_to?(:view_time_entries, project, :global => true) |
|
257 | 257 | index = nil |
|
258 | 258 | @available_columns.each_with_index {|column, i| index = i if column.name == :estimated_hours} |
|
259 | 259 | index = (index ? index + 1 : -1) |
|
260 | 260 | # insert the column after estimated_hours or at the end |
|
261 | 261 | @available_columns.insert index, QueryColumn.new(:spent_hours, |
|
262 | 262 | :sortable => "COALESCE((SELECT SUM(hours) FROM #{TimeEntry.table_name} WHERE #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id), 0)", |
|
263 | 263 | :default_order => 'desc', |
|
264 | 264 | :caption => :label_spent_time |
|
265 | 265 | ) |
|
266 | 266 | end |
|
267 | 267 | |
|
268 | 268 | if User.current.allowed_to?(:set_issues_private, nil, :global => true) || |
|
269 | 269 | User.current.allowed_to?(:set_own_issues_private, nil, :global => true) |
|
270 | 270 | @available_columns << QueryColumn.new(:is_private, :sortable => "#{Issue.table_name}.is_private") |
|
271 | 271 | end |
|
272 | 272 | |
|
273 | 273 | disabled_fields = Tracker.disabled_core_fields(trackers).map {|field| field.sub(/_id$/, '')} |
|
274 | 274 | @available_columns.reject! {|column| |
|
275 | 275 | disabled_fields.include?(column.name.to_s) |
|
276 | 276 | } |
|
277 | 277 | |
|
278 | 278 | @available_columns |
|
279 | 279 | end |
|
280 | 280 | |
|
281 | 281 | def default_columns_names |
|
282 | 282 | @default_columns_names ||= begin |
|
283 | 283 | default_columns = Setting.issue_list_default_columns.map(&:to_sym) |
|
284 | 284 | |
|
285 | 285 | project.present? ? default_columns : [:project] | default_columns |
|
286 | 286 | end |
|
287 | 287 | end |
|
288 | 288 | |
|
289 | 289 | # Returns the issue count |
|
290 | 290 | def issue_count |
|
291 | 291 | Issue.visible.joins(:status, :project).where(statement).count |
|
292 | 292 | rescue ::ActiveRecord::StatementInvalid => e |
|
293 | 293 | raise StatementInvalid.new(e.message) |
|
294 | 294 | end |
|
295 | 295 | |
|
296 | 296 | # Returns the issue count by group or nil if query is not grouped |
|
297 | 297 | def issue_count_by_group |
|
298 | 298 | r = nil |
|
299 | 299 | if grouped? |
|
300 | 300 | begin |
|
301 | 301 | # Rails3 will raise an (unexpected) RecordNotFound if there's only a nil group value |
|
302 | 302 | r = Issue.visible. |
|
303 | 303 | joins(:status, :project). |
|
304 | 304 | where(statement). |
|
305 | 305 | joins(joins_for_order_statement(group_by_statement)). |
|
306 | 306 | group(group_by_statement). |
|
307 | 307 | count |
|
308 | 308 | rescue ActiveRecord::RecordNotFound |
|
309 | 309 | r = {nil => issue_count} |
|
310 | 310 | end |
|
311 | 311 | c = group_by_column |
|
312 | 312 | if c.is_a?(QueryCustomFieldColumn) |
|
313 | 313 | r = r.keys.inject({}) {|h, k| h[c.custom_field.cast_value(k)] = r[k]; h} |
|
314 | 314 | end |
|
315 | 315 | end |
|
316 | 316 | r |
|
317 | 317 | rescue ::ActiveRecord::StatementInvalid => e |
|
318 | 318 | raise StatementInvalid.new(e.message) |
|
319 | 319 | end |
|
320 | 320 | |
|
321 | 321 | # Returns the issues |
|
322 | 322 | # Valid options are :order, :offset, :limit, :include, :conditions |
|
323 | 323 | def issues(options={}) |
|
324 | 324 | order_option = [group_by_sort_order, options[:order]].flatten.reject(&:blank?) |
|
325 | 325 | |
|
326 | 326 | scope = Issue.visible. |
|
327 | 327 | joins(:status, :project). |
|
328 | 328 | where(statement). |
|
329 | 329 | includes(([:status, :project] + (options[:include] || [])).uniq). |
|
330 | 330 | where(options[:conditions]). |
|
331 | 331 | order(order_option). |
|
332 | 332 | joins(joins_for_order_statement(order_option.join(','))). |
|
333 | 333 | limit(options[:limit]). |
|
334 | 334 | offset(options[:offset]) |
|
335 | 335 | |
|
336 | 336 | scope = scope.preload(:custom_values) |
|
337 | if has_column?(:author) | |
|
338 | scope = scope.preload(:author) | |
|
339 | end | |
|
337 | 340 | |
|
338 | 341 | issues = scope.all |
|
339 | 342 | |
|
340 | 343 | if has_column?(:spent_hours) |
|
341 | 344 | Issue.load_visible_spent_hours(issues) |
|
342 | 345 | end |
|
343 | 346 | if has_column?(:relations) |
|
344 | 347 | Issue.load_visible_relations(issues) |
|
345 | 348 | end |
|
346 | 349 | issues |
|
347 | 350 | rescue ::ActiveRecord::StatementInvalid => e |
|
348 | 351 | raise StatementInvalid.new(e.message) |
|
349 | 352 | end |
|
350 | 353 | |
|
351 | 354 | # Returns the issues ids |
|
352 | 355 | def issue_ids(options={}) |
|
353 | 356 | order_option = [group_by_sort_order, options[:order]].flatten.reject(&:blank?) |
|
354 | 357 | |
|
355 | 358 | Issue.visible. |
|
356 | 359 | joins(:status, :project). |
|
357 | 360 | where(statement). |
|
358 | 361 | includes(([:status, :project] + (options[:include] || [])).uniq). |
|
359 | 362 | where(options[:conditions]). |
|
360 | 363 | order(order_option). |
|
361 | 364 | joins(joins_for_order_statement(order_option.join(','))). |
|
362 | 365 | limit(options[:limit]). |
|
363 | 366 | offset(options[:offset]). |
|
364 | 367 | find_ids |
|
365 | 368 | rescue ::ActiveRecord::StatementInvalid => e |
|
366 | 369 | raise StatementInvalid.new(e.message) |
|
367 | 370 | end |
|
368 | 371 | |
|
369 | 372 | # Returns the journals |
|
370 | 373 | # Valid options are :order, :offset, :limit |
|
371 | 374 | def journals(options={}) |
|
372 | 375 | Journal.visible. |
|
373 | 376 | joins(:issue => [:project, :status]). |
|
374 | 377 | where(statement). |
|
375 | 378 | order(options[:order]). |
|
376 | 379 | limit(options[:limit]). |
|
377 | 380 | offset(options[:offset]). |
|
378 | 381 | preload(:details, :user, {:issue => [:project, :author, :tracker, :status]}). |
|
379 | 382 | all |
|
380 | 383 | rescue ::ActiveRecord::StatementInvalid => e |
|
381 | 384 | raise StatementInvalid.new(e.message) |
|
382 | 385 | end |
|
383 | 386 | |
|
384 | 387 | # Returns the versions |
|
385 | 388 | # Valid options are :conditions |
|
386 | 389 | def versions(options={}) |
|
387 | 390 | Version.visible. |
|
388 | 391 | where(project_statement). |
|
389 | 392 | where(options[:conditions]). |
|
390 | 393 | includes(:project). |
|
391 | 394 | all |
|
392 | 395 | rescue ::ActiveRecord::StatementInvalid => e |
|
393 | 396 | raise StatementInvalid.new(e.message) |
|
394 | 397 | end |
|
395 | 398 | |
|
396 | 399 | def sql_for_watcher_id_field(field, operator, value) |
|
397 | 400 | db_table = Watcher.table_name |
|
398 | 401 | "#{Issue.table_name}.id #{ operator == '=' ? 'IN' : 'NOT IN' } (SELECT #{db_table}.watchable_id FROM #{db_table} WHERE #{db_table}.watchable_type='Issue' AND " + |
|
399 | 402 | sql_for_field(field, '=', value, db_table, 'user_id') + ')' |
|
400 | 403 | end |
|
401 | 404 | |
|
402 | 405 | def sql_for_member_of_group_field(field, operator, value) |
|
403 | 406 | if operator == '*' # Any group |
|
404 | 407 | groups = Group.all |
|
405 | 408 | operator = '=' # Override the operator since we want to find by assigned_to |
|
406 | 409 | elsif operator == "!*" |
|
407 | 410 | groups = Group.all |
|
408 | 411 | operator = '!' # Override the operator since we want to find by assigned_to |
|
409 | 412 | else |
|
410 | 413 | groups = Group.where(:id => value).all |
|
411 | 414 | end |
|
412 | 415 | groups ||= [] |
|
413 | 416 | |
|
414 | 417 | members_of_groups = groups.inject([]) {|user_ids, group| |
|
415 | 418 | user_ids + group.user_ids + [group.id] |
|
416 | 419 | }.uniq.compact.sort.collect(&:to_s) |
|
417 | 420 | |
|
418 | 421 | '(' + sql_for_field("assigned_to_id", operator, members_of_groups, Issue.table_name, "assigned_to_id", false) + ')' |
|
419 | 422 | end |
|
420 | 423 | |
|
421 | 424 | def sql_for_assigned_to_role_field(field, operator, value) |
|
422 | 425 | case operator |
|
423 | 426 | when "*", "!*" # Member / Not member |
|
424 | 427 | sw = operator == "!*" ? 'NOT' : '' |
|
425 | 428 | nl = operator == "!*" ? "#{Issue.table_name}.assigned_to_id IS NULL OR" : '' |
|
426 | 429 | "(#{nl} #{Issue.table_name}.assigned_to_id #{sw} IN (SELECT DISTINCT #{Member.table_name}.user_id FROM #{Member.table_name}" + |
|
427 | 430 | " WHERE #{Member.table_name}.project_id = #{Issue.table_name}.project_id))" |
|
428 | 431 | when "=", "!" |
|
429 | 432 | role_cond = value.any? ? |
|
430 | 433 | "#{MemberRole.table_name}.role_id IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")" : |
|
431 | 434 | "1=0" |
|
432 | 435 | |
|
433 | 436 | sw = operator == "!" ? 'NOT' : '' |
|
434 | 437 | nl = operator == "!" ? "#{Issue.table_name}.assigned_to_id IS NULL OR" : '' |
|
435 | 438 | "(#{nl} #{Issue.table_name}.assigned_to_id #{sw} IN (SELECT DISTINCT #{Member.table_name}.user_id FROM #{Member.table_name}, #{MemberRole.table_name}" + |
|
436 | 439 | " WHERE #{Member.table_name}.project_id = #{Issue.table_name}.project_id AND #{Member.table_name}.id = #{MemberRole.table_name}.member_id AND #{role_cond}))" |
|
437 | 440 | end |
|
438 | 441 | end |
|
439 | 442 | |
|
440 | 443 | def sql_for_is_private_field(field, operator, value) |
|
441 | 444 | op = (operator == "=" ? 'IN' : 'NOT IN') |
|
442 | 445 | va = value.map {|v| v == '0' ? connection.quoted_false : connection.quoted_true}.uniq.join(',') |
|
443 | 446 | |
|
444 | 447 | "#{Issue.table_name}.is_private #{op} (#{va})" |
|
445 | 448 | end |
|
446 | 449 | |
|
447 | 450 | def sql_for_relations(field, operator, value, options={}) |
|
448 | 451 | relation_options = IssueRelation::TYPES[field] |
|
449 | 452 | return relation_options unless relation_options |
|
450 | 453 | |
|
451 | 454 | relation_type = field |
|
452 | 455 | join_column, target_join_column = "issue_from_id", "issue_to_id" |
|
453 | 456 | if relation_options[:reverse] || options[:reverse] |
|
454 | 457 | relation_type = relation_options[:reverse] || relation_type |
|
455 | 458 | join_column, target_join_column = target_join_column, join_column |
|
456 | 459 | end |
|
457 | 460 | |
|
458 | 461 | sql = case operator |
|
459 | 462 | when "*", "!*" |
|
460 | 463 | op = (operator == "*" ? 'IN' : 'NOT IN') |
|
461 | 464 | "#{Issue.table_name}.id #{op} (SELECT DISTINCT #{IssueRelation.table_name}.#{join_column} FROM #{IssueRelation.table_name} WHERE #{IssueRelation.table_name}.relation_type = '#{connection.quote_string(relation_type)}')" |
|
462 | 465 | when "=", "!" |
|
463 | 466 | op = (operator == "=" ? 'IN' : 'NOT IN') |
|
464 | 467 | "#{Issue.table_name}.id #{op} (SELECT DISTINCT #{IssueRelation.table_name}.#{join_column} FROM #{IssueRelation.table_name} WHERE #{IssueRelation.table_name}.relation_type = '#{connection.quote_string(relation_type)}' AND #{IssueRelation.table_name}.#{target_join_column} = #{value.first.to_i})" |
|
465 | 468 | when "=p", "=!p", "!p" |
|
466 | 469 | op = (operator == "!p" ? 'NOT IN' : 'IN') |
|
467 | 470 | comp = (operator == "=!p" ? '<>' : '=') |
|
468 | 471 | "#{Issue.table_name}.id #{op} (SELECT DISTINCT #{IssueRelation.table_name}.#{join_column} FROM #{IssueRelation.table_name}, #{Issue.table_name} relissues WHERE #{IssueRelation.table_name}.relation_type = '#{connection.quote_string(relation_type)}' AND #{IssueRelation.table_name}.#{target_join_column} = relissues.id AND relissues.project_id #{comp} #{value.first.to_i})" |
|
469 | 472 | end |
|
470 | 473 | |
|
471 | 474 | if relation_options[:sym] == field && !options[:reverse] |
|
472 | 475 | sqls = [sql, sql_for_relations(field, operator, value, :reverse => true)] |
|
473 | 476 | sql = sqls.join(["!", "!*", "!p"].include?(operator) ? " AND " : " OR ") |
|
474 | 477 | end |
|
475 | 478 | "(#{sql})" |
|
476 | 479 | end |
|
477 | 480 | |
|
478 | 481 | IssueRelation::TYPES.keys.each do |relation_type| |
|
479 | 482 | alias_method "sql_for_#{relation_type}_field".to_sym, :sql_for_relations |
|
480 | 483 | end |
|
481 | 484 | end |
General Comments 0
You need to be logged in to leave comments.
Login now