Chris@1295: # encoding: utf-8 Chris@1295: # Chris@1295: # Redmine - project management software Chris@1295: # Copyright (C) 2006-2012 Jean-Philippe Lang Chris@1295: # Chris@1295: # This program is free software; you can redistribute it and/or Chris@1295: # modify it under the terms of the GNU General Public License Chris@1295: # as published by the Free Software Foundation; either version 2 Chris@1295: # of the License, or (at your option) any later version. Chris@1295: # Chris@1295: # This program is distributed in the hope that it will be useful, Chris@1295: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1295: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1295: # GNU General Public License for more details. Chris@1295: # Chris@1295: # You should have received a copy of the GNU General Public License Chris@1295: # along with this program; if not, write to the Free Software Chris@1295: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1295: Chris@1295: require 'forwardable' Chris@1295: require 'cgi' Chris@1295: Chris@1295: module ApplicationHelper Chris@1295: include Redmine::WikiFormatting::Macros::Definitions Chris@1295: include Redmine::I18n Chris@1295: include GravatarHelper::PublicMethods Chris@1295: Chris@1295: extend Forwardable Chris@1295: def_delegators :wiki_helper, :wikitoolbar_for, :heads_for_wiki_formatter Chris@1295: Chris@1295: # Return true if user is authorized for controller/action, otherwise false Chris@1295: def authorize_for(controller, action) Chris@1295: User.current.allowed_to?({:controller => controller, :action => action}, @project) Chris@1295: end Chris@1295: Chris@1295: # Display a link if user is authorized Chris@1295: # Chris@1295: # @param [String] name Anchor text (passed to link_to) Chris@1295: # @param [Hash] options Hash params. This will checked by authorize_for to see if the user is authorized Chris@1295: # @param [optional, Hash] html_options Options passed to link_to Chris@1295: # @param [optional, Hash] parameters_for_method_reference Extra parameters for link_to Chris@1295: def link_to_if_authorized(name, options = {}, html_options = nil, *parameters_for_method_reference) Chris@1295: link_to(name, options, html_options, *parameters_for_method_reference) if authorize_for(options[:controller] || params[:controller], options[:action]) Chris@1295: end Chris@1295: Chris@1295: # Displays a link to user's account page if active Chris@1295: def link_to_user(user, options={}) Chris@1295: if user.is_a?(User) Chris@1295: name = h(user.name(options[:format])) Chris@1295: if user.active? || (User.current.admin? && user.logged?) Chris@1295: link_to name, user_path(user), :class => user.css_classes Chris@1295: else Chris@1295: name Chris@1295: end Chris@1295: else Chris@1295: h(user.to_s) Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: # Displays a link to +issue+ with its subject. Chris@1295: # Examples: Chris@1295: # Chris@1295: # link_to_issue(issue) # => Defect #6: This is the subject Chris@1295: # link_to_issue(issue, :truncate => 6) # => Defect #6: This i... Chris@1295: # link_to_issue(issue, :subject => false) # => Defect #6 Chris@1295: # link_to_issue(issue, :project => true) # => Foo - Defect #6 Chris@1295: # link_to_issue(issue, :subject => false, :tracker => false) # => #6 Chris@1295: # Chris@1295: def link_to_issue(issue, options={}) Chris@1295: title = nil Chris@1295: subject = nil Chris@1295: text = options[:tracker] == false ? "##{issue.id}" : "#{issue.tracker} ##{issue.id}" Chris@1295: if options[:subject] == false Chris@1295: title = truncate(issue.subject, :length => 60) Chris@1295: else Chris@1295: subject = issue.subject Chris@1295: if options[:truncate] Chris@1295: subject = truncate(subject, :length => options[:truncate]) Chris@1295: end Chris@1295: end Chris@1295: s = link_to text, issue_path(issue), :class => issue.css_classes, :title => title Chris@1295: s << h(": #{subject}") if subject Chris@1295: s = h("#{issue.project} - ") + s if options[:project] Chris@1295: s Chris@1295: end Chris@1295: Chris@1295: # Generates a link to an attachment. Chris@1295: # Options: Chris@1295: # * :text - Link text (default to attachment filename) Chris@1295: # * :download - Force download (default: false) Chris@1295: def link_to_attachment(attachment, options={}) Chris@1295: text = options.delete(:text) || attachment.filename Chris@1295: action = options.delete(:download) ? 'download' : 'show' Chris@1295: opt_only_path = {} Chris@1295: opt_only_path[:only_path] = (options[:only_path] == false ? false : true) Chris@1295: options.delete(:only_path) Chris@1295: link_to(h(text), Chris@1295: {:controller => 'attachments', :action => action, Chris@1295: :id => attachment, :filename => attachment.filename}.merge(opt_only_path), Chris@1295: options) Chris@1295: end Chris@1295: Chris@1295: # Generates a link to a SCM revision Chris@1295: # Options: Chris@1295: # * :text - Link text (default to the formatted revision) Chris@1295: def link_to_revision(revision, repository, options={}) Chris@1295: if repository.is_a?(Project) Chris@1295: repository = repository.repository Chris@1295: end Chris@1295: text = options.delete(:text) || format_revision(revision) Chris@1295: rev = revision.respond_to?(:identifier) ? revision.identifier : revision Chris@1295: link_to( Chris@1295: h(text), Chris@1295: {:controller => 'repositories', :action => 'revision', :id => repository.project, :repository_id => repository.identifier_param, :rev => rev}, Chris@1295: :title => l(:label_revision_id, format_revision(revision)) Chris@1295: ) Chris@1295: end Chris@1295: Chris@1295: # Generates a link to a message Chris@1295: def link_to_message(message, options={}, html_options = nil) Chris@1295: link_to( Chris@1295: h(truncate(message.subject, :length => 60)), Chris@1295: { :controller => 'messages', :action => 'show', Chris@1295: :board_id => message.board_id, Chris@1295: :id => (message.parent_id || message.id), Chris@1295: :r => (message.parent_id && message.id), Chris@1295: :anchor => (message.parent_id ? "message-#{message.id}" : nil) Chris@1295: }.merge(options), Chris@1295: html_options Chris@1295: ) Chris@1295: end Chris@1295: Chris@1295: # Generates a link to a project if active Chris@1295: # Examples: Chris@1295: # Chris@1295: # link_to_project(project) # => link to the specified project overview Chris@1295: # link_to_project(project, :action=>'settings') # => link to project settings Chris@1295: # link_to_project(project, {:only_path => false}, :class => "project") # => 3rd arg adds html options Chris@1295: # link_to_project(project, {}, :class => "project") # => html options with default url (project overview) Chris@1295: # Chris@1295: def link_to_project(project, options={}, html_options = nil) Chris@1295: if project.archived? Chris@1295: h(project) Chris@1295: else Chris@1295: url = {:controller => 'projects', :action => 'show', :id => project}.merge(options) Chris@1295: link_to(h(project), url, html_options) Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: def wiki_page_path(page, options={}) Chris@1295: url_for({:controller => 'wiki', :action => 'show', :project_id => page.project, :id => page.title}.merge(options)) Chris@1295: end Chris@1295: Chris@1295: def thumbnail_tag(attachment) Chris@1295: link_to image_tag(url_for(:controller => 'attachments', :action => 'thumbnail', :id => attachment)), Chris@1295: {:controller => 'attachments', :action => 'show', :id => attachment, :filename => attachment.filename}, Chris@1295: :title => attachment.filename Chris@1295: end Chris@1295: Chris@1295: def toggle_link(name, id, options={}) Chris@1295: onclick = "$('##{id}').toggle(); " Chris@1295: onclick << (options[:focus] ? "$('##{options[:focus]}').focus(); " : "this.blur(); ") Chris@1295: onclick << "return false;" Chris@1295: link_to(name, "#", :onclick => onclick) Chris@1295: end Chris@1295: Chris@1295: def image_to_function(name, function, html_options = {}) Chris@1295: html_options.symbolize_keys! Chris@1295: tag(:input, html_options.merge({ Chris@1295: :type => "image", :src => image_path(name), Chris@1295: :onclick => (html_options[:onclick] ? "#{html_options[:onclick]}; " : "") + "#{function};" Chris@1295: })) Chris@1295: end Chris@1295: Chris@1295: def format_activity_title(text) Chris@1295: h(truncate_single_line(text, :length => 100)) Chris@1295: end Chris@1295: Chris@1295: def format_activity_day(date) Chris@1295: date == User.current.today ? l(:label_today).titleize : format_date(date) Chris@1295: end Chris@1295: Chris@1295: def format_activity_description(text) Chris@1295: h(truncate(text.to_s, :length => 120).gsub(%r{[\r\n]*<(pre|code)>.*$}m, '...') Chris@1295: ).gsub(/[\r\n]+/, "
").html_safe Chris@1295: end Chris@1295: Chris@1295: def format_version_name(version) Chris@1295: if version.project == @project Chris@1295: h(version) Chris@1295: else Chris@1295: h("#{version.project} - #{version}") Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: def due_date_distance_in_words(date) Chris@1295: if date Chris@1295: l((date < Date.today ? :label_roadmap_overdue : :label_roadmap_due_in), distance_of_date_in_words(Date.today, date)) Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: # Renders a tree of projects as a nested set of unordered lists Chris@1295: # The given collection may be a subset of the whole project tree Chris@1295: # (eg. some intermediate nodes are private and can not be seen) Chris@1295: def render_project_nested_lists(projects) Chris@1295: s = '' Chris@1295: if projects.any? Chris@1295: ancestors = [] Chris@1295: original_project = @project Chris@1295: projects.sort_by(&:lft).each do |project| Chris@1295: # set the project environment to please macros. Chris@1295: @project = project Chris@1295: if (ancestors.empty? || project.is_descendant_of?(ancestors.last)) Chris@1295: s << "\n" Chris@1295: end Chris@1295: end Chris@1295: classes = (ancestors.empty? ? 'root' : 'child') Chris@1295: s << "
  • " Chris@1295: s << h(block_given? ? yield(project) : project.name) Chris@1295: s << "
    \n" Chris@1295: ancestors << project Chris@1295: end Chris@1295: s << ("
  • \n" * ancestors.size) Chris@1295: @project = original_project Chris@1295: end Chris@1295: s.html_safe Chris@1295: end Chris@1295: Chris@1295: def render_page_hierarchy(pages, node=nil, options={}) Chris@1295: content = '' Chris@1295: if pages[node] Chris@1295: content << "\n" Chris@1295: end Chris@1295: content.html_safe Chris@1295: end Chris@1295: Chris@1295: # Renders flash messages Chris@1295: def render_flash_messages Chris@1295: s = '' Chris@1295: flash.each do |k,v| Chris@1295: s << content_tag('div', v.html_safe, :class => "flash #{k}", :id => "flash_#{k}") Chris@1295: end Chris@1295: s.html_safe Chris@1295: end Chris@1295: Chris@1295: # Renders tabs and their content Chris@1295: def render_tabs(tabs) Chris@1295: if tabs.any? Chris@1295: render :partial => 'common/tabs', :locals => {:tabs => tabs} Chris@1295: else Chris@1295: content_tag 'p', l(:label_no_data), :class => "nodata" Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: # Renders the project quick-jump box Chris@1295: def render_project_jump_box Chris@1295: return unless User.current.logged? Chris@1295: projects = User.current.memberships.collect(&:project).compact.select(&:active?).uniq Chris@1295: if projects.any? Chris@1295: options = Chris@1295: ("" + Chris@1295: '').html_safe Chris@1295: Chris@1295: options << project_tree_options_for_select(projects, :selected => @project) do |p| Chris@1295: { :value => project_path(:id => p, :jump => current_menu_item) } Chris@1295: end Chris@1295: Chris@1295: select_tag('project_quick_jump_box', options, :onchange => 'if (this.value != \'\') { window.location = this.value; }') Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: def project_tree_options_for_select(projects, options = {}) Chris@1295: s = '' Chris@1295: project_tree(projects) do |project, level| Chris@1295: name_prefix = (level > 0 ? ' ' * 2 * level + '» ' : '').html_safe Chris@1295: tag_options = {:value => project.id} Chris@1295: if project == options[:selected] || (options[:selected].respond_to?(:include?) && options[:selected].include?(project)) Chris@1295: tag_options[:selected] = 'selected' Chris@1295: else Chris@1295: tag_options[:selected] = nil Chris@1295: end Chris@1295: tag_options.merge!(yield(project)) if block_given? Chris@1295: s << content_tag('option', name_prefix + h(project), tag_options) Chris@1295: end Chris@1295: s.html_safe Chris@1295: end Chris@1295: Chris@1295: # Yields the given block for each project with its level in the tree Chris@1295: # Chris@1295: # Wrapper for Project#project_tree Chris@1295: def project_tree(projects, &block) Chris@1295: Project.project_tree(projects, &block) Chris@1295: end Chris@1295: Chris@1295: def principals_check_box_tags(name, principals) Chris@1295: s = '' Chris@1295: principals.sort.each do |principal| Chris@1295: s << "\n" Chris@1295: end Chris@1295: s.html_safe Chris@1295: end Chris@1295: Chris@1295: # Returns a string for users/groups option tags Chris@1295: def principals_options_for_select(collection, selected=nil) Chris@1295: s = '' Chris@1295: if collection.include?(User.current) Chris@1295: s << content_tag('option', "<< #{l(:label_me)} >>", :value => User.current.id) Chris@1295: end Chris@1295: groups = '' Chris@1295: collection.sort.each do |element| Chris@1295: selected_attribute = ' selected="selected"' if option_value_selected?(element, selected) Chris@1295: (element.is_a?(Group) ? groups : s) << %() Chris@1295: end Chris@1295: unless groups.empty? Chris@1295: s << %(#{groups}) Chris@1295: end Chris@1295: s.html_safe Chris@1295: end Chris@1295: Chris@1295: # Options for the new membership projects combo-box Chris@1295: def options_for_membership_project_select(principal, projects) Chris@1295: options = content_tag('option', "--- #{l(:actionview_instancetag_blank_option)} ---") Chris@1295: options << project_tree_options_for_select(projects) do |p| Chris@1295: {:disabled => principal.projects.include?(p)} Chris@1295: end Chris@1295: options Chris@1295: end Chris@1295: Chris@1295: # Truncates and returns the string as a single line Chris@1295: def truncate_single_line(string, *args) Chris@1295: truncate(string.to_s, *args).gsub(%r{[\r\n]+}m, ' ') Chris@1295: end Chris@1295: Chris@1295: # Truncates at line break after 250 characters or options[:length] Chris@1295: def truncate_lines(string, options={}) Chris@1295: length = options[:length] || 250 Chris@1295: if string.to_s =~ /\A(.{#{length}}.*?)$/m Chris@1295: "#{$1}..." Chris@1295: else Chris@1295: string Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: def anchor(text) Chris@1295: text.to_s.gsub(' ', '_') Chris@1295: end Chris@1295: Chris@1295: def html_hours(text) Chris@1295: text.gsub(%r{(\d+)\.(\d+)}, '\1.\2').html_safe Chris@1295: end Chris@1295: Chris@1295: def authoring(created, author, options={}) Chris@1295: l(options[:label] || :label_added_time_by, :author => link_to_user(author), :age => time_tag(created)).html_safe Chris@1295: end Chris@1295: Chris@1295: def time_tag(time) Chris@1295: text = distance_of_time_in_words(Time.now, time) Chris@1295: if @project Chris@1295: link_to(text, {:controller => 'activities', :action => 'index', :id => @project, :from => User.current.time_to_date(time)}, :title => format_time(time)) Chris@1295: else Chris@1295: content_tag('acronym', text, :title => format_time(time)) Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: def syntax_highlight_lines(name, content) Chris@1295: lines = [] Chris@1295: syntax_highlight(name, content).each_line { |line| lines << line } Chris@1295: lines Chris@1295: end Chris@1295: Chris@1295: def syntax_highlight(name, content) Chris@1295: Redmine::SyntaxHighlighting.highlight_by_filename(content, name) Chris@1295: end Chris@1295: Chris@1295: def to_path_param(path) Chris@1295: str = path.to_s.split(%r{[/\\]}).select{|p| !p.blank?}.join("/") Chris@1295: str.blank? ? nil : str Chris@1295: end Chris@1295: Chris@1295: def pagination_links_full(paginator, count=nil, options={}) Chris@1295: page_param = options.delete(:page_param) || :page Chris@1295: per_page_links = options.delete(:per_page_links) Chris@1295: url_param = params.dup Chris@1295: Chris@1295: html = '' Chris@1295: if paginator.current.previous Chris@1295: # \xc2\xab(utf-8) = « Chris@1295: html << link_to_content_update( Chris@1295: "\xc2\xab " + l(:label_previous), Chris@1295: url_param.merge(page_param => paginator.current.previous)) + ' ' Chris@1295: end Chris@1295: Chris@1295: html << (pagination_links_each(paginator, options) do |n| Chris@1295: link_to_content_update(n.to_s, url_param.merge(page_param => n)) Chris@1295: end || '') Chris@1295: Chris@1295: if paginator.current.next Chris@1295: # \xc2\xbb(utf-8) = » Chris@1295: html << ' ' + link_to_content_update( Chris@1295: (l(:label_next) + " \xc2\xbb"), Chris@1295: url_param.merge(page_param => paginator.current.next)) Chris@1295: end Chris@1295: Chris@1295: unless count.nil? Chris@1295: html << " (#{paginator.current.first_item}-#{paginator.current.last_item}/#{count})" Chris@1295: if per_page_links != false && links = per_page_links(paginator.items_per_page, count) Chris@1295: html << " | #{links}" Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: html.html_safe Chris@1295: end Chris@1295: Chris@1295: def per_page_links(selected=nil, item_count=nil) Chris@1295: values = Setting.per_page_options_array Chris@1295: if item_count && values.any? Chris@1295: if item_count > values.first Chris@1295: max = values.detect {|value| value >= item_count} || item_count Chris@1295: else Chris@1295: max = item_count Chris@1295: end Chris@1295: values = values.select {|value| value <= max || value == selected} Chris@1295: end Chris@1295: if values.empty? || (values.size == 1 && values.first == selected) Chris@1295: return nil Chris@1295: end Chris@1295: links = values.collect do |n| Chris@1295: n == selected ? n : link_to_content_update(n, params.merge(:per_page => n)) Chris@1295: end Chris@1295: l(:label_display_per_page, links.join(', ')) Chris@1295: end Chris@1295: Chris@1295: def reorder_links(name, url, method = :post) Chris@1295: link_to(image_tag('2uparrow.png', :alt => l(:label_sort_highest)), Chris@1295: url.merge({"#{name}[move_to]" => 'highest'}), Chris@1295: :method => method, :title => l(:label_sort_highest)) + Chris@1295: link_to(image_tag('1uparrow.png', :alt => l(:label_sort_higher)), Chris@1295: url.merge({"#{name}[move_to]" => 'higher'}), Chris@1295: :method => method, :title => l(:label_sort_higher)) + Chris@1295: link_to(image_tag('1downarrow.png', :alt => l(:label_sort_lower)), Chris@1295: url.merge({"#{name}[move_to]" => 'lower'}), Chris@1295: :method => method, :title => l(:label_sort_lower)) + Chris@1295: link_to(image_tag('2downarrow.png', :alt => l(:label_sort_lowest)), Chris@1295: url.merge({"#{name}[move_to]" => 'lowest'}), Chris@1295: :method => method, :title => l(:label_sort_lowest)) Chris@1295: end Chris@1295: Chris@1295: def breadcrumb(*args) Chris@1295: elements = args.flatten Chris@1295: elements.any? ? content_tag('p', (args.join(" \xc2\xbb ") + " \xc2\xbb ").html_safe, :class => 'breadcrumb') : nil Chris@1295: end Chris@1295: Chris@1295: def other_formats_links(&block) Chris@1295: concat('

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

    '.html_safe) Chris@1295: end Chris@1295: Chris@1295: def page_header_title Chris@1295: if @project.nil? || @project.new_record? Chris@1295: h(Setting.app_title) Chris@1295: else Chris@1295: b = [] Chris@1295: ancestors = (@project.root? ? [] : @project.ancestors.visible.all) Chris@1295: if ancestors.any? Chris@1295: root = ancestors.shift Chris@1295: b << link_to_project(root, {:jump => current_menu_item}, :class => 'root') Chris@1295: if ancestors.size > 2 Chris@1295: b << "\xe2\x80\xa6" Chris@1295: ancestors = ancestors[-2, 2] Chris@1295: end Chris@1295: b += ancestors.collect {|p| link_to_project(p, {:jump => current_menu_item}, :class => 'ancestor') } Chris@1295: end Chris@1295: b << h(@project) Chris@1295: b.join(" \xc2\xbb ").html_safe Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: def html_title(*args) Chris@1295: if args.empty? Chris@1295: title = @html_title || [] Chris@1295: title << @project.name if @project Chris@1295: title << Setting.app_title unless Setting.app_title == title.last Chris@1295: title.select {|t| !t.blank? }.join(' - ') Chris@1295: else Chris@1295: @html_title ||= [] Chris@1295: @html_title += args Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: # Returns the theme, controller name, and action as css classes for the Chris@1295: # HTML body. Chris@1295: def body_css_classes Chris@1295: css = [] Chris@1295: if theme = Redmine::Themes.theme(Setting.ui_theme) Chris@1295: css << 'theme-' + theme.name Chris@1295: end Chris@1295: Chris@1295: css << 'controller-' + controller_name Chris@1295: css << 'action-' + action_name Chris@1295: css.join(' ') Chris@1295: end Chris@1295: Chris@1295: def accesskey(s) Chris@1295: Redmine::AccessKeys.key_for s Chris@1295: end Chris@1295: Chris@1295: # Formats text according to system settings. Chris@1295: # 2 ways to call this method: Chris@1295: # * with a String: textilizable(text, options) Chris@1295: # * with an object and one of its attribute: textilizable(issue, :description, options) Chris@1295: def textilizable(*args) Chris@1295: options = args.last.is_a?(Hash) ? args.pop : {} Chris@1295: case args.size Chris@1295: when 1 Chris@1295: obj = options[:object] Chris@1295: text = args.shift Chris@1295: when 2 Chris@1295: obj = args.shift Chris@1295: attr = args.shift Chris@1295: text = obj.send(attr).to_s Chris@1295: else Chris@1295: raise ArgumentError, 'invalid arguments to textilizable' Chris@1295: end Chris@1295: return '' if text.blank? Chris@1295: project = options[:project] || @project || (obj && obj.respond_to?(:project) ? obj.project : nil) Chris@1295: only_path = options.delete(:only_path) == false ? false : true Chris@1295: Chris@1295: text = text.dup Chris@1295: macros = catch_macros(text) Chris@1295: text = Redmine::WikiFormatting.to_html(Setting.text_formatting, text, :object => obj, :attribute => attr) Chris@1295: Chris@1295: @parsed_headings = [] Chris@1295: @heading_anchors = {} Chris@1295: @current_section = 0 if options[:edit_section_links] Chris@1295: Chris@1295: parse_sections(text, project, obj, attr, only_path, options) Chris@1295: text = parse_non_pre_blocks(text, obj, macros) do |text| Chris@1295: [:parse_inline_attachments, :parse_wiki_links, :parse_redmine_links].each do |method_name| Chris@1295: send method_name, text, project, obj, attr, only_path, options Chris@1295: end Chris@1295: end Chris@1295: parse_headings(text, project, obj, attr, only_path, options) Chris@1295: Chris@1295: if @parsed_headings.any? Chris@1295: replace_toc(text, @parsed_headings) Chris@1295: end Chris@1295: Chris@1295: text.html_safe Chris@1295: end Chris@1295: Chris@1295: def parse_non_pre_blocks(text, obj, macros) Chris@1295: s = StringScanner.new(text) Chris@1295: tags = [] Chris@1295: parsed = '' Chris@1295: while !s.eos? Chris@1295: s.scan(/(.*?)(<(\/)?(pre|code)(.*?)>|\z)/im) Chris@1295: text, full_tag, closing, tag = s[1], s[2], s[3], s[4] Chris@1295: if tags.empty? Chris@1295: yield text Chris@1295: inject_macros(text, obj, macros) if macros.any? Chris@1295: else Chris@1295: inject_macros(text, obj, macros, false) if macros.any? Chris@1295: end Chris@1295: parsed << text Chris@1295: if tag Chris@1295: if closing Chris@1295: if tags.last == tag.downcase Chris@1295: tags.pop Chris@1295: end Chris@1295: else Chris@1295: tags << tag.downcase Chris@1295: end Chris@1295: parsed << full_tag Chris@1295: end Chris@1295: end Chris@1295: # Close any non closing tags Chris@1295: while tag = tags.pop Chris@1295: parsed << "" Chris@1295: end Chris@1295: parsed Chris@1295: end Chris@1295: Chris@1295: def parse_inline_attachments(text, project, obj, attr, only_path, options) Chris@1295: # when using an image link, try to use an attachment, if possible Chris@1295: attachments = options[:attachments] || [] Chris@1295: attachments += obj.attachments if obj.respond_to?(:attachments) Chris@1295: if attachments.present? Chris@1295: text.gsub!(/src="([^\/"]+\.(bmp|gif|jpg|jpe|jpeg|png))"(\s+alt="([^"]*)")?/i) do |m| Chris@1295: filename, ext, alt, alttext = $1.downcase, $2, $3, $4 Chris@1295: # search for the picture in attachments Chris@1295: if found = Attachment.latest_attach(attachments, filename) Chris@1295: image_url = url_for :only_path => only_path, :controller => 'attachments', Chris@1295: :action => 'download', :id => found Chris@1295: desc = found.description.to_s.gsub('"', '') Chris@1295: if !desc.blank? && alttext.blank? Chris@1295: alt = " title=\"#{desc}\" alt=\"#{desc}\"" Chris@1295: end Chris@1295: "src=\"#{image_url}\"#{alt}" Chris@1295: else Chris@1295: m Chris@1295: end Chris@1295: end Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: # Wiki links Chris@1295: # Chris@1295: # Examples: Chris@1295: # [[mypage]] Chris@1295: # [[mypage|mytext]] Chris@1295: # wiki links can refer other project wikis, using project name or identifier: Chris@1295: # [[project:]] -> wiki starting page Chris@1295: # [[project:|mytext]] Chris@1295: # [[project:mypage]] Chris@1295: # [[project:mypage|mytext]] Chris@1295: def parse_wiki_links(text, project, obj, attr, only_path, options) Chris@1295: text.gsub!(/(!)?(\[\[([^\]\n\|]+)(\|([^\]\n\|]+))?\]\])/) do |m| Chris@1295: link_project = project Chris@1295: esc, all, page, title = $1, $2, $3, $5 Chris@1295: if esc.nil? Chris@1295: if page =~ /^([^\:]+)\:(.*)$/ Chris@1295: link_project = Project.find_by_identifier($1) || Project.find_by_name($1) Chris@1295: page = $2 Chris@1295: title ||= $1 if page.blank? Chris@1295: end Chris@1295: Chris@1295: if link_project && link_project.wiki Chris@1295: # extract anchor Chris@1295: anchor = nil Chris@1295: if page =~ /^(.+?)\#(.+)$/ Chris@1295: page, anchor = $1, $2 Chris@1295: end Chris@1295: anchor = sanitize_anchor_name(anchor) if anchor.present? Chris@1295: # check if page exists Chris@1295: wiki_page = link_project.wiki.find_page(page) Chris@1295: url = if anchor.present? && wiki_page.present? && (obj.is_a?(WikiContent) || obj.is_a?(WikiContent::Version)) && obj.page == wiki_page Chris@1295: "##{anchor}" Chris@1295: else Chris@1295: case options[:wiki_links] Chris@1295: when :local; "#{page.present? ? Wiki.titleize(page) : ''}.html" + (anchor.present? ? "##{anchor}" : '') Chris@1295: when :anchor; "##{page.present? ? Wiki.titleize(page) : title}" + (anchor.present? ? "_#{anchor}" : '') # used for single-file wiki export Chris@1295: else Chris@1295: wiki_page_id = page.present? ? Wiki.titleize(page) : nil Chris@1295: parent = wiki_page.nil? && obj.is_a?(WikiContent) && obj.page && project == link_project ? obj.page.title : nil Chris@1295: url_for(:only_path => only_path, :controller => 'wiki', :action => 'show', :project_id => link_project, Chris@1295: :id => wiki_page_id, :version => nil, :anchor => anchor, :parent => parent) Chris@1295: end Chris@1295: end Chris@1295: link_to(title.present? ? title.html_safe : h(page), url, :class => ('wiki-page' + (wiki_page ? '' : ' new'))) Chris@1295: else Chris@1295: # project or wiki doesn't exist Chris@1295: all Chris@1295: end Chris@1295: else Chris@1295: all Chris@1295: end Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: # Redmine links Chris@1295: # Chris@1295: # Examples: Chris@1295: # Issues: Chris@1295: # #52 -> Link to issue #52 Chris@1295: # Changesets: Chris@1295: # r52 -> Link to revision 52 Chris@1295: # commit:a85130f -> Link to scmid starting with a85130f Chris@1295: # Documents: Chris@1295: # document#17 -> Link to document with id 17 Chris@1295: # document:Greetings -> Link to the document with title "Greetings" Chris@1295: # document:"Some document" -> Link to the document with title "Some document" Chris@1295: # Versions: Chris@1295: # version#3 -> Link to version with id 3 Chris@1295: # version:1.0.0 -> Link to version named "1.0.0" Chris@1295: # version:"1.0 beta 2" -> Link to version named "1.0 beta 2" Chris@1295: # Attachments: Chris@1295: # attachment:file.zip -> Link to the attachment of the current object named file.zip Chris@1295: # Source files: Chris@1295: # source:some/file -> Link to the file located at /some/file in the project's repository Chris@1295: # source:some/file@52 -> Link to the file's revision 52 Chris@1295: # source:some/file#L120 -> Link to line 120 of the file Chris@1295: # source:some/file@52#L120 -> Link to line 120 of the file's revision 52 Chris@1295: # export:some/file -> Force the download of the file Chris@1295: # Forum messages: Chris@1295: # message#1218 -> Link to message with id 1218 Chris@1295: # Chris@1295: # Links can refer other objects from other projects, using project identifier: Chris@1295: # identifier:r52 Chris@1295: # identifier:document:"Some document" Chris@1295: # identifier:version:1.0.0 Chris@1295: # identifier:source:some/file Chris@1295: def parse_redmine_links(text, default_project, obj, attr, only_path, options) Chris@1295: text.gsub!(%r{([\s\(,\-\[\>]|^)(!)?(([a-z0-9\-_]+):)?(attachment|document|version|forum|news|message|project|commit|source|export)?(((#)|((([a-z0-9\-_]+)\|)?(r)))((\d+)((#note)?-(\d+))?)|(:)([^"\s<>][^\s<>]*?|"[^"]+?"))(?=(?=[[:punct:]][^A-Za-z0-9_/])|,|\s|\]|<|$)}) do |m| Chris@1295: leading, esc, project_prefix, project_identifier, prefix, repo_prefix, repo_identifier, sep, identifier, comment_suffix, comment_id = $1, $2, $3, $4, $5, $10, $11, $8 || $12 || $18, $14 || $19, $15, $17 Chris@1295: link = nil Chris@1295: project = default_project Chris@1295: if project_identifier Chris@1295: project = Project.visible.find_by_identifier(project_identifier) Chris@1295: end Chris@1295: if esc.nil? Chris@1295: if prefix.nil? && sep == 'r' Chris@1295: if project Chris@1295: repository = nil Chris@1295: if repo_identifier Chris@1295: repository = project.repositories.detect {|repo| repo.identifier == repo_identifier} Chris@1295: else Chris@1295: repository = project.repository Chris@1295: end Chris@1295: # project.changesets.visible raises an SQL error because of a double join on repositories Chris@1295: if repository && (changeset = Changeset.visible.find_by_repository_id_and_revision(repository.id, identifier)) Chris@1295: link = link_to(h("#{project_prefix}#{repo_prefix}r#{identifier}"), {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :repository_id => repository.identifier_param, :rev => changeset.revision}, Chris@1295: :class => 'changeset', Chris@1295: :title => truncate_single_line(changeset.comments, :length => 100)) Chris@1295: end Chris@1295: end Chris@1295: elsif sep == '#' Chris@1295: oid = identifier.to_i Chris@1295: case prefix Chris@1295: when nil Chris@1295: if oid.to_s == identifier && issue = Issue.visible.find_by_id(oid, :include => :status) Chris@1295: anchor = comment_id ? "note-#{comment_id}" : nil Chris@1295: link = link_to("##{oid}", {:only_path => only_path, :controller => 'issues', :action => 'show', :id => oid, :anchor => anchor}, Chris@1295: :class => issue.css_classes, Chris@1295: :title => "#{truncate(issue.subject, :length => 100)} (#{issue.status.name})") Chris@1295: end Chris@1295: when 'document' Chris@1295: if document = Document.visible.find_by_id(oid) Chris@1295: link = link_to h(document.title), {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document}, Chris@1295: :class => 'document' Chris@1295: end Chris@1295: when 'version' Chris@1295: if version = Version.visible.find_by_id(oid) Chris@1295: link = link_to h(version.name), {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version}, Chris@1295: :class => 'version' Chris@1295: end Chris@1295: when 'message' Chris@1295: if message = Message.visible.find_by_id(oid, :include => :parent) Chris@1295: link = link_to_message(message, {:only_path => only_path}, :class => 'message') Chris@1295: end Chris@1295: when 'forum' Chris@1295: if board = Board.visible.find_by_id(oid) Chris@1295: link = link_to h(board.name), {:only_path => only_path, :controller => 'boards', :action => 'show', :id => board, :project_id => board.project}, Chris@1295: :class => 'board' Chris@1295: end Chris@1295: when 'news' Chris@1295: if news = News.visible.find_by_id(oid) Chris@1295: link = link_to h(news.title), {:only_path => only_path, :controller => 'news', :action => 'show', :id => news}, Chris@1295: :class => 'news' Chris@1295: end Chris@1295: when 'project' Chris@1295: if p = Project.visible.find_by_id(oid) Chris@1295: link = link_to_project(p, {:only_path => only_path}, :class => 'project') Chris@1295: end Chris@1295: end Chris@1295: elsif sep == ':' Chris@1295: # removes the double quotes if any Chris@1295: name = identifier.gsub(%r{^"(.*)"$}, "\\1") Chris@1295: case prefix Chris@1295: when 'document' Chris@1295: if project && document = project.documents.visible.find_by_title(name) Chris@1295: link = link_to h(document.title), {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document}, Chris@1295: :class => 'document' Chris@1295: end Chris@1295: when 'version' Chris@1295: if project && version = project.versions.visible.find_by_name(name) Chris@1295: link = link_to h(version.name), {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version}, Chris@1295: :class => 'version' Chris@1295: end Chris@1295: when 'forum' Chris@1295: if project && board = project.boards.visible.find_by_name(name) Chris@1295: link = link_to h(board.name), {:only_path => only_path, :controller => 'boards', :action => 'show', :id => board, :project_id => board.project}, Chris@1295: :class => 'board' Chris@1295: end Chris@1295: when 'news' Chris@1295: if project && news = project.news.visible.find_by_title(name) Chris@1295: link = link_to h(news.title), {:only_path => only_path, :controller => 'news', :action => 'show', :id => news}, Chris@1295: :class => 'news' Chris@1295: end Chris@1295: when 'commit', 'source', 'export' Chris@1295: if project Chris@1295: repository = nil Chris@1295: if name =~ %r{^(([a-z0-9\-_]+)\|)(.+)$} Chris@1295: repo_prefix, repo_identifier, name = $1, $2, $3 Chris@1295: repository = project.repositories.detect {|repo| repo.identifier == repo_identifier} Chris@1295: else Chris@1295: repository = project.repository Chris@1295: end Chris@1295: if prefix == 'commit' Chris@1295: if repository && (changeset = Changeset.visible.find(:first, :conditions => ["repository_id = ? AND scmid LIKE ?", repository.id, "#{name}%"])) Chris@1295: link = link_to h("#{project_prefix}#{repo_prefix}#{name}"), {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :repository_id => repository.identifier_param, :rev => changeset.identifier}, Chris@1295: :class => 'changeset', Chris@1295: :title => truncate_single_line(h(changeset.comments), :length => 100) Chris@1295: end Chris@1295: else Chris@1295: if repository && User.current.allowed_to?(:browse_repository, project) Chris@1295: name =~ %r{^[/\\]*(.*?)(@([0-9a-f]+))?(#(L\d+))?$} Chris@1295: path, rev, anchor = $1, $3, $5 Chris@1295: link = link_to h("#{project_prefix}#{prefix}:#{repo_prefix}#{name}"), {:controller => 'repositories', :action => (prefix == 'export' ? 'raw' : 'entry'), :id => project, :repository_id => repository.identifier_param, Chris@1295: :path => to_path_param(path), Chris@1295: :rev => rev, Chris@1295: :anchor => anchor}, Chris@1295: :class => (prefix == 'export' ? 'source download' : 'source') Chris@1295: end Chris@1295: end Chris@1295: repo_prefix = nil Chris@1295: end Chris@1295: when 'attachment' Chris@1295: attachments = options[:attachments] || (obj && obj.respond_to?(:attachments) ? obj.attachments : nil) Chris@1295: if attachments && attachment = Attachment.latest_attach(attachments, name) Chris@1295: link = link_to h(attachment.filename), {:only_path => only_path, :controller => 'attachments', :action => 'download', :id => attachment}, Chris@1295: :class => 'attachment' Chris@1295: end Chris@1295: when 'project' Chris@1295: if p = Project.visible.find(:first, :conditions => ["identifier = :s OR LOWER(name) = :s", {:s => name.downcase}]) Chris@1295: link = link_to_project(p, {:only_path => only_path}, :class => 'project') Chris@1295: end Chris@1295: end Chris@1295: end Chris@1295: end Chris@1295: (leading + (link || "#{project_prefix}#{prefix}#{repo_prefix}#{sep}#{identifier}#{comment_suffix}")) Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: HEADING_RE = /(]+)?>(.+?)<\/h(\d)>)/i unless const_defined?(:HEADING_RE) Chris@1295: Chris@1295: def parse_sections(text, project, obj, attr, only_path, options) Chris@1295: return unless options[:edit_section_links] Chris@1295: text.gsub!(HEADING_RE) do Chris@1295: heading = $1 Chris@1295: @current_section += 1 Chris@1295: if @current_section > 1 Chris@1295: content_tag('div', Chris@1295: link_to(image_tag('edit.png'), options[:edit_section_links].merge(:section => @current_section)), Chris@1295: :class => 'contextual', Chris@1295: :title => l(:button_edit_section)) + heading.html_safe Chris@1295: else Chris@1295: heading Chris@1295: end Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: # Headings and TOC Chris@1295: # Adds ids and links to headings unless options[:headings] is set to false Chris@1295: def parse_headings(text, project, obj, attr, only_path, options) Chris@1295: return if options[:headings] == false Chris@1295: Chris@1295: text.gsub!(HEADING_RE) do Chris@1295: level, attrs, content = $2.to_i, $3, $4 Chris@1295: item = strip_tags(content).strip Chris@1295: anchor = sanitize_anchor_name(item) Chris@1295: # used for single-file wiki export Chris@1295: anchor = "#{obj.page.title}_#{anchor}" if options[:wiki_links] == :anchor && (obj.is_a?(WikiContent) || obj.is_a?(WikiContent::Version)) Chris@1295: @heading_anchors[anchor] ||= 0 Chris@1295: idx = (@heading_anchors[anchor] += 1) Chris@1295: if idx > 1 Chris@1295: anchor = "#{anchor}-#{idx}" Chris@1295: end Chris@1295: @parsed_headings << [level, anchor, item] Chris@1295: "\n#{content}" Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: MACROS_RE = /( Chris@1295: (!)? # escaping Chris@1295: ( Chris@1295: \{\{ # opening tag Chris@1295: ([\w]+) # macro name Chris@1295: (\(([^\n\r]*?)\))? # optional arguments Chris@1295: ([\n\r].*?[\n\r])? # optional block of text Chris@1295: \}\} # closing tag Chris@1295: ) Chris@1295: )/mx unless const_defined?(:MACROS_RE) Chris@1295: Chris@1295: MACRO_SUB_RE = /( Chris@1295: \{\{ Chris@1295: macro\((\d+)\) Chris@1295: \}\} Chris@1295: )/x unless const_defined?(:MACRO_SUB_RE) Chris@1295: Chris@1295: # Extracts macros from text Chris@1295: def catch_macros(text) Chris@1295: macros = {} Chris@1295: text.gsub!(MACROS_RE) do Chris@1295: all, macro = $1, $4.downcase Chris@1295: if macro_exists?(macro) || all =~ MACRO_SUB_RE Chris@1295: index = macros.size Chris@1295: macros[index] = all Chris@1295: "{{macro(#{index})}}" Chris@1295: else Chris@1295: all Chris@1295: end Chris@1295: end Chris@1295: macros Chris@1295: end Chris@1295: Chris@1295: # Executes and replaces macros in text Chris@1295: def inject_macros(text, obj, macros, execute=true) Chris@1295: text.gsub!(MACRO_SUB_RE) do Chris@1295: all, index = $1, $2.to_i Chris@1295: orig = macros.delete(index) Chris@1295: if execute && orig && orig =~ MACROS_RE Chris@1295: esc, all, macro, args, block = $2, $3, $4.downcase, $6.to_s, $7.try(:strip) Chris@1295: if esc.nil? Chris@1295: h(exec_macro(macro, obj, args, block) || all) Chris@1295: else Chris@1295: h(all) Chris@1295: end Chris@1295: elsif orig Chris@1295: h(orig) Chris@1295: else Chris@1295: h(all) Chris@1295: end Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: TOC_RE = /

    \{\{([<>]?)toc\}\}<\/p>/i unless const_defined?(:TOC_RE) Chris@1295: Chris@1295: # Renders the TOC with given headings Chris@1295: def replace_toc(text, headings) Chris@1295: text.gsub!(TOC_RE) do Chris@1295: # Keep only the 4 first levels Chris@1295: headings = headings.select{|level, anchor, item| level <= 4} Chris@1295: if headings.empty? Chris@1295: '' Chris@1295: else Chris@1295: div_class = 'toc' Chris@1295: div_class << ' right' if $1 == '>' Chris@1295: div_class << ' left' if $1 == '<' Chris@1295: out = "

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

    "). # 2+ newline -> 2 br Chris@1295: gsub(/([^\n]\n)(?=[^\n])/, '\1
    '). # 1 newline -> br Chris@1295: html_safe Chris@1295: end Chris@1295: Chris@1295: def lang_options_for_select(blank=true) Chris@1295: (blank ? [["(auto)", ""]] : []) + languages_options Chris@1295: end Chris@1295: Chris@1295: def label_tag_for(name, option_tags = nil, options = {}) Chris@1295: label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "") Chris@1295: content_tag("label", label_text) Chris@1295: end Chris@1295: Chris@1295: def labelled_form_for(*args, &proc) Chris@1295: args << {} unless args.last.is_a?(Hash) Chris@1295: options = args.last Chris@1295: if args.first.is_a?(Symbol) Chris@1295: options.merge!(:as => args.shift) Chris@1295: end Chris@1295: options.merge!({:builder => Redmine::Views::LabelledFormBuilder}) Chris@1295: form_for(*args, &proc) Chris@1295: end Chris@1295: Chris@1295: def labelled_fields_for(*args, &proc) Chris@1295: args << {} unless args.last.is_a?(Hash) Chris@1295: options = args.last Chris@1295: options.merge!({:builder => Redmine::Views::LabelledFormBuilder}) Chris@1295: fields_for(*args, &proc) Chris@1295: end Chris@1295: Chris@1295: def labelled_remote_form_for(*args, &proc) Chris@1295: ActiveSupport::Deprecation.warn "ApplicationHelper#labelled_remote_form_for is deprecated and will be removed in Redmine 2.2." Chris@1295: args << {} unless args.last.is_a?(Hash) Chris@1295: options = args.last Chris@1295: options.merge!({:builder => Redmine::Views::LabelledFormBuilder, :remote => true}) Chris@1295: form_for(*args, &proc) Chris@1295: end Chris@1295: Chris@1295: def error_messages_for(*objects) Chris@1295: html = "" Chris@1295: objects = objects.map {|o| o.is_a?(String) ? instance_variable_get("@#{o}") : o}.compact Chris@1295: errors = objects.map {|o| o.errors.full_messages}.flatten Chris@1295: if errors.any? Chris@1295: html << "
    \n" Chris@1295: end Chris@1295: html.html_safe Chris@1295: end Chris@1295: Chris@1295: def delete_link(url, options={}) Chris@1295: options = { Chris@1295: :method => :delete, Chris@1295: :data => {:confirm => l(:text_are_you_sure)}, Chris@1295: :class => 'icon icon-del' Chris@1295: }.merge(options) Chris@1295: Chris@1295: link_to l(:button_delete), url, options Chris@1295: end Chris@1295: Chris@1295: def preview_link(url, form, target='preview', options={}) Chris@1295: content_tag 'a', l(:label_preview), { Chris@1295: :href => "#", Chris@1295: :onclick => %|submitPreview("#{escape_javascript url_for(url)}", "#{escape_javascript form}", "#{escape_javascript target}"); return false;|, Chris@1295: :accesskey => accesskey(:preview) Chris@1295: }.merge(options) Chris@1295: end Chris@1295: Chris@1295: def link_to_function(name, function, html_options={}) Chris@1295: content_tag(:a, name, {:href => '#', :onclick => "#{function}; return false;"}.merge(html_options)) Chris@1295: end Chris@1295: Chris@1295: # Helper to render JSON in views Chris@1295: def raw_json(arg) Chris@1295: arg.to_json.to_s.gsub('/', '\/').html_safe Chris@1295: end Chris@1295: Chris@1295: def back_url Chris@1295: url = params[:back_url] Chris@1295: if url.nil? && referer = request.env['HTTP_REFERER'] Chris@1295: url = CGI.unescape(referer.to_s) Chris@1295: end Chris@1295: url Chris@1295: end Chris@1295: Chris@1295: def back_url_hidden_field_tag Chris@1295: url = back_url Chris@1295: hidden_field_tag('back_url', url, :id => nil) unless url.blank? Chris@1295: end Chris@1295: Chris@1295: def check_all_links(form_name) Chris@1295: link_to_function(l(:button_check_all), "checkAll('#{form_name}', true)") + Chris@1295: " | ".html_safe + Chris@1295: link_to_function(l(:button_uncheck_all), "checkAll('#{form_name}', false)") Chris@1295: end Chris@1295: Chris@1295: def progress_bar(pcts, options={}) Chris@1295: pcts = [pcts, pcts] unless pcts.is_a?(Array) Chris@1295: pcts = pcts.collect(&:round) Chris@1295: pcts[1] = pcts[1] - pcts[0] Chris@1295: pcts << (100 - pcts[1] - pcts[0]) Chris@1295: width = options[:width] || '100px;' Chris@1295: legend = options[:legend] || '' Chris@1295: content_tag('table', Chris@1295: content_tag('tr', Chris@1295: (pcts[0] > 0 ? content_tag('td', '', :style => "width: #{pcts[0]}%;", :class => 'closed') : ''.html_safe) + Chris@1295: (pcts[1] > 0 ? content_tag('td', '', :style => "width: #{pcts[1]}%;", :class => 'done') : ''.html_safe) + Chris@1295: (pcts[2] > 0 ? content_tag('td', '', :style => "width: #{pcts[2]}%;", :class => 'todo') : ''.html_safe) Chris@1295: ), :class => 'progress', :style => "width: #{width};").html_safe + Chris@1295: content_tag('p', legend, :class => 'pourcent').html_safe Chris@1295: end Chris@1295: Chris@1295: def checked_image(checked=true) Chris@1295: if checked Chris@1295: image_tag 'toggle_check.png' Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: def context_menu(url) Chris@1295: unless @context_menu_included Chris@1295: content_for :header_tags do Chris@1295: javascript_include_tag('context_menu') + Chris@1295: stylesheet_link_tag('context_menu') Chris@1295: end Chris@1295: if l(:direction) == 'rtl' Chris@1295: content_for :header_tags do Chris@1295: stylesheet_link_tag('context_menu_rtl') Chris@1295: end Chris@1295: end Chris@1295: @context_menu_included = true Chris@1295: end Chris@1295: javascript_tag "contextMenuInit('#{ url_for(url) }')" Chris@1295: end Chris@1295: Chris@1295: def calendar_for(field_id) Chris@1295: include_calendar_headers_tags Chris@1295: javascript_tag("$(function() { $('##{field_id}').datepicker(datepickerOptions); });") Chris@1295: end Chris@1295: Chris@1295: def include_calendar_headers_tags Chris@1295: unless @calendar_headers_tags_included Chris@1295: @calendar_headers_tags_included = true Chris@1295: content_for :header_tags do Chris@1295: start_of_week = Setting.start_of_week Chris@1295: start_of_week = l(:general_first_day_of_week, :default => '1') if start_of_week.blank? Chris@1295: # Redmine uses 1..7 (monday..sunday) in settings and locales Chris@1295: # JQuery uses 0..6 (sunday..saturday), 7 needs to be changed to 0 Chris@1295: start_of_week = start_of_week.to_i % 7 Chris@1295: Chris@1295: tags = javascript_tag( Chris@1295: "var datepickerOptions={dateFormat: 'yy-mm-dd', firstDay: #{start_of_week}, " + Chris@1295: "showOn: 'button', buttonImageOnly: true, buttonImage: '" + Chris@1295: path_to_image('/images/calendar.png') + Chris@1295: "', showButtonPanel: true};") Chris@1295: jquery_locale = l('jquery.locale', :default => current_language.to_s) Chris@1295: unless jquery_locale == 'en' Chris@1295: tags << javascript_include_tag("i18n/jquery.ui.datepicker-#{jquery_locale}.js") Chris@1295: end Chris@1295: tags Chris@1295: end Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: # Overrides Rails' stylesheet_link_tag with themes and plugins support. Chris@1295: # Examples: Chris@1295: # stylesheet_link_tag('styles') # => picks styles.css from the current theme or defaults Chris@1295: # stylesheet_link_tag('styles', :plugin => 'foo) # => picks styles.css from plugin's assets Chris@1295: # Chris@1295: def stylesheet_link_tag(*sources) Chris@1295: options = sources.last.is_a?(Hash) ? sources.pop : {} Chris@1295: plugin = options.delete(:plugin) Chris@1295: sources = sources.map do |source| Chris@1295: if plugin Chris@1295: "/plugin_assets/#{plugin}/stylesheets/#{source}" Chris@1295: elsif current_theme && current_theme.stylesheets.include?(source) Chris@1295: current_theme.stylesheet_path(source) Chris@1295: else Chris@1295: source Chris@1295: end Chris@1295: end Chris@1295: super sources, options Chris@1295: end Chris@1295: Chris@1295: # Overrides Rails' image_tag with themes and plugins support. Chris@1295: # Examples: Chris@1295: # image_tag('image.png') # => picks image.png from the current theme or defaults Chris@1295: # image_tag('image.png', :plugin => 'foo) # => picks image.png from plugin's assets Chris@1295: # Chris@1295: def image_tag(source, options={}) Chris@1295: if plugin = options.delete(:plugin) Chris@1295: source = "/plugin_assets/#{plugin}/images/#{source}" Chris@1295: elsif current_theme && current_theme.images.include?(source) Chris@1295: source = current_theme.image_path(source) Chris@1295: end Chris@1295: super source, options Chris@1295: end Chris@1295: Chris@1295: # Overrides Rails' javascript_include_tag with plugins support Chris@1295: # Examples: Chris@1295: # javascript_include_tag('scripts') # => picks scripts.js from defaults Chris@1295: # javascript_include_tag('scripts', :plugin => 'foo) # => picks scripts.js from plugin's assets Chris@1295: # Chris@1295: def javascript_include_tag(*sources) Chris@1295: options = sources.last.is_a?(Hash) ? sources.pop : {} Chris@1295: if plugin = options.delete(:plugin) Chris@1295: sources = sources.map do |source| Chris@1295: if plugin Chris@1295: "/plugin_assets/#{plugin}/javascripts/#{source}" Chris@1295: else Chris@1295: source Chris@1295: end Chris@1295: end Chris@1295: end Chris@1295: super sources, options Chris@1295: end Chris@1295: Chris@1295: def content_for(name, content = nil, &block) Chris@1295: @has_content ||= {} Chris@1295: @has_content[name] = true Chris@1295: super(name, content, &block) Chris@1295: end Chris@1295: Chris@1295: def has_content?(name) Chris@1295: (@has_content && @has_content[name]) || false Chris@1295: end Chris@1295: Chris@1295: def sidebar_content? Chris@1295: has_content?(:sidebar) || view_layouts_base_sidebar_hook_response.present? Chris@1295: end Chris@1295: Chris@1295: def view_layouts_base_sidebar_hook_response Chris@1295: @view_layouts_base_sidebar_hook_response ||= call_hook(:view_layouts_base_sidebar) Chris@1295: end Chris@1295: Chris@1295: def email_delivery_enabled? Chris@1295: !!ActionMailer::Base.perform_deliveries Chris@1295: end Chris@1295: Chris@1295: # Returns the avatar image tag for the given +user+ if avatars are enabled Chris@1295: # +user+ can be a User or a string that will be scanned for an email address (eg. 'joe ') Chris@1295: def avatar(user, options = { }) Chris@1295: if Setting.gravatar_enabled? Chris@1295: options.merge!({:ssl => (request && request.ssl?), :default => Setting.gravatar_default}) Chris@1295: email = nil Chris@1295: if user.respond_to?(:mail) Chris@1295: email = user.mail Chris@1295: elsif user.to_s =~ %r{<(.+?)>} Chris@1295: email = $1 Chris@1295: end Chris@1295: return gravatar(email.to_s.downcase, options) unless email.blank? rescue nil Chris@1295: else Chris@1295: '' Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: def sanitize_anchor_name(anchor) Chris@1295: if ''.respond_to?(:encoding) || RUBY_PLATFORM == 'java' Chris@1295: anchor.gsub(%r{[^\p{Word}\s\-]}, '').gsub(%r{\s+(\-+\s*)?}, '-') Chris@1295: else Chris@1295: # TODO: remove when ruby1.8 is no longer supported Chris@1295: anchor.gsub(%r{[^\w\s\-]}, '').gsub(%r{\s+(\-+\s*)?}, '-') Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: # Returns the javascript tags that are included in the html layout head Chris@1295: def javascript_heads Chris@1295: tags = javascript_include_tag('jquery-1.7.2-ui-1.8.21-ujs-2.0.3', 'application') Chris@1295: unless User.current.pref.warn_on_leaving_unsaved == '0' Chris@1295: tags << "\n".html_safe + javascript_tag("$(window).load(function(){ warnLeavingUnsaved('#{escape_javascript l(:text_warn_on_leaving_unsaved)}'); });") Chris@1295: end Chris@1295: tags Chris@1295: end Chris@1295: Chris@1295: def favicon Chris@1295: "".html_safe Chris@1295: end Chris@1295: Chris@1295: def robot_exclusion_tag Chris@1295: ''.html_safe Chris@1295: end Chris@1295: Chris@1295: # Returns true if arg is expected in the API response Chris@1295: def include_in_api_response?(arg) Chris@1295: unless @included_in_api_response Chris@1295: param = params[:include] Chris@1295: @included_in_api_response = param.is_a?(Array) ? param.collect(&:to_s) : param.to_s.split(',') Chris@1295: @included_in_api_response.collect!(&:strip) Chris@1295: end Chris@1295: @included_in_api_response.include?(arg.to_s) Chris@1295: end Chris@1295: Chris@1295: # Returns options or nil if nometa param or X-Redmine-Nometa header Chris@1295: # was set in the request Chris@1295: def api_meta(options) Chris@1295: if params[:nometa].present? || request.headers['X-Redmine-Nometa'] Chris@1295: # compatibility mode for activeresource clients that raise Chris@1295: # an error when unserializing an array with attributes Chris@1295: nil Chris@1295: else Chris@1295: options Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: private Chris@1295: Chris@1295: def wiki_helper Chris@1295: helper = Redmine::WikiFormatting.helper_for(Setting.text_formatting) Chris@1295: extend helper Chris@1295: return self Chris@1295: end Chris@1295: Chris@1295: def link_to_content_update(text, url_params = {}, html_options = {}) Chris@1295: link_to(text, url_params, html_options) Chris@1295: end Chris@1295: end