##// END OF EJS Templates
Add params to Redmine::Search::Fetcher#initialize optional parameter (#23153)....
Jean-Philippe Lang -
r15201:48a79bc0938c
parent child
Show More
@@ -1,99 +1,99
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2016 Jean-Philippe Lang
2 # Copyright (C) 2006-2016 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
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
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.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 class SearchController < ApplicationController
18 class SearchController < ApplicationController
19 before_filter :find_optional_project
19 before_filter :find_optional_project
20 accept_api_auth :index
20 accept_api_auth :index
21
21
22 def index
22 def index
23 @question = params[:q] || ""
23 @question = params[:q] || ""
24 @question.strip!
24 @question.strip!
25 @all_words = params[:all_words] ? params[:all_words].present? : true
25 @all_words = params[:all_words] ? params[:all_words].present? : true
26 @titles_only = params[:titles_only] ? params[:titles_only].present? : false
26 @titles_only = params[:titles_only] ? params[:titles_only].present? : false
27 @search_attachments = params[:attachments].presence || '0'
27 @search_attachments = params[:attachments].presence || '0'
28 @open_issues = params[:open_issues] ? params[:open_issues].present? : false
28 @open_issues = params[:open_issues] ? params[:open_issues].present? : false
29
29
30 case params[:format]
30 case params[:format]
31 when 'xml', 'json'
31 when 'xml', 'json'
32 @offset, @limit = api_offset_and_limit
32 @offset, @limit = api_offset_and_limit
33 else
33 else
34 @offset = nil
34 @offset = nil
35 @limit = Setting.search_results_per_page.to_i
35 @limit = Setting.search_results_per_page.to_i
36 @limit = 10 if @limit == 0
36 @limit = 10 if @limit == 0
37 end
37 end
38
38
39 # quick jump to an issue
39 # quick jump to an issue
40 if (m = @question.match(/^#?(\d+)$/)) && (issue = Issue.visible.find_by_id(m[1].to_i))
40 if (m = @question.match(/^#?(\d+)$/)) && (issue = Issue.visible.find_by_id(m[1].to_i))
41 redirect_to issue_path(issue)
41 redirect_to issue_path(issue)
42 return
42 return
43 end
43 end
44
44
45 projects_to_search =
45 projects_to_search =
46 case params[:scope]
46 case params[:scope]
47 when 'all'
47 when 'all'
48 nil
48 nil
49 when 'my_projects'
49 when 'my_projects'
50 User.current.projects
50 User.current.projects
51 when 'subprojects'
51 when 'subprojects'
52 @project ? (@project.self_and_descendants.active.to_a) : nil
52 @project ? (@project.self_and_descendants.active.to_a) : nil
53 else
53 else
54 @project
54 @project
55 end
55 end
56
56
57 @object_types = Redmine::Search.available_search_types.dup
57 @object_types = Redmine::Search.available_search_types.dup
58 if projects_to_search.is_a? Project
58 if projects_to_search.is_a? Project
59 # don't search projects
59 # don't search projects
60 @object_types.delete('projects')
60 @object_types.delete('projects')
61 # only show what the user is allowed to view
61 # only show what the user is allowed to view
62 @object_types = @object_types.select {|o| User.current.allowed_to?("view_#{o}".to_sym, projects_to_search)}
62 @object_types = @object_types.select {|o| User.current.allowed_to?("view_#{o}".to_sym, projects_to_search)}
63 end
63 end
64
64
65 @scope = @object_types.select {|t| params[t]}
65 @scope = @object_types.select {|t| params[t]}
66 @scope = @object_types if @scope.empty?
66 @scope = @object_types if @scope.empty?
67
67
68 fetcher = Redmine::Search::Fetcher.new(
68 fetcher = Redmine::Search::Fetcher.new(
69 @question, User.current, @scope, projects_to_search,
69 @question, User.current, @scope, projects_to_search,
70 :all_words => @all_words, :titles_only => @titles_only, :attachments => @search_attachments, :open_issues => @open_issues,
70 :all_words => @all_words, :titles_only => @titles_only, :attachments => @search_attachments, :open_issues => @open_issues,
71 :cache => params[:page].present?
71 :cache => params[:page].present?, :params => params
72 )
72 )
73
73
74 if fetcher.tokens.present?
74 if fetcher.tokens.present?
75 @result_count = fetcher.result_count
75 @result_count = fetcher.result_count
76 @result_count_by_type = fetcher.result_count_by_type
76 @result_count_by_type = fetcher.result_count_by_type
77 @tokens = fetcher.tokens
77 @tokens = fetcher.tokens
78
78
79 @result_pages = Paginator.new @result_count, @limit, params['page']
79 @result_pages = Paginator.new @result_count, @limit, params['page']
80 @offset ||= @result_pages.offset
80 @offset ||= @result_pages.offset
81 @results = fetcher.results(@offset, @result_pages.per_page)
81 @results = fetcher.results(@offset, @result_pages.per_page)
82 else
82 else
83 @question = ""
83 @question = ""
84 end
84 end
85 respond_to do |format|
85 respond_to do |format|
86 format.html { render :layout => false if request.xhr? }
86 format.html { render :layout => false if request.xhr? }
87 format.api { @results ||= []; render :layout => false }
87 format.api { @results ||= []; render :layout => false }
88 end
88 end
89 end
89 end
90
90
91 private
91 private
92 def find_optional_project
92 def find_optional_project
93 return true unless params[:id]
93 return true unless params[:id]
94 @project = Project.find(params[:id])
94 @project = Project.find(params[:id])
95 check_project_privacy
95 check_project_privacy
96 rescue ActiveRecord::RecordNotFound
96 rescue ActiveRecord::RecordNotFound
97 render_404
97 render_404
98 end
98 end
99 end
99 end
General Comments 0
You need to be logged in to leave comments. Login now