@@ -1,113 +1,113 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2014 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | class SearchController < ApplicationController |
|
19 | 19 | before_filter :find_optional_project |
|
20 | 20 | |
|
21 | 21 | def index |
|
22 | 22 | @question = params[:q] || "" |
|
23 | 23 | @question.strip! |
|
24 | 24 | @all_words = params[:all_words] ? params[:all_words].present? : true |
|
25 | 25 | @titles_only = params[:titles_only] ? params[:titles_only].present? : false |
|
26 | 26 | |
|
27 | 27 | projects_to_search = |
|
28 | 28 | case params[:scope] |
|
29 | 29 | when 'all' |
|
30 | 30 | nil |
|
31 | 31 | when 'my_projects' |
|
32 |
User.current. |
|
|
32 | User.current.projects | |
|
33 | 33 | when 'subprojects' |
|
34 | 34 | @project ? (@project.self_and_descendants.active.to_a) : nil |
|
35 | 35 | else |
|
36 | 36 | @project |
|
37 | 37 | end |
|
38 | 38 | |
|
39 | 39 | # quick jump to an issue |
|
40 | 40 | if (m = @question.match(/^#?(\d+)$/)) && (issue = Issue.visible.find_by_id(m[1].to_i)) |
|
41 | 41 | redirect_to issue_path(issue) |
|
42 | 42 | return |
|
43 | 43 | end |
|
44 | 44 | |
|
45 | 45 | @object_types = Redmine::Search.available_search_types.dup |
|
46 | 46 | if projects_to_search.is_a? Project |
|
47 | 47 | # don't search projects |
|
48 | 48 | @object_types.delete('projects') |
|
49 | 49 | # only show what the user is allowed to view |
|
50 | 50 | @object_types = @object_types.select {|o| User.current.allowed_to?("view_#{o}".to_sym, projects_to_search)} |
|
51 | 51 | end |
|
52 | 52 | |
|
53 | 53 | @scope = @object_types.select {|t| params[t]} |
|
54 | 54 | @scope = @object_types if @scope.empty? |
|
55 | 55 | |
|
56 | 56 | # extract tokens from the question |
|
57 | 57 | # eg. hello "bye bye" => ["hello", "bye bye"] |
|
58 | 58 | @tokens = @question.scan(%r{((\s|^)"[\s\w]+"(\s|$)|\S+)}).collect {|m| m.first.gsub(%r{(^\s*"\s*|\s*"\s*$)}, '')} |
|
59 | 59 | # tokens must be at least 2 characters long |
|
60 | 60 | @tokens = @tokens.uniq.select {|w| w.length > 1 } |
|
61 | 61 | |
|
62 | 62 | if !@tokens.empty? |
|
63 | 63 | # no more than 5 tokens to search for |
|
64 | 64 | @tokens.slice! 5..-1 if @tokens.size > 5 |
|
65 | 65 | |
|
66 | 66 | limit = 10 |
|
67 | 67 | |
|
68 | 68 | @result_count = 0 |
|
69 | 69 | @result_count_by_type = Hash.new {|h,k| h[k] = 0} |
|
70 | 70 | ranks_and_ids = [] |
|
71 | 71 | |
|
72 | 72 | # get all the results ranks and ids |
|
73 | 73 | @scope.each do |scope| |
|
74 | 74 | klass = scope.singularize.camelcase.constantize |
|
75 | 75 | ranks_and_ids_in_scope = klass.search_result_ranks_and_ids(@tokens, User.current, projects_to_search, |
|
76 | 76 | :all_words => @all_words, |
|
77 | 77 | :titles_only => @titles_only |
|
78 | 78 | ) |
|
79 | 79 | @result_count_by_type[scope] += ranks_and_ids_in_scope.size |
|
80 | 80 | @result_count += ranks_and_ids_in_scope.size |
|
81 | 81 | ranks_and_ids += ranks_and_ids_in_scope.map {|r| [scope, r]} |
|
82 | 82 | end |
|
83 | 83 | @result_pages = Paginator.new @result_count, limit, params['page'] |
|
84 | 84 | |
|
85 | 85 | # sort results, higher rank and id first |
|
86 | 86 | ranks_and_ids.sort! {|a,b| b.last <=> a.last } |
|
87 | 87 | ranks_and_ids = ranks_and_ids[@result_pages.offset, limit] || [] |
|
88 | 88 | |
|
89 | 89 | # load the results to display |
|
90 | 90 | results_by_scope = Hash.new {|h,k| h[k] = []} |
|
91 | 91 | ranks_and_ids.group_by(&:first).each do |scope, rs| |
|
92 | 92 | klass = scope.singularize.camelcase.constantize |
|
93 | 93 | results_by_scope[scope] += klass.search_results_from_ids(rs.map(&:last).map(&:last)) |
|
94 | 94 | end |
|
95 | 95 | |
|
96 | 96 | @results = ranks_and_ids.map do |scope, r| |
|
97 | 97 | results_by_scope[scope].detect {|record| record.id == r.last} |
|
98 | 98 | end.compact |
|
99 | 99 | else |
|
100 | 100 | @question = "" |
|
101 | 101 | end |
|
102 | 102 | render :layout => false if request.xhr? |
|
103 | 103 | end |
|
104 | 104 | |
|
105 | 105 | private |
|
106 | 106 | def find_optional_project |
|
107 | 107 | return true unless params[:id] |
|
108 | 108 | @project = Project.find(params[:id]) |
|
109 | 109 | check_project_privacy |
|
110 | 110 | rescue ActiveRecord::RecordNotFound |
|
111 | 111 | render_404 |
|
112 | 112 | end |
|
113 | 113 | end |
General Comments 0
You need to be logged in to leave comments.
Login now