search_controller.rb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/
  2. class SearchController < ApplicationController
  3. before_filter :authentication_check
  4. # GET /api/v1/search
  5. def search
  6. # build result list
  7. tickets = Ticket.search(
  8. :limit => params[:limit],
  9. :query => params[:term],
  10. :current_user => current_user,
  11. )
  12. assets = {}
  13. ticket_result = []
  14. tickets.each do |ticket|
  15. assets = ticket.assets(assets)
  16. ticket_result.push ticket.id
  17. end
  18. # do query
  19. users = User.search(
  20. :query => params[:term],
  21. :limit => params[:limit],
  22. :current_user => current_user,
  23. )
  24. user_result = []
  25. users.each do |user|
  26. user_result.push user.id
  27. assets = user.assets(assets)
  28. end
  29. organizations = Organization.search(
  30. :query => params[:term],
  31. :limit => params[:limit],
  32. :current_user => current_user,
  33. )
  34. organization_result = []
  35. organizations.each do |organization|
  36. organization_result.push organization.id
  37. assets = organization.assets(assets)
  38. end
  39. result = []
  40. if ticket_result[0]
  41. data = {
  42. :name => 'Ticket',
  43. :ids => ticket_result,
  44. }
  45. result.push data
  46. end
  47. if user_result[0]
  48. data = {
  49. :name => 'User',
  50. :ids => user_result,
  51. }
  52. result.push data
  53. end
  54. if organization_result[0]
  55. data = {
  56. :name => 'Organization',
  57. :ids => organization_result,
  58. }
  59. result.push data
  60. end
  61. # return result
  62. render :json => {
  63. :assets => assets,
  64. :result => result,
  65. }
  66. end
  67. end