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