Chris@14: # redMine - project management software
Chris@14: # Copyright (C) 2006-2007 Jean-Philippe Lang
Chris@14: #
Chris@14: # This program is free software; you can redistribute it and/or
Chris@14: # modify it under the terms of the GNU General Public License
Chris@14: # as published by the Free Software Foundation; either version 2
Chris@14: # of the License, or (at your option) any later version.
Chris@14: #
Chris@14: # This program is distributed in the hope that it will be useful,
Chris@14: # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@14: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@14: # GNU General Public License for more details.
Chris@14: #
Chris@14: # You should have received a copy of the GNU General Public License
Chris@14: # along with this program; if not, write to the Free Software
Chris@14: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@14:
Chris@14: require 'forwardable'
Chris@14: require 'cgi'
Chris@14:
Chris@14: module ApplicationHelper
Chris@14: include Redmine::WikiFormatting::Macros::Definitions
Chris@14: include Redmine::I18n
Chris@14: include GravatarHelper::PublicMethods
Chris@14:
Chris@14: extend Forwardable
Chris@14: def_delegators :wiki_helper, :wikitoolbar_for, :heads_for_wiki_formatter
Chris@14:
Chris@14: # Return true if user is authorized for controller/action, otherwise false
Chris@14: def authorize_for(controller, action)
Chris@14: User.current.allowed_to?({:controller => controller, :action => action}, @project)
Chris@14: end
Chris@14:
Chris@14: # Display a link if user is authorized
Chris@14: def link_to_if_authorized(name, options = {}, html_options = nil, *parameters_for_method_reference)
Chris@14: link_to(name, options, html_options, *parameters_for_method_reference) if authorize_for(options[:controller] || params[:controller], options[:action])
Chris@14: end
Chris@14:
Chris@14: # Display a link to remote if user is authorized
Chris@14: def link_to_remote_if_authorized(name, options = {}, html_options = nil)
Chris@14: url = options[:url] || {}
Chris@14: link_to_remote(name, options, html_options) if authorize_for(url[:controller] || params[:controller], url[:action])
Chris@14: end
Chris@14:
Chris@14: # Displays a link to user's account page if active
Chris@14: def link_to_user(user, options={})
Chris@14: if user.is_a?(User)
Chris@14: name = h(user.name(options[:format]))
Chris@14: if user.active?
Chris@14: link_to name, :controller => 'users', :action => 'show', :id => user
Chris@14: else
Chris@14: name
Chris@14: end
Chris@14: else
Chris@14: h(user.to_s)
Chris@14: end
Chris@14: end
Chris@14:
Chris@14: # Displays a link to +issue+ with its subject.
Chris@14: # Examples:
Chris@14: #
Chris@14: # link_to_issue(issue) # => Defect #6: This is the subject
Chris@14: # link_to_issue(issue, :truncate => 6) # => Defect #6: This i...
Chris@14: # link_to_issue(issue, :subject => false) # => Defect #6
Chris@14: # link_to_issue(issue, :project => true) # => Foo - Defect #6
Chris@14: #
Chris@14: def link_to_issue(issue, options={})
Chris@14: title = nil
Chris@14: subject = nil
Chris@14: if options[:subject] == false
Chris@14: title = truncate(issue.subject, :length => 60)
Chris@14: else
Chris@14: subject = issue.subject
Chris@14: if options[:truncate]
Chris@14: subject = truncate(subject, :length => options[:truncate])
Chris@14: end
Chris@14: end
Chris@14: s = link_to "#{issue.tracker} ##{issue.id}", {:controller => "issues", :action => "show", :id => issue},
Chris@14: :class => issue.css_classes,
Chris@14: :title => title
Chris@14: s << ": #{h subject}" if subject
Chris@14: s = "#{h issue.project} - " + s if options[:project]
Chris@14: s
Chris@14: end
Chris@14:
Chris@14: # Generates a link to an attachment.
Chris@14: # Options:
Chris@14: # * :text - Link text (default to attachment filename)
Chris@14: # * :download - Force download (default: false)
Chris@14: def link_to_attachment(attachment, options={})
Chris@14: text = options.delete(:text) || attachment.filename
Chris@14: action = options.delete(:download) ? 'download' : 'show'
Chris@14:
Chris@14: link_to(h(text), {:controller => 'attachments', :action => action, :id => attachment, :filename => attachment.filename }, options)
Chris@14: end
Chris@14:
Chris@14: # Generates a link to a SCM revision
Chris@14: # Options:
Chris@14: # * :text - Link text (default to the formatted revision)
Chris@14: def link_to_revision(revision, project, options={})
Chris@14: text = options.delete(:text) || format_revision(revision)
Chris@14:
Chris@14: link_to(text, {:controller => 'repositories', :action => 'revision', :id => project, :rev => revision}, :title => l(:label_revision_id, revision))
Chris@14: end
Chris@14:
Chris@14: def toggle_link(name, id, options={})
Chris@14: onclick = "Element.toggle('#{id}'); "
Chris@14: onclick << (options[:focus] ? "Form.Element.focus('#{options[:focus]}'); " : "this.blur(); ")
Chris@14: onclick << "return false;"
Chris@14: link_to(name, "#", :onclick => onclick)
Chris@14: end
Chris@14:
Chris@14: def image_to_function(name, function, html_options = {})
Chris@14: html_options.symbolize_keys!
Chris@14: tag(:input, html_options.merge({
Chris@14: :type => "image", :src => image_path(name),
Chris@14: :onclick => (html_options[:onclick] ? "#{html_options[:onclick]}; " : "") + "#{function};"
Chris@14: }))
Chris@14: end
Chris@14:
Chris@14: def prompt_to_remote(name, text, param, url, html_options = {})
Chris@14: html_options[:onclick] = "promptToRemote('#{text}', '#{param}', '#{url_for(url)}'); return false;"
Chris@14: link_to name, {}, html_options
Chris@14: end
Chris@14:
Chris@14: def format_activity_title(text)
Chris@14: h(truncate_single_line(text, :length => 100))
Chris@14: end
Chris@14:
Chris@14: def format_activity_day(date)
Chris@14: date == Date.today ? l(:label_today).titleize : format_date(date)
Chris@14: end
Chris@14:
Chris@14: def format_activity_description(text)
Chris@14: h(truncate(text.to_s, :length => 120).gsub(%r{[\r\n]*<(pre|code)>.*$}m, '...')).gsub(/[\r\n]+/, "
")
Chris@14: end
Chris@14:
Chris@14: def format_version_name(version)
Chris@14: if version.project == @project
Chris@14: h(version)
Chris@14: else
Chris@14: h("#{version.project} - #{version}")
Chris@14: end
Chris@14: end
Chris@14:
Chris@14: def due_date_distance_in_words(date)
Chris@14: if date
Chris@14: l((date < Date.today ? :label_roadmap_overdue : :label_roadmap_due_in), distance_of_date_in_words(Date.today, date))
Chris@14: end
Chris@14: end
Chris@14:
Chris@14: def render_page_hierarchy(pages, node=nil)
Chris@14: content = ''
Chris@14: if pages[node]
Chris@14: content << "
' + l(:label_export_to)) Chris@14: yield Redmine::Views::OtherFormatsBuilder.new(self) Chris@14: concat('
') Chris@14: end Chris@14: Chris@14: def page_header_title Chris@14: if @project.nil? || @project.new_record? Chris@14: h(Setting.app_title) Chris@14: else Chris@14: b = [] Chris@14: ancestors = (@project.root? ? [] : @project.ancestors.visible) Chris@14: if ancestors.any? Chris@14: root = ancestors.shift Chris@14: b << link_to(h(root), {:controller => 'projects', :action => 'show', :id => root, :jump => current_menu_item}, :class => 'root') Chris@14: if ancestors.size > 2 Chris@14: b << '…' Chris@14: ancestors = ancestors[-2, 2] Chris@14: end Chris@14: b += ancestors.collect {|p| link_to(h(p), {:controller => 'projects', :action => 'show', :id => p, :jump => current_menu_item}, :class => 'ancestor') } Chris@14: end Chris@14: b << h(@project) Chris@14: b.join(' » ') Chris@14: end Chris@14: end Chris@14: Chris@14: def html_title(*args) Chris@14: if args.empty? Chris@14: title = [] Chris@14: title << @project.name if @project Chris@14: title += @html_title if @html_title Chris@14: title << Setting.app_title Chris@14: title.select {|t| !t.blank? }.join(' - ') Chris@14: else Chris@14: @html_title ||= [] Chris@14: @html_title += args Chris@14: end Chris@14: end Chris@14: Chris@14: def accesskey(s) Chris@14: Redmine::AccessKeys.key_for s Chris@14: end Chris@14: Chris@14: # Formats text according to system settings. Chris@14: # 2 ways to call this method: Chris@14: # * with a String: textilizable(text, options) Chris@14: # * with an object and one of its attribute: textilizable(issue, :description, options) Chris@14: def textilizable(*args) Chris@14: options = args.last.is_a?(Hash) ? args.pop : {} Chris@14: case args.size Chris@14: when 1 Chris@14: obj = options[:object] Chris@14: text = args.shift Chris@14: when 2 Chris@14: obj = args.shift Chris@14: attr = args.shift Chris@14: text = obj.send(attr).to_s Chris@14: else Chris@14: raise ArgumentError, 'invalid arguments to textilizable' Chris@14: end Chris@14: return '' if text.blank? Chris@14: project = options[:project] || @project || (obj && obj.respond_to?(:project) ? obj.project : nil) Chris@14: only_path = options.delete(:only_path) == false ? false : true Chris@14: Chris@14: text = Redmine::WikiFormatting.to_html(Setting.text_formatting, text, :object => obj, :attribute => attr) { |macro, args| exec_macro(macro, obj, args) } Chris@14: Chris@14: parse_non_pre_blocks(text) do |text| Chris@14: [:parse_inline_attachments, :parse_wiki_links, :parse_redmine_links].each do |method_name| Chris@14: send method_name, text, project, obj, attr, only_path, options Chris@14: end Chris@14: end Chris@14: end Chris@14: Chris@14: def parse_non_pre_blocks(text) Chris@14: s = StringScanner.new(text) Chris@14: tags = [] Chris@14: parsed = '' Chris@14: while !s.eos? Chris@14: s.scan(/(.*?)(<(\/)?(pre|code)(.*?)>|\z)/im) Chris@14: text, full_tag, closing, tag = s[1], s[2], s[3], s[4] Chris@14: if tags.empty? Chris@14: yield text Chris@14: end Chris@14: parsed << text Chris@14: if tag Chris@14: if closing Chris@14: if tags.last == tag.downcase Chris@14: tags.pop Chris@14: end Chris@14: else Chris@14: tags << tag.downcase Chris@14: end Chris@14: parsed << full_tag Chris@14: end Chris@14: end Chris@14: # Close any non closing tags Chris@14: while tag = tags.pop Chris@14: parsed << "#{tag}>" Chris@14: end Chris@14: parsed Chris@14: end Chris@14: Chris@14: def parse_inline_attachments(text, project, obj, attr, only_path, options) Chris@14: # when using an image link, try to use an attachment, if possible Chris@14: if options[:attachments] || (obj && obj.respond_to?(:attachments)) Chris@14: attachments = nil Chris@14: text.gsub!(/src="([^\/"]+\.(bmp|gif|jpg|jpeg|png))"(\s+alt="([^"]*)")?/i) do |m| Chris@14: filename, ext, alt, alttext = $1.downcase, $2, $3, $4 Chris@14: attachments ||= (options[:attachments] || obj.attachments).sort_by(&:created_on).reverse Chris@14: # search for the picture in attachments Chris@14: if found = attachments.detect { |att| att.filename.downcase == filename } Chris@14: image_url = url_for :only_path => only_path, :controller => 'attachments', :action => 'download', :id => found Chris@14: desc = found.description.to_s.gsub('"', '') Chris@14: if !desc.blank? && alttext.blank? Chris@14: alt = " title=\"#{desc}\" alt=\"#{desc}\"" Chris@14: end Chris@14: "src=\"#{image_url}\"#{alt}" Chris@14: else Chris@14: m Chris@14: end Chris@14: end Chris@14: end Chris@14: end Chris@14: Chris@14: # Wiki links Chris@14: # Chris@14: # Examples: Chris@14: # [[mypage]] Chris@14: # [[mypage|mytext]] Chris@14: # wiki links can refer other project wikis, using project name or identifier: Chris@14: # [[project:]] -> wiki starting page Chris@14: # [[project:|mytext]] Chris@14: # [[project:mypage]] Chris@14: # [[project:mypage|mytext]] Chris@14: def parse_wiki_links(text, project, obj, attr, only_path, options) Chris@14: text.gsub!(/(!)?(\[\[([^\]\n\|]+)(\|([^\]\n\|]+))?\]\])/) do |m| Chris@14: link_project = project Chris@14: esc, all, page, title = $1, $2, $3, $5 Chris@14: if esc.nil? Chris@14: if page =~ /^([^\:]+)\:(.*)$/ Chris@14: link_project = Project.find_by_name($1) || Project.find_by_identifier($1) Chris@14: page = $2 Chris@14: title ||= $1 if page.blank? Chris@14: end Chris@14: Chris@14: if link_project && link_project.wiki Chris@14: # extract anchor Chris@14: anchor = nil Chris@14: if page =~ /^(.+?)\#(.+)$/ Chris@14: page, anchor = $1, $2 Chris@14: end Chris@14: # check if page exists Chris@14: wiki_page = link_project.wiki.find_page(page) Chris@14: url = case options[:wiki_links] Chris@14: when :local; "#{title}.html" Chris@14: when :anchor; "##{title}" # used for single-file wiki export Chris@14: else Chris@14: url_for(:only_path => only_path, :controller => 'wiki', :action => 'index', :id => link_project, :page => Wiki.titleize(page), :anchor => anchor) Chris@14: end Chris@14: link_to((title || page), url, :class => ('wiki-page' + (wiki_page ? '' : ' new'))) Chris@14: else Chris@14: # project or wiki doesn't exist Chris@14: all Chris@14: end Chris@14: else Chris@14: all Chris@14: end Chris@14: end Chris@14: end Chris@14: Chris@14: # Redmine links Chris@14: # Chris@14: # Examples: Chris@14: # Issues: Chris@14: # #52 -> Link to issue #52 Chris@14: # Changesets: Chris@14: # r52 -> Link to revision 52 Chris@14: # commit:a85130f -> Link to scmid starting with a85130f Chris@14: # Documents: Chris@14: # document#17 -> Link to document with id 17 Chris@14: # document:Greetings -> Link to the document with title "Greetings" Chris@14: # document:"Some document" -> Link to the document with title "Some document" Chris@14: # Versions: Chris@14: # version#3 -> Link to version with id 3 Chris@14: # version:1.0.0 -> Link to version named "1.0.0" Chris@14: # version:"1.0 beta 2" -> Link to version named "1.0 beta 2" Chris@14: # Attachments: Chris@14: # attachment:file.zip -> Link to the attachment of the current object named file.zip Chris@14: # Source files: Chris@14: # source:some/file -> Link to the file located at /some/file in the project's repository Chris@14: # source:some/file@52 -> Link to the file's revision 52 Chris@14: # source:some/file#L120 -> Link to line 120 of the file Chris@14: # source:some/file@52#L120 -> Link to line 120 of the file's revision 52 Chris@14: # export:some/file -> Force the download of the file Chris@14: # Forum messages: Chris@14: # message#1218 -> Link to message with id 1218 Chris@14: def parse_redmine_links(text, project, obj, attr, only_path, options) Chris@14: text.gsub!(%r{([\s\(,\-\[\>]|^)(!)?(attachment|document|version|commit|source|export|message|project)?((#|r)(\d+)|(:)([^"\s<>][^\s<>]*?|"[^"]+?"))(?=(?=[[:punct:]]\W)|,|\s|\]|<|$)}) do |m| Chris@14: leading, esc, prefix, sep, identifier = $1, $2, $3, $5 || $7, $6 || $8 Chris@14: link = nil Chris@14: if esc.nil? Chris@14: if prefix.nil? && sep == 'r' Chris@14: if project && (changeset = project.changesets.find_by_revision(identifier)) Chris@14: link = link_to("r#{identifier}", {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :rev => changeset.revision}, Chris@14: :class => 'changeset', Chris@14: :title => truncate_single_line(changeset.comments, :length => 100)) Chris@14: end Chris@14: elsif sep == '#' Chris@14: oid = identifier.to_i Chris@14: case prefix Chris@14: when nil Chris@14: if issue = Issue.visible.find_by_id(oid, :include => :status) Chris@14: link = link_to("##{oid}", {:only_path => only_path, :controller => 'issues', :action => 'show', :id => oid}, Chris@14: :class => issue.css_classes, Chris@14: :title => "#{truncate(issue.subject, :length => 100)} (#{issue.status.name})") Chris@14: end Chris@14: when 'document' Chris@14: if document = Document.find_by_id(oid, :include => [:project], :conditions => Project.visible_by(User.current)) Chris@14: link = link_to h(document.title), {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document}, Chris@14: :class => 'document' Chris@14: end Chris@14: when 'version' Chris@14: if version = Version.find_by_id(oid, :include => [:project], :conditions => Project.visible_by(User.current)) Chris@14: link = link_to h(version.name), {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version}, Chris@14: :class => 'version' Chris@14: end Chris@14: when 'message' Chris@14: if message = Message.find_by_id(oid, :include => [:parent, {:board => :project}], :conditions => Project.visible_by(User.current)) Chris@14: link = link_to h(truncate(message.subject, :length => 60)), {:only_path => only_path, Chris@14: :controller => 'messages', Chris@14: :action => 'show', Chris@14: :board_id => message.board, Chris@14: :id => message.root, Chris@14: :anchor => (message.parent ? "message-#{message.id}" : nil)}, Chris@14: :class => 'message' Chris@14: end Chris@14: when 'project' Chris@14: if p = Project.visible.find_by_id(oid) Chris@14: link = link_to h(p.name), {:only_path => only_path, :controller => 'projects', :action => 'show', :id => p}, Chris@14: :class => 'project' Chris@14: end Chris@14: end Chris@14: elsif sep == ':' Chris@14: # removes the double quotes if any Chris@14: name = identifier.gsub(%r{^"(.*)"$}, "\\1") Chris@14: case prefix Chris@14: when 'document' Chris@14: if project && document = project.documents.find_by_title(name) Chris@14: link = link_to h(document.title), {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document}, Chris@14: :class => 'document' Chris@14: end Chris@14: when 'version' Chris@14: if project && version = project.versions.find_by_name(name) Chris@14: link = link_to h(version.name), {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version}, Chris@14: :class => 'version' Chris@14: end Chris@14: when 'commit' Chris@14: if project && (changeset = project.changesets.find(:first, :conditions => ["scmid LIKE ?", "#{name}%"])) Chris@14: link = link_to h("#{name}"), {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :rev => changeset.revision}, Chris@14: :class => 'changeset', Chris@14: :title => truncate_single_line(changeset.comments, :length => 100) Chris@14: end Chris@14: when 'source', 'export' Chris@14: if project && project.repository Chris@14: name =~ %r{^[/\\]*(.*?)(@([0-9a-f]+))?(#(L\d+))?$} Chris@14: path, rev, anchor = $1, $3, $5 Chris@14: link = link_to h("#{prefix}:#{name}"), {:controller => 'repositories', :action => 'entry', :id => project, Chris@14: :path => to_path_param(path), Chris@14: :rev => rev, Chris@14: :anchor => anchor, Chris@14: :format => (prefix == 'export' ? 'raw' : nil)}, Chris@14: :class => (prefix == 'export' ? 'source download' : 'source') Chris@14: end Chris@14: when 'attachment' Chris@14: attachments = options[:attachments] || (obj && obj.respond_to?(:attachments) ? obj.attachments : nil) Chris@14: if attachments && attachment = attachments.detect {|a| a.filename == name } Chris@14: link = link_to h(attachment.filename), {:only_path => only_path, :controller => 'attachments', :action => 'download', :id => attachment}, Chris@14: :class => 'attachment' Chris@14: end Chris@14: when 'project' Chris@14: if p = Project.visible.find(:first, :conditions => ["identifier = :s OR LOWER(name) = :s", {:s => name.downcase}]) Chris@14: link = link_to h(p.name), {:only_path => only_path, :controller => 'projects', :action => 'show', :id => p}, Chris@14: :class => 'project' Chris@14: end Chris@14: end Chris@14: end Chris@14: end Chris@14: leading + (link || "#{prefix}#{sep}#{identifier}") Chris@14: end Chris@14: end Chris@14: Chris@14: # Same as Rails' simple_format helper without using paragraphs Chris@14: def simple_format_without_paragraph(text) Chris@14: text.to_s. Chris@14: gsub(/\r\n?/, "\n"). # \r\n and \r -> \n Chris@14: gsub(/\n\n+/, "