search_controller.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. class SearchController < ApplicationController
  3. before_filter :authentication_check
  4. # GET /api/v1/search_user_org
  5. def search_user_org
  6. # enable search only for agents and admins
  7. if !current_user.is_role('Agent') && !current_user.is_role('Admin')
  8. response_access_deny
  9. return true
  10. end
  11. # get params
  12. query = params[:query]
  13. limit = params[:limit] || 10
  14. # try search index backend
  15. assets = {}
  16. result = []
  17. if SearchIndexBackend.enabled?
  18. items = SearchIndexBackend.search( query, limit, ['User', 'Organization'] )
  19. items.each { |item|
  20. require item[:type].to_filename
  21. record = Kernel.const_get( item[:type] ).find( item[:id] )
  22. assets = record.assets(assets)
  23. result.push item
  24. }
  25. else
  26. # do query
  27. users = User.search(
  28. :query => query,
  29. :limit => limit,
  30. :current_user => current_user,
  31. )
  32. user_result = []
  33. users.each do |user|
  34. item = {
  35. :id => user.id,
  36. :type => user.class.to_s
  37. }
  38. result.push item
  39. assets = user.assets(assets)
  40. end
  41. organizations = Organization.search(
  42. :query => query,
  43. :limit => limit,
  44. :current_user => current_user,
  45. )
  46. organization_result = []
  47. organizations.each do |organization|
  48. item = {
  49. :id => organization.id,
  50. :type => organization.class.to_s
  51. }
  52. result.push item
  53. assets = organization.assets(assets)
  54. end
  55. end
  56. render :json => {
  57. :assets => assets,
  58. :result => result,
  59. }
  60. end
  61. # GET /api/v1/search
  62. def search
  63. # build result list
  64. tickets = Ticket.search(
  65. :limit => params[:limit],
  66. :query => params[:term],
  67. :current_user => current_user,
  68. )
  69. assets = {}
  70. ticket_result = []
  71. tickets.each do |ticket|
  72. assets = ticket.assets(assets)
  73. ticket_result.push ticket.id
  74. end
  75. # do query
  76. users = User.search(
  77. :query => params[:term],
  78. :limit => params[:limit],
  79. :current_user => current_user,
  80. )
  81. user_result = []
  82. users.each do |user|
  83. user_result.push user.id
  84. assets = user.assets(assets)
  85. end
  86. organizations = Organization.search(
  87. :query => params[:term],
  88. :limit => params[:limit],
  89. :current_user => current_user,
  90. )
  91. organization_result = []
  92. organizations.each do |organization|
  93. organization_result.push organization.id
  94. assets = organization.assets(assets)
  95. end
  96. result = []
  97. if ticket_result[0]
  98. data = {
  99. :name => 'Ticket',
  100. :ids => ticket_result,
  101. }
  102. result.push data
  103. end
  104. if user_result[0]
  105. data = {
  106. :name => 'User',
  107. :ids => user_result,
  108. }
  109. result.push data
  110. end
  111. if organization_result[0]
  112. data = {
  113. :name => 'Organization',
  114. :ids => organization_result,
  115. }
  116. result.push data
  117. end
  118. # return result
  119. render :json => {
  120. :assets => assets,
  121. :result => result,
  122. }
  123. end
  124. end