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