Chris@909: # encoding: utf-8 Chris@909: # Chris@507: # Redmine - project management software Chris@1115: # Copyright (C) 2006-2012 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@909: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@909: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@0: module SearchHelper Chris@0: def highlight_tokens(text, tokens) Chris@0: return text unless text && tokens && !tokens.empty? Chris@0: re_tokens = tokens.collect {|t| Regexp.escape(t)} Chris@909: regexp = Regexp.new "(#{re_tokens.join('|')})", Regexp::IGNORECASE Chris@0: result = '' Chris@0: text.split(regexp).each_with_index do |words, i| Chris@0: if result.length > 1200 Chris@0: # maximum length of the preview reached Chris@0: result << '...' Chris@0: break Chris@0: end Chris@0: words = words.mb_chars Chris@0: if i.even? Chris@0: result << h(words.length > 100 ? "#{words.slice(0..44)} ... #{words.slice(-45..-1)}" : words) Chris@0: else Chris@0: t = (tokens.index(words.downcase) || 0) % 4 Chris@0: result << content_tag('span', h(words), :class => "highlight token-#{t}") Chris@0: end Chris@0: end Chris@1115: result.html_safe Chris@0: end Chris@909: Chris@0: def type_label(t) chris@37: l("label_#{t.singularize}_plural", :default => t.to_s.humanize) Chris@0: end Chris@909: Chris@0: def project_select_tag Chris@0: options = [[l(:label_project_all), 'all']] Chris@0: options << [l(:label_my_projects), 'my_projects'] unless User.current.memberships.empty? Chris@0: options << [l(:label_and_its_subprojects, @project.name), 'subprojects'] unless @project.nil? || @project.descendants.active.empty? Chris@0: options << [@project.name, ''] unless @project.nil? Chris@909: label_tag("scope", l(:description_project_scope), :class => "hidden-for-sighted") + Chris@0: select_tag('scope', options_for_select(options, params[:scope].to_s)) if options.size > 1 Chris@0: end Chris@909: Chris@0: def render_results_by_type(results_by_type) Chris@0: links = [] Chris@0: # Sorts types by results count Chris@0: results_by_type.keys.sort {|a, b| results_by_type[b] <=> results_by_type[a]}.each do |t| Chris@0: c = results_by_type[t] Chris@0: next if c == 0 Chris@0: text = "#{type_label(t)} (#{c})" Chris@909: links << link_to(h(text), :q => params[:q], :titles_only => params[:titles_only], Chris@909: :all_words => params[:all_words], :scope => params[:scope], t => 1) Chris@0: end Chris@1115: (''.html_safe) unless links.empty? Chris@0: end Chris@0: end