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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2019 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. class SearchController < ApplicationController
  19. before_action :find_optional_project_by_id, :authorize_global
  20. accept_api_auth :index
  21. def index
  22. @question = params[:q]&.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 !api_request? && (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