You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

search_helper.rb 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006- Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. module SearchHelper
  19. def highlight_tokens(text, tokens)
  20. return text unless text && tokens && !tokens.empty?
  21. re_tokens = tokens.collect {|t| Regexp.escape(t)}
  22. regexp = Regexp.new "(#{re_tokens.join('|')})", Regexp::IGNORECASE
  23. result = +''
  24. text.split(regexp).each_with_index do |words, i|
  25. if result.length > 1200
  26. # maximum length of the preview reached
  27. result << '...'
  28. break
  29. end
  30. if i.even?
  31. result << h(words.length > 100 ? "#{words.slice(0..44)} ... #{words.slice(-45..-1)}" : words)
  32. else
  33. t = (tokens.index(words.downcase) || 0) % 4
  34. result << content_tag('span', h(words), :class => "highlight token-#{t}")
  35. end
  36. end
  37. result.html_safe
  38. end
  39. def type_label(t)
  40. l("label_#{t.singularize}_plural", :default => t.to_s.humanize)
  41. end
  42. def project_select_tag
  43. options = [[l(:label_project_all), 'all']]
  44. options << [l(:label_my_projects), 'my_projects'] unless User.current.memberships.empty?
  45. options << [l(:label_my_bookmarks), 'bookmarks'] unless User.current.bookmarked_project_ids.empty?
  46. options << [l(:label_and_its_subprojects, @project.name), 'subprojects'] unless @project.nil? || @project.descendants.active.empty?
  47. options << [@project.name, ''] unless @project.nil?
  48. label_tag("scope", l(:description_project_scope), :class => "hidden-for-sighted") +
  49. select_tag('scope', options_for_select(options, params[:scope].to_s)) if options.size > 1
  50. end
  51. def render_results_by_type(results_by_type)
  52. links = []
  53. # Sorts types by results count
  54. results_by_type.keys.sort_by {|k| results_by_type[k]}.reverse_each do |t|
  55. c = results_by_type[t]
  56. next if c == 0
  57. text = "#{type_label(t)} (#{c})"
  58. links << link_to(h(text), :q => params[:q], :titles_only => params[:titles_only],
  59. :all_words => params[:all_words], :scope => params[:scope], t => 1)
  60. end
  61. ('<ul>'.html_safe +
  62. links.map {|link| content_tag('li', link)}.join(' ').html_safe +
  63. '</ul>'.html_safe) unless links.empty?
  64. end
  65. def issues_filter_path(question, options)
  66. projects_scope = options[:projects_scope]
  67. titles_only = options[:titles_only]
  68. all_words = options[:all_words]
  69. open_issues = options[:open_issues]
  70. field_to_search = titles_only ? 'subject' : 'any_searchable'
  71. params = {
  72. :set_filter => 1,
  73. :f => ['status_id', field_to_search],
  74. :op => {
  75. 'status_id' => open_issues ? 'o' : '*',
  76. field_to_search => all_words ? '~' : '*~'
  77. },
  78. :v => {field_to_search => [question]},
  79. :sort => 'updated_on:desc'
  80. }
  81. case projects_scope
  82. when 'all'
  83. # nothing to do
  84. when 'my_projects'
  85. params[:f] << 'project_id'
  86. params[:op]['project_id'] = '='
  87. params[:v]['project_id'] = ['mine']
  88. when 'bookmarks'
  89. params[:f] << 'project_id'
  90. params[:op]['project_id'] = '='
  91. params[:v]['project_id'] = ['bookmarks']
  92. when 'subprojects'
  93. params[:f] << 'subproject_id'
  94. params[:op]['subproject_id'] = '*'
  95. params[:project_id] = @project.id
  96. else
  97. if @project
  98. # current project only
  99. params[:f] << 'subproject_id'
  100. params[:op]['subproject_id'] = '!*'
  101. params[:project_id] = @project.id
  102. end
  103. # else all projects
  104. end
  105. issues_path(params)
  106. end
  107. end