Project

General

Profile

Patch #11721 ยป redmine_oracle.diff

Patch for get the upgrade working - Juan G, 2012-08-28 08:54

View differences:

src/app/models/time_entry.rb (revisi?n: 3943)
117 117
  def editable_by?(usr)
118 118
    (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project)
119 119
  end
120
  if ActiveRecord::Base.connection.adapter_name == 'OracleEnhanced'
121
    set_date_columns :spent_on
122
  end
120 123
end
src/app/models/query.rb (revisi?n: 3943)
1
# Redmine - project management software
2
# Copyright (C) 2006-2011  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
    @caption_key = options[:caption] || "field_#{name}"
31
  end
32

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

  
37
  # Returns true if the column is sortable, otherwise false
38
  def sortable?
39
    !@sortable.nil?
40
  end
41
  
42
  def sortable
43
    @sortable.is_a?(Proc) ? @sortable.call : @sortable
44
  end
45

  
46
  def value(issue)
47
    issue.send name
48
  end
49

  
50
  def css_classes
51
    name
52
  end
53
end
54

  
55
class QueryCustomFieldColumn < QueryColumn
56

  
57
  def initialize(custom_field)
58
    self.name = "cf_#{custom_field.id}".to_sym
59
    self.sortable = custom_field.order_statement || false
60
    if %w(list date bool int).include?(custom_field.field_format) && !custom_field.multiple?
61
      self.groupable = custom_field.order_statement
62
    end
63
    self.groupable ||= false
64
    @cf = custom_field
65
  end
66

  
67
  def caption
68
    @cf.name
69
  end
70

  
71
  def custom_field
72
    @cf
73
  end
74

  
75
  def value(issue)
76
    cv = issue.custom_values.select {|v| v.custom_field_id == @cf.id}.collect {|v| @cf.cast_value(v.value)}
77
    cv.size > 1 ? cv : cv.first
78
  end
79

  
80
  def css_classes
81
    @css_classes ||= "#{name} #{@cf.field_format}"
82
  end
83
end
84

  
85
class Query < ActiveRecord::Base
86
  class StatementInvalid < ::ActiveRecord::StatementInvalid
87
  end
88

  
89
  belongs_to :project
90
  belongs_to :user
91
  serialize :filters
92
  serialize :column_names
93
  serialize :sort_criteria, Array
94

  
95
  attr_protected :project_id, :user_id
96

  
97
  validates_presence_of :name
98
  validates_length_of :name, :maximum => 255
99
  validate :validate_query_filters
100

  
101
  @@operators = { "="   => :label_equals,
102
                  "!"   => :label_not_equals,
103
                  "o"   => :label_open_issues,
104
                  "c"   => :label_closed_issues,
105
                  "!*"  => :label_none,
106
                  "*"   => :label_all,
107
                  ">="  => :label_greater_or_equal,
108
                  "<="  => :label_less_or_equal,
109
                  "><"  => :label_between,
110
                  "<t+" => :label_in_less_than,
111
                  ">t+" => :label_in_more_than,
112
                  "t+"  => :label_in,
113
                  "t"   => :label_today,
114
                  "w"   => :label_this_week,
115
                  ">t-" => :label_less_than_ago,
116
                  "<t-" => :label_more_than_ago,
117
                  "t-"  => :label_ago,
118
                  "~"   => :label_contains,
119
                  "!~"  => :label_not_contains }
120

  
121
  cattr_reader :operators
122

  
123
  @@operators_by_filter_type = { :list => [ "=", "!" ],
124
                                 :list_status => [ "o", "=", "!", "c", "*" ],
125
                                 :list_optional => [ "=", "!", "!*", "*" ],
126
                                 :list_subprojects => [ "*", "!*", "=" ],
127
                                 :date => [ "=", ">=", "<=", "><", "<t+", ">t+", "t+", "t", "w", ">t-", "<t-", "t-", "!*", "*" ],
128
                                 :date_past => [ "=", ">=", "<=", "><", ">t-", "<t-", "t-", "t", "w", "!*", "*" ],
129
                                 :string => [ "=", "~", "!", "!~", "!*", "*" ],
130
                                 :text => [  "~", "!~", "!*", "*" ],
131
                                 :integer => [ "=", ">=", "<=", "><", "!*", "*" ],
132
                                 :float => [ "=", ">=", "<=", "><", "!*", "*" ] }
133

  
134
  cattr_reader :operators_by_filter_type
135

  
136
  @@available_columns = [
137
    QueryColumn.new(:project, :sortable => "#{Project.table_name}.name", :groupable => true),
138
    QueryColumn.new(:tracker, :sortable => "#{Tracker.table_name}.position", :groupable => true),
139
    QueryColumn.new(:parent, :sortable => ["#{Issue.table_name}.root_id", "#{Issue.table_name}.lft ASC"], :default_order => 'desc', :caption => :field_parent_issue),
140
    QueryColumn.new(:status, :sortable => "#{IssueStatus.table_name}.position", :groupable => true),
141
    QueryColumn.new(:priority, :sortable => "#{IssuePriority.table_name}.position", :default_order => 'desc', :groupable => true),
142
    QueryColumn.new(:subject, :sortable => "#{Issue.table_name}.subject"),
143
    QueryColumn.new(:author, :sortable => lambda {User.fields_for_order_statement("authors")}, :groupable => true),
144
    QueryColumn.new(:assigned_to, :sortable => lambda {User.fields_for_order_statement}, :groupable => true),
145
    QueryColumn.new(:updated_on, :sortable => "#{Issue.table_name}.updated_on", :default_order => 'desc'),
146
    QueryColumn.new(:category, :sortable => "#{IssueCategory.table_name}.name", :groupable => true),
147
    QueryColumn.new(:fixed_version, :sortable => ["#{Version.table_name}.effective_date", "#{Version.table_name}.name"], :default_order => 'desc', :groupable => true),
148
    QueryColumn.new(:start_date, :sortable => "#{Issue.table_name}.start_date"),
149
    QueryColumn.new(:due_date, :sortable => "#{Issue.table_name}.due_date"),
150
    QueryColumn.new(:estimated_hours, :sortable => "#{Issue.table_name}.estimated_hours"),
151
    QueryColumn.new(:done_ratio, :sortable => "#{Issue.table_name}.done_ratio", :groupable => true),
152
    QueryColumn.new(:created_on, :sortable => "#{Issue.table_name}.created_on", :default_order => 'desc'),
153
  ]
154
  cattr_reader :available_columns
155

  
156
  named_scope :visible, lambda {|*args|
157
    user = args.shift || User.current
158
    base = Project.allowed_to_condition(user, :view_issues, *args)
159
    user_id = user.logged? ? user.id : 0
160
    {
161
      :conditions => ["(#{table_name}.project_id IS NULL OR (#{base})) AND (#{table_name}.is_public = ? OR #{table_name}.user_id = ?)", true, user_id],
162
      :include => :project
163
    }
164
  }
165

  
166
  def initialize(attributes=nil, *args)
167
    super attributes
168
    self.filters ||= { 'status_id' => {:operator => "o", :values => [""]} }
169
    @is_for_all = project.nil?
170
  end
171

  
172
  def validate_query_filters
173
    filters.each_key do |field|
174
      if values_for(field)
175
        case type_for(field)
176
        when :integer
177
          add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^\d+$/) }
178
        when :float
179
          add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^\d+(\.\d*)?$/) }
180
        when :date, :date_past
181
          case operator_for(field)
182
          when "=", ">=", "<=", "><"
183
            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?) }
184
          when ">t-", "<t-", "t-"
185
            add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^\d+$/) }
186
          end
187
        end
188
      end
189

  
190
      add_filter_error(field, :blank) unless
191
          # filter requires one or more values
192
          (values_for(field) and !values_for(field).first.blank?) or
193
          # filter doesn't require any value
194
          ["o", "c", "!*", "*", "t", "w"].include? operator_for(field)
195
    end if filters
196
  end
197

  
198
  def add_filter_error(field, message)
199
    m = label_for(field) + " " + l(message, :scope => 'activerecord.errors.messages')
200
    errors.add(:base, m)
201
  end
202

  
203
  # Returns true if the query is visible to +user+ or the current user.
204
  def visible?(user=User.current)
205
    (project.nil? || user.allowed_to?(:view_issues, project)) && (self.is_public? || self.user_id == user.id)
206
  end
207

  
208
  def editable_by?(user)
209
    return false unless user
210
    # Admin can edit them all and regular users can edit their private queries
211
    return true if user.admin? || (!is_public && self.user_id == user.id)
212
    # Members can not edit public queries that are for all project (only admin is allowed to)
213
    is_public && !@is_for_all && user.allowed_to?(:manage_public_queries, project)
214
  end
215

  
216
  def available_filters
217
    return @available_filters if @available_filters
218

  
219
    trackers = project.nil? ? Tracker.find(:all, :order => 'position') : project.rolled_up_trackers
220

  
221
    @available_filters = { "status_id" => { :type => :list_status, :order => 1, :values => IssueStatus.find(:all, :order => 'position').collect{|s| [s.name, s.id.to_s] } },
222
                           "tracker_id" => { :type => :list, :order => 2, :values => trackers.collect{|s| [s.name, s.id.to_s] } },
223
                           "priority_id" => { :type => :list, :order => 3, :values => IssuePriority.all.collect{|s| [s.name, s.id.to_s] } },
224
                           "subject" => { :type => :text, :order => 8 },
225
                           "created_on" => { :type => :date_past, :order => 9 },
226
                           "updated_on" => { :type => :date_past, :order => 10 },
227
                           "start_date" => { :type => :date, :order => 11 },
228
                           "due_date" => { :type => :date, :order => 12 },
229
                           "estimated_hours" => { :type => :float, :order => 13 },
230
                           "done_ratio" =>  { :type => :integer, :order => 14 }}
231

  
232
    principals = []
233
    if project
234
      principals += project.principals.sort
235
      unless project.leaf?
236
        subprojects = project.descendants.visible.all
237
        if subprojects.any?
238
          @available_filters["subproject_id"] = { :type => :list_subprojects, :order => 13, :values => subprojects.collect{|s| [s.name, s.id.to_s] } }
239
          principals += Principal.member_of(subprojects)
240
        end
241
      end
242
    else
243
      all_projects = Project.visible.all
244
      if all_projects.any?
245
        # members of visible projects
246
        principals += Principal.member_of(all_projects)
247

  
248
        # project filter
249
        project_values = []
250
        if User.current.logged? && User.current.memberships.any?
251
          project_values << ["<< #{l(:label_my_projects).downcase} >>", "mine"]
252
        end
253
        Project.project_tree(all_projects) do |p, level|
254
          prefix = (level > 0 ? ('--' * level + ' ') : '')
255
          project_values << ["#{prefix}#{p.name}", p.id.to_s]
256
        end
257
        @available_filters["project_id"] = { :type => :list, :order => 1, :values => project_values} unless project_values.empty?
258
      end
259
    end
260
    principals.uniq!
261
    principals.sort!
262
    users = principals.select {|p| p.is_a?(User)}
263

  
264
    assigned_to_values = []
265
    assigned_to_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged?
266
    assigned_to_values += (Setting.issue_group_assignment? ? principals : users).collect{|s| [s.name, s.id.to_s] }
267
    @available_filters["assigned_to_id"] = { :type => :list_optional, :order => 4, :values => assigned_to_values } unless assigned_to_values.empty?
268

  
269
    author_values = []
270
    author_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged?
271
    author_values += users.collect{|s| [s.name, s.id.to_s] }
272
    @available_filters["author_id"] = { :type => :list, :order => 5, :values => author_values } unless author_values.empty?
273

  
274
    group_values = Group.all.collect {|g| [g.name, g.id.to_s] }
275
    @available_filters["member_of_group"] = { :type => :list_optional, :order => 6, :values => group_values } unless group_values.empty?
276

  
277
    role_values = Role.givable.collect {|r| [r.name, r.id.to_s] }
278
    @available_filters["assigned_to_role"] = { :type => :list_optional, :order => 7, :values => role_values } unless role_values.empty?
279

  
280
    if User.current.logged?
281
      @available_filters["watcher_id"] = { :type => :list, :order => 15, :values => [["<< #{l(:label_me)} >>", "me"]] }
282
    end
283

  
284
    if project
285
      # project specific filters
286
      categories = project.issue_categories.all
287
      unless categories.empty?
288
        @available_filters["category_id"] = { :type => :list_optional, :order => 6, :values => categories.collect{|s| [s.name, s.id.to_s] } }
289
      end
290
      versions = project.shared_versions.all
291
      unless versions.empty?
292
        @available_filters["fixed_version_id"] = { :type => :list_optional, :order => 7, :values => versions.sort.collect{|s| ["#{s.project.name} - #{s.name}", s.id.to_s] } }
293
      end
294
      add_custom_fields_filters(project.all_issue_custom_fields)
295
    else
296
      # global filters for cross project issue list
297
      system_shared_versions = Version.visible.find_all_by_sharing('system')
298
      unless system_shared_versions.empty?
299
        @available_filters["fixed_version_id"] = { :type => :list_optional, :order => 7, :values => system_shared_versions.sort.collect{|s| ["#{s.project.name} - #{s.name}", s.id.to_s] } }
300
      end
301
      add_custom_fields_filters(IssueCustomField.find(:all, :conditions => {:is_filter => true, :is_for_all => true}))
302
    end
303
    @available_filters
304
  end
305

  
306
  def add_filter(field, operator, values)
307
    # values must be an array
308
    return unless values.nil? || values.is_a?(Array)
309
    # check if field is defined as an available filter
310
    if available_filters.has_key? field
311
      filter_options = available_filters[field]
312
      # check if operator is allowed for that filter
313
      #if @@operators_by_filter_type[filter_options[:type]].include? operator
314
      #  allowed_values = values & ([""] + (filter_options[:values] || []).collect {|val| val[1]})
315
      #  filters[field] = {:operator => operator, :values => allowed_values } if (allowed_values.first and !allowed_values.first.empty?) or ["o", "c", "!*", "*", "t"].include? operator
316
      #end
317
      filters[field] = {:operator => operator, :values => (values || [''])}
318
    end
319
  end
320

  
321
  def add_short_filter(field, expression)
322
    return unless expression && available_filters.has_key?(field)
323
    field_type = available_filters[field][:type]
324
    @@operators_by_filter_type[field_type].sort.reverse.detect do |operator|
325
      next unless expression =~ /^#{Regexp.escape(operator)}(.*)$/
326
      add_filter field, operator, $1.present? ? $1.split('|') : ['']
327
    end || add_filter(field, '=', expression.split('|'))
328
  end
329

  
330
  # Add multiple filters using +add_filter+
331
  def add_filters(fields, operators, values)
332
    if fields.is_a?(Array) && operators.is_a?(Hash) && (values.nil? || values.is_a?(Hash))
333
      fields.each do |field|
334
        add_filter(field, operators[field], values && values[field])
335
      end
336
    end
337
  end
338

  
339
  def has_filter?(field)
340
    filters and filters[field]
341
  end
342

  
343
  def type_for(field)
344
    available_filters[field][:type] if available_filters.has_key?(field)
345
  end
346

  
347
  def operator_for(field)
348
    has_filter?(field) ? filters[field][:operator] : nil
349
  end
350

  
351
  def values_for(field)
352
    has_filter?(field) ? filters[field][:values] : nil
353
  end
354

  
355
  def value_for(field, index=0)
356
    (values_for(field) || [])[index]
357
  end
358

  
359
  def label_for(field)
360
    label = available_filters[field][:name] if available_filters.has_key?(field)
361
    label ||= l("field_#{field.to_s.gsub(/_id$/, '')}", :default => field)
362
  end
363

  
364
  def available_columns
365
    return @available_columns if @available_columns
366
    @available_columns = ::Query.available_columns.dup
367
    @available_columns += (project ?
368
                            project.all_issue_custom_fields :
369
                            IssueCustomField.find(:all)
370
                           ).collect {|cf| QueryCustomFieldColumn.new(cf) }
371

  
372
    if User.current.allowed_to?(:view_time_entries, project, :global => true)
373
      index = nil
374
      @available_columns.each_with_index {|column, i| index = i if column.name == :estimated_hours}
375
      index = (index ? index + 1 : -1)
376
      # insert the column after estimated_hours or at the end
377
      @available_columns.insert index, QueryColumn.new(:spent_hours,
378
        :sortable => "(SELECT COALESCE(SUM(hours), 0) FROM #{TimeEntry.table_name} WHERE #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id)",
379
        :default_order => 'desc',
380
        :caption => :label_spent_time
381
      )
382
    end
383
    @available_columns
384
  end
385

  
386
  def self.available_columns=(v)
387
    self.available_columns = (v)
388
  end
389

  
390
  def self.add_available_column(column)
391
    self.available_columns << (column) if column.is_a?(QueryColumn)
392
  end
393

  
394
  # Returns an array of columns that can be used to group the results
395
  def groupable_columns
396
    available_columns.select {|c| c.groupable}
397
  end
398

  
399
  # Returns a Hash of columns and the key for sorting
400
  def sortable_columns
401
    {'id' => "#{Issue.table_name}.id"}.merge(available_columns.inject({}) {|h, column|
402
                                               h[column.name.to_s] = column.sortable
403
                                               h
404
                                             })
405
  end
406

  
407
  def columns
408
    # preserve the column_names order
409
    (has_default_columns? ? default_columns_names : column_names).collect do |name|
410
       available_columns.find { |col| col.name == name }
411
    end.compact
412
  end
413

  
414
  def default_columns_names
415
    @default_columns_names ||= begin
416
      default_columns = Setting.issue_list_default_columns.map(&:to_sym)
417

  
418
      project.present? ? default_columns : [:project] | default_columns
419
    end
420
  end
421

  
422
  def column_names=(names)
423
    if names
424
      names = names.select {|n| n.is_a?(Symbol) || !n.blank? }
425
      names = names.collect {|n| n.is_a?(Symbol) ? n : n.to_sym }
426
      # Set column_names to nil if default columns
427
      if names == default_columns_names
428
        names = nil
429
      end
430
    end
431
    write_attribute(:column_names, names)
432
  end
433

  
434
  def has_column?(column)
435
    column_names && column_names.include?(column.is_a?(QueryColumn) ? column.name : column)
436
  end
437

  
438
  def has_default_columns?
439
    column_names.nil? || column_names.empty?
440
  end
441

  
442
  def sort_criteria=(arg)
443
    c = []
444
    if arg.is_a?(Hash)
445
      arg = arg.keys.sort.collect {|k| arg[k]}
446
    end
447
    c = arg.select {|k,o| !k.to_s.blank?}.slice(0,3).collect {|k,o| [k.to_s, o == 'desc' ? o : 'asc']}
448
    write_attribute(:sort_criteria, c)
449
  end
450

  
451
  def sort_criteria
452
    read_attribute(:sort_criteria) || []
453
  end
454

  
455
  def sort_criteria_key(arg)
456
    sort_criteria && sort_criteria[arg] && sort_criteria[arg].first
457
  end
458

  
459
  def sort_criteria_order(arg)
460
    sort_criteria && sort_criteria[arg] && sort_criteria[arg].last
461
  end
462

  
463
  # Returns the SQL sort order that should be prepended for grouping
464
  def group_by_sort_order
465
    if grouped? && (column = group_by_column)
466
      column.sortable.is_a?(Array) ?
467
        column.sortable.collect {|s| "#{s} #{column.default_order}"}.join(',') :
468
        "#{column.sortable} #{column.default_order}"
469
    end
470
  end
471

  
472
  # Returns true if the query is a grouped query
473
  def grouped?
474
    !group_by_column.nil?
475
  end
476

  
477
  def group_by_column
478
    groupable_columns.detect {|c| c.groupable && c.name.to_s == group_by}
479
  end
480

  
481
  def group_by_statement
482
    group_by_column.try(:groupable)
483
  end
484

  
485
  def project_statement
486
    project_clauses = []
487
    if project && !project.descendants.active.empty?
488
      ids = [project.id]
489
      if has_filter?("subproject_id")
490
        case operator_for("subproject_id")
491
        when '='
492
          # include the selected subprojects
493
          ids += values_for("subproject_id").each(&:to_i)
494
        when '!*'
495
          # main project only
496
        else
497
          # all subprojects
498
          ids += project.descendants.collect(&:id)
499
        end
500
      elsif Setting.display_subprojects_issues?
501
        ids += project.descendants.collect(&:id)
502
      end
503
      project_clauses << "#{Project.table_name}.id IN (%s)" % ids.join(',')
504
    elsif project
505
      project_clauses << "#{Project.table_name}.id = %d" % project.id
506
    end
507
    project_clauses.any? ? project_clauses.join(' AND ') : nil
508
  end
509

  
510
  def statement
511
    # filters clauses
512
    filters_clauses = []
513
    filters.each_key do |field|
514
      next if field == "subproject_id"
515
      v = values_for(field).clone
516
      next unless v and !v.empty?
517
      operator = operator_for(field)
518

  
519
      # "me" value subsitution
520
      if %w(assigned_to_id author_id watcher_id).include?(field)
521
        if v.delete("me")
522
          if User.current.logged?
523
            v.push(User.current.id.to_s)
524
            v += User.current.group_ids.map(&:to_s) if field == 'assigned_to_id'
525
          else
526
            v.push("0")
527
          end
528
        end
529
      end
530

  
531
      if field == 'project_id'
532
        if v.delete('mine')
533
          v += User.current.memberships.map(&:project_id).map(&:to_s)
534
        end
535
      end
536

  
537
      if field =~ /^cf_(\d+)$/
538
        # custom field
539
        filters_clauses << sql_for_custom_field(field, operator, v, $1)
540
      elsif respond_to?("sql_for_#{field}_field")
541
        # specific statement
542
        filters_clauses << send("sql_for_#{field}_field", field, operator, v)
543
      else
544
        # regular field
545
        filters_clauses << '(' + sql_for_field(field, operator, v, Issue.table_name, field) + ')'
546
      end
547
    end if filters and valid?
548

  
549
    filters_clauses << project_statement
550
    filters_clauses.reject!(&:blank?)
551

  
552
    filters_clauses.any? ? filters_clauses.join(' AND ') : nil
553
  end
554

  
555
  # Returns the issue count
556
  def issue_count
557
    Issue.visible.count(:include => [:status, :project], :conditions => statement)
558
  rescue ::ActiveRecord::StatementInvalid => e
559
    raise StatementInvalid.new(e.message)
560
  end
561

  
562
  # Returns the issue count by group or nil if query is not grouped
563
  def issue_count_by_group
564
    r = nil
565
    if grouped?
566
      begin
567
        # Rails will raise an (unexpected) RecordNotFound if there's only a nil group value
568
        r = Issue.visible.count(:group => group_by_statement, :include => [:status, :project], :conditions => statement)
569
      rescue ActiveRecord::RecordNotFound
570
        r = {nil => issue_count}
571
      end
572
      c = group_by_column
573
      if c.is_a?(QueryCustomFieldColumn)
574
        r = r.keys.inject({}) {|h, k| h[c.custom_field.cast_value(k)] = r[k]; h}
575
      end
576
    end
577
    r
578
  rescue ::ActiveRecord::StatementInvalid => e
579
    raise StatementInvalid.new(e.message)
580
  end
581

  
582
  # Returns the issues
583
  # Valid options are :order, :offset, :limit, :include, :conditions
584
  def issues(options={})
585
    order_option = [group_by_sort_order, options[:order]].reject {|s| s.blank?}.join(',')
586
    order_option = nil if order_option.blank?
587
    
588
    joins = (order_option && order_option.include?('authors')) ? "LEFT OUTER JOIN users authors ON authors.id = #{Issue.table_name}.author_id" : nil
589

  
590
    issues = Issue.visible.scoped(:conditions => options[:conditions]).find :all, :include => ([:status, :project] + (options[:include] || [])).uniq,
591
                     :conditions => statement,
592
                     :order => order_option,
593
                     :joins => joins,
594
                     :limit  => options[:limit],
595
                     :offset => options[:offset]
596

  
597
    if has_column?(:spent_hours)
598
      Issue.load_visible_spent_hours(issues)
599
    end
600
    issues
601
  rescue ::ActiveRecord::StatementInvalid => e
602
    raise StatementInvalid.new(e.message)
603
  end
604

  
605
  # Returns the issues ids
606
  def issue_ids(options={})
607
    order_option = [group_by_sort_order, options[:order]].reject {|s| s.blank?}.join(',')
608
    order_option = nil if order_option.blank?
609
    
610
    joins = (order_option && order_option.include?('authors')) ? "LEFT OUTER JOIN users authors ON authors.id = #{Issue.table_name}.author_id" : nil
611

  
612
    Issue.visible.scoped(:conditions => options[:conditions]).scoped(:include => ([:status, :project] + (options[:include] || [])).uniq,
613
                     :conditions => statement,
614
                     :order => order_option,
615
                     :joins => joins,
616
                     :limit  => options[:limit],
617
                     :offset => options[:offset]).find_ids
618
  rescue ::ActiveRecord::StatementInvalid => e
619
    raise StatementInvalid.new(e.message)
620
  end
621

  
622
  # Returns the journals
623
  # Valid options are :order, :offset, :limit
624
  def journals(options={})
625
    Journal.visible.find :all, :include => [:details, :user, {:issue => [:project, :author, :tracker, :status]}],
626
                       :conditions => statement,
627
                       :order => options[:order],
628
                       :limit => options[:limit],
629
                       :offset => options[:offset]
630
  rescue ::ActiveRecord::StatementInvalid => e
631
    raise StatementInvalid.new(e.message)
632
  end
633

  
634
  # Returns the versions
635
  # Valid options are :conditions
636
  def versions(options={})
637
    Version.visible.scoped(:conditions => options[:conditions]).find :all, :include => :project, :conditions => project_statement
638
  rescue ::ActiveRecord::StatementInvalid => e
639
    raise StatementInvalid.new(e.message)
640
  end
641

  
642
  def sql_for_watcher_id_field(field, operator, value)
643
    db_table = Watcher.table_name
644
    "#{Issue.table_name}.id #{ operator == '=' ? 'IN' : 'NOT IN' } (SELECT #{db_table}.watchable_id FROM #{db_table} WHERE #{db_table}.watchable_type='Issue' AND " +
645
      sql_for_field(field, '=', value, db_table, 'user_id') + ')'
646
  end
647

  
648
  def sql_for_member_of_group_field(field, operator, value)
649
    if operator == '*' # Any group
650
      groups = Group.all
651
      operator = '=' # Override the operator since we want to find by assigned_to
652
    elsif operator == "!*"
653
      groups = Group.all
654
      operator = '!' # Override the operator since we want to find by assigned_to
655
    else
656
      groups = Group.find_all_by_id(value)
657
    end
658
    groups ||= []
659

  
660
    members_of_groups = groups.inject([]) {|user_ids, group|
661
      if group && group.user_ids.present?
662
        user_ids << group.user_ids
663
      end
664
      user_ids.flatten.uniq.compact
665
    }.sort.collect(&:to_s)
666

  
667
    '(' + sql_for_field("assigned_to_id", operator, members_of_groups, Issue.table_name, "assigned_to_id", false) + ')'
668
  end
669

  
670
  def sql_for_assigned_to_role_field(field, operator, value)
671
    case operator
672
    when "*", "!*" # Member / Not member
673
      sw = operator == "!*" ? 'NOT' : ''
674
      nl = operator == "!*" ? "#{Issue.table_name}.assigned_to_id IS NULL OR" : ''
675
      "(#{nl} #{Issue.table_name}.assigned_to_id #{sw} IN (SELECT DISTINCT #{Member.table_name}.user_id FROM #{Member.table_name}" +
676
        " WHERE #{Member.table_name}.project_id = #{Issue.table_name}.project_id))"
677
    when "=", "!"
678
      role_cond = value.any? ? 
679
        "#{MemberRole.table_name}.role_id IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")" :
680
        "1=0"
681
      
682
      sw = operator == "!" ? 'NOT' : ''
683
      nl = operator == "!" ? "#{Issue.table_name}.assigned_to_id IS NULL OR" : ''
684
      "(#{nl} #{Issue.table_name}.assigned_to_id #{sw} IN (SELECT DISTINCT #{Member.table_name}.user_id FROM #{Member.table_name}, #{MemberRole.table_name}" +
685
        " WHERE #{Member.table_name}.project_id = #{Issue.table_name}.project_id AND #{Member.table_name}.id = #{MemberRole.table_name}.member_id AND #{role_cond}))"
686
    end
687
  end
688

  
689
  private
690

  
691
  def sql_for_custom_field(field, operator, value, custom_field_id)
692
    db_table = CustomValue.table_name
693
    db_field = 'value'
694
    filter = @available_filters[field]
695
    if filter && filter[:format] == 'user'
696
      if value.delete('me')
697
        value.push User.current.id.to_s
698
      end
699
    end
700
    not_in = nil
701
    if operator == '!'
702
      # Makes ! operator work for custom fields with multiple values
703
      operator = '='
704
      not_in = 'NOT'
705
    end
706
    "#{Issue.table_name}.id #{not_in} IN (SELECT #{Issue.table_name}.id FROM #{Issue.table_name} LEFT OUTER JOIN #{db_table} ON #{db_table}.customized_type='Issue' AND #{db_table}.customized_id=#{Issue.table_name}.id AND #{db_table}.custom_field_id=#{custom_field_id} WHERE " +
707
      sql_for_field(field, operator, value, db_table, db_field, true) + ')'
708
  end
709

  
710
  # Helper method to generate the WHERE sql for a +field+, +operator+ and a +value+
711
  def sql_for_field(field, operator, value, db_table, db_field, is_custom_filter=false)
712
    sql = ''
713
    case operator
714
    when "="
715
      if value.any?
716
        case type_for(field)
717
        when :date, :date_past
718
          sql = date_clause(db_table, db_field, (Date.parse(value.first) rescue nil), (Date.parse(value.first) rescue nil))
719
        when :integer
720
          if is_custom_filter
721
            sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) = #{value.first.to_i})"
722
          else
723
            sql = "#{db_table}.#{db_field} = #{value.first.to_i}"
724
          end
725
        when :float
726
          if is_custom_filter
727
            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})"
728
          else
729
            sql = "#{db_table}.#{db_field} BETWEEN #{value.first.to_f - 1e-5} AND #{value.first.to_f + 1e-5}"
730
          end
731
        else
732
          sql = "#{db_table}.#{db_field} IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")"
733
        end
734
      else
735
        # IN an empty set
736
        sql = "1=0"
737
      end
738
    when "!"
739
      if value.any?
740
        sql = "(#{db_table}.#{db_field} IS NULL OR #{db_table}.#{db_field} NOT IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + "))"
741
      else
742
        # NOT IN an empty set
743
        sql = "1=1"
744
      end
745
    when "!*"
746
      sql = "#{db_table}.#{db_field} IS NULL"
747
      sql << " OR #{db_table}.#{db_field} = ''" if is_custom_filter
748
    when "*"
749
      sql = "#{db_table}.#{db_field} IS NOT NULL"
750
      sql << " AND #{db_table}.#{db_field} <> ''" if is_custom_filter
751
    when ">="
752
      if [:date, :date_past].include?(type_for(field))
753
        sql = date_clause(db_table, db_field, (Date.parse(value.first) rescue nil), nil)
754
      else
755
        if is_custom_filter
756
          sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) >= #{value.first.to_f})"
757
        else
758
          sql = "#{db_table}.#{db_field} >= #{value.first.to_f}"
759
        end
760
      end
761
    when "<="
762
      if [:date, :date_past].include?(type_for(field))
763
        sql = date_clause(db_table, db_field, nil, (Date.parse(value.first) rescue nil))
764
      else
765
        if is_custom_filter
766
          sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) <= #{value.first.to_f})"
767
        else
768
          sql = "#{db_table}.#{db_field} <= #{value.first.to_f}"
769
        end
770
      end
771
    when "><"
772
      if [:date, :date_past].include?(type_for(field))
773
        sql = date_clause(db_table, db_field, (Date.parse(value[0]) rescue nil), (Date.parse(value[1]) rescue nil))
774
      else
775
        if is_custom_filter
776
          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})"
777
        else
778
          sql = "#{db_table}.#{db_field} BETWEEN #{value[0].to_f} AND #{value[1].to_f}"
779
        end
780
      end
781
    when "o"
782
      sql = "#{Issue.table_name}.status_id IN (SELECT id FROM #{IssueStatus.table_name} WHERE is_closed=#{connection.quoted_false})" if field == "status_id"
783
    when "c"
784
      sql = "#{Issue.table_name}.status_id IN (SELECT id FROM #{IssueStatus.table_name} WHERE is_closed=#{connection.quoted_true})" if field == "status_id"
785
    when ">t-"
786
      sql = relative_date_clause(db_table, db_field, - value.first.to_i, 0)
787
    when "<t-"
788
      sql = relative_date_clause(db_table, db_field, nil, - value.first.to_i)
789
    when "t-"
790
      sql = relative_date_clause(db_table, db_field, - value.first.to_i, - value.first.to_i)
791
    when ">t+"
792
      sql = relative_date_clause(db_table, db_field, value.first.to_i, nil)
793
    when "<t+"
794
      sql = relative_date_clause(db_table, db_field, 0, value.first.to_i)
795
    when "t+"
796
      sql = relative_date_clause(db_table, db_field, value.first.to_i, value.first.to_i)
797
    when "t"
798
      sql = relative_date_clause(db_table, db_field, 0, 0)
799
    when "w"
800
      first_day_of_week = l(:general_first_day_of_week).to_i
801
      day_of_week = Date.today.cwday
802
      days_ago = (day_of_week >= first_day_of_week ? day_of_week - first_day_of_week : day_of_week + 7 - first_day_of_week)
803
      sql = relative_date_clause(db_table, db_field, - days_ago, - days_ago + 6)
804
    when "~"
805
      sql = "LOWER(#{db_table}.#{db_field}) LIKE '%#{connection.quote_string(value.first.to_s.downcase)}%'"
806
    when "!~"
807
      sql = "LOWER(#{db_table}.#{db_field}) NOT LIKE '%#{connection.quote_string(value.first.to_s.downcase)}%'"
808
    else
809
      raise "Unknown query operator #{operator}"
810
    end
811

  
812
    return sql
813
  end
814

  
815
  def add_custom_fields_filters(custom_fields)
816
    @available_filters ||= {}
817

  
818
    custom_fields.select(&:is_filter?).each do |field|
819
      case field.field_format
820
      when "text"
821
        options = { :type => :text, :order => 20 }
822
      when "list"
823
        options = { :type => :list_optional, :values => field.possible_values, :order => 20}
824
      when "date"
825
        options = { :type => :date, :order => 20 }
826
      when "bool"
827
        options = { :type => :list, :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]], :order => 20 }
828
      when "int"
829
        options = { :type => :integer, :order => 20 }
830
      when "float"
831
        options = { :type => :float, :order => 20 }
832
      when "user", "version"
833
        next unless project
834
        values = field.possible_values_options(project)
835
        if User.current.logged? && field.field_format == 'user'
836
          values.unshift ["<< #{l(:label_me)} >>", "me"]
837
        end
838
        options = { :type => :list_optional, :values => values, :order => 20}
839
      else
840
        options = { :type => :string, :order => 20 }
841
      end
842
      @available_filters["cf_#{field.id}"] = options.merge({ :name => field.name, :format => field.field_format })
843
    end
844
  end
845

  
846
  # Returns a SQL clause for a date or datetime field.
847
  def date_clause(table, field, from, to)
848
    s = []
849
    if from
850
      from_yesterday = from - 1
851
      from_yesterday_utc = Time.gm(from_yesterday.year, from_yesterday.month, from_yesterday.day)
852
      s << ("#{table}.#{field} > '%s'" % [connection.quoted_date(from_yesterday_utc.end_of_day)])
853
    end
854
    if to
855
      to_utc = Time.gm(to.year, to.month, to.day)
856
      s << ("#{table}.#{field} <= '%s'" % [connection.quoted_date(to_utc.end_of_day)])
857
    end
858
    s.join(' AND ')
859
  end
860

  
861
  # Returns a SQL clause for a date or datetime field using relative dates.
862
  def relative_date_clause(table, field, days_from, days_to)
863
    date_clause(table, field, (days_from ? Date.today + days_from : nil), (days_to ? Date.today + days_to : nil))
864
  end
865
end
1
# Redmine - project management software
2
# Copyright (C) 2006-2011  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
    @caption_key = options[:caption] || "field_#{name}"
31
  end
32

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

  
37
  # Returns true if the column is sortable, otherwise false
38
  def sortable?
39
    !@sortable.nil?
40
  end
41
  
42
  def sortable
43
    @sortable.is_a?(Proc) ? @sortable.call : @sortable
44
  end
45

  
46
  def value(issue)
47
    issue.send name
48
  end
49

  
50
  def css_classes
51
    name
52
  end
53
end
54

  
55
class QueryCustomFieldColumn < QueryColumn
56

  
57
  def initialize(custom_field)
58
    self.name = "cf_#{custom_field.id}".to_sym
59
    self.sortable = custom_field.order_statement || false
60
    if %w(list date bool int).include?(custom_field.field_format) && !custom_field.multiple?
61
      self.groupable = custom_field.order_statement
62
    end
63
    self.groupable ||= false
64
    @cf = custom_field
65
  end
66

  
67
  def caption
68
    @cf.name
69
  end
70

  
71
  def custom_field
72
    @cf
73
  end
74

  
75
  def value(issue)
76
    cv = issue.custom_values.select {|v| v.custom_field_id == @cf.id}.collect {|v| @cf.cast_value(v.value)}
77
    cv.size > 1 ? cv : cv.first
78
  end
79

  
80
  def css_classes
81
    @css_classes ||= "#{name} #{@cf.field_format}"
82
  end
83
end
84

  
85
class Query < ActiveRecord::Base
86
  class StatementInvalid < ::ActiveRecord::StatementInvalid
87
  end
88

  
89
  belongs_to :project
90
  belongs_to :user
91
  serialize :filters
92
  serialize :column_names
93
  serialize :sort_criteria, Array
94

  
95
  attr_protected :project_id, :user_id
96

  
97
  validates_presence_of :name
98
  validates_length_of :name, :maximum => 255
99
  validate :validate_query_filters
100

  
101
  @@operators = { "="   => :label_equals,
102
                  "!"   => :label_not_equals,
103
                  "o"   => :label_open_issues,
104
                  "c"   => :label_closed_issues,
105
                  "!*"  => :label_none,
106
                  "*"   => :label_all,
107
                  ">="  => :label_greater_or_equal,
108
                  "<="  => :label_less_or_equal,
109
                  "><"  => :label_between,
110
                  "<t+" => :label_in_less_than,
111
                  ">t+" => :label_in_more_than,
112
                  "t+"  => :label_in,
113
                  "t"   => :label_today,
114
                  "w"   => :label_this_week,
115
                  ">t-" => :label_less_than_ago,
116
                  "<t-" => :label_more_than_ago,
117
                  "t-"  => :label_ago,
118
                  "~"   => :label_contains,
119
                  "!~"  => :label_not_contains }
120

  
121
  cattr_reader :operators
122

  
123
  @@operators_by_filter_type = { :list => [ "=", "!" ],
124
                                 :list_status => [ "o", "=", "!", "c", "*" ],
125
                                 :list_optional => [ "=", "!", "!*", "*" ],
126
                                 :list_subprojects => [ "*", "!*", "=" ],
127
                                 :date => [ "=", ">=", "<=", "><", "<t+", ">t+", "t+", "t", "w", ">t-", "<t-", "t-", "!*", "*" ],
128
                                 :date_past => [ "=", ">=", "<=", "><", ">t-", "<t-", "t-", "t", "w", "!*", "*" ],
129
                                 :string => [ "=", "~", "!", "!~", "!*", "*" ],
130
                                 :text => [  "~", "!~", "!*", "*" ],
131
                                 :integer => [ "=", ">=", "<=", "><", "!*", "*" ],
132
                                 :float => [ "=", ">=", "<=", "><", "!*", "*" ] }
133

  
134
  cattr_reader :operators_by_filter_type
135

  
136
  @@available_columns = [
137
    QueryColumn.new(:project, :sortable => "#{Project.table_name}.name", :groupable => true),
138
    QueryColumn.new(:tracker, :sortable => "#{Tracker.table_name}.position", :groupable => true),
139
    QueryColumn.new(:parent, :sortable => ["#{Issue.table_name}.root_id", "#{Issue.table_name}.lft ASC"], :default_order => 'desc', :caption => :field_parent_issue),
140
    QueryColumn.new(:status, :sortable => "#{IssueStatus.table_name}.position", :groupable => true),
141
    QueryColumn.new(:priority, :sortable => "#{IssuePriority.table_name}.position", :default_order => 'desc', :groupable => true),
142
    QueryColumn.new(:subject, :sortable => "#{Issue.table_name}.subject"),
143
    QueryColumn.new(:author, :sortable => lambda {User.fields_for_order_statement("authors")}, :groupable => true),
144
    QueryColumn.new(:assigned_to, :sortable => lambda {User.fields_for_order_statement}, :groupable => true),
145
    QueryColumn.new(:updated_on, :sortable => "#{Issue.table_name}.updated_on", :default_order => 'desc'),
146
    QueryColumn.new(:category, :sortable => "#{IssueCategory.table_name}.name", :groupable => true),
147
    QueryColumn.new(:fixed_version, :sortable => ["#{Version.table_name}.effective_date", "#{Version.table_name}.name"], :default_order => 'desc', :groupable => true),
148
    QueryColumn.new(:start_date, :sortable => "#{Issue.table_name}.start_date"),
149
    QueryColumn.new(:due_date, :sortable => "#{Issue.table_name}.due_date"),
150
    QueryColumn.new(:estimated_hours, :sortable => "#{Issue.table_name}.estimated_hours"),
151
    QueryColumn.new(:done_ratio, :sortable => "#{Issue.table_name}.done_ratio", :groupable => true),
152
    QueryColumn.new(:created_on, :sortable => "#{Issue.table_name}.created_on", :default_order => 'desc'),
153
  ]
154
  cattr_reader :available_columns
155

  
156
  named_scope :visible, lambda {|*args|
157
    user = args.shift || User.current
158
    base = Project.allowed_to_condition(user, :view_issues, *args)
159
    user_id = user.logged? ? user.id : 0
160
    {
161
      :conditions => ["(#{table_name}.project_id IS NULL OR (#{base})) AND (#{table_name}.is_public = ? OR #{table_name}.user_id = ?)", true, user_id],
162
      :include => :project
163
    }
164
  }
165

  
166
  def initialize(attributes=nil, *args)
167
    super attributes
168
    self.filters ||= { 'status_id' => {:operator => "o", :values => [""]} }
169
    @is_for_all = project.nil?
170
  end
171

  
172
  def validate_query_filters
173
    filters.each_key do |field|
174
      if values_for(field)
175
        case type_for(field)
176
        when :integer
177
          add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^\d+$/) }
178
        when :float
179
          add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^\d+(\.\d*)?$/) }
180
        when :date, :date_past
181
          case operator_for(field)
182
          when "=", ">=", "<=", "><"
183
            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?) }
184
          when ">t-", "<t-", "t-"
185
            add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^\d+$/) }
186
          end
187
        end
188
      end
189

  
190
      add_filter_error(field, :blank) unless
191
          # filter requires one or more values
192
          (values_for(field) and !values_for(field).first.blank?) or
193
          # filter doesn't require any value
194
          ["o", "c", "!*", "*", "t", "w"].include? operator_for(field)
195
    end if filters
196
  end
197

  
198
  def add_filter_error(field, message)
199
    m = label_for(field) + " " + l(message, :scope => 'activerecord.errors.messages')
200
    errors.add(:base, m)
201
  end
202

  
203
  # Returns true if the query is visible to +user+ or the current user.
204
  def visible?(user=User.current)
205
    (project.nil? || user.allowed_to?(:view_issues, project)) && (self.is_public? || self.user_id == user.id)
206
  end
207

  
208
  def editable_by?(user)
209
    return false unless user
210
    # Admin can edit them all and regular users can edit their private queries
211
    return true if user.admin? || (!is_public && self.user_id == user.id)
212
    # Members can not edit public queries that are for all project (only admin is allowed to)
213
    is_public && !@is_for_all && user.allowed_to?(:manage_public_queries, project)
214
  end
215

  
216
  def available_filters
217
    return @available_filters if @available_filters
218

  
219
    trackers = project.nil? ? Tracker.find(:all, :order => 'position') : project.rolled_up_trackers
220

  
221
    @available_filters = { "status_id" => { :type => :list_status, :order => 1, :values => IssueStatus.find(:all, :order => 'position').collect{|s| [s.name, s.id.to_s] } },
222
                           "tracker_id" => { :type => :list, :order => 2, :values => trackers.collect{|s| [s.name, s.id.to_s] } },
223
                           "priority_id" => { :type => :list, :order => 3, :values => IssuePriority.all.collect{|s| [s.name, s.id.to_s] } },
224
                           "subject" => { :type => :text, :order => 8 },
225
                           "created_on" => { :type => :date_past, :order => 9 },
226
                           "updated_on" => { :type => :date_past, :order => 10 },
227
                           "start_date" => { :type => :date, :order => 11 },
228
                           "due_date" => { :type => :date, :order => 12 },
229
                           "estimated_hours" => { :type => :float, :order => 13 },
230
                           "done_ratio" =>  { :type => :integer, :order => 14 }}
231

  
232
    principals = []
233
    if project
234
      principals += project.principals.sort
235
      unless project.leaf?
236
        subprojects = project.descendants.visible.all
237
        if subprojects.any?
238
          @available_filters["subproject_id"] = { :type => :list_subprojects, :order => 13, :values => subprojects.collect{|s| [s.name, s.id.to_s] } }
239
          principals += Principal.member_of(subprojects)
240
        end
241
      end
242
    else
243
      all_projects = Project.visible.all
244
      if all_projects.any?
245
        # members of visible projects
246
        principals += Principal.member_of(all_projects)
247

  
248
        # project filter
249
        project_values = []
250
        if User.current.logged? && User.current.memberships.any?
251
          project_values << ["<< #{l(:label_my_projects).downcase} >>", "mine"]
252
        end
253
        Project.project_tree(all_projects) do |p, level|
254
          prefix = (level > 0 ? ('--' * level + ' ') : '')
255
          project_values << ["#{prefix}#{p.name}", p.id.to_s]
256
        end
257
        @available_filters["project_id"] = { :type => :list, :order => 1, :values => project_values} unless project_values.empty?
258
      end
259
    end
260
    principals.uniq!
261
    principals.sort!
262
    users = principals.select {|p| p.is_a?(User)}
263

  
264
    assigned_to_values = []
265
    assigned_to_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged?
266
    assigned_to_values += (Setting.issue_group_assignment? ? principals : users).collect{|s| [s.name, s.id.to_s] }
267
    @available_filters["assigned_to_id"] = { :type => :list_optional, :order => 4, :values => assigned_to_values } unless assigned_to_values.empty?
268

  
269
    author_values = []
270
    author_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged?
271
    author_values += users.collect{|s| [s.name, s.id.to_s] }
272
    @available_filters["author_id"] = { :type => :list, :order => 5, :values => author_values } unless author_values.empty?
273

  
274
    group_values = Group.all.collect {|g| [g.name, g.id.to_s] }
275
    @available_filters["member_of_group"] = { :type => :list_optional, :order => 6, :values => group_values } unless group_values.empty?
276

  
277
    role_values = Role.givable.collect {|r| [r.name, r.id.to_s] }
278
    @available_filters["assigned_to_role"] = { :type => :list_optional, :order => 7, :values => role_values } unless role_values.empty?
279

  
280
    if User.current.logged?
281
      @available_filters["watcher_id"] = { :type => :list, :order => 15, :values => [["<< #{l(:label_me)} >>", "me"]] }
282
    end
283

  
284
    if project
285
      # project specific filters
286
      categories = project.issue_categories.all
287
      unless categories.empty?
288
        @available_filters["category_id"] = { :type => :list_optional, :order => 6, :values => categories.collect{|s| [s.name, s.id.to_s] } }
289
      end
290
      versions = project.shared_versions.all
291
      unless versions.empty?
292
        @available_filters["fixed_version_id"] = { :type => :list_optional, :order => 7, :values => versions.sort.collect{|s| ["#{s.project.name} - #{s.name}", s.id.to_s] } }
293
      end
294
      add_custom_fields_filters(project.all_issue_custom_fields)
295
    else
296
      # global filters for cross project issue list
297
      system_shared_versions = Version.visible.find_all_by_sharing('system')
298
      unless system_shared_versions.empty?
299
        @available_filters["fixed_version_id"] = { :type => :list_optional, :order => 7, :values => system_shared_versions.sort.collect{|s| ["#{s.project.name} - #{s.name}", s.id.to_s] } }
300
      end
301
      add_custom_fields_filters(IssueCustomField.find(:all, :conditions => {:is_filter => true, :is_for_all => true}))
302
    end
303
    @available_filters
304
  end
305

  
306
  def add_filter(field, operator, values)
307
    # values must be an array
308
    return unless values.nil? || values.is_a?(Array)
309
    # check if field is defined as an available filter
310
    if available_filters.has_key? field
311
      filter_options = available_filters[field]
312
      # check if operator is allowed for that filter
313
      #if @@operators_by_filter_type[filter_options[:type]].include? operator
314
      #  allowed_values = values & ([""] + (filter_options[:values] || []).collect {|val| val[1]})
315
      #  filters[field] = {:operator => operator, :values => allowed_values } if (allowed_values.first and !allowed_values.first.empty?) or ["o", "c", "!*", "*", "t"].include? operator
316
      #end
317
      filters[field] = {:operator => operator, :values => (values || [''])}
318
    end
319
  end
320

  
321
  def add_short_filter(field, expression)
322
    return unless expression && available_filters.has_key?(field)
323
    field_type = available_filters[field][:type]
324
    @@operators_by_filter_type[field_type].sort.reverse.detect do |operator|
325
      next unless expression =~ /^#{Regexp.escape(operator)}(.*)$/
326
      add_filter field, operator, $1.present? ? $1.split('|') : ['']
327
    end || add_filter(field, '=', expression.split('|'))
328
  end
329

  
330
  # Add multiple filters using +add_filter+
331
  def add_filters(fields, operators, values)
332
    if fields.is_a?(Array) && operators.is_a?(Hash) && (values.nil? || values.is_a?(Hash))
333
      fields.each do |field|
334
        add_filter(field, operators[field], values && values[field])
335
      end
336
    end
337
  end
338

  
339
  def has_filter?(field)
340
    filters and filters[field]
341
  end
342

  
343
  def type_for(field)
344
    available_filters[field][:type] if available_filters.has_key?(field)
345
  end
346

  
347
  def operator_for(field)
348
    has_filter?(field) ? filters[field][:operator] : nil
349
  end
350

  
351
  def values_for(field)
352
    has_filter?(field) ? filters[field][:values] : nil
353
  end
354

  
355
  def value_for(field, index=0)
356
    (values_for(field) || [])[index]
357
  end
358

  
359
  def label_for(field)
360
    label = available_filters[field][:name] if available_filters.has_key?(field)
361
    label ||= l("field_#{field.to_s.gsub(/_id$/, '')}", :default => field)
362
  end
363

  
364
  def available_columns
365
    return @available_columns if @available_columns
366
    @available_columns = ::Query.available_columns.dup
367
    @available_columns += (project ?
368
                            project.all_issue_custom_fields :
369
                            IssueCustomField.find(:all)
370
                           ).collect {|cf| QueryCustomFieldColumn.new(cf) }
371

  
372
    if User.current.allowed_to?(:view_time_entries, project, :global => true)
373
      index = nil
374
      @available_columns.each_with_index {|column, i| index = i if column.name == :estimated_hours}
375
      index = (index ? index + 1 : -1)
376
      # insert the column after estimated_hours or at the end
377
      @available_columns.insert index, QueryColumn.new(:spent_hours,
378
        :sortable => "(SELECT COALESCE(SUM(hours), 0) FROM #{TimeEntry.table_name} WHERE #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id)",
379
        :default_order => 'desc',
380
        :caption => :label_spent_time
381
      )
382
    end
383
    @available_columns
384
  end
385

  
386
  def self.available_columns=(v)
387
    self.available_columns = (v)
388
  end
389

  
390
  def self.add_available_column(column)
391
    self.available_columns << (column) if column.is_a?(QueryColumn)
392
  end
393

  
394
  # Returns an array of columns that can be used to group the results
395
  def groupable_columns
396
    available_columns.select {|c| c.groupable}
397
  end
398

  
399
  # Returns a Hash of columns and the key for sorting
400
  def sortable_columns
401
    {'id' => "#{Issue.table_name}.id"}.merge(available_columns.inject({}) {|h, column|
402
                                               h[column.name.to_s] = column.sortable
403
                                               h
404
                                             })
405
  end
406

  
407
  def columns
408
    # preserve the column_names order
409
    (has_default_columns? ? default_columns_names : column_names).collect do |name|
410
       available_columns.find { |col| col.name == name }
411
    end.compact
412
  end
413

  
414
  def default_columns_names
415
    @default_columns_names ||= begin
416
      default_columns = Setting.issue_list_default_columns.map(&:to_sym)
417

  
418
      project.present? ? default_columns : [:project] | default_columns
419
    end
420
  end
421

  
422
  def column_names=(names)
423
    if names
424
      names = names.select {|n| n.is_a?(Symbol) || !n.blank? }
425
      names = names.collect {|n| n.is_a?(Symbol) ? n : n.to_sym }
426
      # Set column_names to nil if default columns
427
      if names == default_columns_names
428
        names = nil
429
      end
430
    end
431
    write_attribute(:column_names, names)
432
  end
433

  
434
  def has_column?(column)
435
    column_names && column_names.include?(column.is_a?(QueryColumn) ? column.name.to_s : column.to_s)
436
  end
437

  
438
  def has_default_columns?
439
    column_names.nil? || column_names.empty?
440
  end
441

  
442
  def sort_criteria=(arg)
443
    c = []
444
    if arg.is_a?(Hash)
445
      arg = arg.keys.sort.collect {|k| arg[k]}
446
    end
447
    c = arg.select {|k,o| !k.to_s.blank?}.slice(0,3).collect {|k,o| [k.to_s, o == 'desc' ? o : 'asc']}
448
    write_attribute(:sort_criteria, c)
449
  end
450

  
451
  def sort_criteria
452
    read_attribute(:sort_criteria) || []
453
  end
454

  
455
  def sort_criteria_key(arg)
456
    sort_criteria && sort_criteria[arg] && sort_criteria[arg].first
457
  end
458

  
459
  def sort_criteria_order(arg)
460
    sort_criteria && sort_criteria[arg] && sort_criteria[arg].last
461
  end
462

  
463
  # Returns the SQL sort order that should be prepended for grouping
464
  def group_by_sort_order
465
    if grouped? && (column = group_by_column)
466
      column.sortable.is_a?(Array) ?
467
        column.sortable.collect {|s| "#{s} #{column.default_order}"}.join(',') :
468
        "#{column.sortable} #{column.default_order}"
469
    end
470
  end
471

  
472
  # Returns true if the query is a grouped query
473
  def grouped?
474
    !group_by_column.nil?
475
  end
476

  
477
  def group_by_column
478
    groupable_columns.detect {|c| c.groupable && c.name.to_s == group_by}
479
  end
480

  
481
  def group_by_statement
482
    group_by_column.try(:groupable)
483
  end
484

  
485
  def project_statement
486
    project_clauses = []
487
    if project && !project.descendants.active.empty?
488
      ids = [project.id]
489
      if has_filter?("subproject_id")
490
        case operator_for("subproject_id")
491
        when '='
492
          # include the selected subprojects
493
          ids += values_for("subproject_id").each(&:to_i)
494
        when '!*'
495
          # main project only
496
        else
497
          # all subprojects
498
          ids += project.descendants.collect(&:id)
499
        end
500
      elsif Setting.display_subprojects_issues?
501
        ids += project.descendants.collect(&:id)
502
      end
503
      project_clauses << "#{Project.table_name}.id IN (%s)" % ids.join(',')
504
    elsif project
505
      project_clauses << "#{Project.table_name}.id = %d" % project.id
506
    end
507
    project_clauses.any? ? project_clauses.join(' AND ') : nil
508
  end
509

  
510
  def statement
511
    # filters clauses
512
    filters_clauses = []
513
    filters.each_key do |field|
514
      next if field == "subproject_id"
515
      v = values_for(field).clone
516
      next unless v and !v.empty?
517
      operator = operator_for(field)
518

  
519
      # "me" value subsitution
520
      if %w(assigned_to_id author_id watcher_id).include?(field)
521
        if v.delete("me")
522
          if User.current.logged?
523
            v.push(User.current.id.to_s)
524
            v += User.current.group_ids.map(&:to_s) if field == 'assigned_to_id'
525
          else
526
            v.push("0")
527
          end
528
        end
529
      end
530

  
531
      if field == 'project_id'
532
        if v.delete('mine')
533
          v += User.current.memberships.map(&:project_id).map(&:to_s)
534
        end
535
      end
536

  
537
      if field =~ /^cf_(\d+)$/
538
        # custom field
539
        filters_clauses << sql_for_custom_field(field, operator, v, $1)
540
      elsif respond_to?("sql_for_#{field}_field")
541
        # specific statement
542
        filters_clauses << send("sql_for_#{field}_field", field, operator, v)
543
      else
544
        # regular field
545
        filters_clauses << '(' + sql_for_field(field, operator, v, Issue.table_name, field) + ')'
546
      end
547
    end if filters and valid?
548

  
549
    filters_clauses << project_statement
550
    filters_clauses.reject!(&:blank?)
551

  
552
    filters_clauses.any? ? filters_clauses.join(' AND ') : nil
553
  end
554

  
555
  # Returns the issue count
556
  def issue_count
557
    Issue.visible.count(:include => [:status, :project], :conditions => statement)
558
  rescue ::ActiveRecord::StatementInvalid => e
559
    raise StatementInvalid.new(e.message)
560
  end
561

  
562
  # Returns the issue count by group or nil if query is not grouped
563
  def issue_count_by_group
564
    r = nil
565
    if grouped?
566
      begin
567
        # Rails will raise an (unexpected) RecordNotFound if there's only a nil group value
568
        r = Issue.visible.count(:group => group_by_statement, :include => [:status, :project], :conditions => statement)
569
      rescue ActiveRecord::RecordNotFound
570
        r = {nil => issue_count}
571
      end
572
      c = group_by_column
573
      if c.is_a?(QueryCustomFieldColumn)
574
        r = r.keys.inject({}) {|h, k| h[c.custom_field.cast_value(k)] = r[k]; h}
575
      end
576
    end
577
    r
578
  rescue ::ActiveRecord::StatementInvalid => e
579
    raise StatementInvalid.new(e.message)
580
  end
581

  
582
  # Returns the issues
583
  # Valid options are :order, :offset, :limit, :include, :conditions
584
  def issues(options={})
585
    order_option = [group_by_sort_order, options[:order]].reject {|s| s.blank?}.join(',')
586
    order_option = nil if order_option.blank?
587
    
588
    joins = (order_option && order_option.include?('authors')) ? "LEFT OUTER JOIN users authors ON authors.id = #{Issue.table_name}.author_id" : nil
589

  
590
    issues = Issue.visible.scoped(:conditions => options[:conditions]).find :all, :include => ([:status, :project] + (options[:include] || [])).uniq,
591
                     :conditions => statement,
592
                     :order => order_option,
593
                     :joins => joins,
594
                     :limit  => options[:limit],
595
                     :offset => options[:offset]
596

  
597
    if has_column?(:spent_hours)
598
      Issue.load_visible_spent_hours(issues)
599
    end
600
    issues
601
  rescue ::ActiveRecord::StatementInvalid => e
602
    raise StatementInvalid.new(e.message)
603
  end
604

  
605
  # Returns the issues ids
606
  def issue_ids(options={})
607
    order_option = [group_by_sort_order, options[:order]].reject {|s| s.blank?}.join(',')
608
    order_option = nil if order_option.blank?
609
    
610
    joins = (order_option && order_option.include?('authors')) ? "LEFT OUTER JOIN users authors ON authors.id = #{Issue.table_name}.author_id" : nil
611

  
612
    Issue.visible.scoped(:conditions => options[:conditions]).scoped(:include => ([:status, :project] + (options[:include] || [])).uniq,
613
                     :conditions => statement,
614
                     :order => order_option,
615
                     :joins => joins,
616
                     :limit  => options[:limit],
617
                     :offset => options[:offset]).find_ids
618
  rescue ::ActiveRecord::StatementInvalid => e
619
    raise StatementInvalid.new(e.message)
... This diff was truncated because it exceeds the maximum size that can be displayed.
    (1-1/1)