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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2011 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_filter :find_optional_project
  19. helper :messages
  20. include MessagesHelper
  21. def index
  22. @question = params[:q] || ""
  23. @question.strip!
  24. @all_words = params[:all_words] ? params[:all_words].present? : true
  25. @titles_only = params[:titles_only] ? params[:titles_only].present? : false
  26. projects_to_search =
  27. case params[:scope]
  28. when 'all'
  29. nil
  30. when 'my_projects'
  31. User.current.memberships.collect(&:project)
  32. when 'subprojects'
  33. @project ? (@project.self_and_descendants.active.all) : nil
  34. else
  35. @project
  36. end
  37. offset = nil
  38. begin; offset = params[:offset].to_time if params[:offset]; rescue; end
  39. # quick jump to an issue
  40. if @question.match(/^#?(\d+)$/) && Issue.visible.find_by_id($1.to_i)
  41. redirect_to :controller => "issues", :action => "show", :id => $1
  42. return
  43. end
  44. @object_types = Redmine::Search.available_search_types.dup
  45. if projects_to_search.is_a? Project
  46. # don't search projects
  47. @object_types.delete('projects')
  48. # only show what the user is allowed to view
  49. @object_types = @object_types.select {|o| User.current.allowed_to?("view_#{o}".to_sym, projects_to_search)}
  50. end
  51. @scope = @object_types.select {|t| params[t]}
  52. @scope = @object_types if @scope.empty?
  53. # extract tokens from the question
  54. # eg. hello "bye bye" => ["hello", "bye bye"]
  55. @tokens = @question.scan(%r{((\s|^)"[\s\w]+"(\s|$)|\S+)}).collect {|m| m.first.gsub(%r{(^\s*"\s*|\s*"\s*$)}, '')}
  56. # tokens must be at least 2 characters long
  57. @tokens = @tokens.uniq.select {|w| w.length > 1 }
  58. if !@tokens.empty?
  59. # no more than 5 tokens to search for
  60. @tokens.slice! 5..-1 if @tokens.size > 5
  61. @results = []
  62. @results_by_type = Hash.new {|h,k| h[k] = 0}
  63. limit = 10
  64. @scope.each do |s|
  65. r, c = s.singularize.camelcase.constantize.search(@tokens, projects_to_search,
  66. :all_words => @all_words,
  67. :titles_only => @titles_only,
  68. :limit => (limit+1),
  69. :offset => offset,
  70. :before => params[:previous].nil?)
  71. @results += r
  72. @results_by_type[s] += c
  73. end
  74. @results = @results.sort {|a,b| b.event_datetime <=> a.event_datetime}
  75. if params[:previous].nil?
  76. @pagination_previous_date = @results[0].event_datetime if offset && @results[0]
  77. if @results.size > limit
  78. @pagination_next_date = @results[limit-1].event_datetime
  79. @results = @results[0, limit]
  80. end
  81. else
  82. @pagination_next_date = @results[-1].event_datetime if offset && @results[-1]
  83. if @results.size > limit
  84. @pagination_previous_date = @results[-(limit)].event_datetime
  85. @results = @results[-(limit), limit]
  86. end
  87. end
  88. else
  89. @question = ""
  90. end
  91. render :layout => false if request.xhr?
  92. end
  93. private
  94. def find_optional_project
  95. return true unless params[:id]
  96. @project = Project.find(params[:id])
  97. check_project_privacy
  98. rescue ActiveRecord::RecordNotFound
  99. render_404
  100. end
  101. end