|
@@
-1,344
+1,344
|
|
1
|
# redMine - project management software
|
|
1
|
# redMine - project management software
|
|
2
|
# Copyright (C) 2006-2007 Jean-Philippe Lang
|
|
2
|
# Copyright (C) 2006-2007 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
|
|
19
|
attr_accessor :name, :sortable
|
|
20
|
include GLoc
|
|
20
|
include GLoc
|
|
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
|
end
|
|
25
|
end
|
|
26
|
|
|
26
|
|
|
27
|
def caption
|
|
27
|
def caption
|
|
28
|
set_language_if_valid(User.current.language)
|
|
28
|
set_language_if_valid(User.current.language)
|
|
29
|
l("field_#{name}")
|
|
29
|
l("field_#{name}")
|
|
30
|
end
|
|
30
|
end
|
|
31
|
end
|
|
31
|
end
|
|
32
|
|
|
32
|
|
|
33
|
class QueryCustomFieldColumn < QueryColumn
|
|
33
|
class QueryCustomFieldColumn < QueryColumn
|
|
34
|
|
|
34
|
|
|
35
|
def initialize(custom_field)
|
|
35
|
def initialize(custom_field)
|
|
36
|
self.name = "cf_#{custom_field.id}".to_sym
|
|
36
|
self.name = "cf_#{custom_field.id}".to_sym
|
|
37
|
self.sortable = false
|
|
37
|
self.sortable = false
|
|
38
|
@cf = custom_field
|
|
38
|
@cf = custom_field
|
|
39
|
end
|
|
39
|
end
|
|
40
|
|
|
40
|
|
|
41
|
def caption
|
|
41
|
def caption
|
|
42
|
@cf.name
|
|
42
|
@cf.name
|
|
43
|
end
|
|
43
|
end
|
|
44
|
|
|
44
|
|
|
45
|
def custom_field
|
|
45
|
def custom_field
|
|
46
|
@cf
|
|
46
|
@cf
|
|
47
|
end
|
|
47
|
end
|
|
48
|
end
|
|
48
|
end
|
|
49
|
|
|
49
|
|
|
50
|
class Query < ActiveRecord::Base
|
|
50
|
class Query < ActiveRecord::Base
|
|
51
|
belongs_to :project
|
|
51
|
belongs_to :project
|
|
52
|
belongs_to :user
|
|
52
|
belongs_to :user
|
|
53
|
serialize :filters
|
|
53
|
serialize :filters
|
|
54
|
serialize :column_names
|
|
54
|
serialize :column_names
|
|
55
|
|
|
55
|
|
|
56
|
attr_protected :project, :user
|
|
56
|
attr_protected :project, :user
|
|
57
|
attr_accessor :executed_by
|
|
57
|
attr_accessor :executed_by
|
|
58
|
|
|
58
|
|
|
59
|
validates_presence_of :name, :on => :save
|
|
59
|
validates_presence_of :name, :on => :save
|
|
60
|
validates_length_of :name, :maximum => 255
|
|
60
|
validates_length_of :name, :maximum => 255
|
|
61
|
|
|
61
|
|
|
62
|
@@operators = { "=" => :label_equals,
|
|
62
|
@@operators = { "=" => :label_equals,
|
|
63
|
"!" => :label_not_equals,
|
|
63
|
"!" => :label_not_equals,
|
|
64
|
"o" => :label_open_issues,
|
|
64
|
"o" => :label_open_issues,
|
|
65
|
"c" => :label_closed_issues,
|
|
65
|
"c" => :label_closed_issues,
|
|
66
|
"!*" => :label_none,
|
|
66
|
"!*" => :label_none,
|
|
67
|
"*" => :label_all,
|
|
67
|
"*" => :label_all,
|
|
68
|
">=" => '>=',
|
|
68
|
">=" => '>=',
|
|
69
|
"<=" => '<=',
|
|
69
|
"<=" => '<=',
|
|
70
|
"<t+" => :label_in_less_than,
|
|
70
|
"<t+" => :label_in_less_than,
|
|
71
|
">t+" => :label_in_more_than,
|
|
71
|
">t+" => :label_in_more_than,
|
|
72
|
"t+" => :label_in,
|
|
72
|
"t+" => :label_in,
|
|
73
|
"t" => :label_today,
|
|
73
|
"t" => :label_today,
|
|
74
|
"w" => :label_this_week,
|
|
74
|
"w" => :label_this_week,
|
|
75
|
">t-" => :label_less_than_ago,
|
|
75
|
">t-" => :label_less_than_ago,
|
|
76
|
"<t-" => :label_more_than_ago,
|
|
76
|
"<t-" => :label_more_than_ago,
|
|
77
|
"t-" => :label_ago,
|
|
77
|
"t-" => :label_ago,
|
|
78
|
"~" => :label_contains,
|
|
78
|
"~" => :label_contains,
|
|
79
|
"!~" => :label_not_contains }
|
|
79
|
"!~" => :label_not_contains }
|
|
80
|
|
|
80
|
|
|
81
|
cattr_reader :operators
|
|
81
|
cattr_reader :operators
|
|
82
|
|
|
82
|
|
|
83
|
@@operators_by_filter_type = { :list => [ "=", "!" ],
|
|
83
|
@@operators_by_filter_type = { :list => [ "=", "!" ],
|
|
84
|
:list_status => [ "o", "=", "!", "c", "*" ],
|
|
84
|
:list_status => [ "o", "=", "!", "c", "*" ],
|
|
85
|
:list_optional => [ "=", "!", "!*", "*" ],
|
|
85
|
:list_optional => [ "=", "!", "!*", "*" ],
|
|
86
|
:list_one_or_more => [ "*", "=" ],
|
|
86
|
:list_one_or_more => [ "*", "=" ],
|
|
87
|
:date => [ "<t+", ">t+", "t+", "t", "w", ">t-", "<t-", "t-" ],
|
|
87
|
:date => [ "<t+", ">t+", "t+", "t", "w", ">t-", "<t-", "t-" ],
|
|
88
|
:date_past => [ ">t-", "<t-", "t-", "t", "w" ],
|
|
88
|
:date_past => [ ">t-", "<t-", "t-", "t", "w" ],
|
|
89
|
:string => [ "=", "~", "!", "!~" ],
|
|
89
|
:string => [ "=", "~", "!", "!~" ],
|
|
90
|
:text => [ "~", "!~" ],
|
|
90
|
:text => [ "~", "!~" ],
|
|
91
|
:integer => [ "=", ">=", "<=" ] }
|
|
91
|
:integer => [ "=", ">=", "<=" ] }
|
|
92
|
|
|
92
|
|
|
93
|
cattr_reader :operators_by_filter_type
|
|
93
|
cattr_reader :operators_by_filter_type
|
|
94
|
|
|
94
|
|
|
95
|
@@available_columns = [
|
|
95
|
@@available_columns = [
|
|
96
|
QueryColumn.new(:tracker, :sortable => "#{Tracker.table_name}.position"),
|
|
96
|
QueryColumn.new(:tracker, :sortable => "#{Tracker.table_name}.position"),
|
|
97
|
QueryColumn.new(:status, :sortable => "#{IssueStatus.table_name}.position"),
|
|
97
|
QueryColumn.new(:status, :sortable => "#{IssueStatus.table_name}.position"),
|
|
98
|
QueryColumn.new(:priority, :sortable => "#{Enumeration.table_name}.position"),
|
|
98
|
QueryColumn.new(:priority, :sortable => "#{Enumeration.table_name}.position"),
|
|
99
|
QueryColumn.new(:subject),
|
|
99
|
QueryColumn.new(:subject),
|
|
100
|
QueryColumn.new(:assigned_to, :sortable => "#{User.table_name}.lastname"),
|
|
100
|
QueryColumn.new(:assigned_to, :sortable => "#{User.table_name}.lastname"),
|
|
101
|
QueryColumn.new(:updated_on, :sortable => "#{Issue.table_name}.updated_on"),
|
|
101
|
QueryColumn.new(:updated_on, :sortable => "#{Issue.table_name}.updated_on"),
|
|
102
|
QueryColumn.new(:category, :sortable => "#{IssueCategory.table_name}.name"),
|
|
102
|
QueryColumn.new(:category, :sortable => "#{IssueCategory.table_name}.name"),
|
|
103
|
QueryColumn.new(:fixed_version),
|
|
103
|
QueryColumn.new(:fixed_version),
|
|
104
|
QueryColumn.new(:start_date, :sortable => "#{Issue.table_name}.start_date"),
|
|
104
|
QueryColumn.new(:start_date, :sortable => "#{Issue.table_name}.start_date"),
|
|
105
|
QueryColumn.new(:due_date, :sortable => "#{Issue.table_name}.due_date"),
|
|
105
|
QueryColumn.new(:due_date, :sortable => "#{Issue.table_name}.due_date"),
|
|
106
|
QueryColumn.new(:estimated_hours, :sortable => "#{Issue.table_name}.estimated_hours"),
|
|
106
|
QueryColumn.new(:estimated_hours, :sortable => "#{Issue.table_name}.estimated_hours"),
|
|
107
|
QueryColumn.new(:done_ratio, :sortable => "#{Issue.table_name}.done_ratio"),
|
|
107
|
QueryColumn.new(:done_ratio, :sortable => "#{Issue.table_name}.done_ratio"),
|
|
108
|
QueryColumn.new(:created_on, :sortable => "#{Issue.table_name}.created_on"),
|
|
108
|
QueryColumn.new(:created_on, :sortable => "#{Issue.table_name}.created_on"),
|
|
109
|
]
|
|
109
|
]
|
|
110
|
cattr_reader :available_columns
|
|
110
|
cattr_reader :available_columns
|
|
111
|
|
|
111
|
|
|
112
|
def initialize(attributes = nil)
|
|
112
|
def initialize(attributes = nil)
|
|
113
|
super attributes
|
|
113
|
super attributes
|
|
114
|
self.filters ||= { 'status_id' => {:operator => "o", :values => [""]} }
|
|
114
|
self.filters ||= { 'status_id' => {:operator => "o", :values => [""]} }
|
|
115
|
@executed_by = User.current.logged? ? User.current : nil
|
|
115
|
@executed_by = User.current.logged? ? User.current : nil
|
|
116
|
set_language_if_valid(executed_by.language) if executed_by
|
|
116
|
set_language_if_valid(executed_by.language) if executed_by
|
|
117
|
end
|
|
117
|
end
|
|
118
|
|
|
118
|
|
|
119
|
def validate
|
|
119
|
def validate
|
|
120
|
filters.each_key do |field|
|
|
120
|
filters.each_key do |field|
|
|
121
|
errors.add label_for(field), :activerecord_error_blank unless
|
|
121
|
errors.add label_for(field), :activerecord_error_blank unless
|
|
122
|
# filter requires one or more values
|
|
122
|
# filter requires one or more values
|
|
123
|
(values_for(field) and !values_for(field).first.empty?) or
|
|
123
|
(values_for(field) and !values_for(field).first.empty?) or
|
|
124
|
# filter doesn't require any value
|
|
124
|
# filter doesn't require any value
|
|
125
|
["o", "c", "!*", "*", "t", "w"].include? operator_for(field)
|
|
125
|
["o", "c", "!*", "*", "t", "w"].include? operator_for(field)
|
|
126
|
end if filters
|
|
126
|
end if filters
|
|
127
|
end
|
|
127
|
end
|
|
128
|
|
|
128
|
|
|
129
|
def editable_by?(user)
|
|
129
|
def editable_by?(user)
|
|
130
|
return false unless user
|
|
130
|
return false unless user
|
|
131
|
return true if !is_public && self.user_id == user.id
|
|
131
|
return true if !is_public && self.user_id == user.id
|
|
132
|
is_public && user.allowed_to?(:manage_public_queries, project)
|
|
132
|
is_public && user.allowed_to?(:manage_public_queries, project)
|
|
133
|
end
|
|
133
|
end
|
|
134
|
|
|
134
|
|
|
135
|
def available_filters
|
|
135
|
def available_filters
|
|
136
|
return @available_filters if @available_filters
|
|
136
|
return @available_filters if @available_filters
|
|
137
|
@available_filters = { "status_id" => { :type => :list_status, :order => 1, :values => IssueStatus.find(:all, :order => 'position').collect{|s| [s.name, s.id.to_s] } },
|
|
137
|
@available_filters = { "status_id" => { :type => :list_status, :order => 1, :values => IssueStatus.find(:all, :order => 'position').collect{|s| [s.name, s.id.to_s] } },
|
|
138
|
"tracker_id" => { :type => :list, :order => 2, :values => Tracker.find(:all, :order => 'position').collect{|s| [s.name, s.id.to_s] } },
|
|
138
|
"tracker_id" => { :type => :list, :order => 2, :values => Tracker.find(:all, :order => 'position').collect{|s| [s.name, s.id.to_s] } },
|
|
139
|
"priority_id" => { :type => :list, :order => 3, :values => Enumeration.find(:all, :conditions => ['opt=?','IPRI']).collect{|s| [s.name, s.id.to_s] } },
|
|
139
|
"priority_id" => { :type => :list, :order => 3, :values => Enumeration.find(:all, :conditions => ['opt=?','IPRI']).collect{|s| [s.name, s.id.to_s] } },
|
|
140
|
"subject" => { :type => :text, :order => 8 },
|
|
140
|
"subject" => { :type => :text, :order => 8 },
|
|
141
|
"created_on" => { :type => :date_past, :order => 9 },
|
|
141
|
"created_on" => { :type => :date_past, :order => 9 },
|
|
142
|
"updated_on" => { :type => :date_past, :order => 10 },
|
|
142
|
"updated_on" => { :type => :date_past, :order => 10 },
|
|
143
|
"start_date" => { :type => :date, :order => 11 },
|
|
143
|
"start_date" => { :type => :date, :order => 11 },
|
|
144
|
"due_date" => { :type => :date, :order => 12 },
|
|
144
|
"due_date" => { :type => :date, :order => 12 },
|
|
145
|
"done_ratio" => { :type => :integer, :order => 13 }}
|
|
145
|
"done_ratio" => { :type => :integer, :order => 13 }}
|
|
146
|
|
|
146
|
|
|
147
|
user_values = []
|
|
147
|
user_values = []
|
|
148
|
user_values << ["<< #{l(:label_me)} >>", "me"] if executed_by
|
|
148
|
user_values << ["<< #{l(:label_me)} >>", "me"] if executed_by
|
|
149
|
if project
|
|
149
|
if project
|
|
150
|
user_values += project.users.collect{|s| [s.name, s.id.to_s] }
|
|
150
|
user_values += project.users.collect{|s| [s.name, s.id.to_s] }
|
|
151
|
elsif executed_by
|
|
151
|
elsif executed_by
|
|
152
|
# members of the user's projects
|
|
152
|
# members of the user's projects
|
|
153
|
user_values += executed_by.projects.collect(&:users).flatten.uniq.sort.collect{|s| [s.name, s.id.to_s] }
|
|
153
|
user_values += executed_by.projects.collect(&:users).flatten.uniq.sort.collect{|s| [s.name, s.id.to_s] }
|
|
154
|
end
|
|
154
|
end
|
|
155
|
@available_filters["assigned_to_id"] = { :type => :list_optional, :order => 4, :values => user_values } unless user_values.empty?
|
|
155
|
@available_filters["assigned_to_id"] = { :type => :list_optional, :order => 4, :values => user_values } unless user_values.empty?
|
|
156
|
@available_filters["author_id"] = { :type => :list, :order => 5, :values => user_values } unless user_values.empty?
|
|
156
|
@available_filters["author_id"] = { :type => :list, :order => 5, :values => user_values } unless user_values.empty?
|
|
157
|
|
|
157
|
|
|
158
|
if project
|
|
158
|
if project
|
|
159
|
# project specific filters
|
|
159
|
# project specific filters
|
|
160
|
@available_filters["category_id"] = { :type => :list_optional, :order => 6, :values => @project.issue_categories.collect{|s| [s.name, s.id.to_s] } }
|
|
160
|
@available_filters["category_id"] = { :type => :list_optional, :order => 6, :values => @project.issue_categories.collect{|s| [s.name, s.id.to_s] } }
|
|
161
|
@available_filters["fixed_version_id"] = { :type => :list_optional, :order => 7, :values => @project.versions.sort.collect{|s| [s.name, s.id.to_s] } }
|
|
161
|
@available_filters["fixed_version_id"] = { :type => :list_optional, :order => 7, :values => @project.versions.sort.collect{|s| [s.name, s.id.to_s] } }
|
|
162
|
unless @project.active_children.empty?
|
|
162
|
unless @project.active_children.empty?
|
|
163
|
@available_filters["subproject_id"] = { :type => :list_one_or_more, :order => 13, :values => @project.active_children.collect{|s| [s.name, s.id.to_s] } }
|
|
163
|
@available_filters["subproject_id"] = { :type => :list_one_or_more, :order => 13, :values => @project.active_children.collect{|s| [s.name, s.id.to_s] } }
|
|
164
|
end
|
|
164
|
end
|
|
165
|
@project.all_custom_fields.select(&:is_filter?).each do |field|
|
|
165
|
@project.all_custom_fields.select(&:is_filter?).each do |field|
|
|
166
|
case field.field_format
|
|
166
|
case field.field_format
|
|
167
|
when "string", "int"
|
|
|
|
|
168
|
options = { :type => :string, :order => 20 }
|
|
|
|
|
169
|
when "text"
|
|
167
|
when "text"
|
|
170
|
options = { :type => :text, :order => 20 }
|
|
168
|
options = { :type => :text, :order => 20 }
|
|
171
|
when "list"
|
|
169
|
when "list"
|
|
172
|
options = { :type => :list_optional, :values => field.possible_values, :order => 20}
|
|
170
|
options = { :type => :list_optional, :values => field.possible_values, :order => 20}
|
|
173
|
when "date"
|
|
171
|
when "date"
|
|
174
|
options = { :type => :date, :order => 20 }
|
|
172
|
options = { :type => :date, :order => 20 }
|
|
175
|
when "bool"
|
|
173
|
when "bool"
|
|
176
|
options = { :type => :list, :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]], :order => 20 }
|
|
174
|
options = { :type => :list, :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]], :order => 20 }
|
|
|
|
|
175
|
else
|
|
|
|
|
176
|
options = { :type => :string, :order => 20 }
|
|
177
|
end
|
|
177
|
end
|
|
178
|
@available_filters["cf_#{field.id}"] = options.merge({ :name => field.name })
|
|
178
|
@available_filters["cf_#{field.id}"] = options.merge({ :name => field.name })
|
|
179
|
end
|
|
179
|
end
|
|
180
|
# remove category filter if no category defined
|
|
180
|
# remove category filter if no category defined
|
|
181
|
@available_filters.delete "category_id" if @available_filters["category_id"][:values].empty?
|
|
181
|
@available_filters.delete "category_id" if @available_filters["category_id"][:values].empty?
|
|
182
|
end
|
|
182
|
end
|
|
183
|
@available_filters
|
|
183
|
@available_filters
|
|
184
|
end
|
|
184
|
end
|
|
185
|
|
|
185
|
|
|
186
|
def add_filter(field, operator, values)
|
|
186
|
def add_filter(field, operator, values)
|
|
187
|
# values must be an array
|
|
187
|
# values must be an array
|
|
188
|
return unless values and values.is_a? Array # and !values.first.empty?
|
|
188
|
return unless values and values.is_a? Array # and !values.first.empty?
|
|
189
|
# check if field is defined as an available filter
|
|
189
|
# check if field is defined as an available filter
|
|
190
|
if available_filters.has_key? field
|
|
190
|
if available_filters.has_key? field
|
|
191
|
filter_options = available_filters[field]
|
|
191
|
filter_options = available_filters[field]
|
|
192
|
# check if operator is allowed for that filter
|
|
192
|
# check if operator is allowed for that filter
|
|
193
|
#if @@operators_by_filter_type[filter_options[:type]].include? operator
|
|
193
|
#if @@operators_by_filter_type[filter_options[:type]].include? operator
|
|
194
|
# allowed_values = values & ([""] + (filter_options[:values] || []).collect {|val| val[1]})
|
|
194
|
# allowed_values = values & ([""] + (filter_options[:values] || []).collect {|val| val[1]})
|
|
195
|
# filters[field] = {:operator => operator, :values => allowed_values } if (allowed_values.first and !allowed_values.first.empty?) or ["o", "c", "!*", "*", "t"].include? operator
|
|
195
|
# filters[field] = {:operator => operator, :values => allowed_values } if (allowed_values.first and !allowed_values.first.empty?) or ["o", "c", "!*", "*", "t"].include? operator
|
|
196
|
#end
|
|
196
|
#end
|
|
197
|
filters[field] = {:operator => operator, :values => values }
|
|
197
|
filters[field] = {:operator => operator, :values => values }
|
|
198
|
end
|
|
198
|
end
|
|
199
|
end
|
|
199
|
end
|
|
200
|
|
|
200
|
|
|
201
|
def add_short_filter(field, expression)
|
|
201
|
def add_short_filter(field, expression)
|
|
202
|
return unless expression
|
|
202
|
return unless expression
|
|
203
|
parms = expression.scan(/^(o|c|\!|\*)?(.*)$/).first
|
|
203
|
parms = expression.scan(/^(o|c|\!|\*)?(.*)$/).first
|
|
204
|
add_filter field, (parms[0] || "="), [parms[1] || ""]
|
|
204
|
add_filter field, (parms[0] || "="), [parms[1] || ""]
|
|
205
|
end
|
|
205
|
end
|
|
206
|
|
|
206
|
|
|
207
|
def has_filter?(field)
|
|
207
|
def has_filter?(field)
|
|
208
|
filters and filters[field]
|
|
208
|
filters and filters[field]
|
|
209
|
end
|
|
209
|
end
|
|
210
|
|
|
210
|
|
|
211
|
def operator_for(field)
|
|
211
|
def operator_for(field)
|
|
212
|
has_filter?(field) ? filters[field][:operator] : nil
|
|
212
|
has_filter?(field) ? filters[field][:operator] : nil
|
|
213
|
end
|
|
213
|
end
|
|
214
|
|
|
214
|
|
|
215
|
def values_for(field)
|
|
215
|
def values_for(field)
|
|
216
|
has_filter?(field) ? filters[field][:values] : nil
|
|
216
|
has_filter?(field) ? filters[field][:values] : nil
|
|
217
|
end
|
|
217
|
end
|
|
218
|
|
|
218
|
|
|
219
|
def label_for(field)
|
|
219
|
def label_for(field)
|
|
220
|
label = @available_filters[field][:name] if @available_filters.has_key?(field)
|
|
220
|
label = @available_filters[field][:name] if @available_filters.has_key?(field)
|
|
221
|
label ||= field.gsub(/\_id$/, "")
|
|
221
|
label ||= field.gsub(/\_id$/, "")
|
|
222
|
end
|
|
222
|
end
|
|
223
|
|
|
223
|
|
|
224
|
def available_columns
|
|
224
|
def available_columns
|
|
225
|
return @available_columns if @available_columns
|
|
225
|
return @available_columns if @available_columns
|
|
226
|
@available_columns = Query.available_columns
|
|
226
|
@available_columns = Query.available_columns
|
|
227
|
@available_columns += (project ?
|
|
227
|
@available_columns += (project ?
|
|
228
|
project.custom_fields :
|
|
228
|
project.custom_fields :
|
|
229
|
IssueCustomField.find(:all, :conditions => {:is_for_all => true})
|
|
229
|
IssueCustomField.find(:all, :conditions => {:is_for_all => true})
|
|
230
|
).collect {|cf| QueryCustomFieldColumn.new(cf) }
|
|
230
|
).collect {|cf| QueryCustomFieldColumn.new(cf) }
|
|
231
|
end
|
|
231
|
end
|
|
232
|
|
|
232
|
|
|
233
|
def columns
|
|
233
|
def columns
|
|
234
|
if has_default_columns?
|
|
234
|
if has_default_columns?
|
|
235
|
available_columns.select {|c| Setting.issue_list_default_columns.include?(c.name.to_s) }
|
|
235
|
available_columns.select {|c| Setting.issue_list_default_columns.include?(c.name.to_s) }
|
|
236
|
else
|
|
236
|
else
|
|
237
|
# preserve the column_names order
|
|
237
|
# preserve the column_names order
|
|
238
|
column_names.collect {|name| available_columns.find {|col| col.name == name}}.compact
|
|
238
|
column_names.collect {|name| available_columns.find {|col| col.name == name}}.compact
|
|
239
|
end
|
|
239
|
end
|
|
240
|
end
|
|
240
|
end
|
|
241
|
|
|
241
|
|
|
242
|
def column_names=(names)
|
|
242
|
def column_names=(names)
|
|
243
|
names = names.select {|n| n.is_a?(Symbol) || !n.blank? } if names
|
|
243
|
names = names.select {|n| n.is_a?(Symbol) || !n.blank? } if names
|
|
244
|
names = names.collect {|n| n.is_a?(Symbol) ? n : n.to_sym } if names
|
|
244
|
names = names.collect {|n| n.is_a?(Symbol) ? n : n.to_sym } if names
|
|
245
|
write_attribute(:column_names, names)
|
|
245
|
write_attribute(:column_names, names)
|
|
246
|
end
|
|
246
|
end
|
|
247
|
|
|
247
|
|
|
248
|
def has_column?(column)
|
|
248
|
def has_column?(column)
|
|
249
|
column_names && column_names.include?(column.name)
|
|
249
|
column_names && column_names.include?(column.name)
|
|
250
|
end
|
|
250
|
end
|
|
251
|
|
|
251
|
|
|
252
|
def has_default_columns?
|
|
252
|
def has_default_columns?
|
|
253
|
column_names.nil? || column_names.empty?
|
|
253
|
column_names.nil? || column_names.empty?
|
|
254
|
end
|
|
254
|
end
|
|
255
|
|
|
255
|
|
|
256
|
def statement
|
|
256
|
def statement
|
|
257
|
# project/subprojects clause
|
|
257
|
# project/subprojects clause
|
|
258
|
clause = ''
|
|
258
|
clause = ''
|
|
259
|
if project && has_filter?("subproject_id")
|
|
259
|
if project && has_filter?("subproject_id")
|
|
260
|
subproject_ids = []
|
|
260
|
subproject_ids = []
|
|
261
|
if operator_for("subproject_id") == "="
|
|
261
|
if operator_for("subproject_id") == "="
|
|
262
|
subproject_ids = values_for("subproject_id").each(&:to_i)
|
|
262
|
subproject_ids = values_for("subproject_id").each(&:to_i)
|
|
263
|
else
|
|
263
|
else
|
|
264
|
subproject_ids = project.active_children.collect{|p| p.id}
|
|
264
|
subproject_ids = project.active_children.collect{|p| p.id}
|
|
265
|
end
|
|
265
|
end
|
|
266
|
clause << "#{Issue.table_name}.project_id IN (%d,%s)" % [project.id, subproject_ids.join(",")] if project
|
|
266
|
clause << "#{Issue.table_name}.project_id IN (%d,%s)" % [project.id, subproject_ids.join(",")] if project
|
|
267
|
elsif project
|
|
267
|
elsif project
|
|
268
|
clause << "#{Issue.table_name}.project_id=%d" % project.id
|
|
268
|
clause << "#{Issue.table_name}.project_id=%d" % project.id
|
|
269
|
else
|
|
269
|
else
|
|
270
|
clause << Project.visible_by(executed_by)
|
|
270
|
clause << Project.visible_by(executed_by)
|
|
271
|
end
|
|
271
|
end
|
|
272
|
|
|
272
|
|
|
273
|
# filters clauses
|
|
273
|
# filters clauses
|
|
274
|
filters_clauses = []
|
|
274
|
filters_clauses = []
|
|
275
|
filters.each_key do |field|
|
|
275
|
filters.each_key do |field|
|
|
276
|
next if field == "subproject_id"
|
|
276
|
next if field == "subproject_id"
|
|
277
|
v = values_for(field).clone
|
|
277
|
v = values_for(field).clone
|
|
278
|
next unless v and !v.empty?
|
|
278
|
next unless v and !v.empty?
|
|
279
|
|
|
279
|
|
|
280
|
sql = ''
|
|
280
|
sql = ''
|
|
281
|
if field =~ /^cf_(\d+)$/
|
|
281
|
if field =~ /^cf_(\d+)$/
|
|
282
|
# custom field
|
|
282
|
# custom field
|
|
283
|
db_table = CustomValue.table_name
|
|
283
|
db_table = CustomValue.table_name
|
|
284
|
db_field = 'value'
|
|
284
|
db_field = 'value'
|
|
285
|
sql << "#{Issue.table_name}.id IN (SELECT #{db_table}.customized_id FROM #{db_table} where #{db_table}.customized_type='Issue' AND #{db_table}.customized_id=#{Issue.table_name}.id AND #{db_table}.custom_field_id=#{$1} AND "
|
|
285
|
sql << "#{Issue.table_name}.id IN (SELECT #{db_table}.customized_id FROM #{db_table} where #{db_table}.customized_type='Issue' AND #{db_table}.customized_id=#{Issue.table_name}.id AND #{db_table}.custom_field_id=#{$1} AND "
|
|
286
|
else
|
|
286
|
else
|
|
287
|
# regular field
|
|
287
|
# regular field
|
|
288
|
db_table = Issue.table_name
|
|
288
|
db_table = Issue.table_name
|
|
289
|
db_field = field
|
|
289
|
db_field = field
|
|
290
|
sql << '('
|
|
290
|
sql << '('
|
|
291
|
end
|
|
291
|
end
|
|
292
|
|
|
292
|
|
|
293
|
# "me" value subsitution
|
|
293
|
# "me" value subsitution
|
|
294
|
if %w(assigned_to_id author_id).include?(field)
|
|
294
|
if %w(assigned_to_id author_id).include?(field)
|
|
295
|
v.push(executed_by ? executed_by.id.to_s : "0") if v.delete("me")
|
|
295
|
v.push(executed_by ? executed_by.id.to_s : "0") if v.delete("me")
|
|
296
|
end
|
|
296
|
end
|
|
297
|
|
|
297
|
|
|
298
|
case operator_for field
|
|
298
|
case operator_for field
|
|
299
|
when "="
|
|
299
|
when "="
|
|
300
|
sql = sql + "#{db_table}.#{db_field} IN (" + v.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")"
|
|
300
|
sql = sql + "#{db_table}.#{db_field} IN (" + v.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")"
|
|
301
|
when "!"
|
|
301
|
when "!"
|
|
302
|
sql = sql + "#{db_table}.#{db_field} NOT IN (" + v.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")"
|
|
302
|
sql = sql + "#{db_table}.#{db_field} NOT IN (" + v.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")"
|
|
303
|
when "!*"
|
|
303
|
when "!*"
|
|
304
|
sql = sql + "#{db_table}.#{db_field} IS NULL"
|
|
304
|
sql = sql + "#{db_table}.#{db_field} IS NULL"
|
|
305
|
when "*"
|
|
305
|
when "*"
|
|
306
|
sql = sql + "#{db_table}.#{db_field} IS NOT NULL"
|
|
306
|
sql = sql + "#{db_table}.#{db_field} IS NOT NULL"
|
|
307
|
when ">="
|
|
307
|
when ">="
|
|
308
|
sql = sql + "#{db_table}.#{db_field} >= #{v.first.to_i}"
|
|
308
|
sql = sql + "#{db_table}.#{db_field} >= #{v.first.to_i}"
|
|
309
|
when "<="
|
|
309
|
when "<="
|
|
310
|
sql = sql + "#{db_table}.#{db_field} <= #{v.first.to_i}"
|
|
310
|
sql = sql + "#{db_table}.#{db_field} <= #{v.first.to_i}"
|
|
311
|
when "o"
|
|
311
|
when "o"
|
|
312
|
sql = sql + "#{IssueStatus.table_name}.is_closed=#{connection.quoted_false}" if field == "status_id"
|
|
312
|
sql = sql + "#{IssueStatus.table_name}.is_closed=#{connection.quoted_false}" if field == "status_id"
|
|
313
|
when "c"
|
|
313
|
when "c"
|
|
314
|
sql = sql + "#{IssueStatus.table_name}.is_closed=#{connection.quoted_true}" if field == "status_id"
|
|
314
|
sql = sql + "#{IssueStatus.table_name}.is_closed=#{connection.quoted_true}" if field == "status_id"
|
|
315
|
when ">t-"
|
|
315
|
when ">t-"
|
|
316
|
sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date((Date.today - v.first.to_i).to_time), connection.quoted_date((Date.today + 1).to_time)]
|
|
316
|
sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date((Date.today - v.first.to_i).to_time), connection.quoted_date((Date.today + 1).to_time)]
|
|
317
|
when "<t-"
|
|
317
|
when "<t-"
|
|
318
|
sql = sql + "#{db_table}.#{db_field} <= '%s'" % connection.quoted_date((Date.today - v.first.to_i).to_time)
|
|
318
|
sql = sql + "#{db_table}.#{db_field} <= '%s'" % connection.quoted_date((Date.today - v.first.to_i).to_time)
|
|
319
|
when "t-"
|
|
319
|
when "t-"
|
|
320
|
sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date((Date.today - v.first.to_i).to_time), connection.quoted_date((Date.today - v.first.to_i + 1).to_time)]
|
|
320
|
sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date((Date.today - v.first.to_i).to_time), connection.quoted_date((Date.today - v.first.to_i + 1).to_time)]
|
|
321
|
when ">t+"
|
|
321
|
when ">t+"
|
|
322
|
sql = sql + "#{db_table}.#{db_field} >= '%s'" % connection.quoted_date((Date.today + v.first.to_i).to_time)
|
|
322
|
sql = sql + "#{db_table}.#{db_field} >= '%s'" % connection.quoted_date((Date.today + v.first.to_i).to_time)
|
|
323
|
when "<t+"
|
|
323
|
when "<t+"
|
|
324
|
sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date(Date.today.to_time), connection.quoted_date((Date.today + v.first.to_i + 1).to_time)]
|
|
324
|
sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date(Date.today.to_time), connection.quoted_date((Date.today + v.first.to_i + 1).to_time)]
|
|
325
|
when "t+"
|
|
325
|
when "t+"
|
|
326
|
sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date((Date.today + v.first.to_i).to_time), connection.quoted_date((Date.today + v.first.to_i + 1).to_time)]
|
|
326
|
sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date((Date.today + v.first.to_i).to_time), connection.quoted_date((Date.today + v.first.to_i + 1).to_time)]
|
|
327
|
when "t"
|
|
327
|
when "t"
|
|
328
|
sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date(Date.today.to_time), connection.quoted_date((Date.today+1).to_time)]
|
|
328
|
sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date(Date.today.to_time), connection.quoted_date((Date.today+1).to_time)]
|
|
329
|
when "w"
|
|
329
|
when "w"
|
|
330
|
sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date(Time.now.at_beginning_of_week), connection.quoted_date(Time.now.next_week.yesterday)]
|
|
330
|
sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date(Time.now.at_beginning_of_week), connection.quoted_date(Time.now.next_week.yesterday)]
|
|
331
|
when "~"
|
|
331
|
when "~"
|
|
332
|
sql = sql + "#{db_table}.#{db_field} LIKE '%#{connection.quote_string(v.first)}%'"
|
|
332
|
sql = sql + "#{db_table}.#{db_field} LIKE '%#{connection.quote_string(v.first)}%'"
|
|
333
|
when "!~"
|
|
333
|
when "!~"
|
|
334
|
sql = sql + "#{db_table}.#{db_field} NOT LIKE '%#{connection.quote_string(v.first)}%'"
|
|
334
|
sql = sql + "#{db_table}.#{db_field} NOT LIKE '%#{connection.quote_string(v.first)}%'"
|
|
335
|
end
|
|
335
|
end
|
|
336
|
sql << ')'
|
|
336
|
sql << ')'
|
|
337
|
filters_clauses << sql
|
|
337
|
filters_clauses << sql
|
|
338
|
end if filters and valid?
|
|
338
|
end if filters and valid?
|
|
339
|
|
|
339
|
|
|
340
|
clause << ' AND ' unless clause.empty?
|
|
340
|
clause << ' AND ' unless clause.empty?
|
|
341
|
clause << filters_clauses.join(' AND ') unless filters_clauses.empty?
|
|
341
|
clause << filters_clauses.join(' AND ') unless filters_clauses.empty?
|
|
342
|
clause
|
|
342
|
clause
|
|
343
|
end
|
|
343
|
end
|
|
344
|
end
|
|
344
|
end
|