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