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 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2011 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. module SearchHelper
  18. def highlight_tokens(text, tokens)
  19. return text unless text && tokens && !tokens.empty?
  20. re_tokens = tokens.collect {|t| Regexp.escape(t)}
  21. regexp = Regexp.new "(#{re_tokens.join('|')})", Regexp::IGNORECASE
  22. result = ''
  23. text.split(regexp).each_with_index do |words, i|
  24. if result.length > 1200
  25. # maximum length of the preview reached
  26. result << '...'
  27. break
  28. end
  29. words = words.mb_chars
  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
  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_and_its_subprojects, @project.name), 'subprojects'] unless @project.nil? || @project.descendants.active.empty?
  46. options << [@project.name, ''] unless @project.nil?
  47. select_tag('scope', options_for_select(options, params[:scope].to_s)) if options.size > 1
  48. end
  49. def render_results_by_type(results_by_type)
  50. links = []
  51. # Sorts types by results count
  52. results_by_type.keys.sort {|a, b| results_by_type[b] <=> results_by_type[a]}.each do |t|
  53. c = results_by_type[t]
  54. next if c == 0
  55. text = "#{type_label(t)} (#{c})"
  56. links << link_to(h(text), :q => params[:q], :titles_only => params[:titles_only],
  57. :all_words => params[:all_words], :scope => params[:scope], t => 1)
  58. end
  59. ('<ul>' + links.map {|link| content_tag('li', link)}.join(' ') + '</ul>') unless links.empty?
  60. end
  61. end