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