annotate .svn/pristine/04/048c8c7c3fc4fac08662cbdaac59cd6348bb3d14.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents 622f24f53b42
children
rev   line source
Chris@1295 1 # encoding: utf-8
Chris@1295 2 #
Chris@1295 3 # Redmine - project management software
Chris@1295 4 # Copyright (C) 2006-2012 Jean-Philippe Lang
Chris@1295 5 #
Chris@1295 6 # This program is free software; you can redistribute it and/or
Chris@1295 7 # modify it under the terms of the GNU General Public License
Chris@1295 8 # as published by the Free Software Foundation; either version 2
Chris@1295 9 # of the License, or (at your option) any later version.
Chris@1295 10 #
Chris@1295 11 # This program is distributed in the hope that it will be useful,
Chris@1295 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1295 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1295 14 # GNU General Public License for more details.
Chris@1295 15 #
Chris@1295 16 # You should have received a copy of the GNU General Public License
Chris@1295 17 # along with this program; if not, write to the Free Software
Chris@1295 18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1295 19
Chris@1295 20 require 'forwardable'
Chris@1295 21 require 'cgi'
Chris@1295 22
Chris@1295 23 module ApplicationHelper
Chris@1295 24 include Redmine::WikiFormatting::Macros::Definitions
Chris@1295 25 include Redmine::I18n
Chris@1295 26 include GravatarHelper::PublicMethods
Chris@1295 27
Chris@1295 28 extend Forwardable
Chris@1295 29 def_delegators :wiki_helper, :wikitoolbar_for, :heads_for_wiki_formatter
Chris@1295 30
Chris@1295 31 # Return true if user is authorized for controller/action, otherwise false
Chris@1295 32 def authorize_for(controller, action)
Chris@1295 33 User.current.allowed_to?({:controller => controller, :action => action}, @project)
Chris@1295 34 end
Chris@1295 35
Chris@1295 36 # Display a link if user is authorized
Chris@1295 37 #
Chris@1295 38 # @param [String] name Anchor text (passed to link_to)
Chris@1295 39 # @param [Hash] options Hash params. This will checked by authorize_for to see if the user is authorized
Chris@1295 40 # @param [optional, Hash] html_options Options passed to link_to
Chris@1295 41 # @param [optional, Hash] parameters_for_method_reference Extra parameters for link_to
Chris@1295 42 def link_to_if_authorized(name, options = {}, html_options = nil, *parameters_for_method_reference)
Chris@1295 43 link_to(name, options, html_options, *parameters_for_method_reference) if authorize_for(options[:controller] || params[:controller], options[:action])
Chris@1295 44 end
Chris@1295 45
Chris@1295 46 # Displays a link to user's account page if active
Chris@1295 47 def link_to_user(user, options={})
Chris@1295 48 if user.is_a?(User)
Chris@1295 49 name = h(user.name(options[:format]))
Chris@1295 50 if user.active? || (User.current.admin? && user.logged?)
Chris@1295 51 link_to name, user_path(user), :class => user.css_classes
Chris@1295 52 else
Chris@1295 53 name
Chris@1295 54 end
Chris@1295 55 else
Chris@1295 56 h(user.to_s)
Chris@1295 57 end
Chris@1295 58 end
Chris@1295 59
Chris@1295 60 # Displays a link to +issue+ with its subject.
Chris@1295 61 # Examples:
Chris@1295 62 #
Chris@1295 63 # link_to_issue(issue) # => Defect #6: This is the subject
Chris@1295 64 # link_to_issue(issue, :truncate => 6) # => Defect #6: This i...
Chris@1295 65 # link_to_issue(issue, :subject => false) # => Defect #6
Chris@1295 66 # link_to_issue(issue, :project => true) # => Foo - Defect #6
Chris@1295 67 # link_to_issue(issue, :subject => false, :tracker => false) # => #6
Chris@1295 68 #
Chris@1295 69 def link_to_issue(issue, options={})
Chris@1295 70 title = nil
Chris@1295 71 subject = nil
Chris@1295 72 text = options[:tracker] == false ? "##{issue.id}" : "#{issue.tracker} ##{issue.id}"
Chris@1295 73 if options[:subject] == false
Chris@1295 74 title = truncate(issue.subject, :length => 60)
Chris@1295 75 else
Chris@1295 76 subject = issue.subject
Chris@1295 77 if options[:truncate]
Chris@1295 78 subject = truncate(subject, :length => options[:truncate])
Chris@1295 79 end
Chris@1295 80 end
Chris@1295 81 s = link_to text, issue_path(issue), :class => issue.css_classes, :title => title
Chris@1295 82 s << h(": #{subject}") if subject
Chris@1295 83 s = h("#{issue.project} - ") + s if options[:project]
Chris@1295 84 s
Chris@1295 85 end
Chris@1295 86
Chris@1295 87 # Generates a link to an attachment.
Chris@1295 88 # Options:
Chris@1295 89 # * :text - Link text (default to attachment filename)
Chris@1295 90 # * :download - Force download (default: false)
Chris@1295 91 def link_to_attachment(attachment, options={})
Chris@1295 92 text = options.delete(:text) || attachment.filename
Chris@1295 93 action = options.delete(:download) ? 'download' : 'show'
Chris@1295 94 opt_only_path = {}
Chris@1295 95 opt_only_path[:only_path] = (options[:only_path] == false ? false : true)
Chris@1295 96 options.delete(:only_path)
Chris@1295 97 link_to(h(text),
Chris@1295 98 {:controller => 'attachments', :action => action,
Chris@1295 99 :id => attachment, :filename => attachment.filename}.merge(opt_only_path),
Chris@1295 100 options)
Chris@1295 101 end
Chris@1295 102
Chris@1295 103 # Generates a link to a SCM revision
Chris@1295 104 # Options:
Chris@1295 105 # * :text - Link text (default to the formatted revision)
Chris@1295 106 def link_to_revision(revision, repository, options={})
Chris@1295 107 if repository.is_a?(Project)
Chris@1295 108 repository = repository.repository
Chris@1295 109 end
Chris@1295 110 text = options.delete(:text) || format_revision(revision)
Chris@1295 111 rev = revision.respond_to?(:identifier) ? revision.identifier : revision
Chris@1295 112 link_to(
Chris@1295 113 h(text),
Chris@1295 114 {:controller => 'repositories', :action => 'revision', :id => repository.project, :repository_id => repository.identifier_param, :rev => rev},
Chris@1295 115 :title => l(:label_revision_id, format_revision(revision))
Chris@1295 116 )
Chris@1295 117 end
Chris@1295 118
Chris@1295 119 # Generates a link to a message
Chris@1295 120 def link_to_message(message, options={}, html_options = nil)
Chris@1295 121 link_to(
Chris@1295 122 h(truncate(message.subject, :length => 60)),
Chris@1295 123 { :controller => 'messages', :action => 'show',
Chris@1295 124 :board_id => message.board_id,
Chris@1295 125 :id => (message.parent_id || message.id),
Chris@1295 126 :r => (message.parent_id && message.id),
Chris@1295 127 :anchor => (message.parent_id ? "message-#{message.id}" : nil)
Chris@1295 128 }.merge(options),
Chris@1295 129 html_options
Chris@1295 130 )
Chris@1295 131 end
Chris@1295 132
Chris@1295 133 # Generates a link to a project if active
Chris@1295 134 # Examples:
Chris@1295 135 #
Chris@1295 136 # link_to_project(project) # => link to the specified project overview
Chris@1295 137 # link_to_project(project, :action=>'settings') # => link to project settings
Chris@1295 138 # link_to_project(project, {:only_path => false}, :class => "project") # => 3rd arg adds html options
Chris@1295 139 # link_to_project(project, {}, :class => "project") # => html options with default url (project overview)
Chris@1295 140 #
Chris@1295 141 def link_to_project(project, options={}, html_options = nil)
Chris@1295 142 if project.archived?
Chris@1295 143 h(project)
Chris@1295 144 else
Chris@1295 145 url = {:controller => 'projects', :action => 'show', :id => project}.merge(options)
Chris@1295 146 link_to(h(project), url, html_options)
Chris@1295 147 end
Chris@1295 148 end
Chris@1295 149
Chris@1295 150 def wiki_page_path(page, options={})
Chris@1295 151 url_for({:controller => 'wiki', :action => 'show', :project_id => page.project, :id => page.title}.merge(options))
Chris@1295 152 end
Chris@1295 153
Chris@1295 154 def thumbnail_tag(attachment)
Chris@1295 155 link_to image_tag(url_for(:controller => 'attachments', :action => 'thumbnail', :id => attachment)),
Chris@1295 156 {:controller => 'attachments', :action => 'show', :id => attachment, :filename => attachment.filename},
Chris@1295 157 :title => attachment.filename
Chris@1295 158 end
Chris@1295 159
Chris@1295 160 def toggle_link(name, id, options={})
Chris@1295 161 onclick = "$('##{id}').toggle(); "
Chris@1295 162 onclick << (options[:focus] ? "$('##{options[:focus]}').focus(); " : "this.blur(); ")
Chris@1295 163 onclick << "return false;"
Chris@1295 164 link_to(name, "#", :onclick => onclick)
Chris@1295 165 end
Chris@1295 166
Chris@1295 167 def image_to_function(name, function, html_options = {})
Chris@1295 168 html_options.symbolize_keys!
Chris@1295 169 tag(:input, html_options.merge({
Chris@1295 170 :type => "image", :src => image_path(name),
Chris@1295 171 :onclick => (html_options[:onclick] ? "#{html_options[:onclick]}; " : "") + "#{function};"
Chris@1295 172 }))
Chris@1295 173 end
Chris@1295 174
Chris@1295 175 def format_activity_title(text)
Chris@1295 176 h(truncate_single_line(text, :length => 100))
Chris@1295 177 end
Chris@1295 178
Chris@1295 179 def format_activity_day(date)
Chris@1295 180 date == User.current.today ? l(:label_today).titleize : format_date(date)
Chris@1295 181 end
Chris@1295 182
Chris@1295 183 def format_activity_description(text)
Chris@1295 184 h(truncate(text.to_s, :length => 120).gsub(%r{[\r\n]*<(pre|code)>.*$}m, '...')
Chris@1295 185 ).gsub(/[\r\n]+/, "<br />").html_safe
Chris@1295 186 end
Chris@1295 187
Chris@1295 188 def format_version_name(version)
Chris@1295 189 if version.project == @project
Chris@1295 190 h(version)
Chris@1295 191 else
Chris@1295 192 h("#{version.project} - #{version}")
Chris@1295 193 end
Chris@1295 194 end
Chris@1295 195
Chris@1295 196 def due_date_distance_in_words(date)
Chris@1295 197 if date
Chris@1295 198 l((date < Date.today ? :label_roadmap_overdue : :label_roadmap_due_in), distance_of_date_in_words(Date.today, date))
Chris@1295 199 end
Chris@1295 200 end
Chris@1295 201
Chris@1295 202 # Renders a tree of projects as a nested set of unordered lists
Chris@1295 203 # The given collection may be a subset of the whole project tree
Chris@1295 204 # (eg. some intermediate nodes are private and can not be seen)
Chris@1295 205 def render_project_nested_lists(projects)
Chris@1295 206 s = ''
Chris@1295 207 if projects.any?
Chris@1295 208 ancestors = []
Chris@1295 209 original_project = @project
Chris@1295 210 projects.sort_by(&:lft).each do |project|
Chris@1295 211 # set the project environment to please macros.
Chris@1295 212 @project = project
Chris@1295 213 if (ancestors.empty? || project.is_descendant_of?(ancestors.last))
Chris@1295 214 s << "<ul class='projects #{ ancestors.empty? ? 'root' : nil}'>\n"
Chris@1295 215 else
Chris@1295 216 ancestors.pop
Chris@1295 217 s << "</li>"
Chris@1295 218 while (ancestors.any? && !project.is_descendant_of?(ancestors.last))
Chris@1295 219 ancestors.pop
Chris@1295 220 s << "</ul></li>\n"
Chris@1295 221 end
Chris@1295 222 end
Chris@1295 223 classes = (ancestors.empty? ? 'root' : 'child')
Chris@1295 224 s << "<li class='#{classes}'><div class='#{classes}'>"
Chris@1295 225 s << h(block_given? ? yield(project) : project.name)
Chris@1295 226 s << "</div>\n"
Chris@1295 227 ancestors << project
Chris@1295 228 end
Chris@1295 229 s << ("</li></ul>\n" * ancestors.size)
Chris@1295 230 @project = original_project
Chris@1295 231 end
Chris@1295 232 s.html_safe
Chris@1295 233 end
Chris@1295 234
Chris@1295 235 def render_page_hierarchy(pages, node=nil, options={})
Chris@1295 236 content = ''
Chris@1295 237 if pages[node]
Chris@1295 238 content << "<ul class=\"pages-hierarchy\">\n"
Chris@1295 239 pages[node].each do |page|
Chris@1295 240 content << "<li>"
Chris@1295 241 content << link_to(h(page.pretty_title), {:controller => 'wiki', :action => 'show', :project_id => page.project, :id => page.title, :version => nil},
Chris@1295 242 :title => (options[:timestamp] && page.updated_on ? l(:label_updated_time, distance_of_time_in_words(Time.now, page.updated_on)) : nil))
Chris@1295 243 content << "\n" + render_page_hierarchy(pages, page.id, options) if pages[page.id]
Chris@1295 244 content << "</li>\n"
Chris@1295 245 end
Chris@1295 246 content << "</ul>\n"
Chris@1295 247 end
Chris@1295 248 content.html_safe
Chris@1295 249 end
Chris@1295 250
Chris@1295 251 # Renders flash messages
Chris@1295 252 def render_flash_messages
Chris@1295 253 s = ''
Chris@1295 254 flash.each do |k,v|
Chris@1295 255 s << content_tag('div', v.html_safe, :class => "flash #{k}", :id => "flash_#{k}")
Chris@1295 256 end
Chris@1295 257 s.html_safe
Chris@1295 258 end
Chris@1295 259
Chris@1295 260 # Renders tabs and their content
Chris@1295 261 def render_tabs(tabs)
Chris@1295 262 if tabs.any?
Chris@1295 263 render :partial => 'common/tabs', :locals => {:tabs => tabs}
Chris@1295 264 else
Chris@1295 265 content_tag 'p', l(:label_no_data), :class => "nodata"
Chris@1295 266 end
Chris@1295 267 end
Chris@1295 268
Chris@1295 269 # Renders the project quick-jump box
Chris@1295 270 def render_project_jump_box
Chris@1295 271 return unless User.current.logged?
Chris@1295 272 projects = User.current.memberships.collect(&:project).compact.select(&:active?).uniq
Chris@1295 273 if projects.any?
Chris@1295 274 options =
Chris@1295 275 ("<option value=''>#{ l(:label_jump_to_a_project) }</option>" +
Chris@1295 276 '<option value="" disabled="disabled">---</option>').html_safe
Chris@1295 277
Chris@1295 278 options << project_tree_options_for_select(projects, :selected => @project) do |p|
Chris@1295 279 { :value => project_path(:id => p, :jump => current_menu_item) }
Chris@1295 280 end
Chris@1295 281
Chris@1295 282 select_tag('project_quick_jump_box', options, :onchange => 'if (this.value != \'\') { window.location = this.value; }')
Chris@1295 283 end
Chris@1295 284 end
Chris@1295 285
Chris@1295 286 def project_tree_options_for_select(projects, options = {})
Chris@1295 287 s = ''
Chris@1295 288 project_tree(projects) do |project, level|
Chris@1295 289 name_prefix = (level > 0 ? '&nbsp;' * 2 * level + '&#187; ' : '').html_safe
Chris@1295 290 tag_options = {:value => project.id}
Chris@1295 291 if project == options[:selected] || (options[:selected].respond_to?(:include?) && options[:selected].include?(project))
Chris@1295 292 tag_options[:selected] = 'selected'
Chris@1295 293 else
Chris@1295 294 tag_options[:selected] = nil
Chris@1295 295 end
Chris@1295 296 tag_options.merge!(yield(project)) if block_given?
Chris@1295 297 s << content_tag('option', name_prefix + h(project), tag_options)
Chris@1295 298 end
Chris@1295 299 s.html_safe
Chris@1295 300 end
Chris@1295 301
Chris@1295 302 # Yields the given block for each project with its level in the tree
Chris@1295 303 #
Chris@1295 304 # Wrapper for Project#project_tree
Chris@1295 305 def project_tree(projects, &block)
Chris@1295 306 Project.project_tree(projects, &block)
Chris@1295 307 end
Chris@1295 308
Chris@1295 309 def principals_check_box_tags(name, principals)
Chris@1295 310 s = ''
Chris@1295 311 principals.sort.each do |principal|
Chris@1295 312 s << "<label>#{ check_box_tag name, principal.id, false } #{h principal}</label>\n"
Chris@1295 313 end
Chris@1295 314 s.html_safe
Chris@1295 315 end
Chris@1295 316
Chris@1295 317 # Returns a string for users/groups option tags
Chris@1295 318 def principals_options_for_select(collection, selected=nil)
Chris@1295 319 s = ''
Chris@1295 320 if collection.include?(User.current)
Chris@1295 321 s << content_tag('option', "<< #{l(:label_me)} >>", :value => User.current.id)
Chris@1295 322 end
Chris@1295 323 groups = ''
Chris@1295 324 collection.sort.each do |element|
Chris@1295 325 selected_attribute = ' selected="selected"' if option_value_selected?(element, selected)
Chris@1295 326 (element.is_a?(Group) ? groups : s) << %(<option value="#{element.id}"#{selected_attribute}>#{h element.name}</option>)
Chris@1295 327 end
Chris@1295 328 unless groups.empty?
Chris@1295 329 s << %(<optgroup label="#{h(l(:label_group_plural))}">#{groups}</optgroup>)
Chris@1295 330 end
Chris@1295 331 s.html_safe
Chris@1295 332 end
Chris@1295 333
Chris@1295 334 # Options for the new membership projects combo-box
Chris@1295 335 def options_for_membership_project_select(principal, projects)
Chris@1295 336 options = content_tag('option', "--- #{l(:actionview_instancetag_blank_option)} ---")
Chris@1295 337 options << project_tree_options_for_select(projects) do |p|
Chris@1295 338 {:disabled => principal.projects.include?(p)}
Chris@1295 339 end
Chris@1295 340 options
Chris@1295 341 end
Chris@1295 342
Chris@1295 343 # Truncates and returns the string as a single line
Chris@1295 344 def truncate_single_line(string, *args)
Chris@1295 345 truncate(string.to_s, *args).gsub(%r{[\r\n]+}m, ' ')
Chris@1295 346 end
Chris@1295 347
Chris@1295 348 # Truncates at line break after 250 characters or options[:length]
Chris@1295 349 def truncate_lines(string, options={})
Chris@1295 350 length = options[:length] || 250
Chris@1295 351 if string.to_s =~ /\A(.{#{length}}.*?)$/m
Chris@1295 352 "#{$1}..."
Chris@1295 353 else
Chris@1295 354 string
Chris@1295 355 end
Chris@1295 356 end
Chris@1295 357
Chris@1295 358 def anchor(text)
Chris@1295 359 text.to_s.gsub(' ', '_')
Chris@1295 360 end
Chris@1295 361
Chris@1295 362 def html_hours(text)
Chris@1295 363 text.gsub(%r{(\d+)\.(\d+)}, '<span class="hours hours-int">\1</span><span class="hours hours-dec">.\2</span>').html_safe
Chris@1295 364 end
Chris@1295 365
Chris@1295 366 def authoring(created, author, options={})
Chris@1295 367 l(options[:label] || :label_added_time_by, :author => link_to_user(author), :age => time_tag(created)).html_safe
Chris@1295 368 end
Chris@1295 369
Chris@1295 370 def time_tag(time)
Chris@1295 371 text = distance_of_time_in_words(Time.now, time)
Chris@1295 372 if @project
Chris@1295 373 link_to(text, {:controller => 'activities', :action => 'index', :id => @project, :from => User.current.time_to_date(time)}, :title => format_time(time))
Chris@1295 374 else
Chris@1295 375 content_tag('acronym', text, :title => format_time(time))
Chris@1295 376 end
Chris@1295 377 end
Chris@1295 378
Chris@1295 379 def syntax_highlight_lines(name, content)
Chris@1295 380 lines = []
Chris@1295 381 syntax_highlight(name, content).each_line { |line| lines << line }
Chris@1295 382 lines
Chris@1295 383 end
Chris@1295 384
Chris@1295 385 def syntax_highlight(name, content)
Chris@1295 386 Redmine::SyntaxHighlighting.highlight_by_filename(content, name)
Chris@1295 387 end
Chris@1295 388
Chris@1295 389 def to_path_param(path)
Chris@1295 390 str = path.to_s.split(%r{[/\\]}).select{|p| !p.blank?}.join("/")
Chris@1295 391 str.blank? ? nil : str
Chris@1295 392 end
Chris@1295 393
Chris@1295 394 def pagination_links_full(paginator, count=nil, options={})
Chris@1295 395 page_param = options.delete(:page_param) || :page
Chris@1295 396 per_page_links = options.delete(:per_page_links)
Chris@1295 397 url_param = params.dup
Chris@1295 398
Chris@1295 399 html = ''
Chris@1295 400 if paginator.current.previous
Chris@1295 401 # \xc2\xab(utf-8) = &#171;
Chris@1295 402 html << link_to_content_update(
Chris@1295 403 "\xc2\xab " + l(:label_previous),
Chris@1295 404 url_param.merge(page_param => paginator.current.previous)) + ' '
Chris@1295 405 end
Chris@1295 406
Chris@1295 407 html << (pagination_links_each(paginator, options) do |n|
Chris@1295 408 link_to_content_update(n.to_s, url_param.merge(page_param => n))
Chris@1295 409 end || '')
Chris@1295 410
Chris@1295 411 if paginator.current.next
Chris@1295 412 # \xc2\xbb(utf-8) = &#187;
Chris@1295 413 html << ' ' + link_to_content_update(
Chris@1295 414 (l(:label_next) + " \xc2\xbb"),
Chris@1295 415 url_param.merge(page_param => paginator.current.next))
Chris@1295 416 end
Chris@1295 417
Chris@1295 418 unless count.nil?
Chris@1295 419 html << " (#{paginator.current.first_item}-#{paginator.current.last_item}/#{count})"
Chris@1295 420 if per_page_links != false && links = per_page_links(paginator.items_per_page, count)
Chris@1295 421 html << " | #{links}"
Chris@1295 422 end
Chris@1295 423 end
Chris@1295 424
Chris@1295 425 html.html_safe
Chris@1295 426 end
Chris@1295 427
Chris@1295 428 def per_page_links(selected=nil, item_count=nil)
Chris@1295 429 values = Setting.per_page_options_array
Chris@1295 430 if item_count && values.any?
Chris@1295 431 if item_count > values.first
Chris@1295 432 max = values.detect {|value| value >= item_count} || item_count
Chris@1295 433 else
Chris@1295 434 max = item_count
Chris@1295 435 end
Chris@1295 436 values = values.select {|value| value <= max || value == selected}
Chris@1295 437 end
Chris@1295 438 if values.empty? || (values.size == 1 && values.first == selected)
Chris@1295 439 return nil
Chris@1295 440 end
Chris@1295 441 links = values.collect do |n|
Chris@1295 442 n == selected ? n : link_to_content_update(n, params.merge(:per_page => n))
Chris@1295 443 end
Chris@1295 444 l(:label_display_per_page, links.join(', '))
Chris@1295 445 end
Chris@1295 446
Chris@1295 447 def reorder_links(name, url, method = :post)
Chris@1295 448 link_to(image_tag('2uparrow.png', :alt => l(:label_sort_highest)),
Chris@1295 449 url.merge({"#{name}[move_to]" => 'highest'}),
Chris@1295 450 :method => method, :title => l(:label_sort_highest)) +
Chris@1295 451 link_to(image_tag('1uparrow.png', :alt => l(:label_sort_higher)),
Chris@1295 452 url.merge({"#{name}[move_to]" => 'higher'}),
Chris@1295 453 :method => method, :title => l(:label_sort_higher)) +
Chris@1295 454 link_to(image_tag('1downarrow.png', :alt => l(:label_sort_lower)),
Chris@1295 455 url.merge({"#{name}[move_to]" => 'lower'}),
Chris@1295 456 :method => method, :title => l(:label_sort_lower)) +
Chris@1295 457 link_to(image_tag('2downarrow.png', :alt => l(:label_sort_lowest)),
Chris@1295 458 url.merge({"#{name}[move_to]" => 'lowest'}),
Chris@1295 459 :method => method, :title => l(:label_sort_lowest))
Chris@1295 460 end
Chris@1295 461
Chris@1295 462 def breadcrumb(*args)
Chris@1295 463 elements = args.flatten
Chris@1295 464 elements.any? ? content_tag('p', (args.join(" \xc2\xbb ") + " \xc2\xbb ").html_safe, :class => 'breadcrumb') : nil
Chris@1295 465 end
Chris@1295 466
Chris@1295 467 def other_formats_links(&block)
Chris@1295 468 concat('<p class="other-formats">'.html_safe + l(:label_export_to))
Chris@1295 469 yield Redmine::Views::OtherFormatsBuilder.new(self)
Chris@1295 470 concat('</p>'.html_safe)
Chris@1295 471 end
Chris@1295 472
Chris@1295 473 def page_header_title
Chris@1295 474 if @project.nil? || @project.new_record?
Chris@1295 475 h(Setting.app_title)
Chris@1295 476 else
Chris@1295 477 b = []
Chris@1295 478 ancestors = (@project.root? ? [] : @project.ancestors.visible.all)
Chris@1295 479 if ancestors.any?
Chris@1295 480 root = ancestors.shift
Chris@1295 481 b << link_to_project(root, {:jump => current_menu_item}, :class => 'root')
Chris@1295 482 if ancestors.size > 2
Chris@1295 483 b << "\xe2\x80\xa6"
Chris@1295 484 ancestors = ancestors[-2, 2]
Chris@1295 485 end
Chris@1295 486 b += ancestors.collect {|p| link_to_project(p, {:jump => current_menu_item}, :class => 'ancestor') }
Chris@1295 487 end
Chris@1295 488 b << h(@project)
Chris@1295 489 b.join(" \xc2\xbb ").html_safe
Chris@1295 490 end
Chris@1295 491 end
Chris@1295 492
Chris@1295 493 def html_title(*args)
Chris@1295 494 if args.empty?
Chris@1295 495 title = @html_title || []
Chris@1295 496 title << @project.name if @project
Chris@1295 497 title << Setting.app_title unless Setting.app_title == title.last
Chris@1295 498 title.select {|t| !t.blank? }.join(' - ')
Chris@1295 499 else
Chris@1295 500 @html_title ||= []
Chris@1295 501 @html_title += args
Chris@1295 502 end
Chris@1295 503 end
Chris@1295 504
Chris@1295 505 # Returns the theme, controller name, and action as css classes for the
Chris@1295 506 # HTML body.
Chris@1295 507 def body_css_classes
Chris@1295 508 css = []
Chris@1295 509 if theme = Redmine::Themes.theme(Setting.ui_theme)
Chris@1295 510 css << 'theme-' + theme.name
Chris@1295 511 end
Chris@1295 512
Chris@1295 513 css << 'controller-' + controller_name
Chris@1295 514 css << 'action-' + action_name
Chris@1295 515 css.join(' ')
Chris@1295 516 end
Chris@1295 517
Chris@1295 518 def accesskey(s)
Chris@1295 519 Redmine::AccessKeys.key_for s
Chris@1295 520 end
Chris@1295 521
Chris@1295 522 # Formats text according to system settings.
Chris@1295 523 # 2 ways to call this method:
Chris@1295 524 # * with a String: textilizable(text, options)
Chris@1295 525 # * with an object and one of its attribute: textilizable(issue, :description, options)
Chris@1295 526 def textilizable(*args)
Chris@1295 527 options = args.last.is_a?(Hash) ? args.pop : {}
Chris@1295 528 case args.size
Chris@1295 529 when 1
Chris@1295 530 obj = options[:object]
Chris@1295 531 text = args.shift
Chris@1295 532 when 2
Chris@1295 533 obj = args.shift
Chris@1295 534 attr = args.shift
Chris@1295 535 text = obj.send(attr).to_s
Chris@1295 536 else
Chris@1295 537 raise ArgumentError, 'invalid arguments to textilizable'
Chris@1295 538 end
Chris@1295 539 return '' if text.blank?
Chris@1295 540 project = options[:project] || @project || (obj && obj.respond_to?(:project) ? obj.project : nil)
Chris@1295 541 only_path = options.delete(:only_path) == false ? false : true
Chris@1295 542
Chris@1295 543 text = text.dup
Chris@1295 544 macros = catch_macros(text)
Chris@1295 545 text = Redmine::WikiFormatting.to_html(Setting.text_formatting, text, :object => obj, :attribute => attr)
Chris@1295 546
Chris@1295 547 @parsed_headings = []
Chris@1295 548 @heading_anchors = {}
Chris@1295 549 @current_section = 0 if options[:edit_section_links]
Chris@1295 550
Chris@1295 551 parse_sections(text, project, obj, attr, only_path, options)
Chris@1295 552 text = parse_non_pre_blocks(text, obj, macros) do |text|
Chris@1295 553 [:parse_inline_attachments, :parse_wiki_links, :parse_redmine_links].each do |method_name|
Chris@1295 554 send method_name, text, project, obj, attr, only_path, options
Chris@1295 555 end
Chris@1295 556 end
Chris@1295 557 parse_headings(text, project, obj, attr, only_path, options)
Chris@1295 558
Chris@1295 559 if @parsed_headings.any?
Chris@1295 560 replace_toc(text, @parsed_headings)
Chris@1295 561 end
Chris@1295 562
Chris@1295 563 text.html_safe
Chris@1295 564 end
Chris@1295 565
Chris@1295 566 def parse_non_pre_blocks(text, obj, macros)
Chris@1295 567 s = StringScanner.new(text)
Chris@1295 568 tags = []
Chris@1295 569 parsed = ''
Chris@1295 570 while !s.eos?
Chris@1295 571 s.scan(/(.*?)(<(\/)?(pre|code)(.*?)>|\z)/im)
Chris@1295 572 text, full_tag, closing, tag = s[1], s[2], s[3], s[4]
Chris@1295 573 if tags.empty?
Chris@1295 574 yield text
Chris@1295 575 inject_macros(text, obj, macros) if macros.any?
Chris@1295 576 else
Chris@1295 577 inject_macros(text, obj, macros, false) if macros.any?
Chris@1295 578 end
Chris@1295 579 parsed << text
Chris@1295 580 if tag
Chris@1295 581 if closing
Chris@1295 582 if tags.last == tag.downcase
Chris@1295 583 tags.pop
Chris@1295 584 end
Chris@1295 585 else
Chris@1295 586 tags << tag.downcase
Chris@1295 587 end
Chris@1295 588 parsed << full_tag
Chris@1295 589 end
Chris@1295 590 end
Chris@1295 591 # Close any non closing tags
Chris@1295 592 while tag = tags.pop
Chris@1295 593 parsed << "</#{tag}>"
Chris@1295 594 end
Chris@1295 595 parsed
Chris@1295 596 end
Chris@1295 597
Chris@1295 598 def parse_inline_attachments(text, project, obj, attr, only_path, options)
Chris@1295 599 # when using an image link, try to use an attachment, if possible
Chris@1295 600 attachments = options[:attachments] || []
Chris@1295 601 attachments += obj.attachments if obj.respond_to?(:attachments)
Chris@1295 602 if attachments.present?
Chris@1295 603 text.gsub!(/src="([^\/"]+\.(bmp|gif|jpg|jpe|jpeg|png))"(\s+alt="([^"]*)")?/i) do |m|
Chris@1295 604 filename, ext, alt, alttext = $1.downcase, $2, $3, $4
Chris@1295 605 # search for the picture in attachments
Chris@1295 606 if found = Attachment.latest_attach(attachments, filename)
Chris@1295 607 image_url = url_for :only_path => only_path, :controller => 'attachments',
Chris@1295 608 :action => 'download', :id => found
Chris@1295 609 desc = found.description.to_s.gsub('"', '')
Chris@1295 610 if !desc.blank? && alttext.blank?
Chris@1295 611 alt = " title=\"#{desc}\" alt=\"#{desc}\""
Chris@1295 612 end
Chris@1295 613 "src=\"#{image_url}\"#{alt}"
Chris@1295 614 else
Chris@1295 615 m
Chris@1295 616 end
Chris@1295 617 end
Chris@1295 618 end
Chris@1295 619 end
Chris@1295 620
Chris@1295 621 # Wiki links
Chris@1295 622 #
Chris@1295 623 # Examples:
Chris@1295 624 # [[mypage]]
Chris@1295 625 # [[mypage|mytext]]
Chris@1295 626 # wiki links can refer other project wikis, using project name or identifier:
Chris@1295 627 # [[project:]] -> wiki starting page
Chris@1295 628 # [[project:|mytext]]
Chris@1295 629 # [[project:mypage]]
Chris@1295 630 # [[project:mypage|mytext]]
Chris@1295 631 def parse_wiki_links(text, project, obj, attr, only_path, options)
Chris@1295 632 text.gsub!(/(!)?(\[\[([^\]\n\|]+)(\|([^\]\n\|]+))?\]\])/) do |m|
Chris@1295 633 link_project = project
Chris@1295 634 esc, all, page, title = $1, $2, $3, $5
Chris@1295 635 if esc.nil?
Chris@1295 636 if page =~ /^([^\:]+)\:(.*)$/
Chris@1295 637 link_project = Project.find_by_identifier($1) || Project.find_by_name($1)
Chris@1295 638 page = $2
Chris@1295 639 title ||= $1 if page.blank?
Chris@1295 640 end
Chris@1295 641
Chris@1295 642 if link_project && link_project.wiki
Chris@1295 643 # extract anchor
Chris@1295 644 anchor = nil
Chris@1295 645 if page =~ /^(.+?)\#(.+)$/
Chris@1295 646 page, anchor = $1, $2
Chris@1295 647 end
Chris@1295 648 anchor = sanitize_anchor_name(anchor) if anchor.present?
Chris@1295 649 # check if page exists
Chris@1295 650 wiki_page = link_project.wiki.find_page(page)
Chris@1295 651 url = if anchor.present? && wiki_page.present? && (obj.is_a?(WikiContent) || obj.is_a?(WikiContent::Version)) && obj.page == wiki_page
Chris@1295 652 "##{anchor}"
Chris@1295 653 else
Chris@1295 654 case options[:wiki_links]
Chris@1295 655 when :local; "#{page.present? ? Wiki.titleize(page) : ''}.html" + (anchor.present? ? "##{anchor}" : '')
Chris@1295 656 when :anchor; "##{page.present? ? Wiki.titleize(page) : title}" + (anchor.present? ? "_#{anchor}" : '') # used for single-file wiki export
Chris@1295 657 else
Chris@1295 658 wiki_page_id = page.present? ? Wiki.titleize(page) : nil
Chris@1295 659 parent = wiki_page.nil? && obj.is_a?(WikiContent) && obj.page && project == link_project ? obj.page.title : nil
Chris@1295 660 url_for(:only_path => only_path, :controller => 'wiki', :action => 'show', :project_id => link_project,
Chris@1295 661 :id => wiki_page_id, :version => nil, :anchor => anchor, :parent => parent)
Chris@1295 662 end
Chris@1295 663 end
Chris@1295 664 link_to(title.present? ? title.html_safe : h(page), url, :class => ('wiki-page' + (wiki_page ? '' : ' new')))
Chris@1295 665 else
Chris@1295 666 # project or wiki doesn't exist
Chris@1295 667 all
Chris@1295 668 end
Chris@1295 669 else
Chris@1295 670 all
Chris@1295 671 end
Chris@1295 672 end
Chris@1295 673 end
Chris@1295 674
Chris@1295 675 # Redmine links
Chris@1295 676 #
Chris@1295 677 # Examples:
Chris@1295 678 # Issues:
Chris@1295 679 # #52 -> Link to issue #52
Chris@1295 680 # Changesets:
Chris@1295 681 # r52 -> Link to revision 52
Chris@1295 682 # commit:a85130f -> Link to scmid starting with a85130f
Chris@1295 683 # Documents:
Chris@1295 684 # document#17 -> Link to document with id 17
Chris@1295 685 # document:Greetings -> Link to the document with title "Greetings"
Chris@1295 686 # document:"Some document" -> Link to the document with title "Some document"
Chris@1295 687 # Versions:
Chris@1295 688 # version#3 -> Link to version with id 3
Chris@1295 689 # version:1.0.0 -> Link to version named "1.0.0"
Chris@1295 690 # version:"1.0 beta 2" -> Link to version named "1.0 beta 2"
Chris@1295 691 # Attachments:
Chris@1295 692 # attachment:file.zip -> Link to the attachment of the current object named file.zip
Chris@1295 693 # Source files:
Chris@1295 694 # source:some/file -> Link to the file located at /some/file in the project's repository
Chris@1295 695 # source:some/file@52 -> Link to the file's revision 52
Chris@1295 696 # source:some/file#L120 -> Link to line 120 of the file
Chris@1295 697 # source:some/file@52#L120 -> Link to line 120 of the file's revision 52
Chris@1295 698 # export:some/file -> Force the download of the file
Chris@1295 699 # Forum messages:
Chris@1295 700 # message#1218 -> Link to message with id 1218
Chris@1295 701 #
Chris@1295 702 # Links can refer other objects from other projects, using project identifier:
Chris@1295 703 # identifier:r52
Chris@1295 704 # identifier:document:"Some document"
Chris@1295 705 # identifier:version:1.0.0
Chris@1295 706 # identifier:source:some/file
Chris@1295 707 def parse_redmine_links(text, default_project, obj, attr, only_path, options)
Chris@1295 708 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 709 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 710 link = nil
Chris@1295 711 project = default_project
Chris@1295 712 if project_identifier
Chris@1295 713 project = Project.visible.find_by_identifier(project_identifier)
Chris@1295 714 end
Chris@1295 715 if esc.nil?
Chris@1295 716 if prefix.nil? && sep == 'r'
Chris@1295 717 if project
Chris@1295 718 repository = nil
Chris@1295 719 if repo_identifier
Chris@1295 720 repository = project.repositories.detect {|repo| repo.identifier == repo_identifier}
Chris@1295 721 else
Chris@1295 722 repository = project.repository
Chris@1295 723 end
Chris@1295 724 # project.changesets.visible raises an SQL error because of a double join on repositories
Chris@1295 725 if repository && (changeset = Changeset.visible.find_by_repository_id_and_revision(repository.id, identifier))
Chris@1295 726 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 727 :class => 'changeset',
Chris@1295 728 :title => truncate_single_line(changeset.comments, :length => 100))
Chris@1295 729 end
Chris@1295 730 end
Chris@1295 731 elsif sep == '#'
Chris@1295 732 oid = identifier.to_i
Chris@1295 733 case prefix
Chris@1295 734 when nil
Chris@1295 735 if oid.to_s == identifier && issue = Issue.visible.find_by_id(oid, :include => :status)
Chris@1295 736 anchor = comment_id ? "note-#{comment_id}" : nil
Chris@1295 737 link = link_to("##{oid}", {:only_path => only_path, :controller => 'issues', :action => 'show', :id => oid, :anchor => anchor},
Chris@1295 738 :class => issue.css_classes,
Chris@1295 739 :title => "#{truncate(issue.subject, :length => 100)} (#{issue.status.name})")
Chris@1295 740 end
Chris@1295 741 when 'document'
Chris@1295 742 if document = Document.visible.find_by_id(oid)
Chris@1295 743 link = link_to h(document.title), {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document},
Chris@1295 744 :class => 'document'
Chris@1295 745 end
Chris@1295 746 when 'version'
Chris@1295 747 if version = Version.visible.find_by_id(oid)
Chris@1295 748 link = link_to h(version.name), {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version},
Chris@1295 749 :class => 'version'
Chris@1295 750 end
Chris@1295 751 when 'message'
Chris@1295 752 if message = Message.visible.find_by_id(oid, :include => :parent)
Chris@1295 753 link = link_to_message(message, {:only_path => only_path}, :class => 'message')
Chris@1295 754 end
Chris@1295 755 when 'forum'
Chris@1295 756 if board = Board.visible.find_by_id(oid)
Chris@1295 757 link = link_to h(board.name), {:only_path => only_path, :controller => 'boards', :action => 'show', :id => board, :project_id => board.project},
Chris@1295 758 :class => 'board'
Chris@1295 759 end
Chris@1295 760 when 'news'
Chris@1295 761 if news = News.visible.find_by_id(oid)
Chris@1295 762 link = link_to h(news.title), {:only_path => only_path, :controller => 'news', :action => 'show', :id => news},
Chris@1295 763 :class => 'news'
Chris@1295 764 end
Chris@1295 765 when 'project'
Chris@1295 766 if p = Project.visible.find_by_id(oid)
Chris@1295 767 link = link_to_project(p, {:only_path => only_path}, :class => 'project')
Chris@1295 768 end
Chris@1295 769 end
Chris@1295 770 elsif sep == ':'
Chris@1295 771 # removes the double quotes if any
Chris@1295 772 name = identifier.gsub(%r{^"(.*)"$}, "\\1")
Chris@1295 773 case prefix
Chris@1295 774 when 'document'
Chris@1295 775 if project && document = project.documents.visible.find_by_title(name)
Chris@1295 776 link = link_to h(document.title), {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document},
Chris@1295 777 :class => 'document'
Chris@1295 778 end
Chris@1295 779 when 'version'
Chris@1295 780 if project && version = project.versions.visible.find_by_name(name)
Chris@1295 781 link = link_to h(version.name), {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version},
Chris@1295 782 :class => 'version'
Chris@1295 783 end
Chris@1295 784 when 'forum'
Chris@1295 785 if project && board = project.boards.visible.find_by_name(name)
Chris@1295 786 link = link_to h(board.name), {:only_path => only_path, :controller => 'boards', :action => 'show', :id => board, :project_id => board.project},
Chris@1295 787 :class => 'board'
Chris@1295 788 end
Chris@1295 789 when 'news'
Chris@1295 790 if project && news = project.news.visible.find_by_title(name)
Chris@1295 791 link = link_to h(news.title), {:only_path => only_path, :controller => 'news', :action => 'show', :id => news},
Chris@1295 792 :class => 'news'
Chris@1295 793 end
Chris@1295 794 when 'commit', 'source', 'export'
Chris@1295 795 if project
Chris@1295 796 repository = nil
Chris@1295 797 if name =~ %r{^(([a-z0-9\-_]+)\|)(.+)$}
Chris@1295 798 repo_prefix, repo_identifier, name = $1, $2, $3
Chris@1295 799 repository = project.repositories.detect {|repo| repo.identifier == repo_identifier}
Chris@1295 800 else
Chris@1295 801 repository = project.repository
Chris@1295 802 end
Chris@1295 803 if prefix == 'commit'
Chris@1295 804 if repository && (changeset = Changeset.visible.find(:first, :conditions => ["repository_id = ? AND scmid LIKE ?", repository.id, "#{name}%"]))
Chris@1295 805 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 806 :class => 'changeset',
Chris@1295 807 :title => truncate_single_line(h(changeset.comments), :length => 100)
Chris@1295 808 end
Chris@1295 809 else
Chris@1295 810 if repository && User.current.allowed_to?(:browse_repository, project)
Chris@1295 811 name =~ %r{^[/\\]*(.*?)(@([0-9a-f]+))?(#(L\d+))?$}
Chris@1295 812 path, rev, anchor = $1, $3, $5
Chris@1295 813 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 814 :path => to_path_param(path),
Chris@1295 815 :rev => rev,
Chris@1295 816 :anchor => anchor},
Chris@1295 817 :class => (prefix == 'export' ? 'source download' : 'source')
Chris@1295 818 end
Chris@1295 819 end
Chris@1295 820 repo_prefix = nil
Chris@1295 821 end
Chris@1295 822 when 'attachment'
Chris@1295 823 attachments = options[:attachments] || (obj && obj.respond_to?(:attachments) ? obj.attachments : nil)
Chris@1295 824 if attachments && attachment = Attachment.latest_attach(attachments, name)
Chris@1295 825 link = link_to h(attachment.filename), {:only_path => only_path, :controller => 'attachments', :action => 'download', :id => attachment},
Chris@1295 826 :class => 'attachment'
Chris@1295 827 end
Chris@1295 828 when 'project'
Chris@1295 829 if p = Project.visible.find(:first, :conditions => ["identifier = :s OR LOWER(name) = :s", {:s => name.downcase}])
Chris@1295 830 link = link_to_project(p, {:only_path => only_path}, :class => 'project')
Chris@1295 831 end
Chris@1295 832 end
Chris@1295 833 end
Chris@1295 834 end
Chris@1295 835 (leading + (link || "#{project_prefix}#{prefix}#{repo_prefix}#{sep}#{identifier}#{comment_suffix}"))
Chris@1295 836 end
Chris@1295 837 end
Chris@1295 838
Chris@1295 839 HEADING_RE = /(<h(\d)( [^>]+)?>(.+?)<\/h(\d)>)/i unless const_defined?(:HEADING_RE)
Chris@1295 840
Chris@1295 841 def parse_sections(text, project, obj, attr, only_path, options)
Chris@1295 842 return unless options[:edit_section_links]
Chris@1295 843 text.gsub!(HEADING_RE) do
Chris@1295 844 heading = $1
Chris@1295 845 @current_section += 1
Chris@1295 846 if @current_section > 1
Chris@1295 847 content_tag('div',
Chris@1295 848 link_to(image_tag('edit.png'), options[:edit_section_links].merge(:section => @current_section)),
Chris@1295 849 :class => 'contextual',
Chris@1295 850 :title => l(:button_edit_section)) + heading.html_safe
Chris@1295 851 else
Chris@1295 852 heading
Chris@1295 853 end
Chris@1295 854 end
Chris@1295 855 end
Chris@1295 856
Chris@1295 857 # Headings and TOC
Chris@1295 858 # Adds ids and links to headings unless options[:headings] is set to false
Chris@1295 859 def parse_headings(text, project, obj, attr, only_path, options)
Chris@1295 860 return if options[:headings] == false
Chris@1295 861
Chris@1295 862 text.gsub!(HEADING_RE) do
Chris@1295 863 level, attrs, content = $2.to_i, $3, $4
Chris@1295 864 item = strip_tags(content).strip
Chris@1295 865 anchor = sanitize_anchor_name(item)
Chris@1295 866 # used for single-file wiki export
Chris@1295 867 anchor = "#{obj.page.title}_#{anchor}" if options[:wiki_links] == :anchor && (obj.is_a?(WikiContent) || obj.is_a?(WikiContent::Version))
Chris@1295 868 @heading_anchors[anchor] ||= 0
Chris@1295 869 idx = (@heading_anchors[anchor] += 1)
Chris@1295 870 if idx > 1
Chris@1295 871 anchor = "#{anchor}-#{idx}"
Chris@1295 872 end
Chris@1295 873 @parsed_headings << [level, anchor, item]
Chris@1295 874 "<a name=\"#{anchor}\"></a>\n<h#{level} #{attrs}>#{content}<a href=\"##{anchor}\" class=\"wiki-anchor\">&para;</a></h#{level}>"
Chris@1295 875 end
Chris@1295 876 end
Chris@1295 877
Chris@1295 878 MACROS_RE = /(
Chris@1295 879 (!)? # escaping
Chris@1295 880 (
Chris@1295 881 \{\{ # opening tag
Chris@1295 882 ([\w]+) # macro name
Chris@1295 883 (\(([^\n\r]*?)\))? # optional arguments
Chris@1295 884 ([\n\r].*?[\n\r])? # optional block of text
Chris@1295 885 \}\} # closing tag
Chris@1295 886 )
Chris@1295 887 )/mx unless const_defined?(:MACROS_RE)
Chris@1295 888
Chris@1295 889 MACRO_SUB_RE = /(
Chris@1295 890 \{\{
Chris@1295 891 macro\((\d+)\)
Chris@1295 892 \}\}
Chris@1295 893 )/x unless const_defined?(:MACRO_SUB_RE)
Chris@1295 894
Chris@1295 895 # Extracts macros from text
Chris@1295 896 def catch_macros(text)
Chris@1295 897 macros = {}
Chris@1295 898 text.gsub!(MACROS_RE) do
Chris@1295 899 all, macro = $1, $4.downcase
Chris@1295 900 if macro_exists?(macro) || all =~ MACRO_SUB_RE
Chris@1295 901 index = macros.size
Chris@1295 902 macros[index] = all
Chris@1295 903 "{{macro(#{index})}}"
Chris@1295 904 else
Chris@1295 905 all
Chris@1295 906 end
Chris@1295 907 end
Chris@1295 908 macros
Chris@1295 909 end
Chris@1295 910
Chris@1295 911 # Executes and replaces macros in text
Chris@1295 912 def inject_macros(text, obj, macros, execute=true)
Chris@1295 913 text.gsub!(MACRO_SUB_RE) do
Chris@1295 914 all, index = $1, $2.to_i
Chris@1295 915 orig = macros.delete(index)
Chris@1295 916 if execute && orig && orig =~ MACROS_RE
Chris@1295 917 esc, all, macro, args, block = $2, $3, $4.downcase, $6.to_s, $7.try(:strip)
Chris@1295 918 if esc.nil?
Chris@1295 919 h(exec_macro(macro, obj, args, block) || all)
Chris@1295 920 else
Chris@1295 921 h(all)
Chris@1295 922 end
Chris@1295 923 elsif orig
Chris@1295 924 h(orig)
Chris@1295 925 else
Chris@1295 926 h(all)
Chris@1295 927 end
Chris@1295 928 end
Chris@1295 929 end
Chris@1295 930
Chris@1295 931 TOC_RE = /<p>\{\{([<>]?)toc\}\}<\/p>/i unless const_defined?(:TOC_RE)
Chris@1295 932
Chris@1295 933 # Renders the TOC with given headings
Chris@1295 934 def replace_toc(text, headings)
Chris@1295 935 text.gsub!(TOC_RE) do
Chris@1295 936 # Keep only the 4 first levels
Chris@1295 937 headings = headings.select{|level, anchor, item| level <= 4}
Chris@1295 938 if headings.empty?
Chris@1295 939 ''
Chris@1295 940 else
Chris@1295 941 div_class = 'toc'
Chris@1295 942 div_class << ' right' if $1 == '>'
Chris@1295 943 div_class << ' left' if $1 == '<'
Chris@1295 944 out = "<ul class=\"#{div_class}\"><li>"
Chris@1295 945 root = headings.map(&:first).min
Chris@1295 946 current = root
Chris@1295 947 started = false
Chris@1295 948 headings.each do |level, anchor, item|
Chris@1295 949 if level > current
Chris@1295 950 out << '<ul><li>' * (level - current)
Chris@1295 951 elsif level < current
Chris@1295 952 out << "</li></ul>\n" * (current - level) + "</li><li>"
Chris@1295 953 elsif started
Chris@1295 954 out << '</li><li>'
Chris@1295 955 end
Chris@1295 956 out << "<a href=\"##{anchor}\">#{item}</a>"
Chris@1295 957 current = level
Chris@1295 958 started = true
Chris@1295 959 end
Chris@1295 960 out << '</li></ul>' * (current - root)
Chris@1295 961 out << '</li></ul>'
Chris@1295 962 end
Chris@1295 963 end
Chris@1295 964 end
Chris@1295 965
Chris@1295 966 # Same as Rails' simple_format helper without using paragraphs
Chris@1295 967 def simple_format_without_paragraph(text)
Chris@1295 968 text.to_s.
Chris@1295 969 gsub(/\r\n?/, "\n"). # \r\n and \r -> \n
Chris@1295 970 gsub(/\n\n+/, "<br /><br />"). # 2+ newline -> 2 br
Chris@1295 971 gsub(/([^\n]\n)(?=[^\n])/, '\1<br />'). # 1 newline -> br
Chris@1295 972 html_safe
Chris@1295 973 end
Chris@1295 974
Chris@1295 975 def lang_options_for_select(blank=true)
Chris@1295 976 (blank ? [["(auto)", ""]] : []) + languages_options
Chris@1295 977 end
Chris@1295 978
Chris@1295 979 def label_tag_for(name, option_tags = nil, options = {})
Chris@1295 980 label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "")
Chris@1295 981 content_tag("label", label_text)
Chris@1295 982 end
Chris@1295 983
Chris@1295 984 def labelled_form_for(*args, &proc)
Chris@1295 985 args << {} unless args.last.is_a?(Hash)
Chris@1295 986 options = args.last
Chris@1295 987 if args.first.is_a?(Symbol)
Chris@1295 988 options.merge!(:as => args.shift)
Chris@1295 989 end
Chris@1295 990 options.merge!({:builder => Redmine::Views::LabelledFormBuilder})
Chris@1295 991 form_for(*args, &proc)
Chris@1295 992 end
Chris@1295 993
Chris@1295 994 def labelled_fields_for(*args, &proc)
Chris@1295 995 args << {} unless args.last.is_a?(Hash)
Chris@1295 996 options = args.last
Chris@1295 997 options.merge!({:builder => Redmine::Views::LabelledFormBuilder})
Chris@1295 998 fields_for(*args, &proc)
Chris@1295 999 end
Chris@1295 1000
Chris@1295 1001 def labelled_remote_form_for(*args, &proc)
Chris@1295 1002 ActiveSupport::Deprecation.warn "ApplicationHelper#labelled_remote_form_for is deprecated and will be removed in Redmine 2.2."
Chris@1295 1003 args << {} unless args.last.is_a?(Hash)
Chris@1295 1004 options = args.last
Chris@1295 1005 options.merge!({:builder => Redmine::Views::LabelledFormBuilder, :remote => true})
Chris@1295 1006 form_for(*args, &proc)
Chris@1295 1007 end
Chris@1295 1008
Chris@1295 1009 def error_messages_for(*objects)
Chris@1295 1010 html = ""
Chris@1295 1011 objects = objects.map {|o| o.is_a?(String) ? instance_variable_get("@#{o}") : o}.compact
Chris@1295 1012 errors = objects.map {|o| o.errors.full_messages}.flatten
Chris@1295 1013 if errors.any?
Chris@1295 1014 html << "<div id='errorExplanation'><ul>\n"
Chris@1295 1015 errors.each do |error|
Chris@1295 1016 html << "<li>#{h error}</li>\n"
Chris@1295 1017 end
Chris@1295 1018 html << "</ul></div>\n"
Chris@1295 1019 end
Chris@1295 1020 html.html_safe
Chris@1295 1021 end
Chris@1295 1022
Chris@1295 1023 def delete_link(url, options={})
Chris@1295 1024 options = {
Chris@1295 1025 :method => :delete,
Chris@1295 1026 :data => {:confirm => l(:text_are_you_sure)},
Chris@1295 1027 :class => 'icon icon-del'
Chris@1295 1028 }.merge(options)
Chris@1295 1029
Chris@1295 1030 link_to l(:button_delete), url, options
Chris@1295 1031 end
Chris@1295 1032
Chris@1295 1033 def preview_link(url, form, target='preview', options={})
Chris@1295 1034 content_tag 'a', l(:label_preview), {
Chris@1295 1035 :href => "#",
Chris@1295 1036 :onclick => %|submitPreview("#{escape_javascript url_for(url)}", "#{escape_javascript form}", "#{escape_javascript target}"); return false;|,
Chris@1295 1037 :accesskey => accesskey(:preview)
Chris@1295 1038 }.merge(options)
Chris@1295 1039 end
Chris@1295 1040
Chris@1295 1041 def link_to_function(name, function, html_options={})
Chris@1295 1042 content_tag(:a, name, {:href => '#', :onclick => "#{function}; return false;"}.merge(html_options))
Chris@1295 1043 end
Chris@1295 1044
Chris@1295 1045 # Helper to render JSON in views
Chris@1295 1046 def raw_json(arg)
Chris@1295 1047 arg.to_json.to_s.gsub('/', '\/').html_safe
Chris@1295 1048 end
Chris@1295 1049
Chris@1295 1050 def back_url
Chris@1295 1051 url = params[:back_url]
Chris@1295 1052 if url.nil? && referer = request.env['HTTP_REFERER']
Chris@1295 1053 url = CGI.unescape(referer.to_s)
Chris@1295 1054 end
Chris@1295 1055 url
Chris@1295 1056 end
Chris@1295 1057
Chris@1295 1058 def back_url_hidden_field_tag
Chris@1295 1059 url = back_url
Chris@1295 1060 hidden_field_tag('back_url', url, :id => nil) unless url.blank?
Chris@1295 1061 end
Chris@1295 1062
Chris@1295 1063 def check_all_links(form_name)
Chris@1295 1064 link_to_function(l(:button_check_all), "checkAll('#{form_name}', true)") +
Chris@1295 1065 " | ".html_safe +
Chris@1295 1066 link_to_function(l(:button_uncheck_all), "checkAll('#{form_name}', false)")
Chris@1295 1067 end
Chris@1295 1068
Chris@1295 1069 def progress_bar(pcts, options={})
Chris@1295 1070 pcts = [pcts, pcts] unless pcts.is_a?(Array)
Chris@1295 1071 pcts = pcts.collect(&:round)
Chris@1295 1072 pcts[1] = pcts[1] - pcts[0]
Chris@1295 1073 pcts << (100 - pcts[1] - pcts[0])
Chris@1295 1074 width = options[:width] || '100px;'
Chris@1295 1075 legend = options[:legend] || ''
Chris@1295 1076 content_tag('table',
Chris@1295 1077 content_tag('tr',
Chris@1295 1078 (pcts[0] > 0 ? content_tag('td', '', :style => "width: #{pcts[0]}%;", :class => 'closed') : ''.html_safe) +
Chris@1295 1079 (pcts[1] > 0 ? content_tag('td', '', :style => "width: #{pcts[1]}%;", :class => 'done') : ''.html_safe) +
Chris@1295 1080 (pcts[2] > 0 ? content_tag('td', '', :style => "width: #{pcts[2]}%;", :class => 'todo') : ''.html_safe)
Chris@1295 1081 ), :class => 'progress', :style => "width: #{width};").html_safe +
Chris@1295 1082 content_tag('p', legend, :class => 'pourcent').html_safe
Chris@1295 1083 end
Chris@1295 1084
Chris@1295 1085 def checked_image(checked=true)
Chris@1295 1086 if checked
Chris@1295 1087 image_tag 'toggle_check.png'
Chris@1295 1088 end
Chris@1295 1089 end
Chris@1295 1090
Chris@1295 1091 def context_menu(url)
Chris@1295 1092 unless @context_menu_included
Chris@1295 1093 content_for :header_tags do
Chris@1295 1094 javascript_include_tag('context_menu') +
Chris@1295 1095 stylesheet_link_tag('context_menu')
Chris@1295 1096 end
Chris@1295 1097 if l(:direction) == 'rtl'
Chris@1295 1098 content_for :header_tags do
Chris@1295 1099 stylesheet_link_tag('context_menu_rtl')
Chris@1295 1100 end
Chris@1295 1101 end
Chris@1295 1102 @context_menu_included = true
Chris@1295 1103 end
Chris@1295 1104 javascript_tag "contextMenuInit('#{ url_for(url) }')"
Chris@1295 1105 end
Chris@1295 1106
Chris@1295 1107 def calendar_for(field_id)
Chris@1295 1108 include_calendar_headers_tags
Chris@1295 1109 javascript_tag("$(function() { $('##{field_id}').datepicker(datepickerOptions); });")
Chris@1295 1110 end
Chris@1295 1111
Chris@1295 1112 def include_calendar_headers_tags
Chris@1295 1113 unless @calendar_headers_tags_included
Chris@1295 1114 @calendar_headers_tags_included = true
Chris@1295 1115 content_for :header_tags do
Chris@1295 1116 start_of_week = Setting.start_of_week
Chris@1295 1117 start_of_week = l(:general_first_day_of_week, :default => '1') if start_of_week.blank?
Chris@1295 1118 # Redmine uses 1..7 (monday..sunday) in settings and locales
Chris@1295 1119 # JQuery uses 0..6 (sunday..saturday), 7 needs to be changed to 0
Chris@1295 1120 start_of_week = start_of_week.to_i % 7
Chris@1295 1121
Chris@1295 1122 tags = javascript_tag(
Chris@1295 1123 "var datepickerOptions={dateFormat: 'yy-mm-dd', firstDay: #{start_of_week}, " +
Chris@1295 1124 "showOn: 'button', buttonImageOnly: true, buttonImage: '" +
Chris@1295 1125 path_to_image('/images/calendar.png') +
Chris@1295 1126 "', showButtonPanel: true};")
Chris@1295 1127 jquery_locale = l('jquery.locale', :default => current_language.to_s)
Chris@1295 1128 unless jquery_locale == 'en'
Chris@1295 1129 tags << javascript_include_tag("i18n/jquery.ui.datepicker-#{jquery_locale}.js")
Chris@1295 1130 end
Chris@1295 1131 tags
Chris@1295 1132 end
Chris@1295 1133 end
Chris@1295 1134 end
Chris@1295 1135
Chris@1295 1136 # Overrides Rails' stylesheet_link_tag with themes and plugins support.
Chris@1295 1137 # Examples:
Chris@1295 1138 # stylesheet_link_tag('styles') # => picks styles.css from the current theme or defaults
Chris@1295 1139 # stylesheet_link_tag('styles', :plugin => 'foo) # => picks styles.css from plugin's assets
Chris@1295 1140 #
Chris@1295 1141 def stylesheet_link_tag(*sources)
Chris@1295 1142 options = sources.last.is_a?(Hash) ? sources.pop : {}
Chris@1295 1143 plugin = options.delete(:plugin)
Chris@1295 1144 sources = sources.map do |source|
Chris@1295 1145 if plugin
Chris@1295 1146 "/plugin_assets/#{plugin}/stylesheets/#{source}"
Chris@1295 1147 elsif current_theme && current_theme.stylesheets.include?(source)
Chris@1295 1148 current_theme.stylesheet_path(source)
Chris@1295 1149 else
Chris@1295 1150 source
Chris@1295 1151 end
Chris@1295 1152 end
Chris@1295 1153 super sources, options
Chris@1295 1154 end
Chris@1295 1155
Chris@1295 1156 # Overrides Rails' image_tag with themes and plugins support.
Chris@1295 1157 # Examples:
Chris@1295 1158 # image_tag('image.png') # => picks image.png from the current theme or defaults
Chris@1295 1159 # image_tag('image.png', :plugin => 'foo) # => picks image.png from plugin's assets
Chris@1295 1160 #
Chris@1295 1161 def image_tag(source, options={})
Chris@1295 1162 if plugin = options.delete(:plugin)
Chris@1295 1163 source = "/plugin_assets/#{plugin}/images/#{source}"
Chris@1295 1164 elsif current_theme && current_theme.images.include?(source)
Chris@1295 1165 source = current_theme.image_path(source)
Chris@1295 1166 end
Chris@1295 1167 super source, options
Chris@1295 1168 end
Chris@1295 1169
Chris@1295 1170 # Overrides Rails' javascript_include_tag with plugins support
Chris@1295 1171 # Examples:
Chris@1295 1172 # javascript_include_tag('scripts') # => picks scripts.js from defaults
Chris@1295 1173 # javascript_include_tag('scripts', :plugin => 'foo) # => picks scripts.js from plugin's assets
Chris@1295 1174 #
Chris@1295 1175 def javascript_include_tag(*sources)
Chris@1295 1176 options = sources.last.is_a?(Hash) ? sources.pop : {}
Chris@1295 1177 if plugin = options.delete(:plugin)
Chris@1295 1178 sources = sources.map do |source|
Chris@1295 1179 if plugin
Chris@1295 1180 "/plugin_assets/#{plugin}/javascripts/#{source}"
Chris@1295 1181 else
Chris@1295 1182 source
Chris@1295 1183 end
Chris@1295 1184 end
Chris@1295 1185 end
Chris@1295 1186 super sources, options
Chris@1295 1187 end
Chris@1295 1188
Chris@1295 1189 def content_for(name, content = nil, &block)
Chris@1295 1190 @has_content ||= {}
Chris@1295 1191 @has_content[name] = true
Chris@1295 1192 super(name, content, &block)
Chris@1295 1193 end
Chris@1295 1194
Chris@1295 1195 def has_content?(name)
Chris@1295 1196 (@has_content && @has_content[name]) || false
Chris@1295 1197 end
Chris@1295 1198
Chris@1295 1199 def sidebar_content?
Chris@1295 1200 has_content?(:sidebar) || view_layouts_base_sidebar_hook_response.present?
Chris@1295 1201 end
Chris@1295 1202
Chris@1295 1203 def view_layouts_base_sidebar_hook_response
Chris@1295 1204 @view_layouts_base_sidebar_hook_response ||= call_hook(:view_layouts_base_sidebar)
Chris@1295 1205 end
Chris@1295 1206
Chris@1295 1207 def email_delivery_enabled?
Chris@1295 1208 !!ActionMailer::Base.perform_deliveries
Chris@1295 1209 end
Chris@1295 1210
Chris@1295 1211 # Returns the avatar image tag for the given +user+ if avatars are enabled
Chris@1295 1212 # +user+ can be a User or a string that will be scanned for an email address (eg. 'joe <joe@foo.bar>')
Chris@1295 1213 def avatar(user, options = { })
Chris@1295 1214 if Setting.gravatar_enabled?
Chris@1295 1215 options.merge!({:ssl => (request && request.ssl?), :default => Setting.gravatar_default})
Chris@1295 1216 email = nil
Chris@1295 1217 if user.respond_to?(:mail)
Chris@1295 1218 email = user.mail
Chris@1295 1219 elsif user.to_s =~ %r{<(.+?)>}
Chris@1295 1220 email = $1
Chris@1295 1221 end
Chris@1295 1222 return gravatar(email.to_s.downcase, options) unless email.blank? rescue nil
Chris@1295 1223 else
Chris@1295 1224 ''
Chris@1295 1225 end
Chris@1295 1226 end
Chris@1295 1227
Chris@1295 1228 def sanitize_anchor_name(anchor)
Chris@1295 1229 if ''.respond_to?(:encoding) || RUBY_PLATFORM == 'java'
Chris@1295 1230 anchor.gsub(%r{[^\p{Word}\s\-]}, '').gsub(%r{\s+(\-+\s*)?}, '-')
Chris@1295 1231 else
Chris@1295 1232 # TODO: remove when ruby1.8 is no longer supported
Chris@1295 1233 anchor.gsub(%r{[^\w\s\-]}, '').gsub(%r{\s+(\-+\s*)?}, '-')
Chris@1295 1234 end
Chris@1295 1235 end
Chris@1295 1236
Chris@1295 1237 # Returns the javascript tags that are included in the html layout head
Chris@1295 1238 def javascript_heads
Chris@1295 1239 tags = javascript_include_tag('jquery-1.7.2-ui-1.8.21-ujs-2.0.3', 'application')
Chris@1295 1240 unless User.current.pref.warn_on_leaving_unsaved == '0'
Chris@1295 1241 tags << "\n".html_safe + javascript_tag("$(window).load(function(){ warnLeavingUnsaved('#{escape_javascript l(:text_warn_on_leaving_unsaved)}'); });")
Chris@1295 1242 end
Chris@1295 1243 tags
Chris@1295 1244 end
Chris@1295 1245
Chris@1295 1246 def favicon
Chris@1295 1247 "<link rel='shortcut icon' href='#{image_path('/favicon.ico')}' />".html_safe
Chris@1295 1248 end
Chris@1295 1249
Chris@1295 1250 def robot_exclusion_tag
Chris@1295 1251 '<meta name="robots" content="noindex,follow,noarchive" />'.html_safe
Chris@1295 1252 end
Chris@1295 1253
Chris@1295 1254 # Returns true if arg is expected in the API response
Chris@1295 1255 def include_in_api_response?(arg)
Chris@1295 1256 unless @included_in_api_response
Chris@1295 1257 param = params[:include]
Chris@1295 1258 @included_in_api_response = param.is_a?(Array) ? param.collect(&:to_s) : param.to_s.split(',')
Chris@1295 1259 @included_in_api_response.collect!(&:strip)
Chris@1295 1260 end
Chris@1295 1261 @included_in_api_response.include?(arg.to_s)
Chris@1295 1262 end
Chris@1295 1263
Chris@1295 1264 # Returns options or nil if nometa param or X-Redmine-Nometa header
Chris@1295 1265 # was set in the request
Chris@1295 1266 def api_meta(options)
Chris@1295 1267 if params[:nometa].present? || request.headers['X-Redmine-Nometa']
Chris@1295 1268 # compatibility mode for activeresource clients that raise
Chris@1295 1269 # an error when unserializing an array with attributes
Chris@1295 1270 nil
Chris@1295 1271 else
Chris@1295 1272 options
Chris@1295 1273 end
Chris@1295 1274 end
Chris@1295 1275
Chris@1295 1276 private
Chris@1295 1277
Chris@1295 1278 def wiki_helper
Chris@1295 1279 helper = Redmine::WikiFormatting.helper_for(Setting.text_formatting)
Chris@1295 1280 extend helper
Chris@1295 1281 return self
Chris@1295 1282 end
Chris@1295 1283
Chris@1295 1284 def link_to_content_update(text, url_params = {}, html_options = {})
Chris@1295 1285 link_to(text, url_params, html_options)
Chris@1295 1286 end
Chris@1295 1287 end