Project

General

Profile

Feature #1159 » query.rb

marco guan, 2013-01-27 14:52

 
1
# Redmine - project management software
2
# Copyright (C) 2006-2012  Jean-Philippe Lang
3
#
4
# This program is free software; you can redistribute it and/or
5
# modify it under the terms of the GNU General Public License
6
# as published by the Free Software Foundation; either version 2
7
# of the License, or (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
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
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17

    
18
class QueryColumn
19
  attr_accessor :name, :sortable, :groupable, :default_order
20
  include Redmine::I18n
21

    
22
  def initialize(name, options={})
23
    self.name = name
24
    self.sortable = options[:sortable]
25
    self.groupable = options[:groupable] || false
26
    if groupable == true
27
      self.groupable = name.to_s
28
    end
29
    self.default_order = options[:default_order]
30
    @inline = options.key?(:inline) ? options[:inline] : true
31
    @caption_key = options[:caption] || "field_#{name}"
32
  end
33

    
34
  def caption
35
    l(@caption_key)
36
  end
37

    
38
  # Returns true if the column is sortable, otherwise false
39
  def sortable?
40
    !@sortable.nil?
41
  end
42

    
43
  def sortable
44
    @sortable.is_a?(Proc) ? @sortable.call : @sortable
45
  end
46

    
47
  def inline?
48
    @inline
49
  end
50

    
51
  def value(issue)
52
    issue.send name
53
  end
54

    
55
  def css_classes
56
    name
57
  end
58
end
59

    
60
class QueryCustomFieldColumn < QueryColumn
61

    
62
  def initialize(custom_field)
63
    self.name = "cf_#{custom_field.id}".to_sym
64
    self.sortable = custom_field.order_statement || false
65
    self.groupable = custom_field.group_statement || false
66
    @inline = true
67
    @cf = custom_field
68
  end
69

    
70
  def caption
71
    @cf.name
72
  end
73

    
74
  def custom_field
75
    @cf
76
  end
77

    
78
  def value(issue)
79
    cv = issue.custom_values.select {|v| v.custom_field_id == @cf.id}.collect {|v| @cf.cast_value(v.value)}
80
    cv.size > 1 ? cv.sort {|a,b| a.to_s <=> b.to_s} : cv.first
81
  end
82

    
83
  def css_classes
84
    @css_classes ||= "#{name} #{@cf.field_format}"
85
  end
86
end
87

    
88
class Query < ActiveRecord::Base
89
  class StatementInvalid < ::ActiveRecord::StatementInvalid
90
  end
91

    
92
  belongs_to :project
93
  belongs_to :user
94
  serialize :filters
95
  serialize :column_names
96
  serialize :sort_criteria, Array
97

    
98
  attr_protected :project_id, :user_id
99

    
100
  validates_presence_of :name
101
  validates_length_of :name, :maximum => 255
102
  validate :validate_query_filters
103

    
104
  @@operators = { "="   => :label_equals,
105
                  "!"   => :label_not_equals,
106
                  "o"   => :label_open_issues,
107
                  "c"   => :label_closed_issues,
108
                  "!*"  => :label_none,
109
                  "*"   => :label_any,
110
                  ">="  => :label_greater_or_equal,
111
                  "<="  => :label_less_or_equal,
112
                  "><"  => :label_between,
113
                  "<t+" => :label_in_less_than,
114
                  ">t+" => :label_in_more_than,
115
                  "><t+"=> :label_in_the_next_days,
116
                  "t+"  => :label_in,
117
                  "t"   => :label_today,
118
                  "w"   => :label_this_week,
119
                  ">t-" => :label_less_than_ago,
120
                  "<t-" => :label_more_than_ago,
121
                  "><t-"=> :label_in_the_past_days,
122
                  "t-"  => :label_ago,
123
                  "~"   => :label_contains,
124
                  "!~"  => :label_not_contains,
125
                  "=p"  => :label_any_issues_in_project,
126
                  "=!p" => :label_any_issues_not_in_project,
127
                  "!p"  => :label_no_issues_in_project}
128

    
129
  cattr_reader :operators
130

    
131
  @@operators_by_filter_type = { :list => [ "=", "!" ],
132
                                 :list_status => [ "o", "=", "!", "c", "*" ],
133
                                 :list_optional => [ "=", "!", "!*", "*" ],
134
                                 :list_subprojects => [ "*", "!*", "=" ],
135
                                 :date => [ "=", ">=", "<=", "><", "<t+", ">t+", "><t+", "t+", "t", "w", ">t-", "<t-", "><t-", "t-", "!*", "*" ],
136
                                 :date_past => [ "=", ">=", "<=", "><", ">t-", "<t-", "><t-", "t-", "t", "w", "!*", "*" ],
137
                                 :string => [ "=", "~", "!", "!~", "!*", "*" ],
138
                                 :text => [  "~", "!~", "!*", "*" ],
139
                                 :integer => [ "=", ">=", "<=", "><", "!*", "*" ],
140
                                 :float => [ "=", ">=", "<=", "><", "!*", "*" ],
141
                                 :relation => ["=", "=p", "=!p", "!p", "!*", "*"]}
142

    
143
  cattr_reader :operators_by_filter_type
144

    
145
  @@available_columns = [
146
    QueryColumn.new(:project, :sortable => "#{Project.table_name}.name", :groupable => true),
147
    QueryColumn.new(:tracker, :sortable => "#{Tracker.table_name}.position", :groupable => true),
148
    QueryColumn.new(:parent, :sortable => ["#{Issue.table_name}.root_id", "#{Issue.table_name}.lft ASC"], :default_order => 'desc', :caption => :field_parent_issue),
149
    QueryColumn.new(:status, :sortable => "#{IssueStatus.table_name}.position", :groupable => true),
150
    QueryColumn.new(:priority, :sortable => "#{IssuePriority.table_name}.position", :default_order => 'desc', :groupable => true),
151
    QueryColumn.new(:subject, :sortable => "#{Issue.table_name}.subject"),
152
    QueryColumn.new(:author, :sortable => lambda {User.fields_for_order_statement("authors")}, :groupable => true),
153
    QueryColumn.new(:assigned_to, :sortable => lambda {User.fields_for_order_statement}, :groupable => true),
154
    QueryColumn.new(:updated_on, :sortable => "#{Issue.table_name}.updated_on", :default_order => 'desc'),
155
    QueryColumn.new(:category, :sortable => "#{IssueCategory.table_name}.name", :groupable => true),
156
    QueryColumn.new(:fixed_version, :sortable => lambda {Version.fields_for_order_statement}, :groupable => true),
157
    QueryColumn.new(:start_date, :sortable => "#{Issue.table_name}.start_date"),
158
    QueryColumn.new(:due_date, :sortable => "#{Issue.table_name}.due_date"),
159
    QueryColumn.new(:estimated_hours, :sortable => "#{Issue.table_name}.estimated_hours"),
160
    QueryColumn.new(:done_ratio, :sortable => "#{Issue.table_name}.done_ratio", :groupable => true),
161
    QueryColumn.new(:created_on, :sortable => "#{Issue.table_name}.created_on", :default_order => 'desc'),
162
    QueryColumn.new(:description, :sortable => "#{Issue.table_name}.description"),
163
    QueryColumn.new(:relations, :caption => :label_related_issues),
164
    QueryColumn.new(:description, :inline => false)
165
  ]
166
  cattr_reader :available_columns
167

    
168
  scope :visible, lambda {|*args|
169
    user = args.shift || User.current
170
    base = Project.allowed_to_condition(user, :view_issues, *args)
171
    user_id = user.logged? ? user.id : 0
172
    {
173
      :conditions => ["(#{table_name}.project_id IS NULL OR (#{base})) AND (#{table_name}.is_public = ? OR #{table_name}.user_id = ?)", true, user_id],
174
      :include => :project
175
    }
176
  }
177

    
178
  def initialize(attributes=nil, *args)
179
    super attributes
180
    self.filters ||= { 'status_id' => {:operator => "o", :values => [""]} }
181
    @is_for_all = project.nil?
182
  end
183

    
184
  def validate_query_filters
185
    filters.each_key do |field|
186
      if values_for(field)
187
        case type_for(field)
188
        when :integer
189
          add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^[+-]?\d+$/) }
190
        when :float
191
          add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^[+-]?\d+(\.\d*)?$/) }
192
        when :date, :date_past
193
          case operator_for(field)
194
          when "=", ">=", "<=", "><"
195
            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?) }
196
          when ">t-", "<t-", "t-", ">t+", "<t+", "t+", "><t+", "><t-"
197
            add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^\d+$/) }
198
          end
199
        end
200
      end
201

    
202
      add_filter_error(field, :blank) unless
203
          # filter requires one or more values
204
          (values_for(field) and !values_for(field).first.blank?) or
205
          # filter doesn't require any value
206
          ["o", "c", "!*", "*", "t", "w"].include? operator_for(field)
207
    end if filters
208
  end
209

    
210
  def add_filter_error(field, message)
211
    m = label_for(field) + " " + l(message, :scope => 'activerecord.errors.messages')
212
    errors.add(:base, m)
213
  end
214

    
215
  # Returns true if the query is visible to +user+ or the current user.
216
  def visible?(user=User.current)
217
    (project.nil? || user.allowed_to?(:view_issues, project)) && (self.is_public? || self.user_id == user.id)
218
  end
219

    
220
  def editable_by?(user)
221
    return false unless user
222
    # Admin can edit them all and regular users can edit their private queries
223
    return true if user.admin? || (!is_public && self.user_id == user.id)
224
    # Members can not edit public queries that are for all project (only admin is allowed to)
225
    is_public && !@is_for_all && user.allowed_to?(:manage_public_queries, project)
226
  end
227

    
228
  def trackers
229
    @trackers ||= project.nil? ? Tracker.find(:all, :order => 'position') : project.rolled_up_trackers
230
  end
231

    
232
  # Returns a hash of localized labels for all filter operators
233
  def self.operators_labels
234
    operators.inject({}) {|h, operator| h[operator.first] = l(operator.last); h}
235
  end
236

    
237
  def available_filters
238
    return @available_filters if @available_filters
239
    @available_filters = {
240
      "status_id" => {
241
        :type => :list_status, :order => 0,
242
        :values => IssueStatus.find(:all, :order => 'position').collect{|s| [s.name, s.id.to_s] }
243
       },
244
      "tracker_id" => {
245
        :type => :list, :order => 2, :values => trackers.collect{|s| [s.name, s.id.to_s] }
246
       },
247
      "priority_id" => {
248
        :type => :list, :order => 3, :values => IssuePriority.all.collect{|s| [s.name, s.id.to_s] }
249
       },
250
      "subject" => { :type => :text, :order => 8 },
251
      "created_on" => { :type => :date_past, :order => 9 },
252
      "updated_on" => { :type => :date_past, :order => 10 },
253
      "start_date" => { :type => :date, :order => 11 },
254
      "due_date" => { :type => :date, :order => 12 },
255
      "estimated_hours" => { :type => :float, :order => 13 },
256
      "done_ratio" =>  { :type => :integer, :order => 14 },
257
      "description" => { :type => :text, :order => 15}
258
    }
259
    IssueRelation::TYPES.each do |relation_type, options|
260
      @available_filters[relation_type] = {
261
        :type => :relation, :order => @available_filters.size + 100,
262
        :label => options[:name]
263
      }
264
    end
265
    principals = []
266
    if project
267
      principals += project.principals.sort
268
      unless project.leaf?
269
        subprojects = project.descendants.visible.all
270
        if subprojects.any?
271
          @available_filters["subproject_id"] = {
272
            :type => :list_subprojects, :order => 13,
273
            :values => subprojects.collect{|s| [s.name, s.id.to_s] }
274
          }
275
          principals += Principal.member_of(subprojects)
276
        end
277
      end
278
    else
279
      if all_projects.any?
280
        # members of visible projects
281
        principals += Principal.member_of(all_projects)
282
        # project filter
283
        project_values = []
284
        if User.current.logged? && User.current.memberships.any?
285
          project_values << ["<< #{l(:label_my_projects).downcase} >>", "mine"]
286
        end
287
        project_values += all_projects_values
288
        @available_filters["project_id"] = {
289
          :type => :list, :order => 1, :values => project_values
290
        } unless project_values.empty?
291
      end
292
    end
293
    principals.uniq!
294
    principals.sort!
295
    users = principals.select {|p| p.is_a?(User)}
296

    
297
    assigned_to_values = []
298
    assigned_to_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged?
299
    assigned_to_values += (Setting.issue_group_assignment? ?
300
                              principals : users).collect{|s| [s.name, s.id.to_s] }
301
    @available_filters["assigned_to_id"] = {
302
      :type => :list_optional, :order => 4, :values => assigned_to_values
303
    } unless assigned_to_values.empty?
304

    
305
    author_values = []
306
    author_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged?
307
    author_values += users.collect{|s| [s.name, s.id.to_s] }
308
    @available_filters["author_id"] = {
309
      :type => :list, :order => 5, :values => author_values
310
    } unless author_values.empty?
311

    
312
    group_values = Group.all.collect {|g| [g.name, g.id.to_s] }
313
    @available_filters["member_of_group"] = {
314
      :type => :list_optional, :order => 6, :values => group_values
315
    } unless group_values.empty?
316

    
317
    role_values = Role.givable.collect {|r| [r.name, r.id.to_s] }
318
    @available_filters["assigned_to_role"] = {
319
      :type => :list_optional, :order => 7, :values => role_values
320
    } unless role_values.empty?
321

    
322
    if User.current.logged?
323
      @available_filters["watcher_id"] = {
324
        :type => :list, :order => 15, :values => [["<< #{l(:label_me)} >>", "me"]]
325
      }
326
    end
327

    
328
    if project
329
      # project specific filters
330
      categories = project.issue_categories.all
331
      unless categories.empty?
332
        @available_filters["category_id"] = {
333
          :type => :list_optional, :order => 6,
334
          :values => categories.collect{|s| [s.name, s.id.to_s] }
335
        }
336
      end
337
      versions = project.shared_versions.all
338
      unless versions.empty?
339
        @available_filters["fixed_version_id"] = {
340
          :type => :list_optional, :order => 7,
341
          :values => versions.sort.collect{|s| ["#{s.project.name} - #{s.name}", s.id.to_s] }
342
        }
343
      end
344
      add_custom_fields_filters(project.all_issue_custom_fields)
345
    else
346
      # global filters for cross project issue list
347
      system_shared_versions = Version.visible.find_all_by_sharing('system')
348
      unless system_shared_versions.empty?
349
        @available_filters["fixed_version_id"] = {
350
          :type => :list_optional, :order => 7,
351
          :values => system_shared_versions.sort.collect{|s|
352
                                       ["#{s.project.name} - #{s.name}", s.id.to_s]
353
                                     }
354
        }
355
      end
356
      add_custom_fields_filters(
357
                   IssueCustomField.find(:all,
358
                                         :conditions => {
359
                                            :is_filter => true,
360
                                            :is_for_all => true
361
                                         }))
362
    end
363
    add_associations_custom_fields_filters :project, :author, :assigned_to, :fixed_version
364
    if User.current.allowed_to?(:set_issues_private, nil, :global => true) ||
365
      User.current.allowed_to?(:set_own_issues_private, nil, :global => true)
366
      @available_filters["is_private"] = {
367
        :type => :list, :order => 16,
368
        :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]]
369
      }
370
    end
371
    Tracker.disabled_core_fields(trackers).each {|field|
372
      @available_filters.delete field
373
    }
374
    @available_filters.each do |field, options|
375
      options[:name] ||= l(options[:label] || "field_#{field}".gsub(/_id$/, ''))
376
    end
377
    @available_filters
378
  end
379

    
380
  # Returns a representation of the available filters for JSON serialization
381
  def available_filters_as_json
382
    json = {}
383
    available_filters.each do |field, options|
384
      json[field] = options.slice(:type, :name, :values).stringify_keys
385
    end
386
    json
387
  end
388

    
389
  def all_projects
390
    @all_projects ||= Project.visible.all
391
  end
392

    
393
  def all_projects_values
394
    return @all_projects_values if @all_projects_values
395

    
396
    values = []
397
    Project.project_tree(all_projects) do |p, level|
398
      prefix = (level > 0 ? ('--' * level + ' ') : '')
399
      values << ["#{prefix}#{p.name}", p.id.to_s]
400
    end
401
    @all_projects_values = values
402
  end
403

    
404
  def add_filter(field, operator, values)
405
    # values must be an array
406
    return unless values.nil? || values.is_a?(Array)
407
    # check if field is defined as an available filter
408
    if available_filters.has_key? field
409
      filter_options = available_filters[field]
410
      # check if operator is allowed for that filter
411
      #if @@operators_by_filter_type[filter_options[:type]].include? operator
412
      #  allowed_values = values & ([""] + (filter_options[:values] || []).collect {|val| val[1]})
413
      #  filters[field] = {:operator => operator, :values => allowed_values } if (allowed_values.first and !allowed_values.first.empty?) or ["o", "c", "!*", "*", "t"].include? operator
414
      #end
415
      filters[field] = {:operator => operator, :values => (values || [''])}
416
    end
417
  end
418

    
419
  def add_short_filter(field, expression)
420
    return unless expression && available_filters.has_key?(field)
421
    field_type = available_filters[field][:type]
422
    @@operators_by_filter_type[field_type].sort.reverse.detect do |operator|
423
      next unless expression =~ /^#{Regexp.escape(operator)}(.*)$/
424
      add_filter field, operator, $1.present? ? $1.split('|') : ['']
425
    end || add_filter(field, '=', expression.split('|'))
426
  end
427

    
428
  # Add multiple filters using +add_filter+
429
  def add_filters(fields, operators, values)
430
    if fields.is_a?(Array) && operators.is_a?(Hash) && (values.nil? || values.is_a?(Hash))
431
      fields.each do |field|
432
        add_filter(field, operators[field], values && values[field])
433
      end
434
    end
435
  end
436

    
437
  def has_filter?(field)
438
    filters and filters[field]
439
  end
440

    
441
  def type_for(field)
442
    available_filters[field][:type] if available_filters.has_key?(field)
443
  end
444

    
445
  def operator_for(field)
446
    has_filter?(field) ? filters[field][:operator] : nil
447
  end
448

    
449
  def values_for(field)
450
    has_filter?(field) ? filters[field][:values] : nil
451
  end
452

    
453
  def value_for(field, index=0)
454
    (values_for(field) || [])[index]
455
  end
456

    
457
  def label_for(field)
458
    label = available_filters[field][:name] if available_filters.has_key?(field)
459
    label ||= l("field_#{field.to_s.gsub(/_id$/, '')}", :default => field)
460
  end
461

    
462
  def available_columns
463
    return @available_columns if @available_columns
464
    @available_columns = ::Query.available_columns.dup
465
    @available_columns += (project ?
466
                            project.all_issue_custom_fields :
467
                            IssueCustomField.find(:all)
468
                           ).collect {|cf| QueryCustomFieldColumn.new(cf) }
469

    
470
    if User.current.allowed_to?(:view_time_entries, project, :global => true)
471
      index = nil
472
      @available_columns.each_with_index {|column, i| index = i if column.name == :estimated_hours}
473
      index = (index ? index + 1 : -1)
474
      # insert the column after estimated_hours or at the end
475
      @available_columns.insert index, QueryColumn.new(:spent_hours,
476
        :sortable => "(SELECT COALESCE(SUM(hours), 0) FROM #{TimeEntry.table_name} WHERE #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id)",
477
        :default_order => 'desc',
478
        :caption => :label_spent_time
479
      )
480
    end
481

    
482
    if User.current.allowed_to?(:set_issues_private, nil, :global => true) ||
483
      User.current.allowed_to?(:set_own_issues_private, nil, :global => true)
484
      @available_columns << QueryColumn.new(:is_private, :sortable => "#{Issue.table_name}.is_private")
485
    end
486

    
487
    disabled_fields = Tracker.disabled_core_fields(trackers).map {|field| field.sub(/_id$/, '')}
488
    @available_columns.reject! {|column|
489
      disabled_fields.include?(column.name.to_s)
490
    }
491

    
492
    @available_columns
493
  end
494

    
495
  def self.available_columns=(v)
496
    self.available_columns = (v)
497
  end
498

    
499
  def self.add_available_column(column)
500
    self.available_columns << (column) if column.is_a?(QueryColumn)
501
  end
502

    
503
  # Returns an array of columns that can be used to group the results
504
  def groupable_columns
505
    available_columns.select {|c| c.groupable}
506
  end
507

    
508
  # Returns a Hash of columns and the key for sorting
509
  def sortable_columns
510
    {'id' => "#{Issue.table_name}.id"}.merge(available_columns.inject({}) {|h, column|
511
                                               h[column.name.to_s] = column.sortable
512
                                               h
513
                                             })
514
  end
515

    
516
  def columns
517
    # preserve the column_names order
518
    (has_default_columns? ? default_columns_names : column_names).collect do |name|
519
       available_columns.find { |col| col.name == name }
520
    end.compact
521
  end
522

    
523
  def inline_columns
524
    columns.select(&:inline?)
525
  end
526

    
527
  def block_columns
528
    columns.reject(&:inline?)
529
  end
530

    
531
  def available_inline_columns
532
    available_columns.select(&:inline?)
533
  end
534

    
535
  def available_block_columns
536
    available_columns.reject(&:inline?)
537
  end
538

    
539
  def default_columns_names
540
    @default_columns_names ||= begin
541
      default_columns = Setting.issue_list_default_columns.map(&:to_sym)
542

    
543
      project.present? ? default_columns : [:project] | default_columns
544
    end
545
  end
546

    
547
  def column_names=(names)
548
    if names
549
      names = names.select {|n| n.is_a?(Symbol) || !n.blank? }
550
      names = names.collect {|n| n.is_a?(Symbol) ? n : n.to_sym }
551
      # Set column_names to nil if default columns
552
      if names == default_columns_names
553
        names = nil
554
      end
555
    end
556
    write_attribute(:column_names, names)
557
  end
558

    
559
  def has_column?(column)
560
    column_names && column_names.include?(column.is_a?(QueryColumn) ? column.name : column)
561
  end
562

    
563
  def has_default_columns?
564
    column_names.nil? || column_names.empty?
565
  end
566

    
567
  def sort_criteria=(arg)
568
    c = []
569
    if arg.is_a?(Hash)
570
      arg = arg.keys.sort.collect {|k| arg[k]}
571
    end
572
    c = arg.select {|k,o| !k.to_s.blank?}.slice(0,3).collect {|k,o| [k.to_s, (o == 'desc' || o == false) ? 'desc' : 'asc']}
573
    write_attribute(:sort_criteria, c)
574
  end
575

    
576
  def sort_criteria
577
    read_attribute(:sort_criteria) || []
578
  end
579

    
580
  def sort_criteria_key(arg)
581
    sort_criteria && sort_criteria[arg] && sort_criteria[arg].first
582
  end
583

    
584
  def sort_criteria_order(arg)
585
    sort_criteria && sort_criteria[arg] && sort_criteria[arg].last
586
  end
587

    
588
  def sort_criteria_order_for(key)
589
    sort_criteria.detect {|k, order| key.to_s == k}.try(:last)
590
  end
591

    
592
  # Returns the SQL sort order that should be prepended for grouping
593
  def group_by_sort_order
594
    if grouped? && (column = group_by_column)
595
      order = sort_criteria_order_for(column.name) || column.default_order
596
      column.sortable.is_a?(Array) ?
597
        column.sortable.collect {|s| "#{s} #{order}"}.join(',') :
598
        "#{column.sortable} #{order}"
599
    end
600
  end
601

    
602
  # Returns true if the query is a grouped query
603
  def grouped?
604
    !group_by_column.nil?
605
  end
606

    
607
  def group_by_column
608
    groupable_columns.detect {|c| c.groupable && c.name.to_s == group_by}
609
  end
610

    
611
  def group_by_statement
612
    group_by_column.try(:groupable)
613
  end
614

    
615
  def project_statement
616
    project_clauses = []
617
    if project && !project.descendants.active.empty?
618
      ids = [project.id]
619
      if has_filter?("subproject_id")
620
        case operator_for("subproject_id")
621
        when '='
622
          # include the selected subprojects
623
          ids += values_for("subproject_id").each(&:to_i)
624
        when '!*'
625
          # main project only
626
        else
627
          # all subprojects
628
          ids += project.descendants.collect(&:id)
629
        end
630
      elsif Setting.display_subprojects_issues?
631
        ids += project.descendants.collect(&:id)
632
      end
633
      project_clauses << "#{Project.table_name}.id IN (%s)" % ids.join(',')
634
    elsif project
635
      project_clauses << "#{Project.table_name}.id = %d" % project.id
636
    end
637
    project_clauses.any? ? project_clauses.join(' AND ') : nil
638
  end
639

    
640
  def statement
641
    # filters clauses
642
    filters_clauses = []
643
    filters.each_key do |field|
644
      next if field == "subproject_id"
645
      v = values_for(field).clone
646
      next unless v and !v.empty?
647
      operator = operator_for(field)
648

    
649
      # "me" value subsitution
650
      if %w(assigned_to_id author_id watcher_id).include?(field)
651
        if v.delete("me")
652
          if User.current.logged?
653
            v.push(User.current.id.to_s)
654
            v += User.current.group_ids.map(&:to_s) if field == 'assigned_to_id'
655
          else
656
            v.push("0")
657
          end
658
        end
659
      end
660

    
661
      if field == 'project_id'
662
        if v.delete('mine')
663
          v += User.current.memberships.map(&:project_id).map(&:to_s)
664
        end
665
      end
666

    
667
      if field =~ /cf_(\d+)$/
668
        # custom field
669
        filters_clauses << sql_for_custom_field(field, operator, v, $1)
670
      elsif respond_to?("sql_for_#{field}_field")
671
        # specific statement
672
        filters_clauses << send("sql_for_#{field}_field", field, operator, v)
673
      else
674
        # regular field
675
        filters_clauses << '(' + sql_for_field(field, operator, v, Issue.table_name, field) + ')'
676
      end
677
    end if filters and valid?
678

    
679
    filters_clauses << project_statement
680
    filters_clauses.reject!(&:blank?)
681

    
682
    filters_clauses.any? ? filters_clauses.join(' AND ') : nil
683
  end
684

    
685
  # Returns the issue count
686
  def issue_count
687
    Issue.visible.count(:include => [:status, :project], :conditions => statement)
688
  rescue ::ActiveRecord::StatementInvalid => e
689
    raise StatementInvalid.new(e.message)
690
  end
691

    
692
  # Returns the issue count by group or nil if query is not grouped
693
  def issue_count_by_group
694
    r = nil
695
    if grouped?
696
      begin
697
        # Rails3 will raise an (unexpected) RecordNotFound if there's only a nil group value
698
        r = Issue.visible.count(:group => group_by_statement, :include => [:status, :project], :conditions => statement)
699
      rescue ActiveRecord::RecordNotFound
700
        r = {nil => issue_count}
701
      end
702
      c = group_by_column
703
      if c.is_a?(QueryCustomFieldColumn)
704
        r = r.keys.inject({}) {|h, k| h[c.custom_field.cast_value(k)] = r[k]; h}
705
      end
706
    end
707
    r
708
  rescue ::ActiveRecord::StatementInvalid => e
709
    raise StatementInvalid.new(e.message)
710
  end
711

    
712
  # Returns the issues
713
  # Valid options are :order, :offset, :limit, :include, :conditions
714
  def issues(options={})
715
    order_option = [group_by_sort_order, options[:order]].reject {|s| s.blank?}.join(',')
716
    order_option = nil if order_option.blank?
717

    
718
    issues = Issue.visible.scoped(:conditions => options[:conditions]).find :all, :include => ([:status, :project] + (options[:include] || [])).uniq,
719
                     :conditions => statement,
720
                     :order => order_option,
721
                     :joins => joins_for_order_statement(order_option),
722
                     :limit  => options[:limit],
723
                     :offset => options[:offset]
724

    
725
    if has_column?(:spent_hours)
726
      Issue.load_visible_spent_hours(issues)
727
    end
728
    if has_column?(:relations)
729
      Issue.load_visible_relations(issues)
730
    end
731
    issues
732
  rescue ::ActiveRecord::StatementInvalid => e
733
    raise StatementInvalid.new(e.message)
734
  end
735

    
736
  # Returns the issues ids
737
  def issue_ids(options={})
738
    order_option = [group_by_sort_order, options[:order]].reject {|s| s.blank?}.join(',')
739
    order_option = nil if order_option.blank?
740

    
741
    Issue.visible.scoped(:conditions => options[:conditions]).scoped(:include => ([:status, :project] + (options[:include] || [])).uniq,
742
                     :conditions => statement,
743
                     :order => order_option,
744
                     :joins => joins_for_order_statement(order_option),
745
                     :limit  => options[:limit],
746
                     :offset => options[:offset]).find_ids
747
  rescue ::ActiveRecord::StatementInvalid => e
748
    raise StatementInvalid.new(e.message)
749
  end
750

    
751
  # Returns the journals
752
  # Valid options are :order, :offset, :limit
753
  def journals(options={})
754
    Journal.visible.find :all, :include => [:details, :user, {:issue => [:project, :author, :tracker, :status]}],
755
                       :conditions => statement,
756
                       :order => options[:order],
757
                       :limit => options[:limit],
758
                       :offset => options[:offset]
759
  rescue ::ActiveRecord::StatementInvalid => e
760
    raise StatementInvalid.new(e.message)
761
  end
762

    
763
  # Returns the versions
764
  # Valid options are :conditions
765
  def versions(options={})
766
    Version.visible.scoped(:conditions => options[:conditions]).find :all, :include => :project, :conditions => project_statement
767
  rescue ::ActiveRecord::StatementInvalid => e
768
    raise StatementInvalid.new(e.message)
769
  end
770

    
771
  def sql_for_watcher_id_field(field, operator, value)
772
    db_table = Watcher.table_name
773
    "#{Issue.table_name}.id #{ operator == '=' ? 'IN' : 'NOT IN' } (SELECT #{db_table}.watchable_id FROM #{db_table} WHERE #{db_table}.watchable_type='Issue' AND " +
774
      sql_for_field(field, '=', value, db_table, 'user_id') + ')'
775
  end
776

    
777
  def sql_for_member_of_group_field(field, operator, value)
778
    if operator == '*' # Any group
779
      groups = Group.all
780
      operator = '=' # Override the operator since we want to find by assigned_to
781
    elsif operator == "!*"
782
      groups = Group.all
783
      operator = '!' # Override the operator since we want to find by assigned_to
784
    else
785
      groups = Group.find_all_by_id(value)
786
    end
787
    groups ||= []
788

    
789
    members_of_groups = groups.inject([]) {|user_ids, group|
790
      if group && group.user_ids.present?
791
        user_ids << group.user_ids
792
      end
793
      user_ids.flatten.uniq.compact
794
    }.sort.collect(&:to_s)
795

    
796
    '(' + sql_for_field("assigned_to_id", operator, members_of_groups, Issue.table_name, "assigned_to_id", false) + ')'
797
  end
798

    
799
  def sql_for_assigned_to_role_field(field, operator, value)
800
    case operator
801
    when "*", "!*" # Member / Not member
802
      sw = operator == "!*" ? 'NOT' : ''
803
      nl = operator == "!*" ? "#{Issue.table_name}.assigned_to_id IS NULL OR" : ''
804
      "(#{nl} #{Issue.table_name}.assigned_to_id #{sw} IN (SELECT DISTINCT #{Member.table_name}.user_id FROM #{Member.table_name}" +
805
        " WHERE #{Member.table_name}.project_id = #{Issue.table_name}.project_id))"
806
    when "=", "!"
807
      role_cond = value.any? ?
808
        "#{MemberRole.table_name}.role_id IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")" :
809
        "1=0"
810

    
811
      sw = operator == "!" ? 'NOT' : ''
812
      nl = operator == "!" ? "#{Issue.table_name}.assigned_to_id IS NULL OR" : ''
813
      "(#{nl} #{Issue.table_name}.assigned_to_id #{sw} IN (SELECT DISTINCT #{Member.table_name}.user_id FROM #{Member.table_name}, #{MemberRole.table_name}" +
814
        " WHERE #{Member.table_name}.project_id = #{Issue.table_name}.project_id AND #{Member.table_name}.id = #{MemberRole.table_name}.member_id AND #{role_cond}))"
815
    end
816
  end
817

    
818
  def sql_for_is_private_field(field, operator, value)
819
    op = (operator == "=" ? 'IN' : 'NOT IN')
820
    va = value.map {|v| v == '0' ? connection.quoted_false : connection.quoted_true}.uniq.join(',')
821

    
822
    "#{Issue.table_name}.is_private #{op} (#{va})"
823
  end
824

    
825
  def sql_for_relations(field, operator, value, options={})
826
    relation_options = IssueRelation::TYPES[field]
827
    return relation_options unless relation_options
828

    
829
    relation_type = field
830
    join_column, target_join_column = "issue_from_id", "issue_to_id"
831
    if relation_options[:reverse] || options[:reverse]
832
      relation_type = relation_options[:reverse] || relation_type
833
      join_column, target_join_column = target_join_column, join_column
834
    end
835

    
836
    sql = case operator
837
      when "*", "!*"
838
        op = (operator == "*" ? 'IN' : 'NOT IN')
839
        "#{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)}')"
840
      when "=", "!"
841
        op = (operator == "=" ? 'IN' : 'NOT IN')
842
        "#{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})"
843
      when "=p", "=!p", "!p"
844
        op = (operator == "!p" ? 'NOT IN' : 'IN')
845
        comp = (operator == "=!p" ? '<>' : '=')
846
        "#{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})"
847
      end
848

    
849
    if relation_options[:sym] == field && !options[:reverse]
850
      sqls = [sql, sql_for_relations(field, operator, value, :reverse => true)]
851
      sqls.join(["!", "!*", "!p"].include?(operator) ? " AND " : " OR ")
852
    else
853
      sql
854
    end
855
  end
856

    
857
  IssueRelation::TYPES.keys.each do |relation_type|
858
    alias_method "sql_for_#{relation_type}_field".to_sym, :sql_for_relations
859
  end
860

    
861
  private
862

    
863
  def sql_for_custom_field(field, operator, value, custom_field_id)
864
    db_table = CustomValue.table_name
865
    db_field = 'value'
866
    filter = @available_filters[field]
867
    return nil unless filter
868
    if filter[:format] == 'user'
869
      if value.delete('me')
870
        value.push User.current.id.to_s
871
      end
872
    end
873
    not_in = nil
874
    if operator == '!'
875
      # Makes ! operator work for custom fields with multiple values
876
      operator = '='
877
      not_in = 'NOT'
878
    end
879
    customized_key = "id"
880
    customized_class = Issue
881
    if field =~ /^(.+)\.cf_/
882
      assoc = $1
883
      customized_key = "#{assoc}_id"
884
      customized_class = Issue.reflect_on_association(assoc.to_sym).klass.base_class rescue nil
885
      raise "Unknown Issue association #{assoc}" unless customized_class
886
    end
887
    "#{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 " +
888
      sql_for_field(field, operator, value, db_table, db_field, true) + ')'
889
  end
890

    
891
  # Helper method to generate the WHERE sql for a +field+, +operator+ and a +value+
892
  def sql_for_field(field, operator, value, db_table, db_field, is_custom_filter=false)
893
    sql = ''
894
    case operator
895
    when "="
896
      if value.any?
897
        case type_for(field)
898
        when :date, :date_past
899
          sql = date_clause(db_table, db_field, (Date.parse(value.first) rescue nil), (Date.parse(value.first) rescue nil))
900
        when :integer
901
          if is_custom_filter
902
            sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) = #{value.first.to_i})"
903
          else
904
            sql = "#{db_table}.#{db_field} = #{value.first.to_i}"
905
          end
906
        when :float
907
          if is_custom_filter
908
            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})"
909
          else
910
            sql = "#{db_table}.#{db_field} BETWEEN #{value.first.to_f - 1e-5} AND #{value.first.to_f + 1e-5}"
911
          end
912
        else
913
          sql = "#{db_table}.#{db_field} IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")"
914
        end
915
      else
916
        # IN an empty set
917
        sql = "1=0"
918
      end
919
    when "!"
920
      if value.any?
921
        sql = "(#{db_table}.#{db_field} IS NULL OR #{db_table}.#{db_field} NOT IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + "))"
922
      else
923
        # NOT IN an empty set
924
        sql = "1=1"
925
      end
926
    when "!*"
927
      sql = "#{db_table}.#{db_field} IS NULL"
928
      sql << " OR #{db_table}.#{db_field} = ''" if is_custom_filter
929
    when "*"
930
      sql = "#{db_table}.#{db_field} IS NOT NULL"
931
      sql << " AND #{db_table}.#{db_field} <> ''" if is_custom_filter
932
    when ">="
933
      if [:date, :date_past].include?(type_for(field))
934
        sql = date_clause(db_table, db_field, (Date.parse(value.first) rescue nil), nil)
935
      else
936
        if is_custom_filter
937
          sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) >= #{value.first.to_f})"
938
        else
939
          sql = "#{db_table}.#{db_field} >= #{value.first.to_f}"
940
        end
941
      end
942
    when "<="
943
      if [:date, :date_past].include?(type_for(field))
944
        sql = date_clause(db_table, db_field, nil, (Date.parse(value.first) rescue nil))
945
      else
946
        if is_custom_filter
947
          sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) <= #{value.first.to_f})"
948
        else
949
          sql = "#{db_table}.#{db_field} <= #{value.first.to_f}"
950
        end
951
      end
952
    when "><"
953
      if [:date, :date_past].include?(type_for(field))
954
        sql = date_clause(db_table, db_field, (Date.parse(value[0]) rescue nil), (Date.parse(value[1]) rescue nil))
955
      else
956
        if is_custom_filter
957
          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})"
958
        else
959
          sql = "#{db_table}.#{db_field} BETWEEN #{value[0].to_f} AND #{value[1].to_f}"
960
        end
961
      end
962
    when "o"
963
      sql = "#{Issue.table_name}.status_id IN (SELECT id FROM #{IssueStatus.table_name} WHERE is_closed=#{connection.quoted_false})" if field == "status_id"
964
    when "c"
965
      sql = "#{Issue.table_name}.status_id IN (SELECT id FROM #{IssueStatus.table_name} WHERE is_closed=#{connection.quoted_true})" if field == "status_id"
966
    when "><t-"
967
      # between today - n days and today
968
      sql = relative_date_clause(db_table, db_field, - value.first.to_i, 0)
969
    when ">t-"
970
      # >= today - n days
971
      sql = relative_date_clause(db_table, db_field, - value.first.to_i, nil)
972
    when "<t-"
973
      # <= today - n days
974
      sql = relative_date_clause(db_table, db_field, nil, - value.first.to_i)
975
    when "t-"
976
      # = n days in past
977
      sql = relative_date_clause(db_table, db_field, - value.first.to_i, - value.first.to_i)
978
    when "><t+"
979
      # between today and today + n days
980
      sql = relative_date_clause(db_table, db_field, 0, value.first.to_i)
981
    when ">t+"
982
      # >= today + n days
983
      sql = relative_date_clause(db_table, db_field, value.first.to_i, nil)
984
    when "<t+"
985
      # <= today + n days
986
      sql = relative_date_clause(db_table, db_field, nil, value.first.to_i)
987
    when "t+"
988
      # = today + n days
989
      sql = relative_date_clause(db_table, db_field, value.first.to_i, value.first.to_i)
990
    when "t"
991
      # = today
992
      sql = relative_date_clause(db_table, db_field, 0, 0)
993
    when "w"
994
      # = this week
995
      first_day_of_week = l(:general_first_day_of_week).to_i
996
      day_of_week = Date.today.cwday
997
      days_ago = (day_of_week >= first_day_of_week ? day_of_week - first_day_of_week : day_of_week + 7 - first_day_of_week)
998
      sql = relative_date_clause(db_table, db_field, - days_ago, - days_ago + 6)
999
    when "~"
1000
      sql = "LOWER(#{db_table}.#{db_field}) LIKE '%#{connection.quote_string(value.first.to_s.downcase)}%'"
1001
    when "!~"
1002
      sql = "LOWER(#{db_table}.#{db_field}) NOT LIKE '%#{connection.quote_string(value.first.to_s.downcase)}%'"
1003
    else
1004
      raise "Unknown query operator #{operator}"
1005
    end
1006

    
1007
    return sql
1008
  end
1009

    
1010
  def add_custom_fields_filters(custom_fields, assoc=nil)
1011
    return unless custom_fields.present?
1012
    @available_filters ||= {}
1013

    
1014
    custom_fields.select(&:is_filter?).each do |field|
1015
      case field.field_format
1016
      when "text"
1017
        options = { :type => :text, :order => 20 }
1018
      when "list"
1019
        options = { :type => :list_optional, :values => field.possible_values, :order => 20}
1020
      when "date"
1021
        options = { :type => :date, :order => 20 }
1022
      when "bool"
1023
        options = { :type => :list, :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]], :order => 20 }
1024
      when "int"
1025
        options = { :type => :integer, :order => 20 }
1026
      when "float"
1027
        options = { :type => :float, :order => 20 }
1028
      when "user", "version"
1029
        next unless project
1030
        values = field.possible_values_options(project)
1031
        if User.current.logged? && field.field_format == 'user'
1032
          values.unshift ["<< #{l(:label_me)} >>", "me"]
1033
        end
1034
        options = { :type => :list_optional, :values => values, :order => 20}
1035
      else
1036
        options = { :type => :string, :order => 20 }
1037
      end
1038
      filter_id = "cf_#{field.id}"
1039
      filter_name = field.name
1040
      if assoc.present?
1041
        filter_id = "#{assoc}.#{filter_id}"
1042
        filter_name = l("label_attribute_of_#{assoc}", :name => filter_name)
1043
      end
1044
      @available_filters[filter_id] = options.merge({
1045
               :name => filter_name,
1046
               :format => field.field_format,
1047
               :field => field
1048
             })
1049
    end
1050
  end
1051

    
1052
  def add_associations_custom_fields_filters(*associations)
1053
    fields_by_class = CustomField.where(:is_filter => true).group_by(&:class)
1054
    associations.each do |assoc|
1055
      association_klass = Issue.reflect_on_association(assoc).klass
1056
      fields_by_class.each do |field_class, fields|
1057
        if field_class.customized_class <= association_klass
1058
          add_custom_fields_filters(fields, assoc)
1059
        end
1060
      end
1061
    end
1062
  end
1063

    
1064
  # Returns a SQL clause for a date or datetime field.
1065
  def date_clause(table, field, from, to)
1066
    s = []
1067
    if from
1068
      from_yesterday = from - 1
1069
      from_yesterday_time = Time.local(from_yesterday.year, from_yesterday.month, from_yesterday.day)
1070
      if self.class.default_timezone == :utc
1071
        from_yesterday_time = from_yesterday_time.utc
1072
      end
1073
      s << ("#{table}.#{field} > '%s'" % [connection.quoted_date(from_yesterday_time.end_of_day)])
1074
    end
1075
    if to
1076
      to_time = Time.local(to.year, to.month, to.day)
1077
      if self.class.default_timezone == :utc
1078
        to_time = to_time.utc
1079
      end
1080
      s << ("#{table}.#{field} <= '%s'" % [connection.quoted_date(to_time.end_of_day)])
1081
    end
1082
    s.join(' AND ')
1083
  end
1084

    
1085
  # Returns a SQL clause for a date or datetime field using relative dates.
1086
  def relative_date_clause(table, field, days_from, days_to)
1087
    date_clause(table, field, (days_from ? Date.today + days_from : nil), (days_to ? Date.today + days_to : nil))
1088
  end
1089

    
1090
  # Additional joins required for the given sort options
1091
  def joins_for_order_statement(order_options)
1092
    joins = []
1093

    
1094
    if order_options
1095
      if order_options.include?('authors')
1096
        joins << "LEFT OUTER JOIN #{User.table_name} authors ON authors.id = #{Issue.table_name}.author_id"
1097
      end
1098
      order_options.scan(/cf_\d+/).uniq.each do |name|
1099
        column = available_columns.detect {|c| c.name.to_s == name}
1100
        join = column && column.custom_field.join_for_order_statement
1101
        if join
1102
          joins << join
1103
        end
1104
      end
1105
    end
1106

    
1107
    joins.any? ? joins.join(' ') : nil
1108
  end
1109
end
(2-2/5)