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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006- 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 'bookmarks'
  47. Project.where(id: User.current.bookmarked_project_ids)
  48. when 'subprojects'
  49. @project ? (@project.self_and_descendants.to_a) : nil
  50. else
  51. @project
  52. end
  53. @object_types = Redmine::Search.available_search_types.dup
  54. if projects_to_search.is_a? Project
  55. # don't search projects
  56. @object_types.delete('projects')
  57. # only show what the user is allowed to view
  58. @object_types = @object_types.select {|o| User.current.allowed_to?(:"view_#{o}", projects_to_search)}
  59. end
  60. @scope = @object_types.select {|t| params[t].present?}
  61. @scope = @object_types if @scope.empty?
  62. fetcher = Redmine::Search::Fetcher.new(
  63. @question, User.current, @scope, projects_to_search,
  64. :all_words => @all_words, :titles_only => @titles_only, :attachments => @search_attachments, :open_issues => @open_issues,
  65. :cache => params[:page].present?, :params => params.to_unsafe_hash
  66. )
  67. if fetcher.tokens.present?
  68. @result_count = fetcher.result_count
  69. @result_count_by_type = fetcher.result_count_by_type
  70. @tokens = fetcher.tokens
  71. @result_pages = Paginator.new @result_count, @limit, params['page']
  72. @offset ||= @result_pages.offset
  73. @results = fetcher.results(@offset, @result_pages.per_page)
  74. else
  75. @question = ""
  76. end
  77. respond_to do |format|
  78. format.html {render :layout => false if request.xhr?}
  79. format.api do
  80. @results ||= []
  81. render :layout => false
  82. end
  83. end
  84. end
  85. end