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