annotate .svn/pristine/2f/2f285623343d12f6ac399f9ec52ab70933439f61.svn-base @ 1524:82fac3dcf466 redmine-2.5-integration

Fix failure to interpret Javascript when autocompleting members for project
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Thu, 11 Sep 2014 10:24:38 +0100
parents dffacf8a6908
children
rev   line source
Chris@1517 1 # Redmine - project management software
Chris@1517 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
Chris@1517 3 #
Chris@1517 4 # This program is free software; you can redistribute it and/or
Chris@1517 5 # modify it under the terms of the GNU General Public License
Chris@1517 6 # as published by the Free Software Foundation; either version 2
Chris@1517 7 # of the License, or (at your option) any later version.
Chris@1517 8 #
Chris@1517 9 # This program is distributed in the hope that it will be useful,
Chris@1517 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1517 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1517 12 # GNU General Public License for more details.
Chris@1517 13 #
Chris@1517 14 # You should have received a copy of the GNU General Public License
Chris@1517 15 # along with this program; if not, write to the Free Software
Chris@1517 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1517 17
Chris@1517 18 class IssueQuery < Query
Chris@1517 19
Chris@1517 20 self.queried_class = Issue
Chris@1517 21
Chris@1517 22 self.available_columns = [
Chris@1517 23 QueryColumn.new(:id, :sortable => "#{Issue.table_name}.id", :default_order => 'desc', :caption => '#', :frozen => true),
Chris@1517 24 QueryColumn.new(:project, :sortable => "#{Project.table_name}.name", :groupable => true),
Chris@1517 25 QueryColumn.new(:tracker, :sortable => "#{Tracker.table_name}.position", :groupable => true),
Chris@1517 26 QueryColumn.new(:parent, :sortable => ["#{Issue.table_name}.root_id", "#{Issue.table_name}.lft ASC"], :default_order => 'desc', :caption => :field_parent_issue),
Chris@1517 27 QueryColumn.new(:status, :sortable => "#{IssueStatus.table_name}.position", :groupable => true),
Chris@1517 28 QueryColumn.new(:priority, :sortable => "#{IssuePriority.table_name}.position", :default_order => 'desc', :groupable => true),
Chris@1517 29 QueryColumn.new(:subject, :sortable => "#{Issue.table_name}.subject"),
Chris@1517 30 QueryColumn.new(:author, :sortable => lambda {User.fields_for_order_statement("authors")}, :groupable => true),
Chris@1517 31 QueryColumn.new(:assigned_to, :sortable => lambda {User.fields_for_order_statement}, :groupable => true),
Chris@1517 32 QueryColumn.new(:updated_on, :sortable => "#{Issue.table_name}.updated_on", :default_order => 'desc'),
Chris@1517 33 QueryColumn.new(:category, :sortable => "#{IssueCategory.table_name}.name", :groupable => true),
Chris@1517 34 QueryColumn.new(:fixed_version, :sortable => lambda {Version.fields_for_order_statement}, :groupable => true),
Chris@1517 35 QueryColumn.new(:start_date, :sortable => "#{Issue.table_name}.start_date"),
Chris@1517 36 QueryColumn.new(:due_date, :sortable => "#{Issue.table_name}.due_date"),
Chris@1517 37 QueryColumn.new(:estimated_hours, :sortable => "#{Issue.table_name}.estimated_hours"),
Chris@1517 38 QueryColumn.new(:done_ratio, :sortable => "#{Issue.table_name}.done_ratio", :groupable => true),
Chris@1517 39 QueryColumn.new(:created_on, :sortable => "#{Issue.table_name}.created_on", :default_order => 'desc'),
Chris@1517 40 QueryColumn.new(:closed_on, :sortable => "#{Issue.table_name}.closed_on", :default_order => 'desc'),
Chris@1517 41 QueryColumn.new(:relations, :caption => :label_related_issues),
Chris@1517 42 QueryColumn.new(:description, :inline => false)
Chris@1517 43 ]
Chris@1517 44
Chris@1517 45 scope :visible, lambda {|*args|
Chris@1517 46 user = args.shift || User.current
Chris@1517 47 base = Project.allowed_to_condition(user, :view_issues, *args)
Chris@1517 48 scope = includes(:project).where("#{table_name}.project_id IS NULL OR (#{base})")
Chris@1517 49
Chris@1517 50 if user.admin?
Chris@1517 51 scope.where("#{table_name}.visibility <> ? OR #{table_name}.user_id = ?", VISIBILITY_PRIVATE, user.id)
Chris@1517 52 elsif user.memberships.any?
Chris@1517 53 scope.where("#{table_name}.visibility = ?" +
Chris@1517 54 " OR (#{table_name}.visibility = ? AND #{table_name}.id IN (" +
Chris@1517 55 "SELECT DISTINCT q.id FROM #{table_name} q" +
Chris@1517 56 " INNER JOIN #{table_name_prefix}queries_roles#{table_name_suffix} qr on qr.query_id = q.id" +
Chris@1517 57 " INNER JOIN #{MemberRole.table_name} mr ON mr.role_id = qr.role_id" +
Chris@1517 58 " INNER JOIN #{Member.table_name} m ON m.id = mr.member_id AND m.user_id = ?" +
Chris@1517 59 " WHERE q.project_id IS NULL OR q.project_id = m.project_id))" +
Chris@1517 60 " OR #{table_name}.user_id = ?",
Chris@1517 61 VISIBILITY_PUBLIC, VISIBILITY_ROLES, user.id, user.id)
Chris@1517 62 elsif user.logged?
Chris@1517 63 scope.where("#{table_name}.visibility = ? OR #{table_name}.user_id = ?", VISIBILITY_PUBLIC, user.id)
Chris@1517 64 else
Chris@1517 65 scope.where("#{table_name}.visibility = ?", VISIBILITY_PUBLIC)
Chris@1517 66 end
Chris@1517 67 }
Chris@1517 68
Chris@1517 69 def initialize(attributes=nil, *args)
Chris@1517 70 super attributes
Chris@1517 71 self.filters ||= { 'status_id' => {:operator => "o", :values => [""]} }
Chris@1517 72 end
Chris@1517 73
Chris@1517 74 # Returns true if the query is visible to +user+ or the current user.
Chris@1517 75 def visible?(user=User.current)
Chris@1517 76 return true if user.admin?
Chris@1517 77 return false unless project.nil? || user.allowed_to?(:view_issues, project)
Chris@1517 78 case visibility
Chris@1517 79 when VISIBILITY_PUBLIC
Chris@1517 80 true
Chris@1517 81 when VISIBILITY_ROLES
Chris@1517 82 if project
Chris@1517 83 (user.roles_for_project(project) & roles).any?
Chris@1517 84 else
Chris@1517 85 Member.where(:user_id => user.id).joins(:roles).where(:member_roles => {:role_id => roles.map(&:id)}).any?
Chris@1517 86 end
Chris@1517 87 else
Chris@1517 88 user == self.user
Chris@1517 89 end
Chris@1517 90 end
Chris@1517 91
Chris@1517 92 def is_private?
Chris@1517 93 visibility == VISIBILITY_PRIVATE
Chris@1517 94 end
Chris@1517 95
Chris@1517 96 def is_public?
Chris@1517 97 !is_private?
Chris@1517 98 end
Chris@1517 99
Chris@1517 100 def draw_relations
Chris@1517 101 r = options[:draw_relations]
Chris@1517 102 r.nil? || r == '1'
Chris@1517 103 end
Chris@1517 104
Chris@1517 105 def draw_relations=(arg)
Chris@1517 106 options[:draw_relations] = (arg == '0' ? '0' : nil)
Chris@1517 107 end
Chris@1517 108
Chris@1517 109 def draw_progress_line
Chris@1517 110 r = options[:draw_progress_line]
Chris@1517 111 r == '1'
Chris@1517 112 end
Chris@1517 113
Chris@1517 114 def draw_progress_line=(arg)
Chris@1517 115 options[:draw_progress_line] = (arg == '1' ? '1' : nil)
Chris@1517 116 end
Chris@1517 117
Chris@1517 118 def build_from_params(params)
Chris@1517 119 super
Chris@1517 120 self.draw_relations = params[:draw_relations] || (params[:query] && params[:query][:draw_relations])
Chris@1517 121 self.draw_progress_line = params[:draw_progress_line] || (params[:query] && params[:query][:draw_progress_line])
Chris@1517 122 self
Chris@1517 123 end
Chris@1517 124
Chris@1517 125 def initialize_available_filters
Chris@1517 126 principals = []
Chris@1517 127 subprojects = []
Chris@1517 128 versions = []
Chris@1517 129 categories = []
Chris@1517 130 issue_custom_fields = []
Chris@1517 131
Chris@1517 132 if project
Chris@1517 133 principals += project.principals.sort
Chris@1517 134 unless project.leaf?
Chris@1517 135 subprojects = project.descendants.visible.all
Chris@1517 136 principals += Principal.member_of(subprojects)
Chris@1517 137 end
Chris@1517 138 versions = project.shared_versions.all
Chris@1517 139 categories = project.issue_categories.all
Chris@1517 140 issue_custom_fields = project.all_issue_custom_fields
Chris@1517 141 else
Chris@1517 142 if all_projects.any?
Chris@1517 143 principals += Principal.member_of(all_projects)
Chris@1517 144 end
Chris@1517 145 versions = Version.visible.where(:sharing => 'system').all
Chris@1517 146 issue_custom_fields = IssueCustomField.where(:is_for_all => true)
Chris@1517 147 end
Chris@1517 148 principals.uniq!
Chris@1517 149 principals.sort!
Chris@1517 150 users = principals.select {|p| p.is_a?(User)}
Chris@1517 151
Chris@1517 152 add_available_filter "status_id",
Chris@1517 153 :type => :list_status, :values => IssueStatus.sorted.collect{|s| [s.name, s.id.to_s] }
Chris@1517 154
Chris@1517 155 if project.nil?
Chris@1517 156 project_values = []
Chris@1517 157 if User.current.logged? && User.current.memberships.any?
Chris@1517 158 project_values << ["<< #{l(:label_my_projects).downcase} >>", "mine"]
Chris@1517 159 end
Chris@1517 160 project_values += all_projects_values
Chris@1517 161 add_available_filter("project_id",
Chris@1517 162 :type => :list, :values => project_values
Chris@1517 163 ) unless project_values.empty?
Chris@1517 164 end
Chris@1517 165
Chris@1517 166 add_available_filter "tracker_id",
Chris@1517 167 :type => :list, :values => trackers.collect{|s| [s.name, s.id.to_s] }
Chris@1517 168 add_available_filter "priority_id",
Chris@1517 169 :type => :list, :values => IssuePriority.all.collect{|s| [s.name, s.id.to_s] }
Chris@1517 170
Chris@1517 171 author_values = []
Chris@1517 172 author_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged?
Chris@1517 173 author_values += users.collect{|s| [s.name, s.id.to_s] }
Chris@1517 174 add_available_filter("author_id",
Chris@1517 175 :type => :list, :values => author_values
Chris@1517 176 ) unless author_values.empty?
Chris@1517 177
Chris@1517 178 assigned_to_values = []
Chris@1517 179 assigned_to_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged?
Chris@1517 180 assigned_to_values += (Setting.issue_group_assignment? ?
Chris@1517 181 principals : users).collect{|s| [s.name, s.id.to_s] }
Chris@1517 182 add_available_filter("assigned_to_id",
Chris@1517 183 :type => :list_optional, :values => assigned_to_values
Chris@1517 184 ) unless assigned_to_values.empty?
Chris@1517 185
Chris@1517 186 group_values = Group.all.collect {|g| [g.name, g.id.to_s] }
Chris@1517 187 add_available_filter("member_of_group",
Chris@1517 188 :type => :list_optional, :values => group_values
Chris@1517 189 ) unless group_values.empty?
Chris@1517 190
Chris@1517 191 role_values = Role.givable.collect {|r| [r.name, r.id.to_s] }
Chris@1517 192 add_available_filter("assigned_to_role",
Chris@1517 193 :type => :list_optional, :values => role_values
Chris@1517 194 ) unless role_values.empty?
Chris@1517 195
Chris@1517 196 if versions.any?
Chris@1517 197 add_available_filter "fixed_version_id",
Chris@1517 198 :type => :list_optional,
Chris@1517 199 :values => versions.sort.collect{|s| ["#{s.project.name} - #{s.name}", s.id.to_s] }
Chris@1517 200 end
Chris@1517 201
Chris@1517 202 if categories.any?
Chris@1517 203 add_available_filter "category_id",
Chris@1517 204 :type => :list_optional,
Chris@1517 205 :values => categories.collect{|s| [s.name, s.id.to_s] }
Chris@1517 206 end
Chris@1517 207
Chris@1517 208 add_available_filter "subject", :type => :text
Chris@1517 209 add_available_filter "created_on", :type => :date_past
Chris@1517 210 add_available_filter "updated_on", :type => :date_past
Chris@1517 211 add_available_filter "closed_on", :type => :date_past
Chris@1517 212 add_available_filter "start_date", :type => :date
Chris@1517 213 add_available_filter "due_date", :type => :date
Chris@1517 214 add_available_filter "estimated_hours", :type => :float
Chris@1517 215 add_available_filter "done_ratio", :type => :integer
Chris@1517 216
Chris@1517 217 if User.current.allowed_to?(:set_issues_private, nil, :global => true) ||
Chris@1517 218 User.current.allowed_to?(:set_own_issues_private, nil, :global => true)
Chris@1517 219 add_available_filter "is_private",
Chris@1517 220 :type => :list,
Chris@1517 221 :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]]
Chris@1517 222 end
Chris@1517 223
Chris@1517 224 if User.current.logged?
Chris@1517 225 add_available_filter "watcher_id",
Chris@1517 226 :type => :list, :values => [["<< #{l(:label_me)} >>", "me"]]
Chris@1517 227 end
Chris@1517 228
Chris@1517 229 if subprojects.any?
Chris@1517 230 add_available_filter "subproject_id",
Chris@1517 231 :type => :list_subprojects,
Chris@1517 232 :values => subprojects.collect{|s| [s.name, s.id.to_s] }
Chris@1517 233 end
Chris@1517 234
Chris@1517 235 add_custom_fields_filters(issue_custom_fields)
Chris@1517 236
Chris@1517 237 add_associations_custom_fields_filters :project, :author, :assigned_to, :fixed_version
Chris@1517 238
Chris@1517 239 IssueRelation::TYPES.each do |relation_type, options|
Chris@1517 240 add_available_filter relation_type, :type => :relation, :label => options[:name]
Chris@1517 241 end
Chris@1517 242
Chris@1517 243 Tracker.disabled_core_fields(trackers).each {|field|
Chris@1517 244 delete_available_filter field
Chris@1517 245 }
Chris@1517 246 end
Chris@1517 247
Chris@1517 248 def available_columns
Chris@1517 249 return @available_columns if @available_columns
Chris@1517 250 @available_columns = self.class.available_columns.dup
Chris@1517 251 @available_columns += (project ?
Chris@1517 252 project.all_issue_custom_fields :
Chris@1517 253 IssueCustomField
Chris@1517 254 ).visible.collect {|cf| QueryCustomFieldColumn.new(cf) }
Chris@1517 255
Chris@1517 256 if User.current.allowed_to?(:view_time_entries, project, :global => true)
Chris@1517 257 index = nil
Chris@1517 258 @available_columns.each_with_index {|column, i| index = i if column.name == :estimated_hours}
Chris@1517 259 index = (index ? index + 1 : -1)
Chris@1517 260 # insert the column after estimated_hours or at the end
Chris@1517 261 @available_columns.insert index, QueryColumn.new(:spent_hours,
Chris@1517 262 :sortable => "COALESCE((SELECT SUM(hours) FROM #{TimeEntry.table_name} WHERE #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id), 0)",
Chris@1517 263 :default_order => 'desc',
Chris@1517 264 :caption => :label_spent_time
Chris@1517 265 )
Chris@1517 266 end
Chris@1517 267
Chris@1517 268 if User.current.allowed_to?(:set_issues_private, nil, :global => true) ||
Chris@1517 269 User.current.allowed_to?(:set_own_issues_private, nil, :global => true)
Chris@1517 270 @available_columns << QueryColumn.new(:is_private, :sortable => "#{Issue.table_name}.is_private")
Chris@1517 271 end
Chris@1517 272
Chris@1517 273 disabled_fields = Tracker.disabled_core_fields(trackers).map {|field| field.sub(/_id$/, '')}
Chris@1517 274 @available_columns.reject! {|column|
Chris@1517 275 disabled_fields.include?(column.name.to_s)
Chris@1517 276 }
Chris@1517 277
Chris@1517 278 @available_columns
Chris@1517 279 end
Chris@1517 280
Chris@1517 281 def default_columns_names
Chris@1517 282 @default_columns_names ||= begin
Chris@1517 283 default_columns = Setting.issue_list_default_columns.map(&:to_sym)
Chris@1517 284
Chris@1517 285 project.present? ? default_columns : [:project] | default_columns
Chris@1517 286 end
Chris@1517 287 end
Chris@1517 288
Chris@1517 289 # Returns the issue count
Chris@1517 290 def issue_count
Chris@1517 291 Issue.visible.joins(:status, :project).where(statement).count
Chris@1517 292 rescue ::ActiveRecord::StatementInvalid => e
Chris@1517 293 raise StatementInvalid.new(e.message)
Chris@1517 294 end
Chris@1517 295
Chris@1517 296 # Returns the issue count by group or nil if query is not grouped
Chris@1517 297 def issue_count_by_group
Chris@1517 298 r = nil
Chris@1517 299 if grouped?
Chris@1517 300 begin
Chris@1517 301 # Rails3 will raise an (unexpected) RecordNotFound if there's only a nil group value
Chris@1517 302 r = Issue.visible.
Chris@1517 303 joins(:status, :project).
Chris@1517 304 where(statement).
Chris@1517 305 joins(joins_for_order_statement(group_by_statement)).
Chris@1517 306 group(group_by_statement).
Chris@1517 307 count
Chris@1517 308 rescue ActiveRecord::RecordNotFound
Chris@1517 309 r = {nil => issue_count}
Chris@1517 310 end
Chris@1517 311 c = group_by_column
Chris@1517 312 if c.is_a?(QueryCustomFieldColumn)
Chris@1517 313 r = r.keys.inject({}) {|h, k| h[c.custom_field.cast_value(k)] = r[k]; h}
Chris@1517 314 end
Chris@1517 315 end
Chris@1517 316 r
Chris@1517 317 rescue ::ActiveRecord::StatementInvalid => e
Chris@1517 318 raise StatementInvalid.new(e.message)
Chris@1517 319 end
Chris@1517 320
Chris@1517 321 # Returns the issues
Chris@1517 322 # Valid options are :order, :offset, :limit, :include, :conditions
Chris@1517 323 def issues(options={})
Chris@1517 324 order_option = [group_by_sort_order, options[:order]].flatten.reject(&:blank?)
Chris@1517 325
Chris@1517 326 scope = Issue.visible.
Chris@1517 327 joins(:status, :project).
Chris@1517 328 where(statement).
Chris@1517 329 includes(([:status, :project] + (options[:include] || [])).uniq).
Chris@1517 330 where(options[:conditions]).
Chris@1517 331 order(order_option).
Chris@1517 332 joins(joins_for_order_statement(order_option.join(','))).
Chris@1517 333 limit(options[:limit]).
Chris@1517 334 offset(options[:offset])
Chris@1517 335
Chris@1517 336 scope = scope.preload(:custom_values)
Chris@1517 337 if has_column?(:author)
Chris@1517 338 scope = scope.preload(:author)
Chris@1517 339 end
Chris@1517 340
Chris@1517 341 issues = scope.all
Chris@1517 342
Chris@1517 343 if has_column?(:spent_hours)
Chris@1517 344 Issue.load_visible_spent_hours(issues)
Chris@1517 345 end
Chris@1517 346 if has_column?(:relations)
Chris@1517 347 Issue.load_visible_relations(issues)
Chris@1517 348 end
Chris@1517 349 issues
Chris@1517 350 rescue ::ActiveRecord::StatementInvalid => e
Chris@1517 351 raise StatementInvalid.new(e.message)
Chris@1517 352 end
Chris@1517 353
Chris@1517 354 # Returns the issues ids
Chris@1517 355 def issue_ids(options={})
Chris@1517 356 order_option = [group_by_sort_order, options[:order]].flatten.reject(&:blank?)
Chris@1517 357
Chris@1517 358 Issue.visible.
Chris@1517 359 joins(:status, :project).
Chris@1517 360 where(statement).
Chris@1517 361 includes(([:status, :project] + (options[:include] || [])).uniq).
Chris@1517 362 where(options[:conditions]).
Chris@1517 363 order(order_option).
Chris@1517 364 joins(joins_for_order_statement(order_option.join(','))).
Chris@1517 365 limit(options[:limit]).
Chris@1517 366 offset(options[:offset]).
Chris@1517 367 find_ids
Chris@1517 368 rescue ::ActiveRecord::StatementInvalid => e
Chris@1517 369 raise StatementInvalid.new(e.message)
Chris@1517 370 end
Chris@1517 371
Chris@1517 372 # Returns the journals
Chris@1517 373 # Valid options are :order, :offset, :limit
Chris@1517 374 def journals(options={})
Chris@1517 375 Journal.visible.
Chris@1517 376 joins(:issue => [:project, :status]).
Chris@1517 377 where(statement).
Chris@1517 378 order(options[:order]).
Chris@1517 379 limit(options[:limit]).
Chris@1517 380 offset(options[:offset]).
Chris@1517 381 preload(:details, :user, {:issue => [:project, :author, :tracker, :status]}).
Chris@1517 382 all
Chris@1517 383 rescue ::ActiveRecord::StatementInvalid => e
Chris@1517 384 raise StatementInvalid.new(e.message)
Chris@1517 385 end
Chris@1517 386
Chris@1517 387 # Returns the versions
Chris@1517 388 # Valid options are :conditions
Chris@1517 389 def versions(options={})
Chris@1517 390 Version.visible.
Chris@1517 391 where(project_statement).
Chris@1517 392 where(options[:conditions]).
Chris@1517 393 includes(:project).
Chris@1517 394 all
Chris@1517 395 rescue ::ActiveRecord::StatementInvalid => e
Chris@1517 396 raise StatementInvalid.new(e.message)
Chris@1517 397 end
Chris@1517 398
Chris@1517 399 def sql_for_watcher_id_field(field, operator, value)
Chris@1517 400 db_table = Watcher.table_name
Chris@1517 401 "#{Issue.table_name}.id #{ operator == '=' ? 'IN' : 'NOT IN' } (SELECT #{db_table}.watchable_id FROM #{db_table} WHERE #{db_table}.watchable_type='Issue' AND " +
Chris@1517 402 sql_for_field(field, '=', value, db_table, 'user_id') + ')'
Chris@1517 403 end
Chris@1517 404
Chris@1517 405 def sql_for_member_of_group_field(field, operator, value)
Chris@1517 406 if operator == '*' # Any group
Chris@1517 407 groups = Group.all
Chris@1517 408 operator = '=' # Override the operator since we want to find by assigned_to
Chris@1517 409 elsif operator == "!*"
Chris@1517 410 groups = Group.all
Chris@1517 411 operator = '!' # Override the operator since we want to find by assigned_to
Chris@1517 412 else
Chris@1517 413 groups = Group.where(:id => value).all
Chris@1517 414 end
Chris@1517 415 groups ||= []
Chris@1517 416
Chris@1517 417 members_of_groups = groups.inject([]) {|user_ids, group|
Chris@1517 418 user_ids + group.user_ids + [group.id]
Chris@1517 419 }.uniq.compact.sort.collect(&:to_s)
Chris@1517 420
Chris@1517 421 '(' + sql_for_field("assigned_to_id", operator, members_of_groups, Issue.table_name, "assigned_to_id", false) + ')'
Chris@1517 422 end
Chris@1517 423
Chris@1517 424 def sql_for_assigned_to_role_field(field, operator, value)
Chris@1517 425 case operator
Chris@1517 426 when "*", "!*" # Member / Not member
Chris@1517 427 sw = operator == "!*" ? 'NOT' : ''
Chris@1517 428 nl = operator == "!*" ? "#{Issue.table_name}.assigned_to_id IS NULL OR" : ''
Chris@1517 429 "(#{nl} #{Issue.table_name}.assigned_to_id #{sw} IN (SELECT DISTINCT #{Member.table_name}.user_id FROM #{Member.table_name}" +
Chris@1517 430 " WHERE #{Member.table_name}.project_id = #{Issue.table_name}.project_id))"
Chris@1517 431 when "=", "!"
Chris@1517 432 role_cond = value.any? ?
Chris@1517 433 "#{MemberRole.table_name}.role_id IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")" :
Chris@1517 434 "1=0"
Chris@1517 435
Chris@1517 436 sw = operator == "!" ? 'NOT' : ''
Chris@1517 437 nl = operator == "!" ? "#{Issue.table_name}.assigned_to_id IS NULL OR" : ''
Chris@1517 438 "(#{nl} #{Issue.table_name}.assigned_to_id #{sw} IN (SELECT DISTINCT #{Member.table_name}.user_id FROM #{Member.table_name}, #{MemberRole.table_name}" +
Chris@1517 439 " WHERE #{Member.table_name}.project_id = #{Issue.table_name}.project_id AND #{Member.table_name}.id = #{MemberRole.table_name}.member_id AND #{role_cond}))"
Chris@1517 440 end
Chris@1517 441 end
Chris@1517 442
Chris@1517 443 def sql_for_is_private_field(field, operator, value)
Chris@1517 444 op = (operator == "=" ? 'IN' : 'NOT IN')
Chris@1517 445 va = value.map {|v| v == '0' ? connection.quoted_false : connection.quoted_true}.uniq.join(',')
Chris@1517 446
Chris@1517 447 "#{Issue.table_name}.is_private #{op} (#{va})"
Chris@1517 448 end
Chris@1517 449
Chris@1517 450 def sql_for_relations(field, operator, value, options={})
Chris@1517 451 relation_options = IssueRelation::TYPES[field]
Chris@1517 452 return relation_options unless relation_options
Chris@1517 453
Chris@1517 454 relation_type = field
Chris@1517 455 join_column, target_join_column = "issue_from_id", "issue_to_id"
Chris@1517 456 if relation_options[:reverse] || options[:reverse]
Chris@1517 457 relation_type = relation_options[:reverse] || relation_type
Chris@1517 458 join_column, target_join_column = target_join_column, join_column
Chris@1517 459 end
Chris@1517 460
Chris@1517 461 sql = case operator
Chris@1517 462 when "*", "!*"
Chris@1517 463 op = (operator == "*" ? 'IN' : 'NOT IN')
Chris@1517 464 "#{Issue.table_name}.id #{op} (SELECT DISTINCT #{IssueRelation.table_name}.#{join_column} FROM #{IssueRelation.table_name} WHERE #{IssueRelation.table_name}.relation_type = '#{connection.quote_string(relation_type)}')"
Chris@1517 465 when "=", "!"
Chris@1517 466 op = (operator == "=" ? 'IN' : 'NOT IN')
Chris@1517 467 "#{Issue.table_name}.id #{op} (SELECT DISTINCT #{IssueRelation.table_name}.#{join_column} FROM #{IssueRelation.table_name} WHERE #{IssueRelation.table_name}.relation_type = '#{connection.quote_string(relation_type)}' AND #{IssueRelation.table_name}.#{target_join_column} = #{value.first.to_i})"
Chris@1517 468 when "=p", "=!p", "!p"
Chris@1517 469 op = (operator == "!p" ? 'NOT IN' : 'IN')
Chris@1517 470 comp = (operator == "=!p" ? '<>' : '=')
Chris@1517 471 "#{Issue.table_name}.id #{op} (SELECT DISTINCT #{IssueRelation.table_name}.#{join_column} FROM #{IssueRelation.table_name}, #{Issue.table_name} relissues WHERE #{IssueRelation.table_name}.relation_type = '#{connection.quote_string(relation_type)}' AND #{IssueRelation.table_name}.#{target_join_column} = relissues.id AND relissues.project_id #{comp} #{value.first.to_i})"
Chris@1517 472 end
Chris@1517 473
Chris@1517 474 if relation_options[:sym] == field && !options[:reverse]
Chris@1517 475 sqls = [sql, sql_for_relations(field, operator, value, :reverse => true)]
Chris@1517 476 sql = sqls.join(["!", "!*", "!p"].include?(operator) ? " AND " : " OR ")
Chris@1517 477 end
Chris@1517 478 "(#{sql})"
Chris@1517 479 end
Chris@1517 480
Chris@1517 481 IssueRelation::TYPES.keys.each do |relation_type|
Chris@1517 482 alias_method "sql_for_#{relation_type}_field".to_sym, :sql_for_relations
Chris@1517 483 end
Chris@1517 484 end