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 << "\n" Chris@14: end Chris@14: content Chris@14: end Chris@14: Chris@14: # Renders flash messages Chris@14: def render_flash_messages Chris@14: s = '' Chris@14: flash.each do |k,v| Chris@14: s << content_tag('div', v, :class => "flash #{k}") Chris@14: end Chris@14: s Chris@14: end Chris@14: Chris@14: # Renders tabs and their content Chris@14: def render_tabs(tabs) Chris@14: if tabs.any? Chris@14: render :partial => 'common/tabs', :locals => {:tabs => tabs} Chris@14: else Chris@14: content_tag 'p', l(:label_no_data), :class => "nodata" Chris@14: end Chris@14: end Chris@14: Chris@14: # Renders the project quick-jump box Chris@14: def render_project_jump_box Chris@14: # Retrieve them now to avoid a COUNT query Chris@14: projects = User.current.projects.all Chris@14: if projects.any? Chris@14: s = '' Chris@14: s Chris@14: end Chris@14: end Chris@14: Chris@14: def project_tree_options_for_select(projects, options = {}) Chris@14: s = '' Chris@14: project_tree(projects) do |project, level| Chris@14: name_prefix = (level > 0 ? (' ' * 2 * level + '» ') : '') Chris@14: tag_options = {:value => project.id} Chris@14: if project == options[:selected] || (options[:selected].respond_to?(:include?) && options[:selected].include?(project)) Chris@14: tag_options[:selected] = 'selected' Chris@14: else Chris@14: tag_options[:selected] = nil Chris@14: end Chris@14: tag_options.merge!(yield(project)) if block_given? Chris@14: s << content_tag('option', name_prefix + h(project), tag_options) Chris@14: end Chris@14: s Chris@14: end Chris@14: Chris@14: # Yields the given block for each project with its level in the tree Chris@14: def project_tree(projects, &block) Chris@14: ancestors = [] Chris@14: projects.sort_by(&:lft).each do |project| Chris@14: while (ancestors.any? && !project.is_descendant_of?(ancestors.last)) Chris@14: ancestors.pop Chris@14: end Chris@14: yield project, ancestors.size Chris@14: ancestors << project Chris@14: end Chris@14: end Chris@14: Chris@14: def project_nested_ul(projects, &block) Chris@14: s = '' Chris@14: if projects.any? Chris@14: ancestors = [] Chris@14: projects.sort_by(&:lft).each do |project| Chris@14: if (ancestors.empty? || project.is_descendant_of?(ancestors.last)) Chris@14: s << "\n" Chris@14: end Chris@14: end Chris@14: s << "
  • " Chris@14: s << yield(project).to_s Chris@14: ancestors << project Chris@14: end Chris@14: s << ("
  • \n" * ancestors.size) Chris@14: end Chris@14: s Chris@14: end Chris@14: Chris@14: def principals_check_box_tags(name, principals) Chris@14: s = '' Chris@14: principals.sort.each do |principal| Chris@14: s << "\n" Chris@14: end Chris@14: s Chris@14: end Chris@14: Chris@14: # Truncates and returns the string as a single line Chris@14: def truncate_single_line(string, *args) Chris@14: truncate(string.to_s, *args).gsub(%r{[\r\n]+}m, ' ') Chris@14: end Chris@14: Chris@14: # Truncates at line break after 250 characters or options[:length] Chris@14: def truncate_lines(string, options={}) Chris@14: length = options[:length] || 250 Chris@14: if string.to_s =~ /\A(.{#{length}}.*?)$/m Chris@14: "#{$1}..." Chris@14: else Chris@14: string Chris@14: end Chris@14: end Chris@14: Chris@14: def html_hours(text) Chris@14: text.gsub(%r{(\d+)\.(\d+)}, '\1.\2') Chris@14: end Chris@14: Chris@14: def authoring(created, author, options={}) Chris@14: l(options[:label] || :label_added_time_by, :author => link_to_user(author), :age => time_tag(created)) Chris@14: end Chris@14: Chris@14: def time_tag(time) Chris@14: text = distance_of_time_in_words(Time.now, time) Chris@14: if @project Chris@14: link_to(text, {:controller => 'projects', :action => 'activity', :id => @project, :from => time.to_date}, :title => format_time(time)) Chris@14: else Chris@14: content_tag('acronym', text, :title => format_time(time)) Chris@14: end Chris@14: end Chris@14: Chris@14: def syntax_highlight(name, content) Chris@14: Redmine::SyntaxHighlighting.highlight_by_filename(content, name) Chris@14: end Chris@14: Chris@14: def to_path_param(path) Chris@14: path.to_s.split(%r{[/\\]}).select {|p| !p.blank?} Chris@14: end Chris@14: Chris@14: def pagination_links_full(paginator, count=nil, options={}) Chris@14: page_param = options.delete(:page_param) || :page Chris@14: per_page_links = options.delete(:per_page_links) Chris@14: url_param = params.dup Chris@14: # don't reuse query params if filters are present Chris@14: url_param.merge!(:fields => nil, :values => nil, :operators => nil) if url_param.delete(:set_filter) Chris@14: Chris@14: html = '' Chris@14: if paginator.current.previous Chris@14: html << link_to_remote_content_update('« ' + l(:label_previous), url_param.merge(page_param => paginator.current.previous)) + ' ' Chris@14: end Chris@14: Chris@14: html << (pagination_links_each(paginator, options) do |n| Chris@14: link_to_remote_content_update(n.to_s, url_param.merge(page_param => n)) Chris@14: end || '') Chris@14: Chris@14: if paginator.current.next Chris@14: html << ' ' + link_to_remote_content_update((l(:label_next) + ' »'), url_param.merge(page_param => paginator.current.next)) Chris@14: end Chris@14: Chris@14: unless count.nil? Chris@14: html << " (#{paginator.current.first_item}-#{paginator.current.last_item}/#{count})" Chris@14: if per_page_links != false && links = per_page_links(paginator.items_per_page) Chris@14: html << " | #{links}" Chris@14: end Chris@14: end Chris@14: Chris@14: html Chris@14: end Chris@14: Chris@14: def per_page_links(selected=nil) Chris@14: url_param = params.dup Chris@14: url_param.clear if url_param.has_key?(:set_filter) Chris@14: Chris@14: links = Setting.per_page_options_array.collect do |n| Chris@14: n == selected ? n : link_to_remote(n, {:update => "content", Chris@14: :url => params.dup.merge(:per_page => n), Chris@14: :method => :get}, Chris@14: {:href => url_for(url_param.merge(:per_page => n))}) Chris@14: end Chris@14: links.size > 1 ? l(:label_display_per_page, links.join(', ')) : nil Chris@14: end Chris@14: Chris@14: def reorder_links(name, url) Chris@14: 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@14: 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@14: 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@14: 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@14: end Chris@14: Chris@14: def breadcrumb(*args) Chris@14: elements = args.flatten Chris@14: elements.any? ? content_tag('p', args.join(' » ') + ' » ', :class => 'breadcrumb') : nil Chris@14: end Chris@14: Chris@14: def other_formats_links(&block) Chris@14: concat('

    ' + 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 << "" 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+/, "

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