##// END OF EJS Templates
Fixed projects search....
Jean-Philippe Lang -
r748:042ef42da0ef
parent child
Show More
@@ -1,97 +1,97
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006 Jean-Philippe Lang
2 # Copyright (C) 2006 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 layout 'base'
19 layout 'base'
20
20
21 helper :messages
21 helper :messages
22 include MessagesHelper
22 include MessagesHelper
23
23
24 def index
24 def index
25 @question = params[:q] || ""
25 @question = params[:q] || ""
26 @question.strip!
26 @question.strip!
27 @all_words = params[:all_words] || (params[:submit] ? false : true)
27 @all_words = params[:all_words] || (params[:submit] ? false : true)
28
28
29 # quick jump to an issue
29 # quick jump to an issue
30 if @question.match(/^#?(\d+)$/) && Issue.find_by_id($1, :include => :project, :conditions => Project.visible_by(logged_in_user))
30 if @question.match(/^#?(\d+)$/) && Issue.find_by_id($1, :include => :project, :conditions => Project.visible_by(logged_in_user))
31 redirect_to :controller => "issues", :action => "show", :id => $1
31 redirect_to :controller => "issues", :action => "show", :id => $1
32 return
32 return
33 end
33 end
34
34
35 if params[:id]
35 if params[:id]
36 find_project
36 find_project
37 return unless check_project_privacy
37 return unless check_project_privacy
38 end
38 end
39
39
40 if @project
40 if @project
41 @object_types = %w(projects issues changesets news documents wiki_pages messages)
41 @object_types = %w(projects issues changesets news documents wiki_pages messages)
42 @object_types.delete('wiki_pages') unless @project.wiki
42 @object_types.delete('wiki_pages') unless @project.wiki
43 @object_types.delete('changesets') unless @project.repository
43 @object_types.delete('changesets') unless @project.repository
44 # only show what the user is allowed to view
44 # only show what the user is allowed to view
45 @object_types = @object_types.select {|o| User.current.allowed_to?("view_#{o}".to_sym, @project)}
45 @object_types = @object_types.select {|o| User.current.allowed_to?("view_#{o}".to_sym, @project)}
46
46
47 @scope = @object_types.select {|t| params[t]}
47 @scope = @object_types.select {|t| params[t]}
48 # default objects to search if none is specified in parameters
48 # default objects to search if none is specified in parameters
49 @scope = @object_types if @scope.empty?
49 @scope = @object_types if @scope.empty?
50 else
50 else
51 @scope = %w(projects)
51 @object_types = @scope = %w(projects)
52 end
52 end
53
53
54 # tokens must be at least 3 character long
54 # tokens must be at least 3 character long
55 @tokens = @question.split.uniq.select {|w| w.length > 2 }
55 @tokens = @question.split.uniq.select {|w| w.length > 2 }
56
56
57 if !@tokens.empty?
57 if !@tokens.empty?
58 # no more than 5 tokens to search for
58 # no more than 5 tokens to search for
59 @tokens.slice! 5..-1 if @tokens.size > 5
59 @tokens.slice! 5..-1 if @tokens.size > 5
60 # strings used in sql like statement
60 # strings used in sql like statement
61 like_tokens = @tokens.collect {|w| "%#{w.downcase}%"}
61 like_tokens = @tokens.collect {|w| "%#{w.downcase}%"}
62 operator = @all_words ? " AND " : " OR "
62 operator = @all_words ? " AND " : " OR "
63 limit = 10
63 limit = 10
64 @results = []
64 @results = []
65 if @project
65 if @project
66 @results += @project.issues.find(:all, :limit => limit, :include => :author, :conditions => [ (["(LOWER(subject) like ? OR LOWER(description) like ?)"] * like_tokens.size).join(operator), * (like_tokens * 2).sort] ) if @scope.include? 'issues'
66 @results += @project.issues.find(:all, :limit => limit, :include => :author, :conditions => [ (["(LOWER(subject) like ? OR LOWER(description) like ?)"] * like_tokens.size).join(operator), * (like_tokens * 2).sort] ) if @scope.include? 'issues'
67 Journal.with_scope :find => {:conditions => ["#{Issue.table_name}.project_id = ?", @project.id]} do
67 Journal.with_scope :find => {:conditions => ["#{Issue.table_name}.project_id = ?", @project.id]} do
68 @results += Journal.find(:all, :include => :issue, :limit => limit, :conditions => [ (["(LOWER(notes) like ? OR LOWER(notes) like ?)"] * like_tokens.size).join(operator), * (like_tokens * 2).sort] ).collect(&:issue) if @scope.include? 'issues'
68 @results += Journal.find(:all, :include => :issue, :limit => limit, :conditions => [ (["(LOWER(notes) like ? OR LOWER(notes) like ?)"] * like_tokens.size).join(operator), * (like_tokens * 2).sort] ).collect(&:issue) if @scope.include? 'issues'
69 end
69 end
70 @results.uniq!
70 @results.uniq!
71 @results += @project.news.find(:all, :limit => limit, :conditions => [ (["(LOWER(title) like ? OR LOWER(description) like ?)"] * like_tokens.size).join(operator), * (like_tokens * 2).sort], :include => :author ) if @scope.include? 'news'
71 @results += @project.news.find(:all, :limit => limit, :conditions => [ (["(LOWER(title) like ? OR LOWER(description) like ?)"] * like_tokens.size).join(operator), * (like_tokens * 2).sort], :include => :author ) if @scope.include? 'news'
72 @results += @project.documents.find(:all, :limit => limit, :conditions => [ (["(LOWER(title) like ? OR LOWER(description) like ?)"] * like_tokens.size).join(operator), * (like_tokens * 2).sort] ) if @scope.include? 'documents'
72 @results += @project.documents.find(:all, :limit => limit, :conditions => [ (["(LOWER(title) like ? OR LOWER(description) like ?)"] * like_tokens.size).join(operator), * (like_tokens * 2).sort] ) if @scope.include? 'documents'
73 @results += @project.wiki.pages.find(:all, :limit => limit, :include => :content, :conditions => [ (["(LOWER(title) like ? OR LOWER(text) like ?)"] * like_tokens.size).join(operator), * (like_tokens * 2).sort] ) if @project.wiki && @scope.include?('wiki_pages')
73 @results += @project.wiki.pages.find(:all, :limit => limit, :include => :content, :conditions => [ (["(LOWER(title) like ? OR LOWER(text) like ?)"] * like_tokens.size).join(operator), * (like_tokens * 2).sort] ) if @project.wiki && @scope.include?('wiki_pages')
74 @results += @project.repository.changesets.find(:all, :limit => limit, :conditions => [ (["(LOWER(comments) like ?)"] * like_tokens.size).join(operator), * (like_tokens).sort] ) if @project.repository && @scope.include?('changesets')
74 @results += @project.repository.changesets.find(:all, :limit => limit, :conditions => [ (["(LOWER(comments) like ?)"] * like_tokens.size).join(operator), * (like_tokens).sort] ) if @project.repository && @scope.include?('changesets')
75 Message.with_scope :find => {:conditions => ["#{Board.table_name}.project_id = ?", @project.id]} do
75 Message.with_scope :find => {:conditions => ["#{Board.table_name}.project_id = ?", @project.id]} do
76 @results += Message.find(:all, :include => :board, :limit => limit, :conditions => [ (["(LOWER(subject) like ? OR LOWER(content) like ?)"] * like_tokens.size).join(operator), * (like_tokens * 2).sort] ) if @scope.include? 'messages'
76 @results += Message.find(:all, :include => :board, :limit => limit, :conditions => [ (["(LOWER(subject) like ? OR LOWER(content) like ?)"] * like_tokens.size).join(operator), * (like_tokens * 2).sort] ) if @scope.include? 'messages'
77 end
77 end
78 else
78 else
79 Project.with_scope(:find => {:conditions => Project.visible_by(logged_in_user)}) do
79 Project.with_scope(:find => {:conditions => Project.visible_by(logged_in_user)}) do
80 @results += Project.find(:all, :limit => limit, :conditions => [ (["(LOWER(name) like ? OR LOWER(description) like ?)"] * like_tokens.size).join(operator), * (like_tokens * 2).sort] ) if @scope.include? 'projects'
80 @results += Project.find(:all, :limit => limit, :conditions => [ (["(LOWER(name) like ? OR LOWER(description) like ?)"] * like_tokens.size).join(operator), * (like_tokens * 2).sort] ) if @scope.include? 'projects'
81 end
81 end
82 # if only one project is found, user is redirected to its overview
82 # if only one project is found, user is redirected to its overview
83 redirect_to :controller => 'projects', :action => 'show', :id => @results.first and return if @results.size == 1
83 redirect_to :controller => 'projects', :action => 'show', :id => @results.first and return if @results.size == 1
84 end
84 end
85 @question = @tokens.join(" ")
85 @question = @tokens.join(" ")
86 else
86 else
87 @question = ""
87 @question = ""
88 end
88 end
89 end
89 end
90
90
91 private
91 private
92 def find_project
92 def find_project
93 @project = Project.find(params[:id])
93 @project = Project.find(params[:id])
94 rescue ActiveRecord::RecordNotFound
94 rescue ActiveRecord::RecordNotFound
95 render_404
95 render_404
96 end
96 end
97 end
97 end
General Comments 0
You need to be logged in to leave comments. Login now