You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

search_controller.rb 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. class SearchController < ApplicationController
  18. before_action :find_optional_project_by_id, :authorize_global
  19. accept_api_auth :index
  20. def index
  21. @question = params[:q] || ""
  22. @question.strip!
  23. @all_words = params[:all_words] ? params[:all_words].present? : true
  24. @titles_only = params[:titles_only] ? params[:titles_only].present? : false
  25. @search_attachments = params[:attachments].presence || '0'
  26. @open_issues = params[:open_issues] ? params[:open_issues].present? : false
  27. case params[:format]
  28. when 'xml', 'json'
  29. @offset, @limit = api_offset_and_limit
  30. else
  31. @offset = nil
  32. @limit = Setting.search_results_per_page.to_i
  33. @limit = 10 if @limit == 0
  34. end
  35. # quick jump to an issue
  36. if (m = @question.match(/^#?(\d+)$/)) && (issue = Issue.visible.find_by_id(m[1].to_i))
  37. redirect_to issue_path(issue)
  38. return
  39. end
  40. projects_to_search =
  41. case params[:scope]
  42. when 'all'
  43. nil
  44. when 'my_projects'
  45. User.current.projects
  46. when 'subprojects'
  47. @project ? (@project.self_and_descendants.to_a) : nil
  48. else
  49. @project
  50. end
  51. @object_types = Redmine::Search.available_search_types.dup
  52. if projects_to_search.is_a? Project
  53. # don't search projects
  54. @object_types.delete('projects')
  55. # only show what the user is allowed to view
  56. @object_types = @object_types.select {|o| User.current.allowed_to?("view_#{o}".to_sym, projects_to_search)}
  57. end
  58. @scope = @object_types.select {|t| params[t]}
  59. @scope = @object_types if @scope.empty?
  60. fetcher = Redmine::Search::Fetcher.new(
  61. @question, User.current, @scope, projects_to_search,
  62. :all_words => @all_words, :titles_only => @titles_only, :attachments => @search_attachments, :open_issues => @open_issues,
  63. :cache => params[:page].present?, :params => params.to_unsafe_hash
  64. )
  65. if fetcher.tokens.present?
  66. @result_count = fetcher.result_count
  67. @result_count_by_type = fetcher.result_count_by_type
  68. @tokens = fetcher.tokens
  69. @result_pages = Paginator.new @result_count, @limit, params['page']
  70. @offset ||= @result_pages.offset
  71. @results = fetcher.results(@offset, @result_pages.per_page)
  72. else
  73. @question = ""
  74. end
  75. respond_to do |format|
  76. format.html { render :layout => false if request.xhr? }
  77. format.api { @results ||= []; render :layout => false }
  78. end
  79. end
  80. end