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