The requested changes are too big and content was truncated. Show full diff
@@ -1,442 +1,443 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2012 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2012 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software |
|
15 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | class IssuesController < ApplicationController |
|
18 | class IssuesController < ApplicationController | |
19 | menu_item :new_issue, :only => [:new, :create] |
|
19 | menu_item :new_issue, :only => [:new, :create] | |
20 | default_search_scope :issues |
|
20 | default_search_scope :issues | |
21 |
|
21 | |||
22 | before_filter :find_issue, :only => [:show, :edit, :update] |
|
22 | before_filter :find_issue, :only => [:show, :edit, :update] | |
23 | before_filter :find_issues, :only => [:bulk_edit, :bulk_update, :destroy] |
|
23 | before_filter :find_issues, :only => [:bulk_edit, :bulk_update, :destroy] | |
24 | before_filter :find_project, :only => [:new, :create] |
|
24 | before_filter :find_project, :only => [:new, :create] | |
25 | before_filter :authorize, :except => [:index] |
|
25 | before_filter :authorize, :except => [:index] | |
26 | before_filter :find_optional_project, :only => [:index] |
|
26 | before_filter :find_optional_project, :only => [:index] | |
27 | before_filter :check_for_default_issue_status, :only => [:new, :create] |
|
27 | before_filter :check_for_default_issue_status, :only => [:new, :create] | |
28 | before_filter :build_new_issue_from_params, :only => [:new, :create] |
|
28 | before_filter :build_new_issue_from_params, :only => [:new, :create] | |
29 | accept_rss_auth :index, :show |
|
29 | accept_rss_auth :index, :show | |
30 | accept_api_auth :index, :show, :create, :update, :destroy |
|
30 | accept_api_auth :index, :show, :create, :update, :destroy | |
31 |
|
31 | |||
32 | rescue_from Query::StatementInvalid, :with => :query_statement_invalid |
|
32 | rescue_from Query::StatementInvalid, :with => :query_statement_invalid | |
33 |
|
33 | |||
34 | helper :journals |
|
34 | helper :journals | |
35 | helper :projects |
|
35 | helper :projects | |
36 | include ProjectsHelper |
|
36 | include ProjectsHelper | |
37 | helper :custom_fields |
|
37 | helper :custom_fields | |
38 | include CustomFieldsHelper |
|
38 | include CustomFieldsHelper | |
39 | helper :issue_relations |
|
39 | helper :issue_relations | |
40 | include IssueRelationsHelper |
|
40 | include IssueRelationsHelper | |
41 | helper :watchers |
|
41 | helper :watchers | |
42 | include WatchersHelper |
|
42 | include WatchersHelper | |
43 | helper :attachments |
|
43 | helper :attachments | |
44 | include AttachmentsHelper |
|
44 | include AttachmentsHelper | |
45 | helper :queries |
|
45 | helper :queries | |
46 | include QueriesHelper |
|
46 | include QueriesHelper | |
47 | helper :repositories |
|
47 | helper :repositories | |
48 | include RepositoriesHelper |
|
48 | include RepositoriesHelper | |
49 | helper :sort |
|
49 | helper :sort | |
50 | include SortHelper |
|
50 | include SortHelper | |
51 | include IssuesHelper |
|
51 | include IssuesHelper | |
52 | helper :timelog |
|
52 | helper :timelog | |
53 | include Redmine::Export::PDF |
|
53 | include Redmine::Export::PDF | |
54 |
|
54 | |||
55 | def index |
|
55 | def index | |
56 | retrieve_query |
|
56 | retrieve_query | |
57 | sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria) |
|
57 | sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria) | |
58 | sort_update(@query.sortable_columns) |
|
58 | sort_update(@query.sortable_columns) | |
|
59 | @query.sort_criteria = sort_criteria.to_a | |||
59 |
|
60 | |||
60 | if @query.valid? |
|
61 | if @query.valid? | |
61 | case params[:format] |
|
62 | case params[:format] | |
62 | when 'csv', 'pdf' |
|
63 | when 'csv', 'pdf' | |
63 | @limit = Setting.issues_export_limit.to_i |
|
64 | @limit = Setting.issues_export_limit.to_i | |
64 | when 'atom' |
|
65 | when 'atom' | |
65 | @limit = Setting.feeds_limit.to_i |
|
66 | @limit = Setting.feeds_limit.to_i | |
66 | when 'xml', 'json' |
|
67 | when 'xml', 'json' | |
67 | @offset, @limit = api_offset_and_limit |
|
68 | @offset, @limit = api_offset_and_limit | |
68 | else |
|
69 | else | |
69 | @limit = per_page_option |
|
70 | @limit = per_page_option | |
70 | end |
|
71 | end | |
71 |
|
72 | |||
72 | @issue_count = @query.issue_count |
|
73 | @issue_count = @query.issue_count | |
73 | @issue_pages = Paginator.new self, @issue_count, @limit, params['page'] |
|
74 | @issue_pages = Paginator.new self, @issue_count, @limit, params['page'] | |
74 | @offset ||= @issue_pages.current.offset |
|
75 | @offset ||= @issue_pages.current.offset | |
75 | @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version], |
|
76 | @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version], | |
76 | :order => sort_clause, |
|
77 | :order => sort_clause, | |
77 | :offset => @offset, |
|
78 | :offset => @offset, | |
78 | :limit => @limit) |
|
79 | :limit => @limit) | |
79 | @issue_count_by_group = @query.issue_count_by_group |
|
80 | @issue_count_by_group = @query.issue_count_by_group | |
80 |
|
81 | |||
81 | respond_to do |format| |
|
82 | respond_to do |format| | |
82 | format.html { render :template => 'issues/index', :layout => !request.xhr? } |
|
83 | format.html { render :template => 'issues/index', :layout => !request.xhr? } | |
83 | format.api { |
|
84 | format.api { | |
84 | Issue.load_visible_relations(@issues) if include_in_api_response?('relations') |
|
85 | Issue.load_visible_relations(@issues) if include_in_api_response?('relations') | |
85 | } |
|
86 | } | |
86 | format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") } |
|
87 | format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") } | |
87 | format.csv { send_data(issues_to_csv(@issues, @project, @query, params), :type => 'text/csv; header=present', :filename => 'export.csv') } |
|
88 | format.csv { send_data(issues_to_csv(@issues, @project, @query, params), :type => 'text/csv; header=present', :filename => 'export.csv') } | |
88 | format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'export.pdf') } |
|
89 | format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'export.pdf') } | |
89 | end |
|
90 | end | |
90 | else |
|
91 | else | |
91 | respond_to do |format| |
|
92 | respond_to do |format| | |
92 | format.html { render(:template => 'issues/index', :layout => !request.xhr?) } |
|
93 | format.html { render(:template => 'issues/index', :layout => !request.xhr?) } | |
93 | format.any(:atom, :csv, :pdf) { render(:nothing => true) } |
|
94 | format.any(:atom, :csv, :pdf) { render(:nothing => true) } | |
94 | format.api { render_validation_errors(@query) } |
|
95 | format.api { render_validation_errors(@query) } | |
95 | end |
|
96 | end | |
96 | end |
|
97 | end | |
97 | rescue ActiveRecord::RecordNotFound |
|
98 | rescue ActiveRecord::RecordNotFound | |
98 | render_404 |
|
99 | render_404 | |
99 | end |
|
100 | end | |
100 |
|
101 | |||
101 | def show |
|
102 | def show | |
102 | @journals = @issue.journals.includes(:user, :details).reorder("#{Journal.table_name}.id ASC").all |
|
103 | @journals = @issue.journals.includes(:user, :details).reorder("#{Journal.table_name}.id ASC").all | |
103 | @journals.each_with_index {|j,i| j.indice = i+1} |
|
104 | @journals.each_with_index {|j,i| j.indice = i+1} | |
104 | @journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project) |
|
105 | @journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project) | |
105 | @journals.reverse! if User.current.wants_comments_in_reverse_order? |
|
106 | @journals.reverse! if User.current.wants_comments_in_reverse_order? | |
106 |
|
107 | |||
107 | @changesets = @issue.changesets.visible.all |
|
108 | @changesets = @issue.changesets.visible.all | |
108 | @changesets.reverse! if User.current.wants_comments_in_reverse_order? |
|
109 | @changesets.reverse! if User.current.wants_comments_in_reverse_order? | |
109 |
|
110 | |||
110 | @relations = @issue.relations.select {|r| r.other_issue(@issue) && r.other_issue(@issue).visible? } |
|
111 | @relations = @issue.relations.select {|r| r.other_issue(@issue) && r.other_issue(@issue).visible? } | |
111 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current) |
|
112 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current) | |
112 | @edit_allowed = User.current.allowed_to?(:edit_issues, @project) |
|
113 | @edit_allowed = User.current.allowed_to?(:edit_issues, @project) | |
113 | @priorities = IssuePriority.active |
|
114 | @priorities = IssuePriority.active | |
114 | @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project) |
|
115 | @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project) | |
115 | respond_to do |format| |
|
116 | respond_to do |format| | |
116 | format.html { |
|
117 | format.html { | |
117 | retrieve_previous_and_next_issue_ids |
|
118 | retrieve_previous_and_next_issue_ids | |
118 | render :template => 'issues/show' |
|
119 | render :template => 'issues/show' | |
119 | } |
|
120 | } | |
120 | format.api |
|
121 | format.api | |
121 | format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' } |
|
122 | format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' } | |
122 | format.pdf { |
|
123 | format.pdf { | |
123 | pdf = issue_to_pdf(@issue, :journals => @journals) |
|
124 | pdf = issue_to_pdf(@issue, :journals => @journals) | |
124 | send_data(pdf, :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") |
|
125 | send_data(pdf, :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") | |
125 | } |
|
126 | } | |
126 | end |
|
127 | end | |
127 | end |
|
128 | end | |
128 |
|
129 | |||
129 | # Add a new issue |
|
130 | # Add a new issue | |
130 | # The new issue will be created from an existing one if copy_from parameter is given |
|
131 | # The new issue will be created from an existing one if copy_from parameter is given | |
131 | def new |
|
132 | def new | |
132 | respond_to do |format| |
|
133 | respond_to do |format| | |
133 | format.html { render :action => 'new', :layout => !request.xhr? } |
|
134 | format.html { render :action => 'new', :layout => !request.xhr? } | |
134 | format.js { render :partial => 'update_form' } |
|
135 | format.js { render :partial => 'update_form' } | |
135 | end |
|
136 | end | |
136 | end |
|
137 | end | |
137 |
|
138 | |||
138 | def create |
|
139 | def create | |
139 | call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue }) |
|
140 | call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue }) | |
140 | @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads])) |
|
141 | @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads])) | |
141 | if @issue.save |
|
142 | if @issue.save | |
142 | call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue}) |
|
143 | call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue}) | |
143 | respond_to do |format| |
|
144 | respond_to do |format| | |
144 | format.html { |
|
145 | format.html { | |
145 | render_attachment_warning_if_needed(@issue) |
|
146 | render_attachment_warning_if_needed(@issue) | |
146 | flash[:notice] = l(:notice_issue_successful_create, :id => view_context.link_to("##{@issue.id}", issue_path(@issue), :title => @issue.subject)) |
|
147 | flash[:notice] = l(:notice_issue_successful_create, :id => view_context.link_to("##{@issue.id}", issue_path(@issue), :title => @issue.subject)) | |
147 | redirect_to(params[:continue] ? { :action => 'new', :project_id => @issue.project, :issue => {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?} } : |
|
148 | redirect_to(params[:continue] ? { :action => 'new', :project_id => @issue.project, :issue => {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?} } : | |
148 | { :action => 'show', :id => @issue }) |
|
149 | { :action => 'show', :id => @issue }) | |
149 | } |
|
150 | } | |
150 | format.api { render :action => 'show', :status => :created, :location => issue_url(@issue) } |
|
151 | format.api { render :action => 'show', :status => :created, :location => issue_url(@issue) } | |
151 | end |
|
152 | end | |
152 | return |
|
153 | return | |
153 | else |
|
154 | else | |
154 | respond_to do |format| |
|
155 | respond_to do |format| | |
155 | format.html { render :action => 'new' } |
|
156 | format.html { render :action => 'new' } | |
156 | format.api { render_validation_errors(@issue) } |
|
157 | format.api { render_validation_errors(@issue) } | |
157 | end |
|
158 | end | |
158 | end |
|
159 | end | |
159 | end |
|
160 | end | |
160 |
|
161 | |||
161 | def edit |
|
162 | def edit | |
162 | return unless update_issue_from_params |
|
163 | return unless update_issue_from_params | |
163 |
|
164 | |||
164 | respond_to do |format| |
|
165 | respond_to do |format| | |
165 | format.html { } |
|
166 | format.html { } | |
166 | format.xml { } |
|
167 | format.xml { } | |
167 | end |
|
168 | end | |
168 | end |
|
169 | end | |
169 |
|
170 | |||
170 | def update |
|
171 | def update | |
171 | return unless update_issue_from_params |
|
172 | return unless update_issue_from_params | |
172 | @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads])) |
|
173 | @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads])) | |
173 | saved = false |
|
174 | saved = false | |
174 | begin |
|
175 | begin | |
175 | saved = @issue.save_issue_with_child_records(params, @time_entry) |
|
176 | saved = @issue.save_issue_with_child_records(params, @time_entry) | |
176 | rescue ActiveRecord::StaleObjectError |
|
177 | rescue ActiveRecord::StaleObjectError | |
177 | @conflict = true |
|
178 | @conflict = true | |
178 | if params[:last_journal_id] |
|
179 | if params[:last_journal_id] | |
179 | @conflict_journals = @issue.journals_after(params[:last_journal_id]).all |
|
180 | @conflict_journals = @issue.journals_after(params[:last_journal_id]).all | |
180 | @conflict_journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project) |
|
181 | @conflict_journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project) | |
181 | end |
|
182 | end | |
182 | end |
|
183 | end | |
183 |
|
184 | |||
184 | if saved |
|
185 | if saved | |
185 | render_attachment_warning_if_needed(@issue) |
|
186 | render_attachment_warning_if_needed(@issue) | |
186 | flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record? |
|
187 | flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record? | |
187 |
|
188 | |||
188 | respond_to do |format| |
|
189 | respond_to do |format| | |
189 | format.html { redirect_back_or_default({:action => 'show', :id => @issue}) } |
|
190 | format.html { redirect_back_or_default({:action => 'show', :id => @issue}) } | |
190 | format.api { render_api_ok } |
|
191 | format.api { render_api_ok } | |
191 | end |
|
192 | end | |
192 | else |
|
193 | else | |
193 | respond_to do |format| |
|
194 | respond_to do |format| | |
194 | format.html { render :action => 'edit' } |
|
195 | format.html { render :action => 'edit' } | |
195 | format.api { render_validation_errors(@issue) } |
|
196 | format.api { render_validation_errors(@issue) } | |
196 | end |
|
197 | end | |
197 | end |
|
198 | end | |
198 | end |
|
199 | end | |
199 |
|
200 | |||
200 | # Bulk edit/copy a set of issues |
|
201 | # Bulk edit/copy a set of issues | |
201 | def bulk_edit |
|
202 | def bulk_edit | |
202 | @issues.sort! |
|
203 | @issues.sort! | |
203 | @copy = params[:copy].present? |
|
204 | @copy = params[:copy].present? | |
204 | @notes = params[:notes] |
|
205 | @notes = params[:notes] | |
205 |
|
206 | |||
206 | if User.current.allowed_to?(:move_issues, @projects) |
|
207 | if User.current.allowed_to?(:move_issues, @projects) | |
207 | @allowed_projects = Issue.allowed_target_projects_on_move |
|
208 | @allowed_projects = Issue.allowed_target_projects_on_move | |
208 | if params[:issue] |
|
209 | if params[:issue] | |
209 | @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:issue][:project_id].to_s} |
|
210 | @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:issue][:project_id].to_s} | |
210 | if @target_project |
|
211 | if @target_project | |
211 | target_projects = [@target_project] |
|
212 | target_projects = [@target_project] | |
212 | end |
|
213 | end | |
213 | end |
|
214 | end | |
214 | end |
|
215 | end | |
215 | target_projects ||= @projects |
|
216 | target_projects ||= @projects | |
216 |
|
217 | |||
217 | if @copy |
|
218 | if @copy | |
218 | @available_statuses = [IssueStatus.default] |
|
219 | @available_statuses = [IssueStatus.default] | |
219 | else |
|
220 | else | |
220 | @available_statuses = @issues.map(&:new_statuses_allowed_to).reduce(:&) |
|
221 | @available_statuses = @issues.map(&:new_statuses_allowed_to).reduce(:&) | |
221 | end |
|
222 | end | |
222 | @custom_fields = target_projects.map{|p|p.all_issue_custom_fields}.reduce(:&) |
|
223 | @custom_fields = target_projects.map{|p|p.all_issue_custom_fields}.reduce(:&) | |
223 | @assignables = target_projects.map(&:assignable_users).reduce(:&) |
|
224 | @assignables = target_projects.map(&:assignable_users).reduce(:&) | |
224 | @trackers = target_projects.map(&:trackers).reduce(:&) |
|
225 | @trackers = target_projects.map(&:trackers).reduce(:&) | |
225 | @versions = target_projects.map {|p| p.shared_versions.open}.reduce(:&) |
|
226 | @versions = target_projects.map {|p| p.shared_versions.open}.reduce(:&) | |
226 | @categories = target_projects.map {|p| p.issue_categories}.reduce(:&) |
|
227 | @categories = target_projects.map {|p| p.issue_categories}.reduce(:&) | |
227 | if @copy |
|
228 | if @copy | |
228 | @attachments_present = @issues.detect {|i| i.attachments.any?}.present? |
|
229 | @attachments_present = @issues.detect {|i| i.attachments.any?}.present? | |
229 | @subtasks_present = @issues.detect {|i| !i.leaf?}.present? |
|
230 | @subtasks_present = @issues.detect {|i| !i.leaf?}.present? | |
230 | end |
|
231 | end | |
231 |
|
232 | |||
232 | @safe_attributes = @issues.map(&:safe_attribute_names).reduce(:&) |
|
233 | @safe_attributes = @issues.map(&:safe_attribute_names).reduce(:&) | |
233 | render :layout => false if request.xhr? |
|
234 | render :layout => false if request.xhr? | |
234 | end |
|
235 | end | |
235 |
|
236 | |||
236 | def bulk_update |
|
237 | def bulk_update | |
237 | @issues.sort! |
|
238 | @issues.sort! | |
238 | @copy = params[:copy].present? |
|
239 | @copy = params[:copy].present? | |
239 | attributes = parse_params_for_bulk_issue_attributes(params) |
|
240 | attributes = parse_params_for_bulk_issue_attributes(params) | |
240 |
|
241 | |||
241 | unsaved_issue_ids = [] |
|
242 | unsaved_issue_ids = [] | |
242 | moved_issues = [] |
|
243 | moved_issues = [] | |
243 |
|
244 | |||
244 | if @copy && params[:copy_subtasks].present? |
|
245 | if @copy && params[:copy_subtasks].present? | |
245 | # Descendant issues will be copied with the parent task |
|
246 | # Descendant issues will be copied with the parent task | |
246 | # Don't copy them twice |
|
247 | # Don't copy them twice | |
247 | @issues.reject! {|issue| @issues.detect {|other| issue.is_descendant_of?(other)}} |
|
248 | @issues.reject! {|issue| @issues.detect {|other| issue.is_descendant_of?(other)}} | |
248 | end |
|
249 | end | |
249 |
|
250 | |||
250 | @issues.each do |issue| |
|
251 | @issues.each do |issue| | |
251 | issue.reload |
|
252 | issue.reload | |
252 | if @copy |
|
253 | if @copy | |
253 | issue = issue.copy({}, |
|
254 | issue = issue.copy({}, | |
254 | :attachments => params[:copy_attachments].present?, |
|
255 | :attachments => params[:copy_attachments].present?, | |
255 | :subtasks => params[:copy_subtasks].present? |
|
256 | :subtasks => params[:copy_subtasks].present? | |
256 | ) |
|
257 | ) | |
257 | end |
|
258 | end | |
258 | journal = issue.init_journal(User.current, params[:notes]) |
|
259 | journal = issue.init_journal(User.current, params[:notes]) | |
259 | issue.safe_attributes = attributes |
|
260 | issue.safe_attributes = attributes | |
260 | call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue }) |
|
261 | call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue }) | |
261 | if issue.save |
|
262 | if issue.save | |
262 | moved_issues << issue |
|
263 | moved_issues << issue | |
263 | else |
|
264 | else | |
264 | # Keep unsaved issue ids to display them in flash error |
|
265 | # Keep unsaved issue ids to display them in flash error | |
265 | unsaved_issue_ids << issue.id |
|
266 | unsaved_issue_ids << issue.id | |
266 | end |
|
267 | end | |
267 | end |
|
268 | end | |
268 | set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids) |
|
269 | set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids) | |
269 |
|
270 | |||
270 | if params[:follow] |
|
271 | if params[:follow] | |
271 | if @issues.size == 1 && moved_issues.size == 1 |
|
272 | if @issues.size == 1 && moved_issues.size == 1 | |
272 | redirect_to :controller => 'issues', :action => 'show', :id => moved_issues.first |
|
273 | redirect_to :controller => 'issues', :action => 'show', :id => moved_issues.first | |
273 | elsif moved_issues.map(&:project).uniq.size == 1 |
|
274 | elsif moved_issues.map(&:project).uniq.size == 1 | |
274 | redirect_to :controller => 'issues', :action => 'index', :project_id => moved_issues.map(&:project).first |
|
275 | redirect_to :controller => 'issues', :action => 'index', :project_id => moved_issues.map(&:project).first | |
275 | end |
|
276 | end | |
276 | else |
|
277 | else | |
277 | redirect_back_or_default({:controller => 'issues', :action => 'index', :project_id => @project}) |
|
278 | redirect_back_or_default({:controller => 'issues', :action => 'index', :project_id => @project}) | |
278 | end |
|
279 | end | |
279 | end |
|
280 | end | |
280 |
|
281 | |||
281 | def destroy |
|
282 | def destroy | |
282 | @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f |
|
283 | @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f | |
283 | if @hours > 0 |
|
284 | if @hours > 0 | |
284 | case params[:todo] |
|
285 | case params[:todo] | |
285 | when 'destroy' |
|
286 | when 'destroy' | |
286 | # nothing to do |
|
287 | # nothing to do | |
287 | when 'nullify' |
|
288 | when 'nullify' | |
288 | TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues]) |
|
289 | TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues]) | |
289 | when 'reassign' |
|
290 | when 'reassign' | |
290 | reassign_to = @project.issues.find_by_id(params[:reassign_to_id]) |
|
291 | reassign_to = @project.issues.find_by_id(params[:reassign_to_id]) | |
291 | if reassign_to.nil? |
|
292 | if reassign_to.nil? | |
292 | flash.now[:error] = l(:error_issue_not_found_in_project) |
|
293 | flash.now[:error] = l(:error_issue_not_found_in_project) | |
293 | return |
|
294 | return | |
294 | else |
|
295 | else | |
295 | TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues]) |
|
296 | TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues]) | |
296 | end |
|
297 | end | |
297 | else |
|
298 | else | |
298 | # display the destroy form if it's a user request |
|
299 | # display the destroy form if it's a user request | |
299 | return unless api_request? |
|
300 | return unless api_request? | |
300 | end |
|
301 | end | |
301 | end |
|
302 | end | |
302 | @issues.each do |issue| |
|
303 | @issues.each do |issue| | |
303 | begin |
|
304 | begin | |
304 | issue.reload.destroy |
|
305 | issue.reload.destroy | |
305 | rescue ::ActiveRecord::RecordNotFound # raised by #reload if issue no longer exists |
|
306 | rescue ::ActiveRecord::RecordNotFound # raised by #reload if issue no longer exists | |
306 | # nothing to do, issue was already deleted (eg. by a parent) |
|
307 | # nothing to do, issue was already deleted (eg. by a parent) | |
307 | end |
|
308 | end | |
308 | end |
|
309 | end | |
309 | respond_to do |format| |
|
310 | respond_to do |format| | |
310 | format.html { redirect_back_or_default(:action => 'index', :project_id => @project) } |
|
311 | format.html { redirect_back_or_default(:action => 'index', :project_id => @project) } | |
311 | format.api { render_api_ok } |
|
312 | format.api { render_api_ok } | |
312 | end |
|
313 | end | |
313 | end |
|
314 | end | |
314 |
|
315 | |||
315 | private |
|
316 | private | |
316 | def find_issue |
|
317 | def find_issue | |
317 | # Issue.visible.find(...) can not be used to redirect user to the login form |
|
318 | # Issue.visible.find(...) can not be used to redirect user to the login form | |
318 | # if the issue actually exists but requires authentication |
|
319 | # if the issue actually exists but requires authentication | |
319 | @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category]) |
|
320 | @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category]) | |
320 | unless @issue.visible? |
|
321 | unless @issue.visible? | |
321 | deny_access |
|
322 | deny_access | |
322 | return |
|
323 | return | |
323 | end |
|
324 | end | |
324 | @project = @issue.project |
|
325 | @project = @issue.project | |
325 | rescue ActiveRecord::RecordNotFound |
|
326 | rescue ActiveRecord::RecordNotFound | |
326 | render_404 |
|
327 | render_404 | |
327 | end |
|
328 | end | |
328 |
|
329 | |||
329 | def find_project |
|
330 | def find_project | |
330 | project_id = params[:project_id] || (params[:issue] && params[:issue][:project_id]) |
|
331 | project_id = params[:project_id] || (params[:issue] && params[:issue][:project_id]) | |
331 | @project = Project.find(project_id) |
|
332 | @project = Project.find(project_id) | |
332 | rescue ActiveRecord::RecordNotFound |
|
333 | rescue ActiveRecord::RecordNotFound | |
333 | render_404 |
|
334 | render_404 | |
334 | end |
|
335 | end | |
335 |
|
336 | |||
336 | def retrieve_previous_and_next_issue_ids |
|
337 | def retrieve_previous_and_next_issue_ids | |
337 | retrieve_query_from_session |
|
338 | retrieve_query_from_session | |
338 | if @query |
|
339 | if @query | |
339 | sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria) |
|
340 | sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria) | |
340 | sort_update(@query.sortable_columns, 'issues_index_sort') |
|
341 | sort_update(@query.sortable_columns, 'issues_index_sort') | |
341 | limit = 500 |
|
342 | limit = 500 | |
342 | issue_ids = @query.issue_ids(:order => sort_clause, :limit => (limit + 1), :include => [:assigned_to, :tracker, :priority, :category, :fixed_version]) |
|
343 | issue_ids = @query.issue_ids(:order => sort_clause, :limit => (limit + 1), :include => [:assigned_to, :tracker, :priority, :category, :fixed_version]) | |
343 | if (idx = issue_ids.index(@issue.id)) && idx < limit |
|
344 | if (idx = issue_ids.index(@issue.id)) && idx < limit | |
344 | if issue_ids.size < 500 |
|
345 | if issue_ids.size < 500 | |
345 | @issue_position = idx + 1 |
|
346 | @issue_position = idx + 1 | |
346 | @issue_count = issue_ids.size |
|
347 | @issue_count = issue_ids.size | |
347 | end |
|
348 | end | |
348 | @prev_issue_id = issue_ids[idx - 1] if idx > 0 |
|
349 | @prev_issue_id = issue_ids[idx - 1] if idx > 0 | |
349 | @next_issue_id = issue_ids[idx + 1] if idx < (issue_ids.size - 1) |
|
350 | @next_issue_id = issue_ids[idx + 1] if idx < (issue_ids.size - 1) | |
350 | end |
|
351 | end | |
351 | end |
|
352 | end | |
352 | end |
|
353 | end | |
353 |
|
354 | |||
354 | # Used by #edit and #update to set some common instance variables |
|
355 | # Used by #edit and #update to set some common instance variables | |
355 | # from the params |
|
356 | # from the params | |
356 | # TODO: Refactor, not everything in here is needed by #edit |
|
357 | # TODO: Refactor, not everything in here is needed by #edit | |
357 | def update_issue_from_params |
|
358 | def update_issue_from_params | |
358 | @edit_allowed = User.current.allowed_to?(:edit_issues, @project) |
|
359 | @edit_allowed = User.current.allowed_to?(:edit_issues, @project) | |
359 | @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project) |
|
360 | @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project) | |
360 | @time_entry.attributes = params[:time_entry] |
|
361 | @time_entry.attributes = params[:time_entry] | |
361 |
|
362 | |||
362 | @issue.init_journal(User.current) |
|
363 | @issue.init_journal(User.current) | |
363 |
|
364 | |||
364 | issue_attributes = params[:issue] |
|
365 | issue_attributes = params[:issue] | |
365 | if issue_attributes && params[:conflict_resolution] |
|
366 | if issue_attributes && params[:conflict_resolution] | |
366 | case params[:conflict_resolution] |
|
367 | case params[:conflict_resolution] | |
367 | when 'overwrite' |
|
368 | when 'overwrite' | |
368 | issue_attributes = issue_attributes.dup |
|
369 | issue_attributes = issue_attributes.dup | |
369 | issue_attributes.delete(:lock_version) |
|
370 | issue_attributes.delete(:lock_version) | |
370 | when 'add_notes' |
|
371 | when 'add_notes' | |
371 | issue_attributes = issue_attributes.slice(:notes) |
|
372 | issue_attributes = issue_attributes.slice(:notes) | |
372 | when 'cancel' |
|
373 | when 'cancel' | |
373 | redirect_to issue_path(@issue) |
|
374 | redirect_to issue_path(@issue) | |
374 | return false |
|
375 | return false | |
375 | end |
|
376 | end | |
376 | end |
|
377 | end | |
377 | @issue.safe_attributes = issue_attributes |
|
378 | @issue.safe_attributes = issue_attributes | |
378 | @priorities = IssuePriority.active |
|
379 | @priorities = IssuePriority.active | |
379 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current) |
|
380 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current) | |
380 | true |
|
381 | true | |
381 | end |
|
382 | end | |
382 |
|
383 | |||
383 | # TODO: Refactor, lots of extra code in here |
|
384 | # TODO: Refactor, lots of extra code in here | |
384 | # TODO: Changing tracker on an existing issue should not trigger this |
|
385 | # TODO: Changing tracker on an existing issue should not trigger this | |
385 | def build_new_issue_from_params |
|
386 | def build_new_issue_from_params | |
386 | if params[:id].blank? |
|
387 | if params[:id].blank? | |
387 | @issue = Issue.new |
|
388 | @issue = Issue.new | |
388 | if params[:copy_from] |
|
389 | if params[:copy_from] | |
389 | begin |
|
390 | begin | |
390 | @copy_from = Issue.visible.find(params[:copy_from]) |
|
391 | @copy_from = Issue.visible.find(params[:copy_from]) | |
391 | @copy_attachments = params[:copy_attachments].present? || request.get? |
|
392 | @copy_attachments = params[:copy_attachments].present? || request.get? | |
392 | @copy_subtasks = params[:copy_subtasks].present? || request.get? |
|
393 | @copy_subtasks = params[:copy_subtasks].present? || request.get? | |
393 | @issue.copy_from(@copy_from, :attachments => @copy_attachments, :subtasks => @copy_subtasks) |
|
394 | @issue.copy_from(@copy_from, :attachments => @copy_attachments, :subtasks => @copy_subtasks) | |
394 | rescue ActiveRecord::RecordNotFound |
|
395 | rescue ActiveRecord::RecordNotFound | |
395 | render_404 |
|
396 | render_404 | |
396 | return |
|
397 | return | |
397 | end |
|
398 | end | |
398 | end |
|
399 | end | |
399 | @issue.project = @project |
|
400 | @issue.project = @project | |
400 | else |
|
401 | else | |
401 | @issue = @project.issues.visible.find(params[:id]) |
|
402 | @issue = @project.issues.visible.find(params[:id]) | |
402 | end |
|
403 | end | |
403 |
|
404 | |||
404 | @issue.project = @project |
|
405 | @issue.project = @project | |
405 | @issue.author ||= User.current |
|
406 | @issue.author ||= User.current | |
406 | # Tracker must be set before custom field values |
|
407 | # Tracker must be set before custom field values | |
407 | @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first) |
|
408 | @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first) | |
408 | if @issue.tracker.nil? |
|
409 | if @issue.tracker.nil? | |
409 | render_error l(:error_no_tracker_in_project) |
|
410 | render_error l(:error_no_tracker_in_project) | |
410 | return false |
|
411 | return false | |
411 | end |
|
412 | end | |
412 | @issue.start_date ||= Date.today if Setting.default_issue_start_date_to_creation_date? |
|
413 | @issue.start_date ||= Date.today if Setting.default_issue_start_date_to_creation_date? | |
413 | @issue.safe_attributes = params[:issue] |
|
414 | @issue.safe_attributes = params[:issue] | |
414 |
|
415 | |||
415 | @priorities = IssuePriority.active |
|
416 | @priorities = IssuePriority.active | |
416 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current, true) |
|
417 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current, true) | |
417 | @available_watchers = (@issue.project.users.sort + @issue.watcher_users).uniq |
|
418 | @available_watchers = (@issue.project.users.sort + @issue.watcher_users).uniq | |
418 | end |
|
419 | end | |
419 |
|
420 | |||
420 | def check_for_default_issue_status |
|
421 | def check_for_default_issue_status | |
421 | if IssueStatus.default.nil? |
|
422 | if IssueStatus.default.nil? | |
422 | render_error l(:error_no_default_issue_status) |
|
423 | render_error l(:error_no_default_issue_status) | |
423 | return false |
|
424 | return false | |
424 | end |
|
425 | end | |
425 | end |
|
426 | end | |
426 |
|
427 | |||
427 | def parse_params_for_bulk_issue_attributes(params) |
|
428 | def parse_params_for_bulk_issue_attributes(params) | |
428 | attributes = (params[:issue] || {}).reject {|k,v| v.blank?} |
|
429 | attributes = (params[:issue] || {}).reject {|k,v| v.blank?} | |
429 | attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'} |
|
430 | attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'} | |
430 | if custom = attributes[:custom_field_values] |
|
431 | if custom = attributes[:custom_field_values] | |
431 | custom.reject! {|k,v| v.blank?} |
|
432 | custom.reject! {|k,v| v.blank?} | |
432 | custom.keys.each do |k| |
|
433 | custom.keys.each do |k| | |
433 | if custom[k].is_a?(Array) |
|
434 | if custom[k].is_a?(Array) | |
434 | custom[k] << '' if custom[k].delete('__none__') |
|
435 | custom[k] << '' if custom[k].delete('__none__') | |
435 | else |
|
436 | else | |
436 | custom[k] = '' if custom[k] == '__none__' |
|
437 | custom[k] = '' if custom[k] == '__none__' | |
437 | end |
|
438 | end | |
438 | end |
|
439 | end | |
439 | end |
|
440 | end | |
440 | attributes |
|
441 | attributes | |
441 | end |
|
442 | end | |
442 | end |
|
443 | end |
@@ -1,234 +1,242 | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | # |
|
2 | # | |
3 | # Helpers to sort tables using clickable column headers. |
|
3 | # Helpers to sort tables using clickable column headers. | |
4 | # |
|
4 | # | |
5 | # Author: Stuart Rackham <srackham@methods.co.nz>, March 2005. |
|
5 | # Author: Stuart Rackham <srackham@methods.co.nz>, March 2005. | |
6 | # Jean-Philippe Lang, 2009 |
|
6 | # Jean-Philippe Lang, 2009 | |
7 | # License: This source code is released under the MIT license. |
|
7 | # License: This source code is released under the MIT license. | |
8 | # |
|
8 | # | |
9 | # - Consecutive clicks toggle the column's sort order. |
|
9 | # - Consecutive clicks toggle the column's sort order. | |
10 | # - Sort state is maintained by a session hash entry. |
|
10 | # - Sort state is maintained by a session hash entry. | |
11 | # - CSS classes identify sort column and state. |
|
11 | # - CSS classes identify sort column and state. | |
12 | # - Typically used in conjunction with the Pagination module. |
|
12 | # - Typically used in conjunction with the Pagination module. | |
13 | # |
|
13 | # | |
14 | # Example code snippets: |
|
14 | # Example code snippets: | |
15 | # |
|
15 | # | |
16 | # Controller: |
|
16 | # Controller: | |
17 | # |
|
17 | # | |
18 | # helper :sort |
|
18 | # helper :sort | |
19 | # include SortHelper |
|
19 | # include SortHelper | |
20 | # |
|
20 | # | |
21 | # def list |
|
21 | # def list | |
22 | # sort_init 'last_name' |
|
22 | # sort_init 'last_name' | |
23 | # sort_update %w(first_name last_name) |
|
23 | # sort_update %w(first_name last_name) | |
24 | # @items = Contact.find_all nil, sort_clause |
|
24 | # @items = Contact.find_all nil, sort_clause | |
25 | # end |
|
25 | # end | |
26 | # |
|
26 | # | |
27 | # Controller (using Pagination module): |
|
27 | # Controller (using Pagination module): | |
28 | # |
|
28 | # | |
29 | # helper :sort |
|
29 | # helper :sort | |
30 | # include SortHelper |
|
30 | # include SortHelper | |
31 | # |
|
31 | # | |
32 | # def list |
|
32 | # def list | |
33 | # sort_init 'last_name' |
|
33 | # sort_init 'last_name' | |
34 | # sort_update %w(first_name last_name) |
|
34 | # sort_update %w(first_name last_name) | |
35 | # @contact_pages, @items = paginate :contacts, |
|
35 | # @contact_pages, @items = paginate :contacts, | |
36 | # :order_by => sort_clause, |
|
36 | # :order_by => sort_clause, | |
37 | # :per_page => 10 |
|
37 | # :per_page => 10 | |
38 | # end |
|
38 | # end | |
39 | # |
|
39 | # | |
40 | # View (table header in list.rhtml): |
|
40 | # View (table header in list.rhtml): | |
41 | # |
|
41 | # | |
42 | # <thead> |
|
42 | # <thead> | |
43 | # <tr> |
|
43 | # <tr> | |
44 | # <%= sort_header_tag('id', :title => 'Sort by contact ID') %> |
|
44 | # <%= sort_header_tag('id', :title => 'Sort by contact ID') %> | |
45 | # <%= sort_header_tag('last_name', :caption => 'Name') %> |
|
45 | # <%= sort_header_tag('last_name', :caption => 'Name') %> | |
46 | # <%= sort_header_tag('phone') %> |
|
46 | # <%= sort_header_tag('phone') %> | |
47 | # <%= sort_header_tag('address', :width => 200) %> |
|
47 | # <%= sort_header_tag('address', :width => 200) %> | |
48 | # </tr> |
|
48 | # </tr> | |
49 | # </thead> |
|
49 | # </thead> | |
50 | # |
|
50 | # | |
51 | # - Introduces instance variables: @sort_default, @sort_criteria |
|
51 | # - Introduces instance variables: @sort_default, @sort_criteria | |
52 | # - Introduces param :sort |
|
52 | # - Introduces param :sort | |
53 | # |
|
53 | # | |
54 |
|
54 | |||
55 | module SortHelper |
|
55 | module SortHelper | |
56 | class SortCriteria |
|
56 | class SortCriteria | |
57 |
|
57 | |||
58 | def initialize |
|
58 | def initialize | |
59 | @criteria = [] |
|
59 | @criteria = [] | |
60 | end |
|
60 | end | |
61 |
|
61 | |||
62 | def available_criteria=(criteria) |
|
62 | def available_criteria=(criteria) | |
63 | unless criteria.is_a?(Hash) |
|
63 | unless criteria.is_a?(Hash) | |
64 | criteria = criteria.inject({}) {|h,k| h[k] = k; h} |
|
64 | criteria = criteria.inject({}) {|h,k| h[k] = k; h} | |
65 | end |
|
65 | end | |
66 | @available_criteria = criteria |
|
66 | @available_criteria = criteria | |
67 | end |
|
67 | end | |
68 |
|
68 | |||
69 | def from_param(param) |
|
69 | def from_param(param) | |
70 | @criteria = param.to_s.split(',').collect {|s| s.split(':')[0..1]} |
|
70 | @criteria = param.to_s.split(',').collect {|s| s.split(':')[0..1]} | |
71 | normalize! |
|
71 | normalize! | |
72 | end |
|
72 | end | |
73 |
|
73 | |||
74 | def criteria=(arg) |
|
74 | def criteria=(arg) | |
75 | @criteria = arg |
|
75 | @criteria = arg | |
76 | normalize! |
|
76 | normalize! | |
77 | end |
|
77 | end | |
78 |
|
78 | |||
79 | def to_param |
|
79 | def to_param | |
80 | @criteria.collect {|k,o| k + (o ? '' : ':desc')}.join(',') |
|
80 | @criteria.collect {|k,o| k + (o ? '' : ':desc')}.join(',') | |
81 | end |
|
81 | end | |
82 |
|
82 | |||
83 | def to_sql |
|
83 | def to_sql | |
84 | sql = @criteria.collect do |k,o| |
|
84 | sql = @criteria.collect do |k,o| | |
85 | if s = @available_criteria[k] |
|
85 | if s = @available_criteria[k] | |
86 | (o ? s.to_a : s.to_a.collect {|c| append_desc(c)}).join(', ') |
|
86 | (o ? s.to_a : s.to_a.collect {|c| append_desc(c)}).join(', ') | |
87 | end |
|
87 | end | |
88 | end.compact.join(', ') |
|
88 | end.compact.join(', ') | |
89 | sql.blank? ? nil : sql |
|
89 | sql.blank? ? nil : sql | |
90 | end |
|
90 | end | |
91 |
|
91 | |||
|
92 | def to_a | |||
|
93 | @criteria.dup | |||
|
94 | end | |||
|
95 | ||||
92 | def add!(key, asc) |
|
96 | def add!(key, asc) | |
93 | @criteria.delete_if {|k,o| k == key} |
|
97 | @criteria.delete_if {|k,o| k == key} | |
94 | @criteria = [[key, asc]] + @criteria |
|
98 | @criteria = [[key, asc]] + @criteria | |
95 | normalize! |
|
99 | normalize! | |
96 | end |
|
100 | end | |
97 |
|
101 | |||
98 | def add(*args) |
|
102 | def add(*args) | |
99 | r = self.class.new.from_param(to_param) |
|
103 | r = self.class.new.from_param(to_param) | |
100 | r.add!(*args) |
|
104 | r.add!(*args) | |
101 | r |
|
105 | r | |
102 | end |
|
106 | end | |
103 |
|
107 | |||
104 | def first_key |
|
108 | def first_key | |
105 | @criteria.first && @criteria.first.first |
|
109 | @criteria.first && @criteria.first.first | |
106 | end |
|
110 | end | |
107 |
|
111 | |||
108 | def first_asc? |
|
112 | def first_asc? | |
109 | @criteria.first && @criteria.first.last |
|
113 | @criteria.first && @criteria.first.last | |
110 | end |
|
114 | end | |
111 |
|
115 | |||
112 | def empty? |
|
116 | def empty? | |
113 | @criteria.empty? |
|
117 | @criteria.empty? | |
114 | end |
|
118 | end | |
115 |
|
119 | |||
116 | private |
|
120 | private | |
117 |
|
121 | |||
118 | def normalize! |
|
122 | def normalize! | |
119 | @criteria ||= [] |
|
123 | @criteria ||= [] | |
120 | @criteria = @criteria.collect {|s| s = s.to_a; [s.first, (s.last == false || s.last == 'desc') ? false : true]} |
|
124 | @criteria = @criteria.collect {|s| s = s.to_a; [s.first, (s.last == false || s.last == 'desc') ? false : true]} | |
121 | @criteria = @criteria.select {|k,o| @available_criteria.has_key?(k)} if @available_criteria |
|
125 | @criteria = @criteria.select {|k,o| @available_criteria.has_key?(k)} if @available_criteria | |
122 | @criteria.slice!(3) |
|
126 | @criteria.slice!(3) | |
123 | self |
|
127 | self | |
124 | end |
|
128 | end | |
125 |
|
129 | |||
126 | # Appends DESC to the sort criterion unless it has a fixed order |
|
130 | # Appends DESC to the sort criterion unless it has a fixed order | |
127 | def append_desc(criterion) |
|
131 | def append_desc(criterion) | |
128 | if criterion =~ / (asc|desc)$/i |
|
132 | if criterion =~ / (asc|desc)$/i | |
129 | criterion |
|
133 | criterion | |
130 | else |
|
134 | else | |
131 | "#{criterion} DESC" |
|
135 | "#{criterion} DESC" | |
132 | end |
|
136 | end | |
133 | end |
|
137 | end | |
134 | end |
|
138 | end | |
135 |
|
139 | |||
136 | def sort_name |
|
140 | def sort_name | |
137 | controller_name + '_' + action_name + '_sort' |
|
141 | controller_name + '_' + action_name + '_sort' | |
138 | end |
|
142 | end | |
139 |
|
143 | |||
140 | # Initializes the default sort. |
|
144 | # Initializes the default sort. | |
141 | # Examples: |
|
145 | # Examples: | |
142 | # |
|
146 | # | |
143 | # sort_init 'name' |
|
147 | # sort_init 'name' | |
144 | # sort_init 'id', 'desc' |
|
148 | # sort_init 'id', 'desc' | |
145 | # sort_init ['name', ['id', 'desc']] |
|
149 | # sort_init ['name', ['id', 'desc']] | |
146 | # sort_init [['name', 'desc'], ['id', 'desc']] |
|
150 | # sort_init [['name', 'desc'], ['id', 'desc']] | |
147 | # |
|
151 | # | |
148 | def sort_init(*args) |
|
152 | def sort_init(*args) | |
149 | case args.size |
|
153 | case args.size | |
150 | when 1 |
|
154 | when 1 | |
151 | @sort_default = args.first.is_a?(Array) ? args.first : [[args.first]] |
|
155 | @sort_default = args.first.is_a?(Array) ? args.first : [[args.first]] | |
152 | when 2 |
|
156 | when 2 | |
153 | @sort_default = [[args.first, args.last]] |
|
157 | @sort_default = [[args.first, args.last]] | |
154 | else |
|
158 | else | |
155 | raise ArgumentError |
|
159 | raise ArgumentError | |
156 | end |
|
160 | end | |
157 | end |
|
161 | end | |
158 |
|
162 | |||
159 | # Updates the sort state. Call this in the controller prior to calling |
|
163 | # Updates the sort state. Call this in the controller prior to calling | |
160 | # sort_clause. |
|
164 | # sort_clause. | |
161 | # - criteria can be either an array or a hash of allowed keys |
|
165 | # - criteria can be either an array or a hash of allowed keys | |
162 | # |
|
166 | # | |
163 | def sort_update(criteria, sort_name=nil) |
|
167 | def sort_update(criteria, sort_name=nil) | |
164 | sort_name ||= self.sort_name |
|
168 | sort_name ||= self.sort_name | |
165 | @sort_criteria = SortCriteria.new |
|
169 | @sort_criteria = SortCriteria.new | |
166 | @sort_criteria.available_criteria = criteria |
|
170 | @sort_criteria.available_criteria = criteria | |
167 | @sort_criteria.from_param(params[:sort] || session[sort_name]) |
|
171 | @sort_criteria.from_param(params[:sort] || session[sort_name]) | |
168 | @sort_criteria.criteria = @sort_default if @sort_criteria.empty? |
|
172 | @sort_criteria.criteria = @sort_default if @sort_criteria.empty? | |
169 | session[sort_name] = @sort_criteria.to_param |
|
173 | session[sort_name] = @sort_criteria.to_param | |
170 | end |
|
174 | end | |
171 |
|
175 | |||
172 | # Clears the sort criteria session data |
|
176 | # Clears the sort criteria session data | |
173 | # |
|
177 | # | |
174 | def sort_clear |
|
178 | def sort_clear | |
175 | session[sort_name] = nil |
|
179 | session[sort_name] = nil | |
176 | end |
|
180 | end | |
177 |
|
181 | |||
178 | # Returns an SQL sort clause corresponding to the current sort state. |
|
182 | # Returns an SQL sort clause corresponding to the current sort state. | |
179 | # Use this to sort the controller's table items collection. |
|
183 | # Use this to sort the controller's table items collection. | |
180 | # |
|
184 | # | |
181 | def sort_clause() |
|
185 | def sort_clause() | |
182 | @sort_criteria.to_sql |
|
186 | @sort_criteria.to_sql | |
183 | end |
|
187 | end | |
184 |
|
188 | |||
|
189 | def sort_criteria | |||
|
190 | @sort_criteria | |||
|
191 | end | |||
|
192 | ||||
185 | # Returns a link which sorts by the named column. |
|
193 | # Returns a link which sorts by the named column. | |
186 | # |
|
194 | # | |
187 | # - column is the name of an attribute in the sorted record collection. |
|
195 | # - column is the name of an attribute in the sorted record collection. | |
188 | # - the optional caption explicitly specifies the displayed link text. |
|
196 | # - the optional caption explicitly specifies the displayed link text. | |
189 | # - 2 CSS classes reflect the state of the link: sort and asc or desc |
|
197 | # - 2 CSS classes reflect the state of the link: sort and asc or desc | |
190 | # |
|
198 | # | |
191 | def sort_link(column, caption, default_order) |
|
199 | def sort_link(column, caption, default_order) | |
192 | css, order = nil, default_order |
|
200 | css, order = nil, default_order | |
193 |
|
201 | |||
194 | if column.to_s == @sort_criteria.first_key |
|
202 | if column.to_s == @sort_criteria.first_key | |
195 | if @sort_criteria.first_asc? |
|
203 | if @sort_criteria.first_asc? | |
196 | css = 'sort asc' |
|
204 | css = 'sort asc' | |
197 | order = 'desc' |
|
205 | order = 'desc' | |
198 | else |
|
206 | else | |
199 | css = 'sort desc' |
|
207 | css = 'sort desc' | |
200 | order = 'asc' |
|
208 | order = 'asc' | |
201 | end |
|
209 | end | |
202 | end |
|
210 | end | |
203 | caption = column.to_s.humanize unless caption |
|
211 | caption = column.to_s.humanize unless caption | |
204 |
|
212 | |||
205 | sort_options = { :sort => @sort_criteria.add(column.to_s, order).to_param } |
|
213 | sort_options = { :sort => @sort_criteria.add(column.to_s, order).to_param } | |
206 | url_options = params.merge(sort_options) |
|
214 | url_options = params.merge(sort_options) | |
207 |
|
215 | |||
208 | # Add project_id to url_options |
|
216 | # Add project_id to url_options | |
209 | url_options = url_options.merge(:project_id => params[:project_id]) if params.has_key?(:project_id) |
|
217 | url_options = url_options.merge(:project_id => params[:project_id]) if params.has_key?(:project_id) | |
210 |
|
218 | |||
211 | link_to_content_update(h(caption), url_options, :class => css) |
|
219 | link_to_content_update(h(caption), url_options, :class => css) | |
212 | end |
|
220 | end | |
213 |
|
221 | |||
214 | # Returns a table header <th> tag with a sort link for the named column |
|
222 | # Returns a table header <th> tag with a sort link for the named column | |
215 | # attribute. |
|
223 | # attribute. | |
216 | # |
|
224 | # | |
217 | # Options: |
|
225 | # Options: | |
218 | # :caption The displayed link name (defaults to titleized column name). |
|
226 | # :caption The displayed link name (defaults to titleized column name). | |
219 | # :title The tag's 'title' attribute (defaults to 'Sort by :caption'). |
|
227 | # :title The tag's 'title' attribute (defaults to 'Sort by :caption'). | |
220 | # |
|
228 | # | |
221 | # Other options hash entries generate additional table header tag attributes. |
|
229 | # Other options hash entries generate additional table header tag attributes. | |
222 | # |
|
230 | # | |
223 | # Example: |
|
231 | # Example: | |
224 | # |
|
232 | # | |
225 | # <%= sort_header_tag('id', :title => 'Sort by contact ID', :width => 40) %> |
|
233 | # <%= sort_header_tag('id', :title => 'Sort by contact ID', :width => 40) %> | |
226 | # |
|
234 | # | |
227 | def sort_header_tag(column, options = {}) |
|
235 | def sort_header_tag(column, options = {}) | |
228 | caption = options.delete(:caption) || column.to_s.humanize |
|
236 | caption = options.delete(:caption) || column.to_s.humanize | |
229 | default_order = options.delete(:default_order) || 'asc' |
|
237 | default_order = options.delete(:default_order) || 'asc' | |
230 | options[:title] = l(:label_sort_by, "\"#{caption}\"") unless options[:title] |
|
238 | options[:title] = l(:label_sort_by, "\"#{caption}\"") unless options[:title] | |
231 | content_tag('th', sort_link(column, caption, default_order), options) |
|
239 | content_tag('th', sort_link(column, caption, default_order), options) | |
232 | end |
|
240 | end | |
233 | end |
|
241 | end | |
234 |
|
242 |
@@ -1,1063 +1,1068 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2012 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2012 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software |
|
15 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | class QueryColumn |
|
18 | class QueryColumn | |
19 | attr_accessor :name, :sortable, :groupable, :default_order |
|
19 | attr_accessor :name, :sortable, :groupable, :default_order | |
20 | include Redmine::I18n |
|
20 | include Redmine::I18n | |
21 |
|
21 | |||
22 | def initialize(name, options={}) |
|
22 | def initialize(name, options={}) | |
23 | self.name = name |
|
23 | self.name = name | |
24 | self.sortable = options[:sortable] |
|
24 | self.sortable = options[:sortable] | |
25 | self.groupable = options[:groupable] || false |
|
25 | self.groupable = options[:groupable] || false | |
26 | if groupable == true |
|
26 | if groupable == true | |
27 | self.groupable = name.to_s |
|
27 | self.groupable = name.to_s | |
28 | end |
|
28 | end | |
29 | self.default_order = options[:default_order] |
|
29 | self.default_order = options[:default_order] | |
30 | @caption_key = options[:caption] || "field_#{name}" |
|
30 | @caption_key = options[:caption] || "field_#{name}" | |
31 | end |
|
31 | end | |
32 |
|
32 | |||
33 | def caption |
|
33 | def caption | |
34 | l(@caption_key) |
|
34 | l(@caption_key) | |
35 | end |
|
35 | end | |
36 |
|
36 | |||
37 | # Returns true if the column is sortable, otherwise false |
|
37 | # Returns true if the column is sortable, otherwise false | |
38 | def sortable? |
|
38 | def sortable? | |
39 | !@sortable.nil? |
|
39 | !@sortable.nil? | |
40 | end |
|
40 | end | |
41 |
|
41 | |||
42 | def sortable |
|
42 | def sortable | |
43 | @sortable.is_a?(Proc) ? @sortable.call : @sortable |
|
43 | @sortable.is_a?(Proc) ? @sortable.call : @sortable | |
44 | end |
|
44 | end | |
45 |
|
45 | |||
46 | def value(issue) |
|
46 | def value(issue) | |
47 | issue.send name |
|
47 | issue.send name | |
48 | end |
|
48 | end | |
49 |
|
49 | |||
50 | def css_classes |
|
50 | def css_classes | |
51 | name |
|
51 | name | |
52 | end |
|
52 | end | |
53 | end |
|
53 | end | |
54 |
|
54 | |||
55 | class QueryCustomFieldColumn < QueryColumn |
|
55 | class QueryCustomFieldColumn < QueryColumn | |
56 |
|
56 | |||
57 | def initialize(custom_field) |
|
57 | def initialize(custom_field) | |
58 | self.name = "cf_#{custom_field.id}".to_sym |
|
58 | self.name = "cf_#{custom_field.id}".to_sym | |
59 | self.sortable = custom_field.order_statement || false |
|
59 | self.sortable = custom_field.order_statement || false | |
60 | self.groupable = custom_field.group_statement || false |
|
60 | self.groupable = custom_field.group_statement || false | |
61 | @cf = custom_field |
|
61 | @cf = custom_field | |
62 | end |
|
62 | end | |
63 |
|
63 | |||
64 | def caption |
|
64 | def caption | |
65 | @cf.name |
|
65 | @cf.name | |
66 | end |
|
66 | end | |
67 |
|
67 | |||
68 | def custom_field |
|
68 | def custom_field | |
69 | @cf |
|
69 | @cf | |
70 | end |
|
70 | end | |
71 |
|
71 | |||
72 | def value(issue) |
|
72 | def value(issue) | |
73 | cv = issue.custom_values.select {|v| v.custom_field_id == @cf.id}.collect {|v| @cf.cast_value(v.value)} |
|
73 | cv = issue.custom_values.select {|v| v.custom_field_id == @cf.id}.collect {|v| @cf.cast_value(v.value)} | |
74 | cv.size > 1 ? cv.sort {|a,b| a.to_s <=> b.to_s} : cv.first |
|
74 | cv.size > 1 ? cv.sort {|a,b| a.to_s <=> b.to_s} : cv.first | |
75 | end |
|
75 | end | |
76 |
|
76 | |||
77 | def css_classes |
|
77 | def css_classes | |
78 | @css_classes ||= "#{name} #{@cf.field_format}" |
|
78 | @css_classes ||= "#{name} #{@cf.field_format}" | |
79 | end |
|
79 | end | |
80 | end |
|
80 | end | |
81 |
|
81 | |||
82 | class Query < ActiveRecord::Base |
|
82 | class Query < ActiveRecord::Base | |
83 | class StatementInvalid < ::ActiveRecord::StatementInvalid |
|
83 | class StatementInvalid < ::ActiveRecord::StatementInvalid | |
84 | end |
|
84 | end | |
85 |
|
85 | |||
86 | belongs_to :project |
|
86 | belongs_to :project | |
87 | belongs_to :user |
|
87 | belongs_to :user | |
88 | serialize :filters |
|
88 | serialize :filters | |
89 | serialize :column_names |
|
89 | serialize :column_names | |
90 | serialize :sort_criteria, Array |
|
90 | serialize :sort_criteria, Array | |
91 |
|
91 | |||
92 | attr_protected :project_id, :user_id |
|
92 | attr_protected :project_id, :user_id | |
93 |
|
93 | |||
94 | validates_presence_of :name |
|
94 | validates_presence_of :name | |
95 | validates_length_of :name, :maximum => 255 |
|
95 | validates_length_of :name, :maximum => 255 | |
96 | validate :validate_query_filters |
|
96 | validate :validate_query_filters | |
97 |
|
97 | |||
98 | @@operators = { "=" => :label_equals, |
|
98 | @@operators = { "=" => :label_equals, | |
99 | "!" => :label_not_equals, |
|
99 | "!" => :label_not_equals, | |
100 | "o" => :label_open_issues, |
|
100 | "o" => :label_open_issues, | |
101 | "c" => :label_closed_issues, |
|
101 | "c" => :label_closed_issues, | |
102 | "!*" => :label_none, |
|
102 | "!*" => :label_none, | |
103 | "*" => :label_any, |
|
103 | "*" => :label_any, | |
104 | ">=" => :label_greater_or_equal, |
|
104 | ">=" => :label_greater_or_equal, | |
105 | "<=" => :label_less_or_equal, |
|
105 | "<=" => :label_less_or_equal, | |
106 | "><" => :label_between, |
|
106 | "><" => :label_between, | |
107 | "<t+" => :label_in_less_than, |
|
107 | "<t+" => :label_in_less_than, | |
108 | ">t+" => :label_in_more_than, |
|
108 | ">t+" => :label_in_more_than, | |
109 | "t+" => :label_in, |
|
109 | "t+" => :label_in, | |
110 | "t" => :label_today, |
|
110 | "t" => :label_today, | |
111 | "w" => :label_this_week, |
|
111 | "w" => :label_this_week, | |
112 | ">t-" => :label_less_than_ago, |
|
112 | ">t-" => :label_less_than_ago, | |
113 | "<t-" => :label_more_than_ago, |
|
113 | "<t-" => :label_more_than_ago, | |
114 | "t-" => :label_ago, |
|
114 | "t-" => :label_ago, | |
115 | "~" => :label_contains, |
|
115 | "~" => :label_contains, | |
116 | "!~" => :label_not_contains, |
|
116 | "!~" => :label_not_contains, | |
117 | "=p" => :label_any_issues_in_project, |
|
117 | "=p" => :label_any_issues_in_project, | |
118 | "=!p" => :label_any_issues_not_in_project, |
|
118 | "=!p" => :label_any_issues_not_in_project, | |
119 | "!p" => :label_no_issues_in_project} |
|
119 | "!p" => :label_no_issues_in_project} | |
120 |
|
120 | |||
121 | cattr_reader :operators |
|
121 | cattr_reader :operators | |
122 |
|
122 | |||
123 | @@operators_by_filter_type = { :list => [ "=", "!" ], |
|
123 | @@operators_by_filter_type = { :list => [ "=", "!" ], | |
124 | :list_status => [ "o", "=", "!", "c", "*" ], |
|
124 | :list_status => [ "o", "=", "!", "c", "*" ], | |
125 | :list_optional => [ "=", "!", "!*", "*" ], |
|
125 | :list_optional => [ "=", "!", "!*", "*" ], | |
126 | :list_subprojects => [ "*", "!*", "=" ], |
|
126 | :list_subprojects => [ "*", "!*", "=" ], | |
127 | :date => [ "=", ">=", "<=", "><", "<t+", ">t+", "t+", "t", "w", ">t-", "<t-", "t-", "!*", "*" ], |
|
127 | :date => [ "=", ">=", "<=", "><", "<t+", ">t+", "t+", "t", "w", ">t-", "<t-", "t-", "!*", "*" ], | |
128 | :date_past => [ "=", ">=", "<=", "><", ">t-", "<t-", "t-", "t", "w", "!*", "*" ], |
|
128 | :date_past => [ "=", ">=", "<=", "><", ">t-", "<t-", "t-", "t", "w", "!*", "*" ], | |
129 | :string => [ "=", "~", "!", "!~", "!*", "*" ], |
|
129 | :string => [ "=", "~", "!", "!~", "!*", "*" ], | |
130 | :text => [ "~", "!~", "!*", "*" ], |
|
130 | :text => [ "~", "!~", "!*", "*" ], | |
131 | :integer => [ "=", ">=", "<=", "><", "!*", "*" ], |
|
131 | :integer => [ "=", ">=", "<=", "><", "!*", "*" ], | |
132 | :float => [ "=", ">=", "<=", "><", "!*", "*" ], |
|
132 | :float => [ "=", ">=", "<=", "><", "!*", "*" ], | |
133 | :relation => ["=", "=p", "=!p", "!p", "!*", "*"]} |
|
133 | :relation => ["=", "=p", "=!p", "!p", "!*", "*"]} | |
134 |
|
134 | |||
135 | cattr_reader :operators_by_filter_type |
|
135 | cattr_reader :operators_by_filter_type | |
136 |
|
136 | |||
137 | @@available_columns = [ |
|
137 | @@available_columns = [ | |
138 | QueryColumn.new(:project, :sortable => "#{Project.table_name}.name", :groupable => true), |
|
138 | QueryColumn.new(:project, :sortable => "#{Project.table_name}.name", :groupable => true), | |
139 | QueryColumn.new(:tracker, :sortable => "#{Tracker.table_name}.position", :groupable => true), |
|
139 | QueryColumn.new(:tracker, :sortable => "#{Tracker.table_name}.position", :groupable => true), | |
140 | QueryColumn.new(:parent, :sortable => ["#{Issue.table_name}.root_id", "#{Issue.table_name}.lft ASC"], :default_order => 'desc', :caption => :field_parent_issue), |
|
140 | QueryColumn.new(:parent, :sortable => ["#{Issue.table_name}.root_id", "#{Issue.table_name}.lft ASC"], :default_order => 'desc', :caption => :field_parent_issue), | |
141 | QueryColumn.new(:status, :sortable => "#{IssueStatus.table_name}.position", :groupable => true), |
|
141 | QueryColumn.new(:status, :sortable => "#{IssueStatus.table_name}.position", :groupable => true), | |
142 | QueryColumn.new(:priority, :sortable => "#{IssuePriority.table_name}.position", :default_order => 'desc', :groupable => true), |
|
142 | QueryColumn.new(:priority, :sortable => "#{IssuePriority.table_name}.position", :default_order => 'desc', :groupable => true), | |
143 | QueryColumn.new(:subject, :sortable => "#{Issue.table_name}.subject"), |
|
143 | QueryColumn.new(:subject, :sortable => "#{Issue.table_name}.subject"), | |
144 | QueryColumn.new(:author, :sortable => lambda {User.fields_for_order_statement("authors")}, :groupable => true), |
|
144 | QueryColumn.new(:author, :sortable => lambda {User.fields_for_order_statement("authors")}, :groupable => true), | |
145 | QueryColumn.new(:assigned_to, :sortable => lambda {User.fields_for_order_statement}, :groupable => true), |
|
145 | QueryColumn.new(:assigned_to, :sortable => lambda {User.fields_for_order_statement}, :groupable => true), | |
146 | QueryColumn.new(:updated_on, :sortable => "#{Issue.table_name}.updated_on", :default_order => 'desc'), |
|
146 | QueryColumn.new(:updated_on, :sortable => "#{Issue.table_name}.updated_on", :default_order => 'desc'), | |
147 | QueryColumn.new(:category, :sortable => "#{IssueCategory.table_name}.name", :groupable => true), |
|
147 | QueryColumn.new(:category, :sortable => "#{IssueCategory.table_name}.name", :groupable => true), | |
148 | QueryColumn.new(:fixed_version, :sortable => lambda {Version.fields_for_order_statement}, :groupable => true), |
|
148 | QueryColumn.new(:fixed_version, :sortable => lambda {Version.fields_for_order_statement}, :groupable => true), | |
149 | QueryColumn.new(:start_date, :sortable => "#{Issue.table_name}.start_date"), |
|
149 | QueryColumn.new(:start_date, :sortable => "#{Issue.table_name}.start_date"), | |
150 | QueryColumn.new(:due_date, :sortable => "#{Issue.table_name}.due_date"), |
|
150 | QueryColumn.new(:due_date, :sortable => "#{Issue.table_name}.due_date"), | |
151 | QueryColumn.new(:estimated_hours, :sortable => "#{Issue.table_name}.estimated_hours"), |
|
151 | QueryColumn.new(:estimated_hours, :sortable => "#{Issue.table_name}.estimated_hours"), | |
152 | QueryColumn.new(:done_ratio, :sortable => "#{Issue.table_name}.done_ratio", :groupable => true), |
|
152 | QueryColumn.new(:done_ratio, :sortable => "#{Issue.table_name}.done_ratio", :groupable => true), | |
153 | QueryColumn.new(:created_on, :sortable => "#{Issue.table_name}.created_on", :default_order => 'desc'), |
|
153 | QueryColumn.new(:created_on, :sortable => "#{Issue.table_name}.created_on", :default_order => 'desc'), | |
154 | QueryColumn.new(:relations, :caption => :label_related_issues) |
|
154 | QueryColumn.new(:relations, :caption => :label_related_issues) | |
155 | ] |
|
155 | ] | |
156 | cattr_reader :available_columns |
|
156 | cattr_reader :available_columns | |
157 |
|
157 | |||
158 | scope :visible, lambda {|*args| |
|
158 | scope :visible, lambda {|*args| | |
159 | user = args.shift || User.current |
|
159 | user = args.shift || User.current | |
160 | base = Project.allowed_to_condition(user, :view_issues, *args) |
|
160 | base = Project.allowed_to_condition(user, :view_issues, *args) | |
161 | user_id = user.logged? ? user.id : 0 |
|
161 | user_id = user.logged? ? user.id : 0 | |
162 | { |
|
162 | { | |
163 | :conditions => ["(#{table_name}.project_id IS NULL OR (#{base})) AND (#{table_name}.is_public = ? OR #{table_name}.user_id = ?)", true, user_id], |
|
163 | :conditions => ["(#{table_name}.project_id IS NULL OR (#{base})) AND (#{table_name}.is_public = ? OR #{table_name}.user_id = ?)", true, user_id], | |
164 | :include => :project |
|
164 | :include => :project | |
165 | } |
|
165 | } | |
166 | } |
|
166 | } | |
167 |
|
167 | |||
168 | def initialize(attributes=nil, *args) |
|
168 | def initialize(attributes=nil, *args) | |
169 | super attributes |
|
169 | super attributes | |
170 | self.filters ||= { 'status_id' => {:operator => "o", :values => [""]} } |
|
170 | self.filters ||= { 'status_id' => {:operator => "o", :values => [""]} } | |
171 | @is_for_all = project.nil? |
|
171 | @is_for_all = project.nil? | |
172 | end |
|
172 | end | |
173 |
|
173 | |||
174 | def validate_query_filters |
|
174 | def validate_query_filters | |
175 | filters.each_key do |field| |
|
175 | filters.each_key do |field| | |
176 | if values_for(field) |
|
176 | if values_for(field) | |
177 | case type_for(field) |
|
177 | case type_for(field) | |
178 | when :integer |
|
178 | when :integer | |
179 | add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^[+-]?\d+$/) } |
|
179 | add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^[+-]?\d+$/) } | |
180 | when :float |
|
180 | when :float | |
181 | add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^[+-]?\d+(\.\d*)?$/) } |
|
181 | add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^[+-]?\d+(\.\d*)?$/) } | |
182 | when :date, :date_past |
|
182 | when :date, :date_past | |
183 | case operator_for(field) |
|
183 | case operator_for(field) | |
184 | when "=", ">=", "<=", "><" |
|
184 | when "=", ">=", "<=", "><" | |
185 | add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && (!v.match(/^\d{4}-\d{2}-\d{2}$/) || (Date.parse(v) rescue nil).nil?) } |
|
185 | add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && (!v.match(/^\d{4}-\d{2}-\d{2}$/) || (Date.parse(v) rescue nil).nil?) } | |
186 | when ">t-", "<t-", "t-" |
|
186 | when ">t-", "<t-", "t-" | |
187 | add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^\d+$/) } |
|
187 | add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^\d+$/) } | |
188 | end |
|
188 | end | |
189 | end |
|
189 | end | |
190 | end |
|
190 | end | |
191 |
|
191 | |||
192 | add_filter_error(field, :blank) unless |
|
192 | add_filter_error(field, :blank) unless | |
193 | # filter requires one or more values |
|
193 | # filter requires one or more values | |
194 | (values_for(field) and !values_for(field).first.blank?) or |
|
194 | (values_for(field) and !values_for(field).first.blank?) or | |
195 | # filter doesn't require any value |
|
195 | # filter doesn't require any value | |
196 | ["o", "c", "!*", "*", "t", "w"].include? operator_for(field) |
|
196 | ["o", "c", "!*", "*", "t", "w"].include? operator_for(field) | |
197 | end if filters |
|
197 | end if filters | |
198 | end |
|
198 | end | |
199 |
|
199 | |||
200 | def add_filter_error(field, message) |
|
200 | def add_filter_error(field, message) | |
201 | m = label_for(field) + " " + l(message, :scope => 'activerecord.errors.messages') |
|
201 | m = label_for(field) + " " + l(message, :scope => 'activerecord.errors.messages') | |
202 | errors.add(:base, m) |
|
202 | errors.add(:base, m) | |
203 | end |
|
203 | end | |
204 |
|
204 | |||
205 | # Returns true if the query is visible to +user+ or the current user. |
|
205 | # Returns true if the query is visible to +user+ or the current user. | |
206 | def visible?(user=User.current) |
|
206 | def visible?(user=User.current) | |
207 | (project.nil? || user.allowed_to?(:view_issues, project)) && (self.is_public? || self.user_id == user.id) |
|
207 | (project.nil? || user.allowed_to?(:view_issues, project)) && (self.is_public? || self.user_id == user.id) | |
208 | end |
|
208 | end | |
209 |
|
209 | |||
210 | def editable_by?(user) |
|
210 | def editable_by?(user) | |
211 | return false unless user |
|
211 | return false unless user | |
212 | # Admin can edit them all and regular users can edit their private queries |
|
212 | # Admin can edit them all and regular users can edit their private queries | |
213 | return true if user.admin? || (!is_public && self.user_id == user.id) |
|
213 | return true if user.admin? || (!is_public && self.user_id == user.id) | |
214 | # Members can not edit public queries that are for all project (only admin is allowed to) |
|
214 | # Members can not edit public queries that are for all project (only admin is allowed to) | |
215 | is_public && !@is_for_all && user.allowed_to?(:manage_public_queries, project) |
|
215 | is_public && !@is_for_all && user.allowed_to?(:manage_public_queries, project) | |
216 | end |
|
216 | end | |
217 |
|
217 | |||
218 | def trackers |
|
218 | def trackers | |
219 | @trackers ||= project.nil? ? Tracker.find(:all, :order => 'position') : project.rolled_up_trackers |
|
219 | @trackers ||= project.nil? ? Tracker.find(:all, :order => 'position') : project.rolled_up_trackers | |
220 | end |
|
220 | end | |
221 |
|
221 | |||
222 | # Returns a hash of localized labels for all filter operators |
|
222 | # Returns a hash of localized labels for all filter operators | |
223 | def self.operators_labels |
|
223 | def self.operators_labels | |
224 | operators.inject({}) {|h, operator| h[operator.first] = l(operator.last); h} |
|
224 | operators.inject({}) {|h, operator| h[operator.first] = l(operator.last); h} | |
225 | end |
|
225 | end | |
226 |
|
226 | |||
227 | def available_filters |
|
227 | def available_filters | |
228 | return @available_filters if @available_filters |
|
228 | return @available_filters if @available_filters | |
229 | @available_filters = { |
|
229 | @available_filters = { | |
230 | "status_id" => { |
|
230 | "status_id" => { | |
231 | :type => :list_status, :order => 0, |
|
231 | :type => :list_status, :order => 0, | |
232 | :values => IssueStatus.find(:all, :order => 'position').collect{|s| [s.name, s.id.to_s] } |
|
232 | :values => IssueStatus.find(:all, :order => 'position').collect{|s| [s.name, s.id.to_s] } | |
233 | }, |
|
233 | }, | |
234 | "tracker_id" => { |
|
234 | "tracker_id" => { | |
235 | :type => :list, :order => 2, :values => trackers.collect{|s| [s.name, s.id.to_s] } |
|
235 | :type => :list, :order => 2, :values => trackers.collect{|s| [s.name, s.id.to_s] } | |
236 | }, |
|
236 | }, | |
237 | "priority_id" => { |
|
237 | "priority_id" => { | |
238 | :type => :list, :order => 3, :values => IssuePriority.all.collect{|s| [s.name, s.id.to_s] } |
|
238 | :type => :list, :order => 3, :values => IssuePriority.all.collect{|s| [s.name, s.id.to_s] } | |
239 | }, |
|
239 | }, | |
240 | "subject" => { :type => :text, :order => 8 }, |
|
240 | "subject" => { :type => :text, :order => 8 }, | |
241 | "created_on" => { :type => :date_past, :order => 9 }, |
|
241 | "created_on" => { :type => :date_past, :order => 9 }, | |
242 | "updated_on" => { :type => :date_past, :order => 10 }, |
|
242 | "updated_on" => { :type => :date_past, :order => 10 }, | |
243 | "start_date" => { :type => :date, :order => 11 }, |
|
243 | "start_date" => { :type => :date, :order => 11 }, | |
244 | "due_date" => { :type => :date, :order => 12 }, |
|
244 | "due_date" => { :type => :date, :order => 12 }, | |
245 | "estimated_hours" => { :type => :float, :order => 13 }, |
|
245 | "estimated_hours" => { :type => :float, :order => 13 }, | |
246 | "done_ratio" => { :type => :integer, :order => 14 } |
|
246 | "done_ratio" => { :type => :integer, :order => 14 } | |
247 | } |
|
247 | } | |
248 | IssueRelation::TYPES.each do |relation_type, options| |
|
248 | IssueRelation::TYPES.each do |relation_type, options| | |
249 | @available_filters[relation_type] = { |
|
249 | @available_filters[relation_type] = { | |
250 | :type => :relation, :order => @available_filters.size + 100, |
|
250 | :type => :relation, :order => @available_filters.size + 100, | |
251 | :label => options[:name] |
|
251 | :label => options[:name] | |
252 | } |
|
252 | } | |
253 | end |
|
253 | end | |
254 | principals = [] |
|
254 | principals = [] | |
255 | if project |
|
255 | if project | |
256 | principals += project.principals.sort |
|
256 | principals += project.principals.sort | |
257 | unless project.leaf? |
|
257 | unless project.leaf? | |
258 | subprojects = project.descendants.visible.all |
|
258 | subprojects = project.descendants.visible.all | |
259 | if subprojects.any? |
|
259 | if subprojects.any? | |
260 | @available_filters["subproject_id"] = { |
|
260 | @available_filters["subproject_id"] = { | |
261 | :type => :list_subprojects, :order => 13, |
|
261 | :type => :list_subprojects, :order => 13, | |
262 | :values => subprojects.collect{|s| [s.name, s.id.to_s] } |
|
262 | :values => subprojects.collect{|s| [s.name, s.id.to_s] } | |
263 | } |
|
263 | } | |
264 | principals += Principal.member_of(subprojects) |
|
264 | principals += Principal.member_of(subprojects) | |
265 | end |
|
265 | end | |
266 | end |
|
266 | end | |
267 | else |
|
267 | else | |
268 | if all_projects.any? |
|
268 | if all_projects.any? | |
269 | # members of visible projects |
|
269 | # members of visible projects | |
270 | principals += Principal.member_of(all_projects) |
|
270 | principals += Principal.member_of(all_projects) | |
271 | # project filter |
|
271 | # project filter | |
272 | project_values = [] |
|
272 | project_values = [] | |
273 | if User.current.logged? && User.current.memberships.any? |
|
273 | if User.current.logged? && User.current.memberships.any? | |
274 | project_values << ["<< #{l(:label_my_projects).downcase} >>", "mine"] |
|
274 | project_values << ["<< #{l(:label_my_projects).downcase} >>", "mine"] | |
275 | end |
|
275 | end | |
276 | project_values += all_projects_values |
|
276 | project_values += all_projects_values | |
277 | @available_filters["project_id"] = { |
|
277 | @available_filters["project_id"] = { | |
278 | :type => :list, :order => 1, :values => project_values |
|
278 | :type => :list, :order => 1, :values => project_values | |
279 | } unless project_values.empty? |
|
279 | } unless project_values.empty? | |
280 | end |
|
280 | end | |
281 | end |
|
281 | end | |
282 | principals.uniq! |
|
282 | principals.uniq! | |
283 | principals.sort! |
|
283 | principals.sort! | |
284 | users = principals.select {|p| p.is_a?(User)} |
|
284 | users = principals.select {|p| p.is_a?(User)} | |
285 |
|
285 | |||
286 | assigned_to_values = [] |
|
286 | assigned_to_values = [] | |
287 | assigned_to_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged? |
|
287 | assigned_to_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged? | |
288 | assigned_to_values += (Setting.issue_group_assignment? ? |
|
288 | assigned_to_values += (Setting.issue_group_assignment? ? | |
289 | principals : users).collect{|s| [s.name, s.id.to_s] } |
|
289 | principals : users).collect{|s| [s.name, s.id.to_s] } | |
290 | @available_filters["assigned_to_id"] = { |
|
290 | @available_filters["assigned_to_id"] = { | |
291 | :type => :list_optional, :order => 4, :values => assigned_to_values |
|
291 | :type => :list_optional, :order => 4, :values => assigned_to_values | |
292 | } unless assigned_to_values.empty? |
|
292 | } unless assigned_to_values.empty? | |
293 |
|
293 | |||
294 | author_values = [] |
|
294 | author_values = [] | |
295 | author_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged? |
|
295 | author_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged? | |
296 | author_values += users.collect{|s| [s.name, s.id.to_s] } |
|
296 | author_values += users.collect{|s| [s.name, s.id.to_s] } | |
297 | @available_filters["author_id"] = { |
|
297 | @available_filters["author_id"] = { | |
298 | :type => :list, :order => 5, :values => author_values |
|
298 | :type => :list, :order => 5, :values => author_values | |
299 | } unless author_values.empty? |
|
299 | } unless author_values.empty? | |
300 |
|
300 | |||
301 | group_values = Group.all.collect {|g| [g.name, g.id.to_s] } |
|
301 | group_values = Group.all.collect {|g| [g.name, g.id.to_s] } | |
302 | @available_filters["member_of_group"] = { |
|
302 | @available_filters["member_of_group"] = { | |
303 | :type => :list_optional, :order => 6, :values => group_values |
|
303 | :type => :list_optional, :order => 6, :values => group_values | |
304 | } unless group_values.empty? |
|
304 | } unless group_values.empty? | |
305 |
|
305 | |||
306 | role_values = Role.givable.collect {|r| [r.name, r.id.to_s] } |
|
306 | role_values = Role.givable.collect {|r| [r.name, r.id.to_s] } | |
307 | @available_filters["assigned_to_role"] = { |
|
307 | @available_filters["assigned_to_role"] = { | |
308 | :type => :list_optional, :order => 7, :values => role_values |
|
308 | :type => :list_optional, :order => 7, :values => role_values | |
309 | } unless role_values.empty? |
|
309 | } unless role_values.empty? | |
310 |
|
310 | |||
311 | if User.current.logged? |
|
311 | if User.current.logged? | |
312 | @available_filters["watcher_id"] = { |
|
312 | @available_filters["watcher_id"] = { | |
313 | :type => :list, :order => 15, :values => [["<< #{l(:label_me)} >>", "me"]] |
|
313 | :type => :list, :order => 15, :values => [["<< #{l(:label_me)} >>", "me"]] | |
314 | } |
|
314 | } | |
315 | end |
|
315 | end | |
316 |
|
316 | |||
317 | if project |
|
317 | if project | |
318 | # project specific filters |
|
318 | # project specific filters | |
319 | categories = project.issue_categories.all |
|
319 | categories = project.issue_categories.all | |
320 | unless categories.empty? |
|
320 | unless categories.empty? | |
321 | @available_filters["category_id"] = { |
|
321 | @available_filters["category_id"] = { | |
322 | :type => :list_optional, :order => 6, |
|
322 | :type => :list_optional, :order => 6, | |
323 | :values => categories.collect{|s| [s.name, s.id.to_s] } |
|
323 | :values => categories.collect{|s| [s.name, s.id.to_s] } | |
324 | } |
|
324 | } | |
325 | end |
|
325 | end | |
326 | versions = project.shared_versions.all |
|
326 | versions = project.shared_versions.all | |
327 | unless versions.empty? |
|
327 | unless versions.empty? | |
328 | @available_filters["fixed_version_id"] = { |
|
328 | @available_filters["fixed_version_id"] = { | |
329 | :type => :list_optional, :order => 7, |
|
329 | :type => :list_optional, :order => 7, | |
330 | :values => versions.sort.collect{|s| ["#{s.project.name} - #{s.name}", s.id.to_s] } |
|
330 | :values => versions.sort.collect{|s| ["#{s.project.name} - #{s.name}", s.id.to_s] } | |
331 | } |
|
331 | } | |
332 | end |
|
332 | end | |
333 | add_custom_fields_filters(project.all_issue_custom_fields) |
|
333 | add_custom_fields_filters(project.all_issue_custom_fields) | |
334 | else |
|
334 | else | |
335 | # global filters for cross project issue list |
|
335 | # global filters for cross project issue list | |
336 | system_shared_versions = Version.visible.find_all_by_sharing('system') |
|
336 | system_shared_versions = Version.visible.find_all_by_sharing('system') | |
337 | unless system_shared_versions.empty? |
|
337 | unless system_shared_versions.empty? | |
338 | @available_filters["fixed_version_id"] = { |
|
338 | @available_filters["fixed_version_id"] = { | |
339 | :type => :list_optional, :order => 7, |
|
339 | :type => :list_optional, :order => 7, | |
340 | :values => system_shared_versions.sort.collect{|s| |
|
340 | :values => system_shared_versions.sort.collect{|s| | |
341 | ["#{s.project.name} - #{s.name}", s.id.to_s] |
|
341 | ["#{s.project.name} - #{s.name}", s.id.to_s] | |
342 | } |
|
342 | } | |
343 | } |
|
343 | } | |
344 | end |
|
344 | end | |
345 | add_custom_fields_filters( |
|
345 | add_custom_fields_filters( | |
346 | IssueCustomField.find(:all, |
|
346 | IssueCustomField.find(:all, | |
347 | :conditions => { |
|
347 | :conditions => { | |
348 | :is_filter => true, |
|
348 | :is_filter => true, | |
349 | :is_for_all => true |
|
349 | :is_for_all => true | |
350 | })) |
|
350 | })) | |
351 | end |
|
351 | end | |
352 | add_associations_custom_fields_filters :project, :author, :assigned_to, :fixed_version |
|
352 | add_associations_custom_fields_filters :project, :author, :assigned_to, :fixed_version | |
353 | if User.current.allowed_to?(:set_issues_private, nil, :global => true) || |
|
353 | if User.current.allowed_to?(:set_issues_private, nil, :global => true) || | |
354 | User.current.allowed_to?(:set_own_issues_private, nil, :global => true) |
|
354 | User.current.allowed_to?(:set_own_issues_private, nil, :global => true) | |
355 | @available_filters["is_private"] = { |
|
355 | @available_filters["is_private"] = { | |
356 | :type => :list, :order => 16, |
|
356 | :type => :list, :order => 16, | |
357 | :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]] |
|
357 | :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]] | |
358 | } |
|
358 | } | |
359 | end |
|
359 | end | |
360 | Tracker.disabled_core_fields(trackers).each {|field| |
|
360 | Tracker.disabled_core_fields(trackers).each {|field| | |
361 | @available_filters.delete field |
|
361 | @available_filters.delete field | |
362 | } |
|
362 | } | |
363 | @available_filters.each do |field, options| |
|
363 | @available_filters.each do |field, options| | |
364 | options[:name] ||= l(options[:label] || "field_#{field}".gsub(/_id$/, '')) |
|
364 | options[:name] ||= l(options[:label] || "field_#{field}".gsub(/_id$/, '')) | |
365 | end |
|
365 | end | |
366 | @available_filters |
|
366 | @available_filters | |
367 | end |
|
367 | end | |
368 |
|
368 | |||
369 | # Returns a representation of the available filters for JSON serialization |
|
369 | # Returns a representation of the available filters for JSON serialization | |
370 | def available_filters_as_json |
|
370 | def available_filters_as_json | |
371 | json = {} |
|
371 | json = {} | |
372 | available_filters.each do |field, options| |
|
372 | available_filters.each do |field, options| | |
373 | json[field] = options.slice(:type, :name, :values).stringify_keys |
|
373 | json[field] = options.slice(:type, :name, :values).stringify_keys | |
374 | end |
|
374 | end | |
375 | json |
|
375 | json | |
376 | end |
|
376 | end | |
377 |
|
377 | |||
378 | def all_projects |
|
378 | def all_projects | |
379 | @all_projects ||= Project.visible.all |
|
379 | @all_projects ||= Project.visible.all | |
380 | end |
|
380 | end | |
381 |
|
381 | |||
382 | def all_projects_values |
|
382 | def all_projects_values | |
383 | return @all_projects_values if @all_projects_values |
|
383 | return @all_projects_values if @all_projects_values | |
384 |
|
384 | |||
385 | values = [] |
|
385 | values = [] | |
386 | Project.project_tree(all_projects) do |p, level| |
|
386 | Project.project_tree(all_projects) do |p, level| | |
387 | prefix = (level > 0 ? ('--' * level + ' ') : '') |
|
387 | prefix = (level > 0 ? ('--' * level + ' ') : '') | |
388 | values << ["#{prefix}#{p.name}", p.id.to_s] |
|
388 | values << ["#{prefix}#{p.name}", p.id.to_s] | |
389 | end |
|
389 | end | |
390 | @all_projects_values = values |
|
390 | @all_projects_values = values | |
391 | end |
|
391 | end | |
392 |
|
392 | |||
393 | def add_filter(field, operator, values) |
|
393 | def add_filter(field, operator, values) | |
394 | # values must be an array |
|
394 | # values must be an array | |
395 | return unless values.nil? || values.is_a?(Array) |
|
395 | return unless values.nil? || values.is_a?(Array) | |
396 | # check if field is defined as an available filter |
|
396 | # check if field is defined as an available filter | |
397 | if available_filters.has_key? field |
|
397 | if available_filters.has_key? field | |
398 | filter_options = available_filters[field] |
|
398 | filter_options = available_filters[field] | |
399 | # check if operator is allowed for that filter |
|
399 | # check if operator is allowed for that filter | |
400 | #if @@operators_by_filter_type[filter_options[:type]].include? operator |
|
400 | #if @@operators_by_filter_type[filter_options[:type]].include? operator | |
401 | # allowed_values = values & ([""] + (filter_options[:values] || []).collect {|val| val[1]}) |
|
401 | # allowed_values = values & ([""] + (filter_options[:values] || []).collect {|val| val[1]}) | |
402 | # filters[field] = {:operator => operator, :values => allowed_values } if (allowed_values.first and !allowed_values.first.empty?) or ["o", "c", "!*", "*", "t"].include? operator |
|
402 | # filters[field] = {:operator => operator, :values => allowed_values } if (allowed_values.first and !allowed_values.first.empty?) or ["o", "c", "!*", "*", "t"].include? operator | |
403 | #end |
|
403 | #end | |
404 | filters[field] = {:operator => operator, :values => (values || [''])} |
|
404 | filters[field] = {:operator => operator, :values => (values || [''])} | |
405 | end |
|
405 | end | |
406 | end |
|
406 | end | |
407 |
|
407 | |||
408 | def add_short_filter(field, expression) |
|
408 | def add_short_filter(field, expression) | |
409 | return unless expression && available_filters.has_key?(field) |
|
409 | return unless expression && available_filters.has_key?(field) | |
410 | field_type = available_filters[field][:type] |
|
410 | field_type = available_filters[field][:type] | |
411 | @@operators_by_filter_type[field_type].sort.reverse.detect do |operator| |
|
411 | @@operators_by_filter_type[field_type].sort.reverse.detect do |operator| | |
412 | next unless expression =~ /^#{Regexp.escape(operator)}(.*)$/ |
|
412 | next unless expression =~ /^#{Regexp.escape(operator)}(.*)$/ | |
413 | add_filter field, operator, $1.present? ? $1.split('|') : [''] |
|
413 | add_filter field, operator, $1.present? ? $1.split('|') : [''] | |
414 | end || add_filter(field, '=', expression.split('|')) |
|
414 | end || add_filter(field, '=', expression.split('|')) | |
415 | end |
|
415 | end | |
416 |
|
416 | |||
417 | # Add multiple filters using +add_filter+ |
|
417 | # Add multiple filters using +add_filter+ | |
418 | def add_filters(fields, operators, values) |
|
418 | def add_filters(fields, operators, values) | |
419 | if fields.is_a?(Array) && operators.is_a?(Hash) && (values.nil? || values.is_a?(Hash)) |
|
419 | if fields.is_a?(Array) && operators.is_a?(Hash) && (values.nil? || values.is_a?(Hash)) | |
420 | fields.each do |field| |
|
420 | fields.each do |field| | |
421 | add_filter(field, operators[field], values && values[field]) |
|
421 | add_filter(field, operators[field], values && values[field]) | |
422 | end |
|
422 | end | |
423 | end |
|
423 | end | |
424 | end |
|
424 | end | |
425 |
|
425 | |||
426 | def has_filter?(field) |
|
426 | def has_filter?(field) | |
427 | filters and filters[field] |
|
427 | filters and filters[field] | |
428 | end |
|
428 | end | |
429 |
|
429 | |||
430 | def type_for(field) |
|
430 | def type_for(field) | |
431 | available_filters[field][:type] if available_filters.has_key?(field) |
|
431 | available_filters[field][:type] if available_filters.has_key?(field) | |
432 | end |
|
432 | end | |
433 |
|
433 | |||
434 | def operator_for(field) |
|
434 | def operator_for(field) | |
435 | has_filter?(field) ? filters[field][:operator] : nil |
|
435 | has_filter?(field) ? filters[field][:operator] : nil | |
436 | end |
|
436 | end | |
437 |
|
437 | |||
438 | def values_for(field) |
|
438 | def values_for(field) | |
439 | has_filter?(field) ? filters[field][:values] : nil |
|
439 | has_filter?(field) ? filters[field][:values] : nil | |
440 | end |
|
440 | end | |
441 |
|
441 | |||
442 | def value_for(field, index=0) |
|
442 | def value_for(field, index=0) | |
443 | (values_for(field) || [])[index] |
|
443 | (values_for(field) || [])[index] | |
444 | end |
|
444 | end | |
445 |
|
445 | |||
446 | def label_for(field) |
|
446 | def label_for(field) | |
447 | label = available_filters[field][:name] if available_filters.has_key?(field) |
|
447 | label = available_filters[field][:name] if available_filters.has_key?(field) | |
448 | label ||= l("field_#{field.to_s.gsub(/_id$/, '')}", :default => field) |
|
448 | label ||= l("field_#{field.to_s.gsub(/_id$/, '')}", :default => field) | |
449 | end |
|
449 | end | |
450 |
|
450 | |||
451 | def available_columns |
|
451 | def available_columns | |
452 | return @available_columns if @available_columns |
|
452 | return @available_columns if @available_columns | |
453 | @available_columns = ::Query.available_columns.dup |
|
453 | @available_columns = ::Query.available_columns.dup | |
454 | @available_columns += (project ? |
|
454 | @available_columns += (project ? | |
455 | project.all_issue_custom_fields : |
|
455 | project.all_issue_custom_fields : | |
456 | IssueCustomField.find(:all) |
|
456 | IssueCustomField.find(:all) | |
457 | ).collect {|cf| QueryCustomFieldColumn.new(cf) } |
|
457 | ).collect {|cf| QueryCustomFieldColumn.new(cf) } | |
458 |
|
458 | |||
459 | if User.current.allowed_to?(:view_time_entries, project, :global => true) |
|
459 | if User.current.allowed_to?(:view_time_entries, project, :global => true) | |
460 | index = nil |
|
460 | index = nil | |
461 | @available_columns.each_with_index {|column, i| index = i if column.name == :estimated_hours} |
|
461 | @available_columns.each_with_index {|column, i| index = i if column.name == :estimated_hours} | |
462 | index = (index ? index + 1 : -1) |
|
462 | index = (index ? index + 1 : -1) | |
463 | # insert the column after estimated_hours or at the end |
|
463 | # insert the column after estimated_hours or at the end | |
464 | @available_columns.insert index, QueryColumn.new(:spent_hours, |
|
464 | @available_columns.insert index, QueryColumn.new(:spent_hours, | |
465 | :sortable => "(SELECT COALESCE(SUM(hours), 0) FROM #{TimeEntry.table_name} WHERE #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id)", |
|
465 | :sortable => "(SELECT COALESCE(SUM(hours), 0) FROM #{TimeEntry.table_name} WHERE #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id)", | |
466 | :default_order => 'desc', |
|
466 | :default_order => 'desc', | |
467 | :caption => :label_spent_time |
|
467 | :caption => :label_spent_time | |
468 | ) |
|
468 | ) | |
469 | end |
|
469 | end | |
470 |
|
470 | |||
471 | if User.current.allowed_to?(:set_issues_private, nil, :global => true) || |
|
471 | if User.current.allowed_to?(:set_issues_private, nil, :global => true) || | |
472 | User.current.allowed_to?(:set_own_issues_private, nil, :global => true) |
|
472 | User.current.allowed_to?(:set_own_issues_private, nil, :global => true) | |
473 | @available_columns << QueryColumn.new(:is_private, :sortable => "#{Issue.table_name}.is_private") |
|
473 | @available_columns << QueryColumn.new(:is_private, :sortable => "#{Issue.table_name}.is_private") | |
474 | end |
|
474 | end | |
475 |
|
475 | |||
476 | disabled_fields = Tracker.disabled_core_fields(trackers).map {|field| field.sub(/_id$/, '')} |
|
476 | disabled_fields = Tracker.disabled_core_fields(trackers).map {|field| field.sub(/_id$/, '')} | |
477 | @available_columns.reject! {|column| |
|
477 | @available_columns.reject! {|column| | |
478 | disabled_fields.include?(column.name.to_s) |
|
478 | disabled_fields.include?(column.name.to_s) | |
479 | } |
|
479 | } | |
480 |
|
480 | |||
481 | @available_columns |
|
481 | @available_columns | |
482 | end |
|
482 | end | |
483 |
|
483 | |||
484 | def self.available_columns=(v) |
|
484 | def self.available_columns=(v) | |
485 | self.available_columns = (v) |
|
485 | self.available_columns = (v) | |
486 | end |
|
486 | end | |
487 |
|
487 | |||
488 | def self.add_available_column(column) |
|
488 | def self.add_available_column(column) | |
489 | self.available_columns << (column) if column.is_a?(QueryColumn) |
|
489 | self.available_columns << (column) if column.is_a?(QueryColumn) | |
490 | end |
|
490 | end | |
491 |
|
491 | |||
492 | # Returns an array of columns that can be used to group the results |
|
492 | # Returns an array of columns that can be used to group the results | |
493 | def groupable_columns |
|
493 | def groupable_columns | |
494 | available_columns.select {|c| c.groupable} |
|
494 | available_columns.select {|c| c.groupable} | |
495 | end |
|
495 | end | |
496 |
|
496 | |||
497 | # Returns a Hash of columns and the key for sorting |
|
497 | # Returns a Hash of columns and the key for sorting | |
498 | def sortable_columns |
|
498 | def sortable_columns | |
499 | {'id' => "#{Issue.table_name}.id"}.merge(available_columns.inject({}) {|h, column| |
|
499 | {'id' => "#{Issue.table_name}.id"}.merge(available_columns.inject({}) {|h, column| | |
500 | h[column.name.to_s] = column.sortable |
|
500 | h[column.name.to_s] = column.sortable | |
501 | h |
|
501 | h | |
502 | }) |
|
502 | }) | |
503 | end |
|
503 | end | |
504 |
|
504 | |||
505 | def columns |
|
505 | def columns | |
506 | # preserve the column_names order |
|
506 | # preserve the column_names order | |
507 | (has_default_columns? ? default_columns_names : column_names).collect do |name| |
|
507 | (has_default_columns? ? default_columns_names : column_names).collect do |name| | |
508 | available_columns.find { |col| col.name == name } |
|
508 | available_columns.find { |col| col.name == name } | |
509 | end.compact |
|
509 | end.compact | |
510 | end |
|
510 | end | |
511 |
|
511 | |||
512 | def default_columns_names |
|
512 | def default_columns_names | |
513 | @default_columns_names ||= begin |
|
513 | @default_columns_names ||= begin | |
514 | default_columns = Setting.issue_list_default_columns.map(&:to_sym) |
|
514 | default_columns = Setting.issue_list_default_columns.map(&:to_sym) | |
515 |
|
515 | |||
516 | project.present? ? default_columns : [:project] | default_columns |
|
516 | project.present? ? default_columns : [:project] | default_columns | |
517 | end |
|
517 | end | |
518 | end |
|
518 | end | |
519 |
|
519 | |||
520 | def column_names=(names) |
|
520 | def column_names=(names) | |
521 | if names |
|
521 | if names | |
522 | names = names.select {|n| n.is_a?(Symbol) || !n.blank? } |
|
522 | names = names.select {|n| n.is_a?(Symbol) || !n.blank? } | |
523 | names = names.collect {|n| n.is_a?(Symbol) ? n : n.to_sym } |
|
523 | names = names.collect {|n| n.is_a?(Symbol) ? n : n.to_sym } | |
524 | # Set column_names to nil if default columns |
|
524 | # Set column_names to nil if default columns | |
525 | if names == default_columns_names |
|
525 | if names == default_columns_names | |
526 | names = nil |
|
526 | names = nil | |
527 | end |
|
527 | end | |
528 | end |
|
528 | end | |
529 | write_attribute(:column_names, names) |
|
529 | write_attribute(:column_names, names) | |
530 | end |
|
530 | end | |
531 |
|
531 | |||
532 | def has_column?(column) |
|
532 | def has_column?(column) | |
533 | column_names && column_names.include?(column.is_a?(QueryColumn) ? column.name : column) |
|
533 | column_names && column_names.include?(column.is_a?(QueryColumn) ? column.name : column) | |
534 | end |
|
534 | end | |
535 |
|
535 | |||
536 | def has_default_columns? |
|
536 | def has_default_columns? | |
537 | column_names.nil? || column_names.empty? |
|
537 | column_names.nil? || column_names.empty? | |
538 | end |
|
538 | end | |
539 |
|
539 | |||
540 | def sort_criteria=(arg) |
|
540 | def sort_criteria=(arg) | |
541 | c = [] |
|
541 | c = [] | |
542 | if arg.is_a?(Hash) |
|
542 | if arg.is_a?(Hash) | |
543 | arg = arg.keys.sort.collect {|k| arg[k]} |
|
543 | arg = arg.keys.sort.collect {|k| arg[k]} | |
544 | end |
|
544 | end | |
545 |
c = arg.select {|k,o| !k.to_s.blank?}.slice(0,3).collect {|k,o| [k.to_s, o == 'desc' ? |
|
545 | c = arg.select {|k,o| !k.to_s.blank?}.slice(0,3).collect {|k,o| [k.to_s, (o == 'desc' || o == false) ? 'desc' : 'asc']} | |
546 | write_attribute(:sort_criteria, c) |
|
546 | write_attribute(:sort_criteria, c) | |
547 | end |
|
547 | end | |
548 |
|
548 | |||
549 | def sort_criteria |
|
549 | def sort_criteria | |
550 | read_attribute(:sort_criteria) || [] |
|
550 | read_attribute(:sort_criteria) || [] | |
551 | end |
|
551 | end | |
552 |
|
552 | |||
553 | def sort_criteria_key(arg) |
|
553 | def sort_criteria_key(arg) | |
554 | sort_criteria && sort_criteria[arg] && sort_criteria[arg].first |
|
554 | sort_criteria && sort_criteria[arg] && sort_criteria[arg].first | |
555 | end |
|
555 | end | |
556 |
|
556 | |||
557 | def sort_criteria_order(arg) |
|
557 | def sort_criteria_order(arg) | |
558 | sort_criteria && sort_criteria[arg] && sort_criteria[arg].last |
|
558 | sort_criteria && sort_criteria[arg] && sort_criteria[arg].last | |
559 | end |
|
559 | end | |
560 |
|
560 | |||
|
561 | def sort_criteria_order_for(key) | |||
|
562 | sort_criteria.detect {|k, order| key.to_s == k}.try(:last) | |||
|
563 | end | |||
|
564 | ||||
561 | # Returns the SQL sort order that should be prepended for grouping |
|
565 | # Returns the SQL sort order that should be prepended for grouping | |
562 | def group_by_sort_order |
|
566 | def group_by_sort_order | |
563 | if grouped? && (column = group_by_column) |
|
567 | if grouped? && (column = group_by_column) | |
|
568 | order = sort_criteria_order_for(column.name) || column.default_order | |||
564 | column.sortable.is_a?(Array) ? |
|
569 | column.sortable.is_a?(Array) ? | |
565 |
column.sortable.collect {|s| "#{s} #{ |
|
570 | column.sortable.collect {|s| "#{s} #{order}"}.join(',') : | |
566 |
"#{column.sortable} #{ |
|
571 | "#{column.sortable} #{order}" | |
567 | end |
|
572 | end | |
568 | end |
|
573 | end | |
569 |
|
574 | |||
570 | # Returns true if the query is a grouped query |
|
575 | # Returns true if the query is a grouped query | |
571 | def grouped? |
|
576 | def grouped? | |
572 | !group_by_column.nil? |
|
577 | !group_by_column.nil? | |
573 | end |
|
578 | end | |
574 |
|
579 | |||
575 | def group_by_column |
|
580 | def group_by_column | |
576 | groupable_columns.detect {|c| c.groupable && c.name.to_s == group_by} |
|
581 | groupable_columns.detect {|c| c.groupable && c.name.to_s == group_by} | |
577 | end |
|
582 | end | |
578 |
|
583 | |||
579 | def group_by_statement |
|
584 | def group_by_statement | |
580 | group_by_column.try(:groupable) |
|
585 | group_by_column.try(:groupable) | |
581 | end |
|
586 | end | |
582 |
|
587 | |||
583 | def project_statement |
|
588 | def project_statement | |
584 | project_clauses = [] |
|
589 | project_clauses = [] | |
585 | if project && !project.descendants.active.empty? |
|
590 | if project && !project.descendants.active.empty? | |
586 | ids = [project.id] |
|
591 | ids = [project.id] | |
587 | if has_filter?("subproject_id") |
|
592 | if has_filter?("subproject_id") | |
588 | case operator_for("subproject_id") |
|
593 | case operator_for("subproject_id") | |
589 | when '=' |
|
594 | when '=' | |
590 | # include the selected subprojects |
|
595 | # include the selected subprojects | |
591 | ids += values_for("subproject_id").each(&:to_i) |
|
596 | ids += values_for("subproject_id").each(&:to_i) | |
592 | when '!*' |
|
597 | when '!*' | |
593 | # main project only |
|
598 | # main project only | |
594 | else |
|
599 | else | |
595 | # all subprojects |
|
600 | # all subprojects | |
596 | ids += project.descendants.collect(&:id) |
|
601 | ids += project.descendants.collect(&:id) | |
597 | end |
|
602 | end | |
598 | elsif Setting.display_subprojects_issues? |
|
603 | elsif Setting.display_subprojects_issues? | |
599 | ids += project.descendants.collect(&:id) |
|
604 | ids += project.descendants.collect(&:id) | |
600 | end |
|
605 | end | |
601 | project_clauses << "#{Project.table_name}.id IN (%s)" % ids.join(',') |
|
606 | project_clauses << "#{Project.table_name}.id IN (%s)" % ids.join(',') | |
602 | elsif project |
|
607 | elsif project | |
603 | project_clauses << "#{Project.table_name}.id = %d" % project.id |
|
608 | project_clauses << "#{Project.table_name}.id = %d" % project.id | |
604 | end |
|
609 | end | |
605 | project_clauses.any? ? project_clauses.join(' AND ') : nil |
|
610 | project_clauses.any? ? project_clauses.join(' AND ') : nil | |
606 | end |
|
611 | end | |
607 |
|
612 | |||
608 | def statement |
|
613 | def statement | |
609 | # filters clauses |
|
614 | # filters clauses | |
610 | filters_clauses = [] |
|
615 | filters_clauses = [] | |
611 | filters.each_key do |field| |
|
616 | filters.each_key do |field| | |
612 | next if field == "subproject_id" |
|
617 | next if field == "subproject_id" | |
613 | v = values_for(field).clone |
|
618 | v = values_for(field).clone | |
614 | next unless v and !v.empty? |
|
619 | next unless v and !v.empty? | |
615 | operator = operator_for(field) |
|
620 | operator = operator_for(field) | |
616 |
|
621 | |||
617 | # "me" value subsitution |
|
622 | # "me" value subsitution | |
618 | if %w(assigned_to_id author_id watcher_id).include?(field) |
|
623 | if %w(assigned_to_id author_id watcher_id).include?(field) | |
619 | if v.delete("me") |
|
624 | if v.delete("me") | |
620 | if User.current.logged? |
|
625 | if User.current.logged? | |
621 | v.push(User.current.id.to_s) |
|
626 | v.push(User.current.id.to_s) | |
622 | v += User.current.group_ids.map(&:to_s) if field == 'assigned_to_id' |
|
627 | v += User.current.group_ids.map(&:to_s) if field == 'assigned_to_id' | |
623 | else |
|
628 | else | |
624 | v.push("0") |
|
629 | v.push("0") | |
625 | end |
|
630 | end | |
626 | end |
|
631 | end | |
627 | end |
|
632 | end | |
628 |
|
633 | |||
629 | if field == 'project_id' |
|
634 | if field == 'project_id' | |
630 | if v.delete('mine') |
|
635 | if v.delete('mine') | |
631 | v += User.current.memberships.map(&:project_id).map(&:to_s) |
|
636 | v += User.current.memberships.map(&:project_id).map(&:to_s) | |
632 | end |
|
637 | end | |
633 | end |
|
638 | end | |
634 |
|
639 | |||
635 | if field =~ /cf_(\d+)$/ |
|
640 | if field =~ /cf_(\d+)$/ | |
636 | # custom field |
|
641 | # custom field | |
637 | filters_clauses << sql_for_custom_field(field, operator, v, $1) |
|
642 | filters_clauses << sql_for_custom_field(field, operator, v, $1) | |
638 | elsif respond_to?("sql_for_#{field}_field") |
|
643 | elsif respond_to?("sql_for_#{field}_field") | |
639 | # specific statement |
|
644 | # specific statement | |
640 | filters_clauses << send("sql_for_#{field}_field", field, operator, v) |
|
645 | filters_clauses << send("sql_for_#{field}_field", field, operator, v) | |
641 | else |
|
646 | else | |
642 | # regular field |
|
647 | # regular field | |
643 | filters_clauses << '(' + sql_for_field(field, operator, v, Issue.table_name, field) + ')' |
|
648 | filters_clauses << '(' + sql_for_field(field, operator, v, Issue.table_name, field) + ')' | |
644 | end |
|
649 | end | |
645 | end if filters and valid? |
|
650 | end if filters and valid? | |
646 |
|
651 | |||
647 | filters_clauses << project_statement |
|
652 | filters_clauses << project_statement | |
648 | filters_clauses.reject!(&:blank?) |
|
653 | filters_clauses.reject!(&:blank?) | |
649 |
|
654 | |||
650 | filters_clauses.any? ? filters_clauses.join(' AND ') : nil |
|
655 | filters_clauses.any? ? filters_clauses.join(' AND ') : nil | |
651 | end |
|
656 | end | |
652 |
|
657 | |||
653 | # Returns the issue count |
|
658 | # Returns the issue count | |
654 | def issue_count |
|
659 | def issue_count | |
655 | Issue.visible.count(:include => [:status, :project], :conditions => statement) |
|
660 | Issue.visible.count(:include => [:status, :project], :conditions => statement) | |
656 | rescue ::ActiveRecord::StatementInvalid => e |
|
661 | rescue ::ActiveRecord::StatementInvalid => e | |
657 | raise StatementInvalid.new(e.message) |
|
662 | raise StatementInvalid.new(e.message) | |
658 | end |
|
663 | end | |
659 |
|
664 | |||
660 | # Returns the issue count by group or nil if query is not grouped |
|
665 | # Returns the issue count by group or nil if query is not grouped | |
661 | def issue_count_by_group |
|
666 | def issue_count_by_group | |
662 | r = nil |
|
667 | r = nil | |
663 | if grouped? |
|
668 | if grouped? | |
664 | begin |
|
669 | begin | |
665 | # Rails3 will raise an (unexpected) RecordNotFound if there's only a nil group value |
|
670 | # Rails3 will raise an (unexpected) RecordNotFound if there's only a nil group value | |
666 | r = Issue.visible.count(:group => group_by_statement, :include => [:status, :project], :conditions => statement) |
|
671 | r = Issue.visible.count(:group => group_by_statement, :include => [:status, :project], :conditions => statement) | |
667 | rescue ActiveRecord::RecordNotFound |
|
672 | rescue ActiveRecord::RecordNotFound | |
668 | r = {nil => issue_count} |
|
673 | r = {nil => issue_count} | |
669 | end |
|
674 | end | |
670 | c = group_by_column |
|
675 | c = group_by_column | |
671 | if c.is_a?(QueryCustomFieldColumn) |
|
676 | if c.is_a?(QueryCustomFieldColumn) | |
672 | r = r.keys.inject({}) {|h, k| h[c.custom_field.cast_value(k)] = r[k]; h} |
|
677 | r = r.keys.inject({}) {|h, k| h[c.custom_field.cast_value(k)] = r[k]; h} | |
673 | end |
|
678 | end | |
674 | end |
|
679 | end | |
675 | r |
|
680 | r | |
676 | rescue ::ActiveRecord::StatementInvalid => e |
|
681 | rescue ::ActiveRecord::StatementInvalid => e | |
677 | raise StatementInvalid.new(e.message) |
|
682 | raise StatementInvalid.new(e.message) | |
678 | end |
|
683 | end | |
679 |
|
684 | |||
680 | # Returns the issues |
|
685 | # Returns the issues | |
681 | # Valid options are :order, :offset, :limit, :include, :conditions |
|
686 | # Valid options are :order, :offset, :limit, :include, :conditions | |
682 | def issues(options={}) |
|
687 | def issues(options={}) | |
683 | order_option = [group_by_sort_order, options[:order]].reject {|s| s.blank?}.join(',') |
|
688 | order_option = [group_by_sort_order, options[:order]].reject {|s| s.blank?}.join(',') | |
684 | order_option = nil if order_option.blank? |
|
689 | order_option = nil if order_option.blank? | |
685 |
|
690 | |||
686 | issues = Issue.visible.scoped(:conditions => options[:conditions]).find :all, :include => ([:status, :project] + (options[:include] || [])).uniq, |
|
691 | issues = Issue.visible.scoped(:conditions => options[:conditions]).find :all, :include => ([:status, :project] + (options[:include] || [])).uniq, | |
687 | :conditions => statement, |
|
692 | :conditions => statement, | |
688 | :order => order_option, |
|
693 | :order => order_option, | |
689 | :joins => joins_for_order_statement(order_option), |
|
694 | :joins => joins_for_order_statement(order_option), | |
690 | :limit => options[:limit], |
|
695 | :limit => options[:limit], | |
691 | :offset => options[:offset] |
|
696 | :offset => options[:offset] | |
692 |
|
697 | |||
693 | if has_column?(:spent_hours) |
|
698 | if has_column?(:spent_hours) | |
694 | Issue.load_visible_spent_hours(issues) |
|
699 | Issue.load_visible_spent_hours(issues) | |
695 | end |
|
700 | end | |
696 | if has_column?(:relations) |
|
701 | if has_column?(:relations) | |
697 | Issue.load_visible_relations(issues) |
|
702 | Issue.load_visible_relations(issues) | |
698 | end |
|
703 | end | |
699 | issues |
|
704 | issues | |
700 | rescue ::ActiveRecord::StatementInvalid => e |
|
705 | rescue ::ActiveRecord::StatementInvalid => e | |
701 | raise StatementInvalid.new(e.message) |
|
706 | raise StatementInvalid.new(e.message) | |
702 | end |
|
707 | end | |
703 |
|
708 | |||
704 | # Returns the issues ids |
|
709 | # Returns the issues ids | |
705 | def issue_ids(options={}) |
|
710 | def issue_ids(options={}) | |
706 | order_option = [group_by_sort_order, options[:order]].reject {|s| s.blank?}.join(',') |
|
711 | order_option = [group_by_sort_order, options[:order]].reject {|s| s.blank?}.join(',') | |
707 | order_option = nil if order_option.blank? |
|
712 | order_option = nil if order_option.blank? | |
708 |
|
713 | |||
709 | Issue.visible.scoped(:conditions => options[:conditions]).scoped(:include => ([:status, :project] + (options[:include] || [])).uniq, |
|
714 | Issue.visible.scoped(:conditions => options[:conditions]).scoped(:include => ([:status, :project] + (options[:include] || [])).uniq, | |
710 | :conditions => statement, |
|
715 | :conditions => statement, | |
711 | :order => order_option, |
|
716 | :order => order_option, | |
712 | :joins => joins_for_order_statement(order_option), |
|
717 | :joins => joins_for_order_statement(order_option), | |
713 | :limit => options[:limit], |
|
718 | :limit => options[:limit], | |
714 | :offset => options[:offset]).find_ids |
|
719 | :offset => options[:offset]).find_ids | |
715 | rescue ::ActiveRecord::StatementInvalid => e |
|
720 | rescue ::ActiveRecord::StatementInvalid => e | |
716 | raise StatementInvalid.new(e.message) |
|
721 | raise StatementInvalid.new(e.message) | |
717 | end |
|
722 | end | |
718 |
|
723 | |||
719 | # Returns the journals |
|
724 | # Returns the journals | |
720 | # Valid options are :order, :offset, :limit |
|
725 | # Valid options are :order, :offset, :limit | |
721 | def journals(options={}) |
|
726 | def journals(options={}) | |
722 | Journal.visible.find :all, :include => [:details, :user, {:issue => [:project, :author, :tracker, :status]}], |
|
727 | Journal.visible.find :all, :include => [:details, :user, {:issue => [:project, :author, :tracker, :status]}], | |
723 | :conditions => statement, |
|
728 | :conditions => statement, | |
724 | :order => options[:order], |
|
729 | :order => options[:order], | |
725 | :limit => options[:limit], |
|
730 | :limit => options[:limit], | |
726 | :offset => options[:offset] |
|
731 | :offset => options[:offset] | |
727 | rescue ::ActiveRecord::StatementInvalid => e |
|
732 | rescue ::ActiveRecord::StatementInvalid => e | |
728 | raise StatementInvalid.new(e.message) |
|
733 | raise StatementInvalid.new(e.message) | |
729 | end |
|
734 | end | |
730 |
|
735 | |||
731 | # Returns the versions |
|
736 | # Returns the versions | |
732 | # Valid options are :conditions |
|
737 | # Valid options are :conditions | |
733 | def versions(options={}) |
|
738 | def versions(options={}) | |
734 | Version.visible.scoped(:conditions => options[:conditions]).find :all, :include => :project, :conditions => project_statement |
|
739 | Version.visible.scoped(:conditions => options[:conditions]).find :all, :include => :project, :conditions => project_statement | |
735 | rescue ::ActiveRecord::StatementInvalid => e |
|
740 | rescue ::ActiveRecord::StatementInvalid => e | |
736 | raise StatementInvalid.new(e.message) |
|
741 | raise StatementInvalid.new(e.message) | |
737 | end |
|
742 | end | |
738 |
|
743 | |||
739 | def sql_for_watcher_id_field(field, operator, value) |
|
744 | def sql_for_watcher_id_field(field, operator, value) | |
740 | db_table = Watcher.table_name |
|
745 | db_table = Watcher.table_name | |
741 | "#{Issue.table_name}.id #{ operator == '=' ? 'IN' : 'NOT IN' } (SELECT #{db_table}.watchable_id FROM #{db_table} WHERE #{db_table}.watchable_type='Issue' AND " + |
|
746 | "#{Issue.table_name}.id #{ operator == '=' ? 'IN' : 'NOT IN' } (SELECT #{db_table}.watchable_id FROM #{db_table} WHERE #{db_table}.watchable_type='Issue' AND " + | |
742 | sql_for_field(field, '=', value, db_table, 'user_id') + ')' |
|
747 | sql_for_field(field, '=', value, db_table, 'user_id') + ')' | |
743 | end |
|
748 | end | |
744 |
|
749 | |||
745 | def sql_for_member_of_group_field(field, operator, value) |
|
750 | def sql_for_member_of_group_field(field, operator, value) | |
746 | if operator == '*' # Any group |
|
751 | if operator == '*' # Any group | |
747 | groups = Group.all |
|
752 | groups = Group.all | |
748 | operator = '=' # Override the operator since we want to find by assigned_to |
|
753 | operator = '=' # Override the operator since we want to find by assigned_to | |
749 | elsif operator == "!*" |
|
754 | elsif operator == "!*" | |
750 | groups = Group.all |
|
755 | groups = Group.all | |
751 | operator = '!' # Override the operator since we want to find by assigned_to |
|
756 | operator = '!' # Override the operator since we want to find by assigned_to | |
752 | else |
|
757 | else | |
753 | groups = Group.find_all_by_id(value) |
|
758 | groups = Group.find_all_by_id(value) | |
754 | end |
|
759 | end | |
755 | groups ||= [] |
|
760 | groups ||= [] | |
756 |
|
761 | |||
757 | members_of_groups = groups.inject([]) {|user_ids, group| |
|
762 | members_of_groups = groups.inject([]) {|user_ids, group| | |
758 | if group && group.user_ids.present? |
|
763 | if group && group.user_ids.present? | |
759 | user_ids << group.user_ids |
|
764 | user_ids << group.user_ids | |
760 | end |
|
765 | end | |
761 | user_ids.flatten.uniq.compact |
|
766 | user_ids.flatten.uniq.compact | |
762 | }.sort.collect(&:to_s) |
|
767 | }.sort.collect(&:to_s) | |
763 |
|
768 | |||
764 | '(' + sql_for_field("assigned_to_id", operator, members_of_groups, Issue.table_name, "assigned_to_id", false) + ')' |
|
769 | '(' + sql_for_field("assigned_to_id", operator, members_of_groups, Issue.table_name, "assigned_to_id", false) + ')' | |
765 | end |
|
770 | end | |
766 |
|
771 | |||
767 | def sql_for_assigned_to_role_field(field, operator, value) |
|
772 | def sql_for_assigned_to_role_field(field, operator, value) | |
768 | case operator |
|
773 | case operator | |
769 | when "*", "!*" # Member / Not member |
|
774 | when "*", "!*" # Member / Not member | |
770 | sw = operator == "!*" ? 'NOT' : '' |
|
775 | sw = operator == "!*" ? 'NOT' : '' | |
771 | nl = operator == "!*" ? "#{Issue.table_name}.assigned_to_id IS NULL OR" : '' |
|
776 | nl = operator == "!*" ? "#{Issue.table_name}.assigned_to_id IS NULL OR" : '' | |
772 | "(#{nl} #{Issue.table_name}.assigned_to_id #{sw} IN (SELECT DISTINCT #{Member.table_name}.user_id FROM #{Member.table_name}" + |
|
777 | "(#{nl} #{Issue.table_name}.assigned_to_id #{sw} IN (SELECT DISTINCT #{Member.table_name}.user_id FROM #{Member.table_name}" + | |
773 | " WHERE #{Member.table_name}.project_id = #{Issue.table_name}.project_id))" |
|
778 | " WHERE #{Member.table_name}.project_id = #{Issue.table_name}.project_id))" | |
774 | when "=", "!" |
|
779 | when "=", "!" | |
775 | role_cond = value.any? ? |
|
780 | role_cond = value.any? ? | |
776 | "#{MemberRole.table_name}.role_id IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")" : |
|
781 | "#{MemberRole.table_name}.role_id IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")" : | |
777 | "1=0" |
|
782 | "1=0" | |
778 |
|
783 | |||
779 | sw = operator == "!" ? 'NOT' : '' |
|
784 | sw = operator == "!" ? 'NOT' : '' | |
780 | nl = operator == "!" ? "#{Issue.table_name}.assigned_to_id IS NULL OR" : '' |
|
785 | nl = operator == "!" ? "#{Issue.table_name}.assigned_to_id IS NULL OR" : '' | |
781 | "(#{nl} #{Issue.table_name}.assigned_to_id #{sw} IN (SELECT DISTINCT #{Member.table_name}.user_id FROM #{Member.table_name}, #{MemberRole.table_name}" + |
|
786 | "(#{nl} #{Issue.table_name}.assigned_to_id #{sw} IN (SELECT DISTINCT #{Member.table_name}.user_id FROM #{Member.table_name}, #{MemberRole.table_name}" + | |
782 | " WHERE #{Member.table_name}.project_id = #{Issue.table_name}.project_id AND #{Member.table_name}.id = #{MemberRole.table_name}.member_id AND #{role_cond}))" |
|
787 | " WHERE #{Member.table_name}.project_id = #{Issue.table_name}.project_id AND #{Member.table_name}.id = #{MemberRole.table_name}.member_id AND #{role_cond}))" | |
783 | end |
|
788 | end | |
784 | end |
|
789 | end | |
785 |
|
790 | |||
786 | def sql_for_is_private_field(field, operator, value) |
|
791 | def sql_for_is_private_field(field, operator, value) | |
787 | op = (operator == "=" ? 'IN' : 'NOT IN') |
|
792 | op = (operator == "=" ? 'IN' : 'NOT IN') | |
788 | va = value.map {|v| v == '0' ? connection.quoted_false : connection.quoted_true}.uniq.join(',') |
|
793 | va = value.map {|v| v == '0' ? connection.quoted_false : connection.quoted_true}.uniq.join(',') | |
789 |
|
794 | |||
790 | "#{Issue.table_name}.is_private #{op} (#{va})" |
|
795 | "#{Issue.table_name}.is_private #{op} (#{va})" | |
791 | end |
|
796 | end | |
792 |
|
797 | |||
793 | def sql_for_relations(field, operator, value, options={}) |
|
798 | def sql_for_relations(field, operator, value, options={}) | |
794 | relation_options = IssueRelation::TYPES[field] |
|
799 | relation_options = IssueRelation::TYPES[field] | |
795 | return relation_options unless relation_options |
|
800 | return relation_options unless relation_options | |
796 |
|
801 | |||
797 | relation_type = field |
|
802 | relation_type = field | |
798 | join_column, target_join_column = "issue_from_id", "issue_to_id" |
|
803 | join_column, target_join_column = "issue_from_id", "issue_to_id" | |
799 | if relation_options[:reverse] || options[:reverse] |
|
804 | if relation_options[:reverse] || options[:reverse] | |
800 | relation_type = relation_options[:reverse] || relation_type |
|
805 | relation_type = relation_options[:reverse] || relation_type | |
801 | join_column, target_join_column = target_join_column, join_column |
|
806 | join_column, target_join_column = target_join_column, join_column | |
802 | end |
|
807 | end | |
803 |
|
808 | |||
804 | sql = case operator |
|
809 | sql = case operator | |
805 | when "*", "!*" |
|
810 | when "*", "!*" | |
806 | op = (operator == "*" ? 'IN' : 'NOT IN') |
|
811 | op = (operator == "*" ? 'IN' : 'NOT IN') | |
807 | "#{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)}')" |
|
812 | "#{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)}')" | |
808 | when "=", "!" |
|
813 | when "=", "!" | |
809 | op = (operator == "=" ? 'IN' : 'NOT IN') |
|
814 | op = (operator == "=" ? 'IN' : 'NOT IN') | |
810 | "#{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})" |
|
815 | "#{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})" | |
811 | when "=p", "=!p", "!p" |
|
816 | when "=p", "=!p", "!p" | |
812 | op = (operator == "!p" ? 'NOT IN' : 'IN') |
|
817 | op = (operator == "!p" ? 'NOT IN' : 'IN') | |
813 | comp = (operator == "=!p" ? '<>' : '=') |
|
818 | comp = (operator == "=!p" ? '<>' : '=') | |
814 | "#{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})" |
|
819 | "#{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})" | |
815 | end |
|
820 | end | |
816 |
|
821 | |||
817 | if relation_options[:sym] == field && !options[:reverse] |
|
822 | if relation_options[:sym] == field && !options[:reverse] | |
818 | sqls = [sql, sql_for_relations(field, operator, value, :reverse => true)] |
|
823 | sqls = [sql, sql_for_relations(field, operator, value, :reverse => true)] | |
819 | sqls.join(["!", "!*", "!p"].include?(operator) ? " AND " : " OR ") |
|
824 | sqls.join(["!", "!*", "!p"].include?(operator) ? " AND " : " OR ") | |
820 | else |
|
825 | else | |
821 | sql |
|
826 | sql | |
822 | end |
|
827 | end | |
823 | end |
|
828 | end | |
824 |
|
829 | |||
825 | IssueRelation::TYPES.keys.each do |relation_type| |
|
830 | IssueRelation::TYPES.keys.each do |relation_type| | |
826 | alias_method "sql_for_#{relation_type}_field".to_sym, :sql_for_relations |
|
831 | alias_method "sql_for_#{relation_type}_field".to_sym, :sql_for_relations | |
827 | end |
|
832 | end | |
828 |
|
833 | |||
829 | private |
|
834 | private | |
830 |
|
835 | |||
831 | def sql_for_custom_field(field, operator, value, custom_field_id) |
|
836 | def sql_for_custom_field(field, operator, value, custom_field_id) | |
832 | db_table = CustomValue.table_name |
|
837 | db_table = CustomValue.table_name | |
833 | db_field = 'value' |
|
838 | db_field = 'value' | |
834 | filter = @available_filters[field] |
|
839 | filter = @available_filters[field] | |
835 | return nil unless filter |
|
840 | return nil unless filter | |
836 | if filter[:format] == 'user' |
|
841 | if filter[:format] == 'user' | |
837 | if value.delete('me') |
|
842 | if value.delete('me') | |
838 | value.push User.current.id.to_s |
|
843 | value.push User.current.id.to_s | |
839 | end |
|
844 | end | |
840 | end |
|
845 | end | |
841 | not_in = nil |
|
846 | not_in = nil | |
842 | if operator == '!' |
|
847 | if operator == '!' | |
843 | # Makes ! operator work for custom fields with multiple values |
|
848 | # Makes ! operator work for custom fields with multiple values | |
844 | operator = '=' |
|
849 | operator = '=' | |
845 | not_in = 'NOT' |
|
850 | not_in = 'NOT' | |
846 | end |
|
851 | end | |
847 | customized_key = "id" |
|
852 | customized_key = "id" | |
848 | customized_class = Issue |
|
853 | customized_class = Issue | |
849 | if field =~ /^(.+)\.cf_/ |
|
854 | if field =~ /^(.+)\.cf_/ | |
850 | assoc = $1 |
|
855 | assoc = $1 | |
851 | customized_key = "#{assoc}_id" |
|
856 | customized_key = "#{assoc}_id" | |
852 | customized_class = Issue.reflect_on_association(assoc.to_sym).klass.base_class rescue nil |
|
857 | customized_class = Issue.reflect_on_association(assoc.to_sym).klass.base_class rescue nil | |
853 | raise "Unknown Issue association #{assoc}" unless customized_class |
|
858 | raise "Unknown Issue association #{assoc}" unless customized_class | |
854 | end |
|
859 | end | |
855 | "#{Issue.table_name}.#{customized_key} #{not_in} IN (SELECT #{customized_class.table_name}.id FROM #{customized_class.table_name} LEFT OUTER JOIN #{db_table} ON #{db_table}.customized_type='#{customized_class}' AND #{db_table}.customized_id=#{customized_class.table_name}.id AND #{db_table}.custom_field_id=#{custom_field_id} WHERE " + |
|
860 | "#{Issue.table_name}.#{customized_key} #{not_in} IN (SELECT #{customized_class.table_name}.id FROM #{customized_class.table_name} LEFT OUTER JOIN #{db_table} ON #{db_table}.customized_type='#{customized_class}' AND #{db_table}.customized_id=#{customized_class.table_name}.id AND #{db_table}.custom_field_id=#{custom_field_id} WHERE " + | |
856 | sql_for_field(field, operator, value, db_table, db_field, true) + ')' |
|
861 | sql_for_field(field, operator, value, db_table, db_field, true) + ')' | |
857 | end |
|
862 | end | |
858 |
|
863 | |||
859 | # Helper method to generate the WHERE sql for a +field+, +operator+ and a +value+ |
|
864 | # Helper method to generate the WHERE sql for a +field+, +operator+ and a +value+ | |
860 | def sql_for_field(field, operator, value, db_table, db_field, is_custom_filter=false) |
|
865 | def sql_for_field(field, operator, value, db_table, db_field, is_custom_filter=false) | |
861 | sql = '' |
|
866 | sql = '' | |
862 | case operator |
|
867 | case operator | |
863 | when "=" |
|
868 | when "=" | |
864 | if value.any? |
|
869 | if value.any? | |
865 | case type_for(field) |
|
870 | case type_for(field) | |
866 | when :date, :date_past |
|
871 | when :date, :date_past | |
867 | sql = date_clause(db_table, db_field, (Date.parse(value.first) rescue nil), (Date.parse(value.first) rescue nil)) |
|
872 | sql = date_clause(db_table, db_field, (Date.parse(value.first) rescue nil), (Date.parse(value.first) rescue nil)) | |
868 | when :integer |
|
873 | when :integer | |
869 | if is_custom_filter |
|
874 | if is_custom_filter | |
870 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) = #{value.first.to_i})" |
|
875 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) = #{value.first.to_i})" | |
871 | else |
|
876 | else | |
872 | sql = "#{db_table}.#{db_field} = #{value.first.to_i}" |
|
877 | sql = "#{db_table}.#{db_field} = #{value.first.to_i}" | |
873 | end |
|
878 | end | |
874 | when :float |
|
879 | when :float | |
875 | if is_custom_filter |
|
880 | if is_custom_filter | |
876 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) BETWEEN #{value.first.to_f - 1e-5} AND #{value.first.to_f + 1e-5})" |
|
881 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) BETWEEN #{value.first.to_f - 1e-5} AND #{value.first.to_f + 1e-5})" | |
877 | else |
|
882 | else | |
878 | sql = "#{db_table}.#{db_field} BETWEEN #{value.first.to_f - 1e-5} AND #{value.first.to_f + 1e-5}" |
|
883 | sql = "#{db_table}.#{db_field} BETWEEN #{value.first.to_f - 1e-5} AND #{value.first.to_f + 1e-5}" | |
879 | end |
|
884 | end | |
880 | else |
|
885 | else | |
881 | sql = "#{db_table}.#{db_field} IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")" |
|
886 | sql = "#{db_table}.#{db_field} IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")" | |
882 | end |
|
887 | end | |
883 | else |
|
888 | else | |
884 | # IN an empty set |
|
889 | # IN an empty set | |
885 | sql = "1=0" |
|
890 | sql = "1=0" | |
886 | end |
|
891 | end | |
887 | when "!" |
|
892 | when "!" | |
888 | if value.any? |
|
893 | if value.any? | |
889 | sql = "(#{db_table}.#{db_field} IS NULL OR #{db_table}.#{db_field} NOT IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + "))" |
|
894 | sql = "(#{db_table}.#{db_field} IS NULL OR #{db_table}.#{db_field} NOT IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + "))" | |
890 | else |
|
895 | else | |
891 | # NOT IN an empty set |
|
896 | # NOT IN an empty set | |
892 | sql = "1=1" |
|
897 | sql = "1=1" | |
893 | end |
|
898 | end | |
894 | when "!*" |
|
899 | when "!*" | |
895 | sql = "#{db_table}.#{db_field} IS NULL" |
|
900 | sql = "#{db_table}.#{db_field} IS NULL" | |
896 | sql << " OR #{db_table}.#{db_field} = ''" if is_custom_filter |
|
901 | sql << " OR #{db_table}.#{db_field} = ''" if is_custom_filter | |
897 | when "*" |
|
902 | when "*" | |
898 | sql = "#{db_table}.#{db_field} IS NOT NULL" |
|
903 | sql = "#{db_table}.#{db_field} IS NOT NULL" | |
899 | sql << " AND #{db_table}.#{db_field} <> ''" if is_custom_filter |
|
904 | sql << " AND #{db_table}.#{db_field} <> ''" if is_custom_filter | |
900 | when ">=" |
|
905 | when ">=" | |
901 | if [:date, :date_past].include?(type_for(field)) |
|
906 | if [:date, :date_past].include?(type_for(field)) | |
902 | sql = date_clause(db_table, db_field, (Date.parse(value.first) rescue nil), nil) |
|
907 | sql = date_clause(db_table, db_field, (Date.parse(value.first) rescue nil), nil) | |
903 | else |
|
908 | else | |
904 | if is_custom_filter |
|
909 | if is_custom_filter | |
905 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) >= #{value.first.to_f})" |
|
910 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) >= #{value.first.to_f})" | |
906 | else |
|
911 | else | |
907 | sql = "#{db_table}.#{db_field} >= #{value.first.to_f}" |
|
912 | sql = "#{db_table}.#{db_field} >= #{value.first.to_f}" | |
908 | end |
|
913 | end | |
909 | end |
|
914 | end | |
910 | when "<=" |
|
915 | when "<=" | |
911 | if [:date, :date_past].include?(type_for(field)) |
|
916 | if [:date, :date_past].include?(type_for(field)) | |
912 | sql = date_clause(db_table, db_field, nil, (Date.parse(value.first) rescue nil)) |
|
917 | sql = date_clause(db_table, db_field, nil, (Date.parse(value.first) rescue nil)) | |
913 | else |
|
918 | else | |
914 | if is_custom_filter |
|
919 | if is_custom_filter | |
915 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) <= #{value.first.to_f})" |
|
920 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) <= #{value.first.to_f})" | |
916 | else |
|
921 | else | |
917 | sql = "#{db_table}.#{db_field} <= #{value.first.to_f}" |
|
922 | sql = "#{db_table}.#{db_field} <= #{value.first.to_f}" | |
918 | end |
|
923 | end | |
919 | end |
|
924 | end | |
920 | when "><" |
|
925 | when "><" | |
921 | if [:date, :date_past].include?(type_for(field)) |
|
926 | if [:date, :date_past].include?(type_for(field)) | |
922 | sql = date_clause(db_table, db_field, (Date.parse(value[0]) rescue nil), (Date.parse(value[1]) rescue nil)) |
|
927 | sql = date_clause(db_table, db_field, (Date.parse(value[0]) rescue nil), (Date.parse(value[1]) rescue nil)) | |
923 | else |
|
928 | else | |
924 | if is_custom_filter |
|
929 | if is_custom_filter | |
925 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) BETWEEN #{value[0].to_f} AND #{value[1].to_f})" |
|
930 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) BETWEEN #{value[0].to_f} AND #{value[1].to_f})" | |
926 | else |
|
931 | else | |
927 | sql = "#{db_table}.#{db_field} BETWEEN #{value[0].to_f} AND #{value[1].to_f}" |
|
932 | sql = "#{db_table}.#{db_field} BETWEEN #{value[0].to_f} AND #{value[1].to_f}" | |
928 | end |
|
933 | end | |
929 | end |
|
934 | end | |
930 | when "o" |
|
935 | when "o" | |
931 | sql = "#{Issue.table_name}.status_id IN (SELECT id FROM #{IssueStatus.table_name} WHERE is_closed=#{connection.quoted_false})" if field == "status_id" |
|
936 | sql = "#{Issue.table_name}.status_id IN (SELECT id FROM #{IssueStatus.table_name} WHERE is_closed=#{connection.quoted_false})" if field == "status_id" | |
932 | when "c" |
|
937 | when "c" | |
933 | sql = "#{Issue.table_name}.status_id IN (SELECT id FROM #{IssueStatus.table_name} WHERE is_closed=#{connection.quoted_true})" if field == "status_id" |
|
938 | sql = "#{Issue.table_name}.status_id IN (SELECT id FROM #{IssueStatus.table_name} WHERE is_closed=#{connection.quoted_true})" if field == "status_id" | |
934 | when ">t-" |
|
939 | when ">t-" | |
935 | sql = relative_date_clause(db_table, db_field, - value.first.to_i, 0) |
|
940 | sql = relative_date_clause(db_table, db_field, - value.first.to_i, 0) | |
936 | when "<t-" |
|
941 | when "<t-" | |
937 | sql = relative_date_clause(db_table, db_field, nil, - value.first.to_i) |
|
942 | sql = relative_date_clause(db_table, db_field, nil, - value.first.to_i) | |
938 | when "t-" |
|
943 | when "t-" | |
939 | sql = relative_date_clause(db_table, db_field, - value.first.to_i, - value.first.to_i) |
|
944 | sql = relative_date_clause(db_table, db_field, - value.first.to_i, - value.first.to_i) | |
940 | when ">t+" |
|
945 | when ">t+" | |
941 | sql = relative_date_clause(db_table, db_field, value.first.to_i, nil) |
|
946 | sql = relative_date_clause(db_table, db_field, value.first.to_i, nil) | |
942 | when "<t+" |
|
947 | when "<t+" | |
943 | sql = relative_date_clause(db_table, db_field, 0, value.first.to_i) |
|
948 | sql = relative_date_clause(db_table, db_field, 0, value.first.to_i) | |
944 | when "t+" |
|
949 | when "t+" | |
945 | sql = relative_date_clause(db_table, db_field, value.first.to_i, value.first.to_i) |
|
950 | sql = relative_date_clause(db_table, db_field, value.first.to_i, value.first.to_i) | |
946 | when "t" |
|
951 | when "t" | |
947 | sql = relative_date_clause(db_table, db_field, 0, 0) |
|
952 | sql = relative_date_clause(db_table, db_field, 0, 0) | |
948 | when "w" |
|
953 | when "w" | |
949 | first_day_of_week = l(:general_first_day_of_week).to_i |
|
954 | first_day_of_week = l(:general_first_day_of_week).to_i | |
950 | day_of_week = Date.today.cwday |
|
955 | day_of_week = Date.today.cwday | |
951 | days_ago = (day_of_week >= first_day_of_week ? day_of_week - first_day_of_week : day_of_week + 7 - first_day_of_week) |
|
956 | days_ago = (day_of_week >= first_day_of_week ? day_of_week - first_day_of_week : day_of_week + 7 - first_day_of_week) | |
952 | sql = relative_date_clause(db_table, db_field, - days_ago, - days_ago + 6) |
|
957 | sql = relative_date_clause(db_table, db_field, - days_ago, - days_ago + 6) | |
953 | when "~" |
|
958 | when "~" | |
954 | sql = "LOWER(#{db_table}.#{db_field}) LIKE '%#{connection.quote_string(value.first.to_s.downcase)}%'" |
|
959 | sql = "LOWER(#{db_table}.#{db_field}) LIKE '%#{connection.quote_string(value.first.to_s.downcase)}%'" | |
955 | when "!~" |
|
960 | when "!~" | |
956 | sql = "LOWER(#{db_table}.#{db_field}) NOT LIKE '%#{connection.quote_string(value.first.to_s.downcase)}%'" |
|
961 | sql = "LOWER(#{db_table}.#{db_field}) NOT LIKE '%#{connection.quote_string(value.first.to_s.downcase)}%'" | |
957 | else |
|
962 | else | |
958 | raise "Unknown query operator #{operator}" |
|
963 | raise "Unknown query operator #{operator}" | |
959 | end |
|
964 | end | |
960 |
|
965 | |||
961 | return sql |
|
966 | return sql | |
962 | end |
|
967 | end | |
963 |
|
968 | |||
964 | def add_custom_fields_filters(custom_fields, assoc=nil) |
|
969 | def add_custom_fields_filters(custom_fields, assoc=nil) | |
965 | return unless custom_fields.present? |
|
970 | return unless custom_fields.present? | |
966 | @available_filters ||= {} |
|
971 | @available_filters ||= {} | |
967 |
|
972 | |||
968 | custom_fields.select(&:is_filter?).each do |field| |
|
973 | custom_fields.select(&:is_filter?).each do |field| | |
969 | case field.field_format |
|
974 | case field.field_format | |
970 | when "text" |
|
975 | when "text" | |
971 | options = { :type => :text, :order => 20 } |
|
976 | options = { :type => :text, :order => 20 } | |
972 | when "list" |
|
977 | when "list" | |
973 | options = { :type => :list_optional, :values => field.possible_values, :order => 20} |
|
978 | options = { :type => :list_optional, :values => field.possible_values, :order => 20} | |
974 | when "date" |
|
979 | when "date" | |
975 | options = { :type => :date, :order => 20 } |
|
980 | options = { :type => :date, :order => 20 } | |
976 | when "bool" |
|
981 | when "bool" | |
977 | options = { :type => :list, :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]], :order => 20 } |
|
982 | options = { :type => :list, :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]], :order => 20 } | |
978 | when "int" |
|
983 | when "int" | |
979 | options = { :type => :integer, :order => 20 } |
|
984 | options = { :type => :integer, :order => 20 } | |
980 | when "float" |
|
985 | when "float" | |
981 | options = { :type => :float, :order => 20 } |
|
986 | options = { :type => :float, :order => 20 } | |
982 | when "user", "version" |
|
987 | when "user", "version" | |
983 | next unless project |
|
988 | next unless project | |
984 | values = field.possible_values_options(project) |
|
989 | values = field.possible_values_options(project) | |
985 | if User.current.logged? && field.field_format == 'user' |
|
990 | if User.current.logged? && field.field_format == 'user' | |
986 | values.unshift ["<< #{l(:label_me)} >>", "me"] |
|
991 | values.unshift ["<< #{l(:label_me)} >>", "me"] | |
987 | end |
|
992 | end | |
988 | options = { :type => :list_optional, :values => values, :order => 20} |
|
993 | options = { :type => :list_optional, :values => values, :order => 20} | |
989 | else |
|
994 | else | |
990 | options = { :type => :string, :order => 20 } |
|
995 | options = { :type => :string, :order => 20 } | |
991 | end |
|
996 | end | |
992 | filter_id = "cf_#{field.id}" |
|
997 | filter_id = "cf_#{field.id}" | |
993 | filter_name = field.name |
|
998 | filter_name = field.name | |
994 | if assoc.present? |
|
999 | if assoc.present? | |
995 | filter_id = "#{assoc}.#{filter_id}" |
|
1000 | filter_id = "#{assoc}.#{filter_id}" | |
996 | filter_name = l("label_attribute_of_#{assoc}", :name => filter_name) |
|
1001 | filter_name = l("label_attribute_of_#{assoc}", :name => filter_name) | |
997 | end |
|
1002 | end | |
998 | @available_filters[filter_id] = options.merge({ |
|
1003 | @available_filters[filter_id] = options.merge({ | |
999 | :name => filter_name, |
|
1004 | :name => filter_name, | |
1000 | :format => field.field_format, |
|
1005 | :format => field.field_format, | |
1001 | :field => field |
|
1006 | :field => field | |
1002 | }) |
|
1007 | }) | |
1003 | end |
|
1008 | end | |
1004 | end |
|
1009 | end | |
1005 |
|
1010 | |||
1006 | def add_associations_custom_fields_filters(*associations) |
|
1011 | def add_associations_custom_fields_filters(*associations) | |
1007 | fields_by_class = CustomField.where(:is_filter => true).group_by(&:class) |
|
1012 | fields_by_class = CustomField.where(:is_filter => true).group_by(&:class) | |
1008 | associations.each do |assoc| |
|
1013 | associations.each do |assoc| | |
1009 | association_klass = Issue.reflect_on_association(assoc).klass |
|
1014 | association_klass = Issue.reflect_on_association(assoc).klass | |
1010 | fields_by_class.each do |field_class, fields| |
|
1015 | fields_by_class.each do |field_class, fields| | |
1011 | if field_class.customized_class <= association_klass |
|
1016 | if field_class.customized_class <= association_klass | |
1012 | add_custom_fields_filters(fields, assoc) |
|
1017 | add_custom_fields_filters(fields, assoc) | |
1013 | end |
|
1018 | end | |
1014 | end |
|
1019 | end | |
1015 | end |
|
1020 | end | |
1016 | end |
|
1021 | end | |
1017 |
|
1022 | |||
1018 | # Returns a SQL clause for a date or datetime field. |
|
1023 | # Returns a SQL clause for a date or datetime field. | |
1019 | def date_clause(table, field, from, to) |
|
1024 | def date_clause(table, field, from, to) | |
1020 | s = [] |
|
1025 | s = [] | |
1021 | if from |
|
1026 | if from | |
1022 | from_yesterday = from - 1 |
|
1027 | from_yesterday = from - 1 | |
1023 | from_yesterday_time = Time.local(from_yesterday.year, from_yesterday.month, from_yesterday.day) |
|
1028 | from_yesterday_time = Time.local(from_yesterday.year, from_yesterday.month, from_yesterday.day) | |
1024 | if self.class.default_timezone == :utc |
|
1029 | if self.class.default_timezone == :utc | |
1025 | from_yesterday_time = from_yesterday_time.utc |
|
1030 | from_yesterday_time = from_yesterday_time.utc | |
1026 | end |
|
1031 | end | |
1027 | s << ("#{table}.#{field} > '%s'" % [connection.quoted_date(from_yesterday_time.end_of_day)]) |
|
1032 | s << ("#{table}.#{field} > '%s'" % [connection.quoted_date(from_yesterday_time.end_of_day)]) | |
1028 | end |
|
1033 | end | |
1029 | if to |
|
1034 | if to | |
1030 | to_time = Time.local(to.year, to.month, to.day) |
|
1035 | to_time = Time.local(to.year, to.month, to.day) | |
1031 | if self.class.default_timezone == :utc |
|
1036 | if self.class.default_timezone == :utc | |
1032 | to_time = to_time.utc |
|
1037 | to_time = to_time.utc | |
1033 | end |
|
1038 | end | |
1034 | s << ("#{table}.#{field} <= '%s'" % [connection.quoted_date(to_time.end_of_day)]) |
|
1039 | s << ("#{table}.#{field} <= '%s'" % [connection.quoted_date(to_time.end_of_day)]) | |
1035 | end |
|
1040 | end | |
1036 | s.join(' AND ') |
|
1041 | s.join(' AND ') | |
1037 | end |
|
1042 | end | |
1038 |
|
1043 | |||
1039 | # Returns a SQL clause for a date or datetime field using relative dates. |
|
1044 | # Returns a SQL clause for a date or datetime field using relative dates. | |
1040 | def relative_date_clause(table, field, days_from, days_to) |
|
1045 | def relative_date_clause(table, field, days_from, days_to) | |
1041 | date_clause(table, field, (days_from ? Date.today + days_from : nil), (days_to ? Date.today + days_to : nil)) |
|
1046 | date_clause(table, field, (days_from ? Date.today + days_from : nil), (days_to ? Date.today + days_to : nil)) | |
1042 | end |
|
1047 | end | |
1043 |
|
1048 | |||
1044 | # Additional joins required for the given sort options |
|
1049 | # Additional joins required for the given sort options | |
1045 | def joins_for_order_statement(order_options) |
|
1050 | def joins_for_order_statement(order_options) | |
1046 | joins = [] |
|
1051 | joins = [] | |
1047 |
|
1052 | |||
1048 | if order_options |
|
1053 | if order_options | |
1049 | if order_options.include?('authors') |
|
1054 | if order_options.include?('authors') | |
1050 | joins << "LEFT OUTER JOIN #{User.table_name} authors ON authors.id = #{Issue.table_name}.author_id" |
|
1055 | joins << "LEFT OUTER JOIN #{User.table_name} authors ON authors.id = #{Issue.table_name}.author_id" | |
1051 | end |
|
1056 | end | |
1052 | order_options.scan(/cf_\d+/).uniq.each do |name| |
|
1057 | order_options.scan(/cf_\d+/).uniq.each do |name| | |
1053 | column = available_columns.detect {|c| c.name.to_s == name} |
|
1058 | column = available_columns.detect {|c| c.name.to_s == name} | |
1054 | join = column && column.custom_field.join_for_order_statement |
|
1059 | join = column && column.custom_field.join_for_order_statement | |
1055 | if join |
|
1060 | if join | |
1056 | joins << join |
|
1061 | joins << join | |
1057 | end |
|
1062 | end | |
1058 | end |
|
1063 | end | |
1059 | end |
|
1064 | end | |
1060 |
|
1065 | |||
1061 | joins.any? ? joins.join(' ') : nil |
|
1066 | joins.any? ? joins.join(' ') : nil | |
1062 | end |
|
1067 | end | |
1063 | end |
|
1068 | end |
1 | NO CONTENT: modified file |
|
NO CONTENT: modified file | ||
The requested commit or file is too big and content was truncated. Show full diff |
General Comments 0
You need to be logged in to leave comments.
Login now