@@ -1,75 +1,81 | |||||
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 | |||
|
22 | include MessagesHelper | |||
|
23 | ||||
21 | def index |
|
24 | def index | |
22 | @question = params[:q] || "" |
|
25 | @question = params[:q] || "" | |
23 | @question.strip! |
|
26 | @question.strip! | |
24 | @all_words = params[:all_words] || (params[:submit] ? false : true) |
|
27 | @all_words = params[:all_words] || (params[:submit] ? false : true) | |
25 | @scope = params[:scope] || (params[:submit] ? [] : %w(projects issues changesets news documents wiki) ) |
|
28 | @scope = params[:scope] || (params[:submit] ? [] : %w(projects issues changesets news documents wiki messages) ) | |
26 |
|
29 | |||
27 | # quick jump to an issue |
|
30 | # quick jump to an issue | |
28 | if @scope.include?('issues') && @question.match(/^#?(\d+)$/) && Issue.find_by_id($1, :include => :project, :conditions => Project.visible_by(logged_in_user)) |
|
31 | if @scope.include?('issues') && @question.match(/^#?(\d+)$/) && Issue.find_by_id($1, :include => :project, :conditions => Project.visible_by(logged_in_user)) | |
29 | redirect_to :controller => "issues", :action => "show", :id => $1 |
|
32 | redirect_to :controller => "issues", :action => "show", :id => $1 | |
30 | return |
|
33 | return | |
31 | end |
|
34 | end | |
32 |
|
35 | |||
33 | if params[:id] |
|
36 | if params[:id] | |
34 | find_project |
|
37 | find_project | |
35 | return unless check_project_privacy |
|
38 | return unless check_project_privacy | |
36 | end |
|
39 | end | |
37 |
|
40 | |||
38 | # tokens must be at least 3 character long |
|
41 | # tokens must be at least 3 character long | |
39 | @tokens = @question.split.uniq.select {|w| w.length > 2 } |
|
42 | @tokens = @question.split.uniq.select {|w| w.length > 2 } | |
40 |
|
43 | |||
41 | if !@tokens.empty? |
|
44 | if !@tokens.empty? | |
42 | # no more than 5 tokens to search for |
|
45 | # no more than 5 tokens to search for | |
43 | @tokens.slice! 5..-1 if @tokens.size > 5 |
|
46 | @tokens.slice! 5..-1 if @tokens.size > 5 | |
44 | # strings used in sql like statement |
|
47 | # strings used in sql like statement | |
45 | like_tokens = @tokens.collect {|w| "%#{w.downcase}%"} |
|
48 | like_tokens = @tokens.collect {|w| "%#{w.downcase}%"} | |
46 | operator = @all_words ? " AND " : " OR " |
|
49 | operator = @all_words ? " AND " : " OR " | |
47 | limit = 10 |
|
50 | limit = 10 | |
48 | @results = [] |
|
51 | @results = [] | |
49 | if @project |
|
52 | if @project | |
50 | @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' |
|
53 | @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' | |
51 | @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' |
|
54 | @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' | |
52 | @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' |
|
55 | @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' | |
53 | @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') |
|
56 | @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') | |
54 | @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') |
|
57 | @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') | |
|
58 | Message.with_scope :find => {:conditions => ["#{Board.table_name}.project_id = ?", @project.id]} do | |||
|
59 | @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' | |||
|
60 | end | |||
55 | else |
|
61 | else | |
56 | Project.with_scope(:find => {:conditions => Project.visible_by(logged_in_user)}) do |
|
62 | Project.with_scope(:find => {:conditions => Project.visible_by(logged_in_user)}) do | |
57 | @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' |
|
63 | @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' | |
58 | end |
|
64 | end | |
59 | # if only one project is found, user is redirected to its overview |
|
65 | # if only one project is found, user is redirected to its overview | |
60 | redirect_to :controller => 'projects', :action => 'show', :id => @results.first and return if @results.size == 1 |
|
66 | redirect_to :controller => 'projects', :action => 'show', :id => @results.first and return if @results.size == 1 | |
61 | end |
|
67 | end | |
62 | @question = @tokens.join(" ") |
|
68 | @question = @tokens.join(" ") | |
63 | else |
|
69 | else | |
64 | @question = "" |
|
70 | @question = "" | |
65 | end |
|
71 | end | |
66 | end |
|
72 | end | |
67 |
|
73 | |||
68 | private |
|
74 | private | |
69 | def find_project |
|
75 | def find_project | |
70 | @project = Project.find(params[:id]) |
|
76 | @project = Project.find(params[:id]) | |
71 | @html_title = @project.name |
|
77 | @html_title = @project.name | |
72 | rescue ActiveRecord::RecordNotFound |
|
78 | rescue ActiveRecord::RecordNotFound | |
73 | render_404 |
|
79 | render_404 | |
74 | end |
|
80 | end | |
75 | end |
|
81 | end |
@@ -1,58 +1,65 | |||||
1 | <h2><%= l(:label_search) %></h2> |
|
1 | <h2><%= l(:label_search) %></h2> | |
2 |
|
2 | |||
3 | <div class="box"> |
|
3 | <div class="box"> | |
4 | <% form_tag({}, :method => :get) do %> |
|
4 | <% form_tag({}, :method => :get) do %> | |
5 | <p><%= text_field_tag 'q', @question, :size => 30 %> |
|
5 | <p><%= text_field_tag 'q', @question, :size => 30 %> | |
6 |
|
6 | |||
7 | <% if @project %> |
|
7 | <% if @project %> | |
8 | <%= check_box_tag 'scope[]', 'issues', (@scope.include? 'issues') %> <label><%= l(:label_issue_plural) %></label> |
|
8 | <%= check_box_tag 'scope[]', 'issues', (@scope.include? 'issues') %> <label><%= l(:label_issue_plural) %></label> | |
9 | <% if @project.repository %> |
|
9 | <% if @project.repository %> | |
10 | <%= check_box_tag 'scope[]', 'changesets', (@scope.include? 'changesets') %> <label><%= l(:label_revision_plural) %></label> |
|
10 | <%= check_box_tag 'scope[]', 'changesets', (@scope.include? 'changesets') %> <label><%= l(:label_revision_plural) %></label> | |
11 | <% end %> |
|
11 | <% end %> | |
12 | <%= check_box_tag 'scope[]', 'news', (@scope.include? 'news') %> <label><%= l(:label_news_plural) %></label> |
|
12 | <%= check_box_tag 'scope[]', 'news', (@scope.include? 'news') %> <label><%= l(:label_news_plural) %></label> | |
13 | <%= check_box_tag 'scope[]', 'documents', (@scope.include? 'documents') %> <label><%= l(:label_document_plural) %></label> |
|
13 | <%= check_box_tag 'scope[]', 'documents', (@scope.include? 'documents') %> <label><%= l(:label_document_plural) %></label> | |
14 | <% if @project.wiki %> |
|
14 | <% if @project.wiki %> | |
15 | <%= check_box_tag 'scope[]', 'wiki', (@scope.include? 'wiki') %> <label><%= l(:label_wiki) %></label> |
|
15 | <%= check_box_tag 'scope[]', 'wiki', (@scope.include? 'wiki') %> <label><%= l(:label_wiki) %></label> | |
16 | <% end %> |
|
16 | <% end %> | |
|
17 | <% if @project.boards.any? %> | |||
|
18 | <%= check_box_tag 'scope[]', 'messages', (@scope.include? 'messages') %> <label><%= l(:label_message_plural) %></label> | |||
|
19 | <% end %> | |||
17 | <% else %> |
|
20 | <% else %> | |
18 | <%= check_box_tag 'scope[]', 'projects', (@scope.include? 'projects') %> <label><%= l(:label_project_plural) %></label> |
|
21 | <%= check_box_tag 'scope[]', 'projects', (@scope.include? 'projects') %> <label><%= l(:label_project_plural) %></label> | |
19 | <% end %> |
|
22 | <% end %> | |
20 | <br /> |
|
23 | <br /> | |
21 | <%= check_box_tag 'all_words', 1, @all_words %> <%= l(:label_all_words) %></p> |
|
24 | <%= check_box_tag 'all_words', 1, @all_words %> <%= l(:label_all_words) %></p> | |
22 | <%= submit_tag l(:button_submit), :name => 'submit' %> |
|
25 | <%= submit_tag l(:button_submit), :name => 'submit' %> | |
23 | <% end %> |
|
26 | <% end %> | |
24 | </div> |
|
27 | </div> | |
25 |
|
28 | |||
26 | <% if @results %> |
|
29 | <% if @results %> | |
27 | <h3><%= lwr(:label_result, @results.length) %></h3> |
|
30 | <h3><%= lwr(:label_result, @results.length) %></h3> | |
28 | <ul> |
|
31 | <ul> | |
29 | <% @results.each do |e| %> |
|
32 | <% @results.each do |e| %> | |
30 | <li><p> |
|
33 | <li><p> | |
31 | <% if e.is_a? Project %> |
|
34 | <% if e.is_a? Project %> | |
32 | <%= link_to highlight_tokens(h(e.name), @tokens), :controller => 'projects', :action => 'show', :id => e %><br /> |
|
35 | <%= link_to highlight_tokens(h(e.name), @tokens), :controller => 'projects', :action => 'show', :id => e %><br /> | |
33 | <%= highlight_tokens(e.description, @tokens) %> |
|
36 | <%= highlight_tokens(e.description, @tokens) %> | |
34 | <% elsif e.is_a? Issue %> |
|
37 | <% elsif e.is_a? Issue %> | |
35 | <%= link_to_issue e %>: <%= highlight_tokens(h(e.subject), @tokens) %><br /> |
|
38 | <%= link_to_issue e %>: <%= highlight_tokens(h(e.subject), @tokens) %><br /> | |
36 | <%= highlight_tokens(e.description, @tokens) %><br /> |
|
39 | <%= highlight_tokens(e.description, @tokens) %><br /> | |
37 | <i><%= e.author.name %>, <%= format_time(e.created_on) %></i> |
|
40 | <i><%= e.author.name %>, <%= format_time(e.created_on) %></i> | |
38 | <% elsif e.is_a? News %> |
|
41 | <% elsif e.is_a? News %> | |
39 | <%=l(:label_news)%>: <%= link_to highlight_tokens(h(e.title), @tokens), :controller => 'news', :action => 'show', :id => e %><br /> |
|
42 | <%=l(:label_news)%>: <%= link_to highlight_tokens(h(e.title), @tokens), :controller => 'news', :action => 'show', :id => e %><br /> | |
40 | <%= highlight_tokens(e.description, @tokens) %><br /> |
|
43 | <%= highlight_tokens(e.description, @tokens) %><br /> | |
41 | <i><%= e.author.name %>, <%= format_time(e.created_on) %></i> |
|
44 | <i><%= e.author.name %>, <%= format_time(e.created_on) %></i> | |
42 | <% elsif e.is_a? Document %> |
|
45 | <% elsif e.is_a? Document %> | |
43 | <%=l(:label_document)%>: <%= link_to highlight_tokens(h(e.title), @tokens), :controller => 'documents', :action => 'show', :id => e %><br /> |
|
46 | <%=l(:label_document)%>: <%= link_to highlight_tokens(h(e.title), @tokens), :controller => 'documents', :action => 'show', :id => e %><br /> | |
44 | <%= highlight_tokens(e.description, @tokens) %><br /> |
|
47 | <%= highlight_tokens(e.description, @tokens) %><br /> | |
45 | <i><%= format_time(e.created_on) %></i> |
|
48 | <i><%= format_time(e.created_on) %></i> | |
46 | <% elsif e.is_a? WikiPage %> |
|
49 | <% elsif e.is_a? WikiPage %> | |
47 | <%=l(:label_wiki)%>: <%= link_to highlight_tokens(h(e.pretty_title), @tokens), :controller => 'wiki', :action => 'index', :id => @project, :page => e.title %><br /> |
|
50 | <%=l(:label_wiki)%>: <%= link_to highlight_tokens(h(e.pretty_title), @tokens), :controller => 'wiki', :action => 'index', :id => @project, :page => e.title %><br /> | |
48 | <%= highlight_tokens(e.content.text, @tokens) %><br /> |
|
51 | <%= highlight_tokens(e.content.text, @tokens) %><br /> | |
49 | <i><%= e.content.author ? e.content.author.name : "Anonymous" %>, <%= format_time(e.content.updated_on) %></i> |
|
52 | <i><%= e.content.author ? e.content.author.name : "Anonymous" %>, <%= format_time(e.content.updated_on) %></i> | |
50 | <% elsif e.is_a? Changeset %> |
|
53 | <% elsif e.is_a? Changeset %> | |
51 | <%=l(:label_revision)%> <%= link_to h(e.revision), :controller => 'repositories', :action => 'revision', :id => @project, :rev => e.revision %><br /> |
|
54 | <%=l(:label_revision)%> <%= link_to h(e.revision), :controller => 'repositories', :action => 'revision', :id => @project, :rev => e.revision %><br /> | |
52 | <%= highlight_tokens(e.comments, @tokens) %><br /> |
|
55 | <%= highlight_tokens(e.comments, @tokens) %><br /> | |
53 | <em><%= e.committer.blank? ? e.committer : "Anonymous" %>, <%= format_time(e.committed_on) %></em> |
|
56 | <em><%= e.committer.blank? ? e.committer : "Anonymous" %>, <%= format_time(e.committed_on) %></em> | |
|
57 | <% elsif e.is_a? Message %> | |||
|
58 | <%=h e.board.name %>: <%= link_to_message e %><br /> | |||
|
59 | <%= highlight_tokens(e.content, @tokens) %><br /> | |||
|
60 | <em><%= e.author ? e.author.name : "Anonymous" %>, <%= format_time(e.created_on) %></em> | |||
54 | <% end %> |
|
61 | <% end %> | |
55 | </p></li> |
|
62 | </p></li> | |
56 | <% end %> |
|
63 | <% end %> | |
57 | </ul> |
|
64 | </ul> | |
58 | <% end %> No newline at end of file |
|
65 | <% end %> |
General Comments 0
You need to be logged in to leave comments.
Login now