<commit_before># Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/

module Organization::Search

=begin

search organizations

  result = Organization.search(
    :current_user => User.find(123),
    :query        => 'search something',
    :limit        => 15,
  )

returns

  result = [organization_model1, organization_model2]

=end

  def search(params)

    # get params
    query = params[:query]
    limit = params[:limit] || 10
    current_user = params[:current_user]

    # enable search only for agents and admins
    return [] if !current_user.is_role('Agent') && !current_user.is_role('Admin')

    # do query
    organizations = Organization.find(
      :all,
      :limit      => limit,
      :conditions => ['name LIKE ? OR note LIKE ?', "%#{query}%", "%#{query}%"],
      :order      => 'name'
    )

    # if only a few organizations are found, search for names of users
    if organizations.length <= 3
      organizations_by_user = Organization.select('DISTINCT(organizations.id)').joins('LEFT OUTER JOIN users ON users.organization_id = organizations.id').find(
        :all,
        :limit      => limit,
        :conditions => ['users.firstname LIKE ? or users.lastname LIKE ? or users.email LIKE ?', "%#{query}%", "%#{query}%", "%#{query}%"],
        :order      => 'organizations.name'
      )
      organizations_by_user.each {|organization_by_user|
        puts 'OOO ' + organization_by_user.inspect
        organization_exists = false
        organizations.each {|organization|
          if organization.id == organization_by_user.id
            organization_exists = true
          end
        }
        if !organization_exists
          organizations.push organization_by_user
        end
      }
    end
    organizations
  end
end<commit_msg>Fixed org lookup, use model with full attributes.<commit_after># Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/

module Organization::Search

=begin

search organizations

  result = Organization.search(
    :current_user => User.find(123),
    :query        => 'search something',
    :limit        => 15,
  )

returns

  result = [organization_model1, organization_model2]

=end

  def search(params)

    # get params
    query = params[:query]
    limit = params[:limit] || 10
    current_user = params[:current_user]

    # enable search only for agents and admins
    return [] if !current_user.is_role('Agent') && !current_user.is_role('Admin')

    # do query
    organizations = Organization.find(
      :all,
      :limit      => limit,
      :conditions => ['name LIKE ? OR note LIKE ?', "%#{query}%", "%#{query}%"],
      :order      => 'name'
    )

    # if only a few organizations are found, search for names of users
    if organizations.length <= 3
      organizations_by_user = Organization.select('DISTINCT(organizations.id)').joins('LEFT OUTER JOIN users ON users.organization_id = organizations.id').find(
        :all,
        :limit      => limit,
        :conditions => ['users.firstname LIKE ? or users.lastname LIKE ? or users.email LIKE ?', "%#{query}%", "%#{query}%", "%#{query}%"],
        :order      => 'organizations.name'
      )
      organizations_by_user.each {|organization_by_user|
        organization_exists = false
        organizations.each {|organization|
          if organization.id == organization_by_user.id
            organization_exists = true
          end
        }

        # get model with full data
        if !organization_exists
          organizations.push Organization.find(organization_by_user)
        end
      }
    end
    organizations
  end
end
{'fraction_non_alphanumeric': 0.11942762795817281, 'fraction_numerical': 0.009906439185470555, 'mean_word_length': 2.6978636826042726, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3607681', 'n_tokens_mistral': 1082, 'n_tokens_neox': 1022, 'n_words': 266}