annotate .svn/pristine/d4/d4b58d822f95056e91ecc443b37eb8146638e83d.svn-base @ 1621:3a510bf6a9bc

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