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