chris@37: # Redmine - project management software chris@37: # Copyright (C) 2006-2010 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@0: # 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@0: # 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: require 'forwardable' Chris@0: require 'cgi' Chris@0: Chris@0: module ApplicationHelper Chris@0: include Redmine::WikiFormatting::Macros::Definitions Chris@0: include Redmine::I18n Chris@0: include GravatarHelper::PublicMethods Chris@0: Chris@0: extend Forwardable Chris@0: def_delegators :wiki_helper, :wikitoolbar_for, :heads_for_wiki_formatter Chris@0: Chris@0: # Return true if user is authorized for controller/action, otherwise false Chris@0: def authorize_for(controller, action) Chris@0: User.current.allowed_to?({:controller => controller, :action => action}, @project) Chris@0: end Chris@0: Chris@0: # Display a link if user is authorized chris@22: # chris@22: # @param [String] name Anchor text (passed to link_to) chris@37: # @param [Hash] options Hash params. This will checked by authorize_for to see if the user is authorized chris@22: # @param [optional, Hash] html_options Options passed to link_to chris@22: # @param [optional, Hash] parameters_for_method_reference Extra parameters for link_to Chris@0: def link_to_if_authorized(name, options = {}, html_options = nil, *parameters_for_method_reference) chris@37: link_to(name, options, html_options, *parameters_for_method_reference) if authorize_for(options[:controller] || params[:controller], options[:action]) Chris@0: end Chris@0: Chris@0: # Display a link to remote if user is authorized Chris@0: def link_to_remote_if_authorized(name, options = {}, html_options = nil) Chris@0: url = options[:url] || {} Chris@0: link_to_remote(name, options, html_options) if authorize_for(url[:controller] || params[:controller], url[:action]) Chris@0: end Chris@0: Chris@0: # Displays a link to user's account page if active Chris@0: def link_to_user(user, options={}) Chris@0: if user.is_a?(User) Chris@0: name = h(user.name(options[:format])) Chris@0: if user.active? chris@140: link_to(name, :controller => 'users', :action => 'show', :id => user) Chris@0: else Chris@0: name Chris@0: end Chris@0: else Chris@0: h(user.to_s) Chris@0: end Chris@0: end Chris@0: Chris@0: # Displays a link to +issue+ with its subject. Chris@0: # Examples: Chris@0: # Chris@0: # link_to_issue(issue) # => Defect #6: This is the subject Chris@0: # link_to_issue(issue, :truncate => 6) # => Defect #6: This i... Chris@0: # link_to_issue(issue, :subject => false) # => Defect #6 Chris@0: # link_to_issue(issue, :project => true) # => Foo - Defect #6 Chris@0: # Chris@0: def link_to_issue(issue, options={}) Chris@0: title = nil Chris@0: subject = nil Chris@0: if options[:subject] == false Chris@0: title = truncate(issue.subject, :length => 60) Chris@0: else Chris@0: subject = issue.subject Chris@0: if options[:truncate] Chris@0: subject = truncate(subject, :length => options[:truncate]) Chris@0: end Chris@0: end Chris@0: s = link_to "#{issue.tracker} ##{issue.id}", {:controller => "issues", :action => "show", :id => issue}, Chris@0: :class => issue.css_classes, Chris@0: :title => title Chris@0: s << ": #{h subject}" if subject Chris@0: s = "#{h issue.project} - " + s if options[:project] Chris@0: s Chris@0: end Chris@0: Chris@0: # Generates a link to an attachment. Chris@0: # Options: Chris@0: # * :text - Link text (default to attachment filename) Chris@0: # * :download - Force download (default: false) Chris@0: def link_to_attachment(attachment, options={}) Chris@0: text = options.delete(:text) || attachment.filename Chris@0: action = options.delete(:download) ? 'download' : 'show' Chris@0: Chris@0: link_to(h(text), {:controller => 'attachments', :action => action, :id => attachment, :filename => attachment.filename }, options) Chris@0: end Chris@0: Chris@0: # Generates a link to a SCM revision Chris@0: # Options: Chris@0: # * :text - Link text (default to the formatted revision) Chris@0: def link_to_revision(revision, project, options={}) Chris@0: text = options.delete(:text) || format_revision(revision) Chris@3: rev = revision.respond_to?(:identifier) ? revision.identifier : revision Chris@0: Chris@3: link_to(text, {:controller => 'repositories', :action => 'revision', :id => project, :rev => rev}, Chris@3: :title => l(:label_revision_id, format_revision(revision))) Chris@0: end Chris@0: Chris@14: # Generates a link to a project if active Chris@14: # Examples: Chris@14: # Chris@14: # link_to_project(project) # => link to the specified project overview Chris@14: # link_to_project(project, :action=>'settings') # => link to project settings Chris@14: # link_to_project(project, {:only_path => false}, :class => "project") # => 3rd arg adds html options Chris@14: # link_to_project(project, {}, :class => "project") # => html options with default url (project overview) Chris@14: # Chris@14: def link_to_project(project, options={}, html_options = nil) Chris@14: if project.active? Chris@14: url = {:controller => 'projects', :action => 'show', :id => project}.merge(options) Chris@14: link_to(h(project), url, html_options) Chris@14: else Chris@14: h(project) Chris@14: end Chris@14: end Chris@14: Chris@0: def toggle_link(name, id, options={}) Chris@0: onclick = "Element.toggle('#{id}'); " Chris@0: onclick << (options[:focus] ? "Form.Element.focus('#{options[:focus]}'); " : "this.blur(); ") Chris@0: onclick << "return false;" Chris@0: link_to(name, "#", :onclick => onclick) Chris@0: end Chris@0: Chris@0: def image_to_function(name, function, html_options = {}) Chris@0: html_options.symbolize_keys! Chris@0: tag(:input, html_options.merge({ Chris@0: :type => "image", :src => image_path(name), Chris@0: :onclick => (html_options[:onclick] ? "#{html_options[:onclick]}; " : "") + "#{function};" Chris@0: })) Chris@0: end Chris@0: Chris@0: def prompt_to_remote(name, text, param, url, html_options = {}) Chris@0: html_options[:onclick] = "promptToRemote('#{text}', '#{param}', '#{url_for(url)}'); return false;" Chris@0: link_to name, {}, html_options Chris@0: end Chris@0: Chris@0: def format_activity_title(text) Chris@0: h(truncate_single_line(text, :length => 100)) Chris@0: end Chris@0: Chris@0: def format_activity_day(date) Chris@0: date == Date.today ? l(:label_today).titleize : format_date(date) Chris@0: end Chris@0: Chris@0: def format_activity_description(text) Chris@0: h(truncate(text.to_s, :length => 120).gsub(%r{[\r\n]*<(pre|code)>.*$}m, '...')).gsub(/[\r\n]+/, "
") Chris@0: end Chris@0: Chris@0: def format_version_name(version) Chris@0: if version.project == @project Chris@0: h(version) Chris@0: else Chris@0: h("#{version.project} - #{version}") Chris@0: end Chris@0: end Chris@0: Chris@0: def due_date_distance_in_words(date) Chris@0: if date Chris@0: l((date < Date.today ? :label_roadmap_overdue : :label_roadmap_due_in), distance_of_date_in_words(Date.today, date)) Chris@0: end Chris@0: end Chris@0: Chris@0: def render_page_hierarchy(pages, node=nil) Chris@0: content = '' Chris@0: if pages[node] Chris@0: content << "\n" Chris@0: end Chris@0: content Chris@0: end Chris@0: Chris@0: # Renders flash messages Chris@0: def render_flash_messages Chris@0: s = '' Chris@0: flash.each do |k,v| Chris@0: s << content_tag('div', v, :class => "flash #{k}") Chris@0: end Chris@0: s Chris@0: end Chris@0: Chris@0: # Renders tabs and their content Chris@0: def render_tabs(tabs) Chris@0: if tabs.any? Chris@0: render :partial => 'common/tabs', :locals => {:tabs => tabs} Chris@0: else Chris@0: content_tag 'p', l(:label_no_data), :class => "nodata" Chris@0: end Chris@0: end Chris@0: Chris@0: # Renders the project quick-jump box Chris@0: def render_project_jump_box Chris@0: # Retrieve them now to avoid a COUNT query Chris@0: projects = User.current.projects.all Chris@0: if projects.any? Chris@0: s = '' Chris@0: s Chris@0: end Chris@0: end Chris@0: Chris@0: def project_tree_options_for_select(projects, options = {}) Chris@0: s = '' Chris@0: project_tree(projects) do |project, level| Chris@0: name_prefix = (level > 0 ? (' ' * 2 * level + '» ') : '') Chris@0: tag_options = {:value => project.id} Chris@0: if project == options[:selected] || (options[:selected].respond_to?(:include?) && options[:selected].include?(project)) Chris@0: tag_options[:selected] = 'selected' Chris@0: else Chris@0: tag_options[:selected] = nil Chris@0: end Chris@0: tag_options.merge!(yield(project)) if block_given? Chris@0: s << content_tag('option', name_prefix + h(project), tag_options) Chris@0: end Chris@0: s Chris@0: end Chris@0: Chris@0: # Yields the given block for each project with its level in the tree chris@37: # chris@37: # Wrapper for Project#project_tree Chris@0: def project_tree(projects, &block) chris@37: Project.project_tree(projects, &block) Chris@0: end Chris@0: Chris@0: def project_nested_ul(projects, &block) Chris@0: s = '' Chris@0: if projects.any? Chris@0: ancestors = [] Chris@0: projects.sort_by(&:lft).each do |project| Chris@0: if (ancestors.empty? || project.is_descendant_of?(ancestors.last)) Chris@0: s << "\n" Chris@0: end Chris@0: end Chris@0: s << "
  • " Chris@0: s << yield(project).to_s Chris@0: ancestors << project Chris@0: end Chris@0: s << ("
  • \n" * ancestors.size) Chris@0: end Chris@0: s Chris@0: end Chris@0: Chris@0: def principals_check_box_tags(name, principals) Chris@0: s = '' Chris@0: principals.sort.each do |principal| chris@135: s << "\n" Chris@0: end Chris@0: s Chris@0: end Chris@0: Chris@0: # Truncates and returns the string as a single line Chris@0: def truncate_single_line(string, *args) Chris@0: truncate(string.to_s, *args).gsub(%r{[\r\n]+}m, ' ') Chris@0: end Chris@0: Chris@0: # Truncates at line break after 250 characters or options[:length] Chris@0: def truncate_lines(string, options={}) Chris@0: length = options[:length] || 250 Chris@0: if string.to_s =~ /\A(.{#{length}}.*?)$/m Chris@0: "#{$1}..." Chris@0: else Chris@0: string Chris@0: end Chris@0: end Chris@0: Chris@0: def html_hours(text) Chris@0: text.gsub(%r{(\d+)\.(\d+)}, '\1.\2') Chris@0: end Chris@0: Chris@0: def authoring(created, author, options={}) Chris@0: l(options[:label] || :label_added_time_by, :author => link_to_user(author), :age => time_tag(created)) Chris@0: end Chris@0: Chris@0: def time_tag(time) Chris@0: text = distance_of_time_in_words(Time.now, time) Chris@0: if @project chris@22: link_to(text, {:controller => 'activities', :action => 'index', :id => @project, :from => time.to_date}, :title => format_time(time)) Chris@0: else Chris@0: content_tag('acronym', text, :title => format_time(time)) Chris@0: end Chris@0: end Chris@0: Chris@0: def syntax_highlight(name, content) Chris@0: Redmine::SyntaxHighlighting.highlight_by_filename(content, name) Chris@0: end Chris@0: Chris@0: def to_path_param(path) Chris@0: path.to_s.split(%r{[/\\]}).select {|p| !p.blank?} Chris@0: end Chris@0: Chris@0: def pagination_links_full(paginator, count=nil, options={}) Chris@0: page_param = options.delete(:page_param) || :page Chris@0: per_page_links = options.delete(:per_page_links) Chris@0: url_param = params.dup Chris@0: # don't reuse query params if filters are present Chris@0: url_param.merge!(:fields => nil, :values => nil, :operators => nil) if url_param.delete(:set_filter) Chris@0: Chris@0: html = '' Chris@0: if paginator.current.previous Chris@0: html << link_to_remote_content_update('« ' + l(:label_previous), url_param.merge(page_param => paginator.current.previous)) + ' ' Chris@0: end Chris@0: Chris@0: html << (pagination_links_each(paginator, options) do |n| Chris@0: link_to_remote_content_update(n.to_s, url_param.merge(page_param => n)) Chris@0: end || '') Chris@0: Chris@0: if paginator.current.next Chris@0: html << ' ' + link_to_remote_content_update((l(:label_next) + ' »'), url_param.merge(page_param => paginator.current.next)) Chris@0: end Chris@0: Chris@0: unless count.nil? Chris@0: html << " (#{paginator.current.first_item}-#{paginator.current.last_item}/#{count})" Chris@0: if per_page_links != false && links = per_page_links(paginator.items_per_page) Chris@0: html << " | #{links}" Chris@0: end Chris@0: end Chris@0: Chris@0: html Chris@0: end Chris@0: Chris@0: def per_page_links(selected=nil) Chris@0: url_param = params.dup Chris@0: url_param.clear if url_param.has_key?(:set_filter) Chris@0: Chris@0: links = Setting.per_page_options_array.collect do |n| Chris@0: n == selected ? n : link_to_remote(n, {:update => "content", Chris@0: :url => params.dup.merge(:per_page => n), Chris@0: :method => :get}, Chris@0: {:href => url_for(url_param.merge(:per_page => n))}) Chris@0: end Chris@0: links.size > 1 ? l(:label_display_per_page, links.join(', ')) : nil Chris@0: end Chris@0: Chris@0: def reorder_links(name, url) Chris@0: link_to(image_tag('2uparrow.png', :alt => l(:label_sort_highest)), url.merge({"#{name}[move_to]" => 'highest'}), :method => :post, :title => l(:label_sort_highest)) + Chris@0: link_to(image_tag('1uparrow.png', :alt => l(:label_sort_higher)), url.merge({"#{name}[move_to]" => 'higher'}), :method => :post, :title => l(:label_sort_higher)) + Chris@0: link_to(image_tag('1downarrow.png', :alt => l(:label_sort_lower)), url.merge({"#{name}[move_to]" => 'lower'}), :method => :post, :title => l(:label_sort_lower)) + Chris@0: link_to(image_tag('2downarrow.png', :alt => l(:label_sort_lowest)), url.merge({"#{name}[move_to]" => 'lowest'}), :method => :post, :title => l(:label_sort_lowest)) Chris@0: end Chris@0: Chris@0: def breadcrumb(*args) Chris@0: elements = args.flatten Chris@0: elements.any? ? content_tag('p', args.join(' » ') + ' » ', :class => 'breadcrumb') : nil Chris@0: end Chris@0: Chris@0: def other_formats_links(&block) Chris@0: concat('

    ' + l(:label_export_to)) Chris@0: yield Redmine::Views::OtherFormatsBuilder.new(self) Chris@0: concat('

    ') Chris@0: end Chris@0: Chris@0: def page_header_title Chris@0: if @project.nil? || @project.new_record? luisf@144: a = [h(Setting.app_title), ''] luisf@144: Chris@0: else luisf@144: pname = [] Chris@0: b = [] Chris@0: ancestors = (@project.root? ? [] : @project.ancestors.visible) Chris@0: if ancestors.any? Chris@0: root = ancestors.shift Chris@14: b << link_to_project(root, {:jump => current_menu_item}, :class => 'root') Chris@0: if ancestors.size > 2 luisf@144: b << '…' Chris@0: ancestors = ancestors[-2, 2] Chris@0: end Chris@14: b += ancestors.collect {|p| link_to_project(p, {:jump => current_menu_item}, :class => 'ancestor') } luisf@144: b = b.join(' » ') luisf@144: b << (' »') Chris@0: end luisf@144: luisf@144: pname << h(@project) luisf@144: luisf@144: a = [pname, b] luisf@144: Chris@0: end Chris@0: end Chris@0: Chris@0: def html_title(*args) Chris@0: if args.empty? Chris@0: title = [] Chris@0: title << @project.name if @project Chris@0: title += @html_title if @html_title Chris@0: title << Setting.app_title Chris@0: title.select {|t| !t.blank? }.join(' - ') Chris@0: else Chris@0: @html_title ||= [] Chris@0: @html_title += args Chris@0: end Chris@0: end Chris@0: Chris@14: # Returns the theme, controller name, and action as css classes for the Chris@14: # HTML body. Chris@14: def body_css_classes Chris@14: css = [] Chris@14: if theme = Redmine::Themes.theme(Setting.ui_theme) Chris@14: css << 'theme-' + theme.name Chris@14: end Chris@14: Chris@14: css << 'controller-' + params[:controller] Chris@14: css << 'action-' + params[:action] Chris@14: css.join(' ') Chris@14: end Chris@14: Chris@0: def accesskey(s) Chris@0: Redmine::AccessKeys.key_for s Chris@0: end Chris@0: Chris@0: # Formats text according to system settings. Chris@0: # 2 ways to call this method: Chris@0: # * with a String: textilizable(text, options) Chris@0: # * with an object and one of its attribute: textilizable(issue, :description, options) Chris@0: def textilizable(*args) Chris@0: options = args.last.is_a?(Hash) ? args.pop : {} Chris@0: case args.size Chris@0: when 1 Chris@0: obj = options[:object] Chris@0: text = args.shift Chris@0: when 2 Chris@0: obj = args.shift Chris@0: attr = args.shift Chris@0: text = obj.send(attr).to_s Chris@0: else Chris@0: raise ArgumentError, 'invalid arguments to textilizable' Chris@0: end Chris@0: return '' if text.blank? Chris@0: project = options[:project] || @project || (obj && obj.respond_to?(:project) ? obj.project : nil) Chris@0: only_path = options.delete(:only_path) == false ? false : true Chris@0: Chris@0: text = Redmine::WikiFormatting.to_html(Setting.text_formatting, text, :object => obj, :attribute => attr) { |macro, args| exec_macro(macro, obj, args) } Chris@0: Chris@0: parse_non_pre_blocks(text) do |text| chris@37: [:parse_inline_attachments, :parse_wiki_links, :parse_redmine_links, :parse_headings].each do |method_name| Chris@0: send method_name, text, project, obj, attr, only_path, options Chris@0: end Chris@0: end Chris@0: end Chris@0: Chris@0: def parse_non_pre_blocks(text) Chris@0: s = StringScanner.new(text) Chris@0: tags = [] Chris@0: parsed = '' Chris@0: while !s.eos? Chris@0: s.scan(/(.*?)(<(\/)?(pre|code)(.*?)>|\z)/im) Chris@0: text, full_tag, closing, tag = s[1], s[2], s[3], s[4] Chris@0: if tags.empty? Chris@0: yield text Chris@0: end Chris@0: parsed << text Chris@0: if tag Chris@0: if closing Chris@0: if tags.last == tag.downcase Chris@0: tags.pop Chris@0: end Chris@0: else Chris@0: tags << tag.downcase Chris@0: end Chris@0: parsed << full_tag Chris@0: end Chris@0: end Chris@0: # Close any non closing tags Chris@0: while tag = tags.pop Chris@0: parsed << "" Chris@0: end Chris@0: parsed Chris@0: end Chris@0: Chris@0: def parse_inline_attachments(text, project, obj, attr, only_path, options) Chris@0: # when using an image link, try to use an attachment, if possible Chris@0: if options[:attachments] || (obj && obj.respond_to?(:attachments)) Chris@0: attachments = nil Chris@0: text.gsub!(/src="([^\/"]+\.(bmp|gif|jpg|jpeg|png))"(\s+alt="([^"]*)")?/i) do |m| Chris@0: filename, ext, alt, alttext = $1.downcase, $2, $3, $4 Chris@0: attachments ||= (options[:attachments] || obj.attachments).sort_by(&:created_on).reverse Chris@0: # search for the picture in attachments Chris@0: if found = attachments.detect { |att| att.filename.downcase == filename } Chris@0: image_url = url_for :only_path => only_path, :controller => 'attachments', :action => 'download', :id => found Chris@0: desc = found.description.to_s.gsub('"', '') Chris@0: if !desc.blank? && alttext.blank? Chris@0: alt = " title=\"#{desc}\" alt=\"#{desc}\"" Chris@0: end Chris@0: "src=\"#{image_url}\"#{alt}" Chris@0: else Chris@0: m Chris@0: end Chris@0: end Chris@0: end Chris@0: end Chris@0: Chris@0: # Wiki links Chris@0: # Chris@0: # Examples: Chris@0: # [[mypage]] Chris@0: # [[mypage|mytext]] Chris@0: # wiki links can refer other project wikis, using project name or identifier: Chris@0: # [[project:]] -> wiki starting page Chris@0: # [[project:|mytext]] Chris@0: # [[project:mypage]] Chris@0: # [[project:mypage|mytext]] Chris@0: def parse_wiki_links(text, project, obj, attr, only_path, options) Chris@0: text.gsub!(/(!)?(\[\[([^\]\n\|]+)(\|([^\]\n\|]+))?\]\])/) do |m| Chris@0: link_project = project Chris@0: esc, all, page, title = $1, $2, $3, $5 Chris@0: if esc.nil? Chris@0: if page =~ /^([^\:]+)\:(.*)$/ chris@37: link_project = Project.find_by_identifier($1) || Project.find_by_name($1) Chris@0: page = $2 Chris@0: title ||= $1 if page.blank? Chris@0: end Chris@0: Chris@0: if link_project && link_project.wiki Chris@0: # extract anchor Chris@0: anchor = nil Chris@0: if page =~ /^(.+?)\#(.+)$/ Chris@0: page, anchor = $1, $2 Chris@0: end Chris@0: # check if page exists Chris@0: wiki_page = link_project.wiki.find_page(page) Chris@0: url = case options[:wiki_links] Chris@0: when :local; "#{title}.html" Chris@0: when :anchor; "##{title}" # used for single-file wiki export Chris@0: else chris@37: wiki_page_id = page.present? ? Wiki.titleize(page) : nil chris@37: url_for(:only_path => only_path, :controller => 'wiki', :action => 'show', :project_id => link_project, :id => wiki_page_id, :anchor => anchor) Chris@0: end Chris@0: link_to((title || page), url, :class => ('wiki-page' + (wiki_page ? '' : ' new'))) Chris@0: else Chris@0: # project or wiki doesn't exist Chris@0: all Chris@0: end Chris@0: else Chris@0: all Chris@0: end Chris@0: end Chris@0: end Chris@0: Chris@0: # Redmine links Chris@0: # Chris@0: # Examples: Chris@0: # Issues: Chris@0: # #52 -> Link to issue #52 Chris@0: # Changesets: Chris@0: # r52 -> Link to revision 52 Chris@0: # commit:a85130f -> Link to scmid starting with a85130f Chris@0: # Documents: Chris@0: # document#17 -> Link to document with id 17 Chris@0: # document:Greetings -> Link to the document with title "Greetings" Chris@0: # document:"Some document" -> Link to the document with title "Some document" Chris@0: # Versions: Chris@0: # version#3 -> Link to version with id 3 Chris@0: # version:1.0.0 -> Link to version named "1.0.0" Chris@0: # version:"1.0 beta 2" -> Link to version named "1.0 beta 2" Chris@0: # Attachments: Chris@0: # attachment:file.zip -> Link to the attachment of the current object named file.zip Chris@0: # Source files: Chris@0: # source:some/file -> Link to the file located at /some/file in the project's repository Chris@0: # source:some/file@52 -> Link to the file's revision 52 Chris@0: # source:some/file#L120 -> Link to line 120 of the file Chris@0: # source:some/file@52#L120 -> Link to line 120 of the file's revision 52 Chris@0: # export:some/file -> Force the download of the file Chris@0: # Forum messages: Chris@0: # message#1218 -> Link to message with id 1218 Chris@0: def parse_redmine_links(text, project, obj, attr, only_path, options) Chris@0: text.gsub!(%r{([\s\(,\-\[\>]|^)(!)?(attachment|document|version|commit|source|export|message|project)?((#|r)(\d+)|(:)([^"\s<>][^\s<>]*?|"[^"]+?"))(?=(?=[[:punct:]]\W)|,|\s|\]|<|$)}) do |m| Chris@0: leading, esc, prefix, sep, identifier = $1, $2, $3, $5 || $7, $6 || $8 Chris@0: link = nil Chris@0: if esc.nil? Chris@0: if prefix.nil? && sep == 'r' Chris@0: if project && (changeset = project.changesets.find_by_revision(identifier)) Chris@0: link = link_to("r#{identifier}", {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :rev => changeset.revision}, Chris@0: :class => 'changeset', Chris@0: :title => truncate_single_line(changeset.comments, :length => 100)) Chris@0: end Chris@0: elsif sep == '#' Chris@0: oid = identifier.to_i Chris@0: case prefix Chris@0: when nil Chris@0: if issue = Issue.visible.find_by_id(oid, :include => :status) Chris@0: link = link_to("##{oid}", {:only_path => only_path, :controller => 'issues', :action => 'show', :id => oid}, Chris@0: :class => issue.css_classes, Chris@0: :title => "#{truncate(issue.subject, :length => 100)} (#{issue.status.name})") Chris@0: end Chris@0: when 'document' Chris@0: if document = Document.find_by_id(oid, :include => [:project], :conditions => Project.visible_by(User.current)) Chris@0: link = link_to h(document.title), {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document}, Chris@0: :class => 'document' Chris@0: end Chris@0: when 'version' Chris@0: if version = Version.find_by_id(oid, :include => [:project], :conditions => Project.visible_by(User.current)) Chris@0: link = link_to h(version.name), {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version}, Chris@0: :class => 'version' Chris@0: end Chris@0: when 'message' Chris@0: if message = Message.find_by_id(oid, :include => [:parent, {:board => :project}], :conditions => Project.visible_by(User.current)) Chris@0: link = link_to h(truncate(message.subject, :length => 60)), {:only_path => only_path, Chris@0: :controller => 'messages', Chris@0: :action => 'show', Chris@0: :board_id => message.board, Chris@0: :id => message.root, Chris@0: :anchor => (message.parent ? "message-#{message.id}" : nil)}, Chris@0: :class => 'message' Chris@0: end Chris@0: when 'project' Chris@0: if p = Project.visible.find_by_id(oid) Chris@14: link = link_to_project(p, {:only_path => only_path}, :class => 'project') Chris@0: end Chris@0: end Chris@0: elsif sep == ':' Chris@0: # removes the double quotes if any Chris@0: name = identifier.gsub(%r{^"(.*)"$}, "\\1") Chris@0: case prefix Chris@0: when 'document' Chris@0: if project && document = project.documents.find_by_title(name) Chris@0: link = link_to h(document.title), {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document}, Chris@0: :class => 'document' Chris@0: end Chris@0: when 'version' Chris@0: if project && version = project.versions.find_by_name(name) Chris@0: link = link_to h(version.name), {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version}, Chris@0: :class => 'version' Chris@0: end Chris@0: when 'commit' Chris@0: if project && (changeset = project.changesets.find(:first, :conditions => ["scmid LIKE ?", "#{name}%"])) Chris@3: link = link_to h("#{name}"), {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :rev => changeset.identifier}, Chris@0: :class => 'changeset', Chris@0: :title => truncate_single_line(changeset.comments, :length => 100) Chris@0: end Chris@0: when 'source', 'export' Chris@0: if project && project.repository Chris@0: name =~ %r{^[/\\]*(.*?)(@([0-9a-f]+))?(#(L\d+))?$} Chris@0: path, rev, anchor = $1, $3, $5 Chris@0: link = link_to h("#{prefix}:#{name}"), {:controller => 'repositories', :action => 'entry', :id => project, Chris@0: :path => to_path_param(path), Chris@0: :rev => rev, Chris@0: :anchor => anchor, Chris@0: :format => (prefix == 'export' ? 'raw' : nil)}, Chris@0: :class => (prefix == 'export' ? 'source download' : 'source') Chris@0: end Chris@0: when 'attachment' Chris@0: attachments = options[:attachments] || (obj && obj.respond_to?(:attachments) ? obj.attachments : nil) Chris@0: if attachments && attachment = attachments.detect {|a| a.filename == name } Chris@0: link = link_to h(attachment.filename), {:only_path => only_path, :controller => 'attachments', :action => 'download', :id => attachment}, Chris@0: :class => 'attachment' Chris@0: end Chris@0: when 'project' Chris@0: if p = Project.visible.find(:first, :conditions => ["identifier = :s OR LOWER(name) = :s", {:s => name.downcase}]) Chris@14: link = link_to_project(p, {:only_path => only_path}, :class => 'project') Chris@0: end Chris@0: end Chris@0: end Chris@0: end Chris@0: leading + (link || "#{prefix}#{sep}#{identifier}") Chris@0: end Chris@0: end chris@37: chris@37: TOC_RE = /

    \{\{([<>]?)toc\}\}<\/p>/i unless const_defined?(:TOC_RE) chris@37: HEADING_RE = /]+)?>(.+?)<\/h(1|2|3|4)>/i unless const_defined?(:HEADING_RE) chris@37: chris@37: # Headings and TOC chris@37: # Adds ids and links to headings and renders the TOC if needed unless options[:headings] is set to false chris@37: def parse_headings(text, project, obj, attr, only_path, options) chris@37: headings = [] chris@37: text.gsub!(HEADING_RE) do chris@37: level, attrs, content = $1.to_i, $2, $3 chris@37: item = strip_tags(content).strip chris@37: anchor = item.gsub(%r{[^\w\s\-]}, '').gsub(%r{\s+(\-+\s*)?}, '-') chris@37: headings << [level, anchor, item] chris@37: "#{content}" chris@37: end unless options[:headings] == false chris@37: chris@37: text.gsub!(TOC_RE) do chris@37: if headings.empty? chris@37: '' chris@37: else chris@37: div_class = 'toc' chris@37: div_class << ' right' if $1 == '>' chris@37: div_class << ' left' if $1 == '<' chris@37: out = "

    ' * (current - root) chris@37: out << '' chris@37: end chris@37: end chris@37: end Chris@0: Chris@0: # Same as Rails' simple_format helper without using paragraphs Chris@0: def simple_format_without_paragraph(text) Chris@0: text.to_s. Chris@0: gsub(/\r\n?/, "\n"). # \r\n and \r -> \n Chris@0: gsub(/\n\n+/, "

    "). # 2+ newline -> 2 br Chris@0: gsub(/([^\n]\n)(?=[^\n])/, '\1
    ') # 1 newline -> br Chris@0: end Chris@0: Chris@0: def lang_options_for_select(blank=true) Chris@0: (blank ? [["(auto)", ""]] : []) + Chris@0: valid_languages.collect{|lang| [ ll(lang.to_s, :general_lang_name), lang.to_s]}.sort{|x,y| x.last <=> y.last } Chris@0: end Chris@0: Chris@0: def label_tag_for(name, option_tags = nil, options = {}) Chris@0: label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "") Chris@0: content_tag("label", label_text) Chris@0: end Chris@0: Chris@0: def labelled_tabular_form_for(name, object, options, &proc) Chris@0: options[:html] ||= {} Chris@0: options[:html][:class] = 'tabular' unless options[:html].has_key?(:class) Chris@0: form_for(name, object, options.merge({ :builder => TabularFormBuilder, :lang => current_language}), &proc) Chris@0: end Chris@0: Chris@0: def back_url_hidden_field_tag Chris@0: back_url = params[:back_url] || request.env['HTTP_REFERER'] Chris@0: back_url = CGI.unescape(back_url.to_s) Chris@0: hidden_field_tag('back_url', CGI.escape(back_url)) unless back_url.blank? Chris@0: end Chris@0: Chris@0: def check_all_links(form_name) Chris@0: link_to_function(l(:button_check_all), "checkAll('#{form_name}', true)") + Chris@0: " | " + Chris@0: link_to_function(l(:button_uncheck_all), "checkAll('#{form_name}', false)") Chris@0: end Chris@0: Chris@0: def progress_bar(pcts, options={}) Chris@0: pcts = [pcts, pcts] unless pcts.is_a?(Array) Chris@0: pcts = pcts.collect(&:round) Chris@0: pcts[1] = pcts[1] - pcts[0] Chris@0: pcts << (100 - pcts[1] - pcts[0]) Chris@0: width = options[:width] || '100px;' Chris@0: legend = options[:legend] || '' Chris@0: content_tag('table', Chris@0: content_tag('tr', Chris@0: (pcts[0] > 0 ? content_tag('td', '', :style => "width: #{pcts[0]}%;", :class => 'closed') : '') + Chris@0: (pcts[1] > 0 ? content_tag('td', '', :style => "width: #{pcts[1]}%;", :class => 'done') : '') + Chris@0: (pcts[2] > 0 ? content_tag('td', '', :style => "width: #{pcts[2]}%;", :class => 'todo') : '') Chris@0: ), :class => 'progress', :style => "width: #{width};") + Chris@0: content_tag('p', legend, :class => 'pourcent') Chris@0: end Chris@0: Chris@0: def checked_image(checked=true) Chris@0: if checked Chris@0: image_tag 'toggle_check.png' Chris@0: end Chris@0: end Chris@0: Chris@0: def context_menu(url) Chris@0: unless @context_menu_included Chris@0: content_for :header_tags do Chris@0: javascript_include_tag('context_menu') + Chris@0: stylesheet_link_tag('context_menu') Chris@0: end Chris@14: if l(:direction) == 'rtl' Chris@14: content_for :header_tags do Chris@14: stylesheet_link_tag('context_menu_rtl') Chris@14: end Chris@14: end Chris@0: @context_menu_included = true Chris@0: end Chris@0: javascript_tag "new ContextMenu('#{ url_for(url) }')" Chris@0: end Chris@0: Chris@0: def context_menu_link(name, url, options={}) Chris@0: options[:class] ||= '' Chris@0: if options.delete(:selected) Chris@0: options[:class] << ' icon-checked disabled' Chris@0: options[:disabled] = true Chris@0: end Chris@0: if options.delete(:disabled) Chris@0: options.delete(:method) Chris@0: options.delete(:confirm) Chris@0: options.delete(:onclick) Chris@0: options[:class] << ' disabled' Chris@0: url = '#' Chris@0: end Chris@0: link_to name, url, options Chris@0: end Chris@0: Chris@0: def calendar_for(field_id) Chris@0: include_calendar_headers_tags Chris@0: image_tag("calendar.png", {:id => "#{field_id}_trigger",:class => "calendar-trigger"}) + Chris@0: javascript_tag("Calendar.setup({inputField : '#{field_id}', ifFormat : '%Y-%m-%d', button : '#{field_id}_trigger' });") Chris@0: end Chris@0: Chris@0: def include_calendar_headers_tags Chris@0: unless @calendar_headers_tags_included Chris@0: @calendar_headers_tags_included = true Chris@0: content_for :header_tags do Chris@0: start_of_week = case Setting.start_of_week.to_i Chris@0: when 1 Chris@0: 'Calendar._FD = 1;' # Monday Chris@0: when 7 Chris@0: 'Calendar._FD = 0;' # Sunday Chris@0: else Chris@0: '' # use language Chris@0: end Chris@0: Chris@0: javascript_include_tag('calendar/calendar') + Chris@0: javascript_include_tag("calendar/lang/calendar-#{current_language.to_s.downcase}.js") + Chris@0: javascript_tag(start_of_week) + Chris@0: javascript_include_tag('calendar/calendar-setup') + Chris@0: stylesheet_link_tag('calendar') Chris@0: end Chris@0: end Chris@0: end Chris@0: Chris@0: def content_for(name, content = nil, &block) Chris@0: @has_content ||= {} Chris@0: @has_content[name] = true Chris@0: super(name, content, &block) Chris@0: end Chris@0: Chris@0: def has_content?(name) Chris@0: (@has_content && @has_content[name]) || false Chris@0: end Chris@0: Chris@0: # Returns the avatar image tag for the given +user+ if avatars are enabled Chris@0: # +user+ can be a User or a string that will be scanned for an email address (eg. 'joe ') Chris@0: def avatar(user, options = { }) Chris@0: if Setting.gravatar_enabled? chris@22: options.merge!({:ssl => (defined?(request) && request.ssl?), :default => Setting.gravatar_default}) Chris@0: email = nil Chris@0: if user.respond_to?(:mail) Chris@0: email = user.mail Chris@0: elsif user.to_s =~ %r{<(.+?)>} Chris@0: email = $1 Chris@0: end Chris@0: return gravatar(email.to_s.downcase, options) unless email.blank? rescue nil chris@22: else chris@22: '' Chris@0: end Chris@0: end Chris@0: Chris@14: def favicon Chris@14: "" Chris@14: end Chris@14: Chris@0: private Chris@0: Chris@0: def wiki_helper Chris@0: helper = Redmine::WikiFormatting.helper_for(Setting.text_formatting) Chris@0: extend helper Chris@0: return self Chris@0: end Chris@0: Chris@0: def link_to_remote_content_update(text, url_params) Chris@0: link_to_remote(text, Chris@0: {:url => url_params, :method => :get, :update => 'content', :complete => 'window.scrollTo(0,0)'}, Chris@0: {:href => url_for(:params => url_params)} Chris@0: ) Chris@0: end Chris@0: Chris@0: end