Chris@507: # Redmine - project management software Chris@1115: # Copyright (C) 2006-2012 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@909: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@909: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@0: class SearchController < ApplicationController Chris@0: before_filter :find_optional_project Chris@0: Chris@0: helper :messages Chris@0: include MessagesHelper Chris@0: Chris@0: def index Chris@0: @question = params[:q] || "" Chris@0: @question.strip! Chris@507: @all_words = params[:all_words] ? params[:all_words].present? : true Chris@507: @titles_only = params[:titles_only] ? params[:titles_only].present? : false Chris@909: Chris@0: projects_to_search = Chris@0: case params[:scope] Chris@0: when 'all' Chris@0: nil Chris@0: when 'my_projects' Chris@0: User.current.memberships.collect(&:project) Chris@0: when 'subprojects' Chris@1115: @project ? (@project.self_and_descendants.active.all) : nil Chris@0: else Chris@0: @project Chris@0: end Chris@909: Chris@0: offset = nil Chris@0: begin; offset = params[:offset].to_time if params[:offset]; rescue; end Chris@909: Chris@0: # quick jump to an issue Chris@0: if @question.match(/^#?(\d+)$/) && Issue.visible.find_by_id($1.to_i) Chris@0: redirect_to :controller => "issues", :action => "show", :id => $1 Chris@0: return Chris@0: end Chris@909: Chris@0: @object_types = Redmine::Search.available_search_types.dup Chris@0: if projects_to_search.is_a? Project Chris@0: # don't search projects Chris@0: @object_types.delete('projects') Chris@0: # only show what the user is allowed to view Chris@0: @object_types = @object_types.select {|o| User.current.allowed_to?("view_#{o}".to_sym, projects_to_search)} Chris@0: end Chris@909: Chris@0: @scope = @object_types.select {|t| params[t]} Chris@0: @scope = @object_types if @scope.empty? Chris@909: Chris@0: # extract tokens from the question Chris@0: # eg. hello "bye bye" => ["hello", "bye bye"] Chris@0: @tokens = @question.scan(%r{((\s|^)"[\s\w]+"(\s|$)|\S+)}).collect {|m| m.first.gsub(%r{(^\s*"\s*|\s*"\s*$)}, '')} Chris@0: # tokens must be at least 2 characters long Chris@0: @tokens = @tokens.uniq.select {|w| w.length > 1 } Chris@909: Chris@0: if !@tokens.empty? Chris@0: # no more than 5 tokens to search for Chris@909: @tokens.slice! 5..-1 if @tokens.size > 5 Chris@909: chris@498: @project_matches = [] Chris@0: @results = [] Chris@0: @results_by_type = Hash.new {|h,k| h[k] = 0} Chris@909: Chris@0: limit = 10 Chris@0: @scope.each do |s| Chris@0: r, c = s.singularize.camelcase.constantize.search(@tokens, projects_to_search, Chris@0: :all_words => @all_words, Chris@0: :titles_only => @titles_only, Chris@0: :limit => (limit+1), Chris@0: :offset => offset, Chris@0: :before => params[:previous].nil?) Chris@0: @results += r Chris@0: @results_by_type[s] += c chris@498: if s == 'projects' chris@498: r, c = s.singularize.camelcase.constantize.search(@tokens, nil, chris@498: :all_words => @all_words, chris@498: :titles_only => 1) chris@498: @project_matches += r chris@498: end Chris@0: end Chris@0: @results = @results.sort {|a,b| b.event_datetime <=> a.event_datetime} Chris@0: if params[:previous].nil? Chris@0: @pagination_previous_date = @results[0].event_datetime if offset && @results[0] Chris@0: if @results.size > limit Chris@909: @pagination_next_date = @results[limit-1].event_datetime Chris@0: @results = @results[0, limit] Chris@0: end Chris@0: else Chris@0: @pagination_next_date = @results[-1].event_datetime if offset && @results[-1] Chris@0: if @results.size > limit Chris@909: @pagination_previous_date = @results[-(limit)].event_datetime Chris@0: @results = @results[-(limit), limit] Chris@0: end Chris@0: end Chris@0: else Chris@0: @question = "" Chris@0: end Chris@0: render :layout => false if request.xhr? Chris@0: end Chris@0: Chris@909: private Chris@0: def find_optional_project Chris@0: return true unless params[:id] Chris@0: @project = Project.find(params[:id]) Chris@0: check_project_privacy Chris@0: rescue ActiveRecord::RecordNotFound Chris@0: render_404 Chris@0: end Chris@0: end