annotate .svn/pristine/f7/f75cc0973adb64fe86bec853f1ce941fdd75f24d.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

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