annotate .svn/pristine/ac/acb16fb7d1a43ca9b265d3fc139a9a8c3d61e7c6.svn-base @ 1327:287f201c2802 redmine-2.2-integration

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