trabajo-afectivo/app/controllers/search_controller.rb

90 lines
2.4 KiB
Ruby
Raw Normal View History

# Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/
2013-05-21 22:30:09 +00:00
class SearchController < ApplicationController
before_filter :authentication_check
# GET /api/search
def search
2013-05-21 22:30:09 +00:00
# build result list
tickets = Ticket.search(
:limit => params[:limit],
:query => params[:term],
:current_user => current_user,
)
users_data = {}
ticket_result = []
tickets.each do |ticket|
ticket_result.push ticket.id
users_data[ ticket['owner_id'] ] = User.user_data_full( ticket['owner_id'] )
users_data[ ticket['customer_id'] ] = User.user_data_full( ticket['customer_id'] )
users_data[ ticket['created_by_id'] ] = User.user_data_full( ticket['created_by_id'] )
end
# do query
users = User.search(
2013-05-22 01:40:24 +00:00
:query => params[:term],
:limit => params[:limit],
2013-05-21 22:30:09 +00:00
:current_user => current_user,
)
user_result = []
users.each do |user|
user_result.push user.id
users_data[ user.id ] = User.user_data_full( user.id )
end
organizations = Organization.search(
2013-05-22 01:40:24 +00:00
:query => params[:term],
:limit => params[:limit],
2013-05-21 22:30:09 +00:00
:current_user => current_user,
)
2013-05-22 01:40:24 +00:00
2013-05-21 22:30:09 +00:00
organizations_data = {}
organization_result = []
organizations.each do |organization|
organization_result.push organization.id
organizations_data[ organization.id ] = Organization.find( organization.id ).attributes
organizations_data[ organization.id ][:user_ids] = []
users = User.where( :organization_id => organization.id ).limit(10)
users.each {|user|
users_data[ user.id ] = User.user_data_full( user.id )
organizations_data[ organization.id ][:user_ids].push user.id
}
2013-05-21 22:30:09 +00:00
end
result = []
if ticket_result[0]
data = {
:name => 'Ticket',
:ids => ticket_result,
}
result.push data
end
if user_result[0]
data = {
:name => 'User',
:ids => user_result,
}
result.push data
end
if organization_result[0]
data = {
:name => 'Organization',
:ids => organization_result,
}
result.push data
end
# return result
render :json => {
:load => {
:tickets => tickets,
:users => users_data,
:organizations => organizations_data,
},
:result => result,
}
end
end