annotate .svn/pristine/ac/ac29f2ab2461516f86b651fe368aa1f1f715c245.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

Merge from branch "live"
author Chris Cannam
date Tue, 09 Sep 2014 09:34:53 +0100
parents cbb26bc654de
children
rev   line source
Chris@909 1 # Redmine - project management software
Chris@909 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
Chris@909 3 #
Chris@909 4 # This program is free software; you can redistribute it and/or
Chris@909 5 # modify it under the terms of the GNU General Public License
Chris@909 6 # as published by the Free Software Foundation; either version 2
Chris@909 7 # of the License, or (at your option) any later version.
Chris@909 8 #
Chris@909 9 # This program is distributed in the hope that it will be useful,
Chris@909 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@909 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@909 12 # GNU General Public License for more details.
Chris@909 13 #
Chris@909 14 # You should have received a copy of the GNU General Public License
Chris@909 15 # along with this program; if not, write to the Free Software
Chris@909 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@909 17
Chris@909 18 class SearchController < ApplicationController
Chris@909 19 before_filter :find_optional_project
Chris@909 20
Chris@909 21 helper :messages
Chris@909 22 include MessagesHelper
Chris@909 23
Chris@909 24 def index
Chris@909 25 @question = params[:q] || ""
Chris@909 26 @question.strip!
Chris@909 27 @all_words = params[:all_words] ? params[:all_words].present? : true
Chris@909 28 @titles_only = params[:titles_only] ? params[:titles_only].present? : false
Chris@909 29
Chris@909 30 projects_to_search =
Chris@909 31 case params[:scope]
Chris@909 32 when 'all'
Chris@909 33 nil
Chris@909 34 when 'my_projects'
Chris@909 35 User.current.memberships.collect(&:project)
Chris@909 36 when 'subprojects'
Chris@909 37 @project ? (@project.self_and_descendants.active) : nil
Chris@909 38 else
Chris@909 39 @project
Chris@909 40 end
Chris@909 41
Chris@909 42 offset = nil
Chris@909 43 begin; offset = params[:offset].to_time if params[:offset]; rescue; end
Chris@909 44
Chris@909 45 # quick jump to an issue
Chris@909 46 if @question.match(/^#?(\d+)$/) && Issue.visible.find_by_id($1.to_i)
Chris@909 47 redirect_to :controller => "issues", :action => "show", :id => $1
Chris@909 48 return
Chris@909 49 end
Chris@909 50
Chris@909 51 @object_types = Redmine::Search.available_search_types.dup
Chris@909 52 if projects_to_search.is_a? Project
Chris@909 53 # don't search projects
Chris@909 54 @object_types.delete('projects')
Chris@909 55 # only show what the user is allowed to view
Chris@909 56 @object_types = @object_types.select {|o| User.current.allowed_to?("view_#{o}".to_sym, projects_to_search)}
Chris@909 57 end
Chris@909 58
Chris@909 59 @scope = @object_types.select {|t| params[t]}
Chris@909 60 @scope = @object_types if @scope.empty?
Chris@909 61
Chris@909 62 # extract tokens from the question
Chris@909 63 # eg. hello "bye bye" => ["hello", "bye bye"]
Chris@909 64 @tokens = @question.scan(%r{((\s|^)"[\s\w]+"(\s|$)|\S+)}).collect {|m| m.first.gsub(%r{(^\s*"\s*|\s*"\s*$)}, '')}
Chris@909 65 # tokens must be at least 2 characters long
Chris@909 66 @tokens = @tokens.uniq.select {|w| w.length > 1 }
Chris@909 67
Chris@909 68 if !@tokens.empty?
Chris@909 69 # no more than 5 tokens to search for
Chris@909 70 @tokens.slice! 5..-1 if @tokens.size > 5
Chris@909 71
Chris@909 72 @results = []
Chris@909 73 @results_by_type = Hash.new {|h,k| h[k] = 0}
Chris@909 74
Chris@909 75 limit = 10
Chris@909 76 @scope.each do |s|
Chris@909 77 r, c = s.singularize.camelcase.constantize.search(@tokens, projects_to_search,
Chris@909 78 :all_words => @all_words,
Chris@909 79 :titles_only => @titles_only,
Chris@909 80 :limit => (limit+1),
Chris@909 81 :offset => offset,
Chris@909 82 :before => params[:previous].nil?)
Chris@909 83 @results += r
Chris@909 84 @results_by_type[s] += c
Chris@909 85 end
Chris@909 86 @results = @results.sort {|a,b| b.event_datetime <=> a.event_datetime}
Chris@909 87 if params[:previous].nil?
Chris@909 88 @pagination_previous_date = @results[0].event_datetime if offset && @results[0]
Chris@909 89 if @results.size > limit
Chris@909 90 @pagination_next_date = @results[limit-1].event_datetime
Chris@909 91 @results = @results[0, limit]
Chris@909 92 end
Chris@909 93 else
Chris@909 94 @pagination_next_date = @results[-1].event_datetime if offset && @results[-1]
Chris@909 95 if @results.size > limit
Chris@909 96 @pagination_previous_date = @results[-(limit)].event_datetime
Chris@909 97 @results = @results[-(limit), limit]
Chris@909 98 end
Chris@909 99 end
Chris@909 100 else
Chris@909 101 @question = ""
Chris@909 102 end
Chris@909 103 render :layout => false if request.xhr?
Chris@909 104 end
Chris@909 105
Chris@909 106 private
Chris@909 107 def find_optional_project
Chris@909 108 return true unless params[:id]
Chris@909 109 @project = Project.find(params[:id])
Chris@909 110 check_project_privacy
Chris@909 111 rescue ActiveRecord::RecordNotFound
Chris@909 112 render_404
Chris@909 113 end
Chris@909 114 end