To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / app / controllers / search_controller.rb @ 1541:2696466256ff

History | View | Annotate | Download (4.13 KB)

1
# Redmine - project management software
2
# Copyright (C) 2006-2014  Jean-Philippe Lang
3
#
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
#
9
# 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
#
14
# 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
    @all_words = params[:all_words] ? params[:all_words].present? : true
25
    @titles_only = params[:titles_only] ? params[:titles_only].present? : false
26

    
27
    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
        @project ? (@project.self_and_descendants.active.all) : nil
35
      else
36
        @project
37
      end
38

    
39
    offset = nil
40
    begin; offset = params[:offset].to_time if params[:offset]; rescue; end
41

    
42
    # quick jump to an issue
43
    if (m = @question.match(/^#?(\d+)$/)) && (issue = Issue.visible.find_by_id(m[1].to_i))
44
      redirect_to issue_path(issue)
45
      return
46
    end
47

    
48
    @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

    
56
    @scope = @object_types.select {|t| params[t]}
57
    @scope = @object_types if @scope.empty?
58

    
59
    # 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

    
65
    if !@tokens.empty?
66
      # no more than 5 tokens to search for
67
      @tokens.slice! 5..-1 if @tokens.size > 5
68

    
69
      @project_matches = []
70
      @results = []
71
      @results_by_type = Hash.new {|h,k| h[k] = 0}
72

    
73
      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
        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
      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
          @pagination_next_date = @results[limit-1].event_datetime
95
          @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
          @pagination_previous_date = @results[-(limit)].event_datetime
101
          @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
private
111
  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