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