annotate app/helpers/application_helper.rb @ 1022:f2ec92061fca browsing

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