summaryrefslogtreecommitdiffstats
path: root/app/controllers/search_controller.rb
diff options
context:
space:
mode:
authorJean-Philippe Lang <jp_lang@yahoo.fr>2007-04-30 08:52:39 +0000
committerJean-Philippe Lang <jp_lang@yahoo.fr>2007-04-30 08:52:39 +0000
commitebe10fa6452de7ea6c5759bfd9c7b439091b54cd (patch)
tree30eb8f2f6fb758c41530597814c2e9d048fc75c8 /app/controllers/search_controller.rb
parent833c5035a697858cfc390b08e5db204ba30831d9 (diff)
downloadredmine-ebe10fa6452de7ea6c5759bfd9c7b439091b54cd.tar.gz
redmine-ebe10fa6452de7ea6c5759bfd9c7b439091b54cd.zip
Added a quick search form in page header. Search functionality moved to a dedicated controller.
When used: * outside of a project: searches projects * inside a project: searches issues, changesets, news, documents and wiki pages of the current project If an issue number is given, user is redirected to the corresponding issue. git-svn-id: http://redmine.rubyforge.org/svn/trunk@489 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'app/controllers/search_controller.rb')
-rw-r--r--app/controllers/search_controller.rb75
1 files changed, 75 insertions, 0 deletions
diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb
new file mode 100644
index 000000000..c463e4a35
--- /dev/null
+++ b/app/controllers/search_controller.rb
@@ -0,0 +1,75 @@
+# redMine - project management software
+# Copyright (C) 2006 Jean-Philippe Lang
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+class SearchController < ApplicationController
+ layout 'base'
+
+ def index
+ @question = params[:q] || ""
+ @question.strip!
+ @all_words = params[:all_words] || (params[:submit] ? false : true)
+ @scope = params[:scope] || (params[:submit] ? [] : %w(projects issues changesets news documents wiki) )
+
+ # quick jump to an issue
+ if @scope.include?('issues') && @question.match(/^#?(\d+)$/) && Issue.find_by_id($1, :include => :project, :conditions => Project.visible_by(logged_in_user))
+ redirect_to :controller => "issues", :action => "show", :id => $1
+ return
+ end
+
+ if params[:id]
+ find_project
+ return unless check_project_privacy
+ end
+
+ # tokens must be at least 3 character long
+ @tokens = @question.split.uniq.select {|w| w.length > 2 }
+
+ if !@tokens.empty?
+ # no more than 5 tokens to search for
+ @tokens.slice! 5..-1 if @tokens.size > 5
+ # strings used in sql like statement
+ like_tokens = @tokens.collect {|w| "%#{w.downcase}%"}
+ operator = @all_words ? " AND " : " OR "
+ limit = 10
+ @results = []
+ if @project
+ @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'
+ @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'
+ @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'
+ @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')
+ @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')
+ else
+ Project.with_scope(:find => {:conditions => Project.visible_by(logged_in_user)}) do
+ @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'
+ end
+ # if only one project is found, user is redirected to its overview
+ redirect_to :controller => 'projects', :action => 'show', :id => @results.first and return if @results.size == 1
+ end
+ @question = @tokens.join(" ")
+ else
+ @question = ""
+ end
+ end
+
+private
+ def find_project
+ @project = Project.find(params[:id])
+ @html_title = @project.name
+ rescue ActiveRecord::RecordNotFound
+ render_404
+ end
+end