annotate app/helpers/application_helper.rb @ 929:5f33065ddc4b redmine-1.3

Update to Redmine SVN rev 9414 on 1.3-stable branch
author Chris Cannam
date Wed, 27 Jun 2012 14:54:18 +0100
parents cbb26bc654de
children ec1c49528f36 433d4f72a19b
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@0 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|
Chris@0 294 s << "<label>#{ check_box_tag name, principal.id, false } #{h principal}</label>\n"
Chris@0 295 end
Chris@909 296 s.html_safe
Chris@909 297 end
Chris@909 298
Chris@909 299 # Returns a string for users/groups option tags
Chris@909 300 def principals_options_for_select(collection, selected=nil)
Chris@909 301 s = ''
Chris@909 302 groups = ''
Chris@909 303 collection.sort.each do |element|
Chris@909 304 selected_attribute = ' selected="selected"' if option_value_selected?(element, selected)
Chris@909 305 (element.is_a?(Group) ? groups : s) << %(<option value="#{element.id}"#{selected_attribute}>#{h element.name}</option>)
Chris@909 306 end
Chris@909 307 unless groups.empty?
Chris@909 308 s << %(<optgroup label="#{h(l(:label_group_plural))}">#{groups}</optgroup>)
Chris@909 309 end
Chris@441 310 s
Chris@0 311 end
Chris@0 312
Chris@0 313 # Truncates and returns the string as a single line
Chris@0 314 def truncate_single_line(string, *args)
Chris@0 315 truncate(string.to_s, *args).gsub(%r{[\r\n]+}m, ' ')
Chris@0 316 end
Chris@441 317
Chris@0 318 # Truncates at line break after 250 characters or options[:length]
Chris@0 319 def truncate_lines(string, options={})
Chris@0 320 length = options[:length] || 250
Chris@0 321 if string.to_s =~ /\A(.{#{length}}.*?)$/m
Chris@0 322 "#{$1}..."
Chris@0 323 else
Chris@0 324 string
Chris@0 325 end
Chris@0 326 end
Chris@0 327
Chris@0 328 def html_hours(text)
Chris@909 329 text.gsub(%r{(\d+)\.(\d+)}, '<span class="hours hours-int">\1</span><span class="hours hours-dec">.\2</span>').html_safe
Chris@0 330 end
Chris@0 331
Chris@0 332 def authoring(created, author, options={})
Chris@909 333 l(options[:label] || :label_added_time_by, :author => link_to_user(author), :age => time_tag(created)).html_safe
Chris@0 334 end
Chris@441 335
Chris@0 336 def time_tag(time)
Chris@0 337 text = distance_of_time_in_words(Time.now, time)
Chris@0 338 if @project
chris@22 339 link_to(text, {:controller => 'activities', :action => 'index', :id => @project, :from => time.to_date}, :title => format_time(time))
Chris@0 340 else
Chris@0 341 content_tag('acronym', text, :title => format_time(time))
Chris@0 342 end
Chris@0 343 end
Chris@0 344
Chris@0 345 def syntax_highlight(name, content)
Chris@0 346 Redmine::SyntaxHighlighting.highlight_by_filename(content, name)
Chris@0 347 end
Chris@0 348
Chris@0 349 def to_path_param(path)
Chris@0 350 path.to_s.split(%r{[/\\]}).select {|p| !p.blank?}
Chris@0 351 end
Chris@0 352
Chris@0 353 def pagination_links_full(paginator, count=nil, options={})
Chris@0 354 page_param = options.delete(:page_param) || :page
Chris@0 355 per_page_links = options.delete(:per_page_links)
Chris@0 356 url_param = params.dup
Chris@0 357
Chris@0 358 html = ''
Chris@0 359 if paginator.current.previous
Chris@909 360 # \xc2\xab(utf-8) = &#171;
Chris@909 361 html << link_to_content_update(
Chris@909 362 "\xc2\xab " + l(:label_previous),
Chris@909 363 url_param.merge(page_param => paginator.current.previous)) + ' '
Chris@0 364 end
Chris@0 365
Chris@0 366 html << (pagination_links_each(paginator, options) do |n|
Chris@441 367 link_to_content_update(n.to_s, url_param.merge(page_param => n))
Chris@0 368 end || '')
Chris@441 369
Chris@0 370 if paginator.current.next
Chris@909 371 # \xc2\xbb(utf-8) = &#187;
Chris@909 372 html << ' ' + link_to_content_update(
Chris@909 373 (l(:label_next) + " \xc2\xbb"),
Chris@909 374 url_param.merge(page_param => paginator.current.next))
Chris@0 375 end
Chris@0 376
Chris@0 377 unless count.nil?
Chris@0 378 html << " (#{paginator.current.first_item}-#{paginator.current.last_item}/#{count})"
Chris@0 379 if per_page_links != false && links = per_page_links(paginator.items_per_page)
Chris@0 380 html << " | #{links}"
Chris@0 381 end
Chris@0 382 end
Chris@0 383
Chris@909 384 html.html_safe
Chris@0 385 end
Chris@441 386
Chris@0 387 def per_page_links(selected=nil)
Chris@0 388 links = Setting.per_page_options_array.collect do |n|
Chris@441 389 n == selected ? n : link_to_content_update(n, params.merge(:per_page => n))
Chris@0 390 end
Chris@0 391 links.size > 1 ? l(:label_display_per_page, links.join(', ')) : nil
Chris@0 392 end
Chris@441 393
Chris@909 394 def reorder_links(name, url, method = :post)
Chris@909 395 link_to(image_tag('2uparrow.png', :alt => l(:label_sort_highest)),
Chris@909 396 url.merge({"#{name}[move_to]" => 'highest'}),
Chris@909 397 :method => method, :title => l(:label_sort_highest)) +
Chris@909 398 link_to(image_tag('1uparrow.png', :alt => l(:label_sort_higher)),
Chris@909 399 url.merge({"#{name}[move_to]" => 'higher'}),
Chris@909 400 :method => method, :title => l(:label_sort_higher)) +
Chris@909 401 link_to(image_tag('1downarrow.png', :alt => l(:label_sort_lower)),
Chris@909 402 url.merge({"#{name}[move_to]" => 'lower'}),
Chris@909 403 :method => method, :title => l(:label_sort_lower)) +
Chris@909 404 link_to(image_tag('2downarrow.png', :alt => l(:label_sort_lowest)),
Chris@909 405 url.merge({"#{name}[move_to]" => 'lowest'}),
Chris@909 406 :method => method, :title => l(:label_sort_lowest))
Chris@0 407 end
Chris@0 408
Chris@0 409 def breadcrumb(*args)
Chris@0 410 elements = args.flatten
Chris@909 411 elements.any? ? content_tag('p', (args.join(" \xc2\xbb ") + " \xc2\xbb ").html_safe, :class => 'breadcrumb') : nil
Chris@0 412 end
Chris@441 413
Chris@0 414 def other_formats_links(&block)
Chris@909 415 concat('<p class="other-formats">'.html_safe + l(:label_export_to))
Chris@0 416 yield Redmine::Views::OtherFormatsBuilder.new(self)
Chris@909 417 concat('</p>'.html_safe)
Chris@0 418 end
Chris@441 419
Chris@0 420 def page_header_title
Chris@0 421 if @project.nil? || @project.new_record?
Chris@0 422 h(Setting.app_title)
Chris@0 423 else
Chris@0 424 b = []
Chris@441 425 ancestors = (@project.root? ? [] : @project.ancestors.visible.all)
Chris@0 426 if ancestors.any?
Chris@0 427 root = ancestors.shift
Chris@14 428 b << link_to_project(root, {:jump => current_menu_item}, :class => 'root')
Chris@0 429 if ancestors.size > 2
Chris@909 430 b << "\xe2\x80\xa6"
Chris@0 431 ancestors = ancestors[-2, 2]
Chris@0 432 end
Chris@14 433 b += ancestors.collect {|p| link_to_project(p, {:jump => current_menu_item}, :class => 'ancestor') }
Chris@0 434 end
Chris@0 435 b << h(@project)
Chris@909 436 b.join(" \xc2\xbb ").html_safe
Chris@0 437 end
Chris@0 438 end
Chris@0 439
Chris@0 440 def html_title(*args)
Chris@0 441 if args.empty?
Chris@909 442 title = @html_title || []
Chris@0 443 title << @project.name if @project
Chris@909 444 title << Setting.app_title unless Setting.app_title == title.last
Chris@0 445 title.select {|t| !t.blank? }.join(' - ')
Chris@0 446 else
Chris@0 447 @html_title ||= []
Chris@0 448 @html_title += args
Chris@0 449 end
Chris@0 450 end
Chris@0 451
Chris@14 452 # Returns the theme, controller name, and action as css classes for the
Chris@14 453 # HTML body.
Chris@14 454 def body_css_classes
Chris@14 455 css = []
Chris@14 456 if theme = Redmine::Themes.theme(Setting.ui_theme)
Chris@14 457 css << 'theme-' + theme.name
Chris@14 458 end
Chris@14 459
Chris@14 460 css << 'controller-' + params[:controller]
Chris@14 461 css << 'action-' + params[:action]
Chris@14 462 css.join(' ')
Chris@14 463 end
Chris@14 464
Chris@0 465 def accesskey(s)
Chris@0 466 Redmine::AccessKeys.key_for s
Chris@0 467 end
Chris@0 468
Chris@0 469 # Formats text according to system settings.
Chris@0 470 # 2 ways to call this method:
Chris@0 471 # * with a String: textilizable(text, options)
Chris@0 472 # * with an object and one of its attribute: textilizable(issue, :description, options)
Chris@0 473 def textilizable(*args)
Chris@0 474 options = args.last.is_a?(Hash) ? args.pop : {}
Chris@0 475 case args.size
Chris@0 476 when 1
Chris@0 477 obj = options[:object]
Chris@0 478 text = args.shift
Chris@0 479 when 2
Chris@0 480 obj = args.shift
Chris@0 481 attr = args.shift
Chris@0 482 text = obj.send(attr).to_s
Chris@0 483 else
Chris@0 484 raise ArgumentError, 'invalid arguments to textilizable'
Chris@0 485 end
Chris@0 486 return '' if text.blank?
Chris@0 487 project = options[:project] || @project || (obj && obj.respond_to?(:project) ? obj.project : nil)
Chris@0 488 only_path = options.delete(:only_path) == false ? false : true
Chris@0 489
Chris@909 490 text = Redmine::WikiFormatting.to_html(Setting.text_formatting, text, :object => obj, :attribute => attr)
Chris@441 491
Chris@119 492 @parsed_headings = []
Chris@929 493 @heading_anchors = {}
Chris@909 494 @current_section = 0 if options[:edit_section_links]
Chris@929 495
Chris@929 496 parse_sections(text, project, obj, attr, only_path, options)
Chris@119 497 text = parse_non_pre_blocks(text) do |text|
Chris@929 498 [:parse_inline_attachments, :parse_wiki_links, :parse_redmine_links, :parse_macros].each do |method_name|
Chris@0 499 send method_name, text, project, obj, attr, only_path, options
Chris@0 500 end
Chris@0 501 end
Chris@929 502 parse_headings(text, project, obj, attr, only_path, options)
Chris@441 503
Chris@119 504 if @parsed_headings.any?
Chris@119 505 replace_toc(text, @parsed_headings)
Chris@119 506 end
Chris@441 507
Chris@119 508 text
Chris@0 509 end
Chris@441 510
Chris@0 511 def parse_non_pre_blocks(text)
Chris@0 512 s = StringScanner.new(text)
Chris@0 513 tags = []
Chris@0 514 parsed = ''
Chris@0 515 while !s.eos?
Chris@0 516 s.scan(/(.*?)(<(\/)?(pre|code)(.*?)>|\z)/im)
Chris@0 517 text, full_tag, closing, tag = s[1], s[2], s[3], s[4]
Chris@0 518 if tags.empty?
Chris@0 519 yield text
Chris@0 520 end
Chris@0 521 parsed << text
Chris@0 522 if tag
Chris@0 523 if closing
Chris@0 524 if tags.last == tag.downcase
Chris@0 525 tags.pop
Chris@0 526 end
Chris@0 527 else
Chris@0 528 tags << tag.downcase
Chris@0 529 end
Chris@0 530 parsed << full_tag
Chris@0 531 end
Chris@0 532 end
Chris@0 533 # Close any non closing tags
Chris@0 534 while tag = tags.pop
Chris@0 535 parsed << "</#{tag}>"
Chris@0 536 end
Chris@909 537 parsed.html_safe
Chris@0 538 end
Chris@441 539
Chris@0 540 def parse_inline_attachments(text, project, obj, attr, only_path, options)
Chris@0 541 # when using an image link, try to use an attachment, if possible
Chris@0 542 if options[:attachments] || (obj && obj.respond_to?(:attachments))
Chris@909 543 attachments = options[:attachments] || obj.attachments
Chris@909 544 text.gsub!(/src="([^\/"]+\.(bmp|gif|jpg|jpe|jpeg|png))"(\s+alt="([^"]*)")?/i) do |m|
Chris@441 545 filename, ext, alt, alttext = $1.downcase, $2, $3, $4
Chris@0 546 # search for the picture in attachments
Chris@909 547 if found = Attachment.latest_attach(attachments, filename)
Chris@909 548 image_url = url_for :only_path => only_path, :controller => 'attachments',
Chris@909 549 :action => 'download', :id => found
Chris@0 550 desc = found.description.to_s.gsub('"', '')
Chris@0 551 if !desc.blank? && alttext.blank?
Chris@0 552 alt = " title=\"#{desc}\" alt=\"#{desc}\""
Chris@0 553 end
Chris@909 554 "src=\"#{image_url}\"#{alt}".html_safe
Chris@0 555 else
Chris@909 556 m.html_safe
Chris@0 557 end
Chris@0 558 end
Chris@0 559 end
Chris@0 560 end
Chris@0 561
Chris@0 562 # Wiki links
Chris@0 563 #
Chris@0 564 # Examples:
Chris@0 565 # [[mypage]]
Chris@0 566 # [[mypage|mytext]]
Chris@0 567 # wiki links can refer other project wikis, using project name or identifier:
Chris@0 568 # [[project:]] -> wiki starting page
Chris@0 569 # [[project:|mytext]]
Chris@0 570 # [[project:mypage]]
Chris@0 571 # [[project:mypage|mytext]]
Chris@0 572 def parse_wiki_links(text, project, obj, attr, only_path, options)
Chris@0 573 text.gsub!(/(!)?(\[\[([^\]\n\|]+)(\|([^\]\n\|]+))?\]\])/) do |m|
Chris@0 574 link_project = project
Chris@0 575 esc, all, page, title = $1, $2, $3, $5
Chris@0 576 if esc.nil?
Chris@0 577 if page =~ /^([^\:]+)\:(.*)$/
chris@37 578 link_project = Project.find_by_identifier($1) || Project.find_by_name($1)
Chris@0 579 page = $2
Chris@0 580 title ||= $1 if page.blank?
Chris@0 581 end
Chris@0 582
Chris@0 583 if link_project && link_project.wiki
Chris@0 584 # extract anchor
Chris@0 585 anchor = nil
Chris@0 586 if page =~ /^(.+?)\#(.+)$/
Chris@0 587 page, anchor = $1, $2
Chris@0 588 end
Chris@909 589 anchor = sanitize_anchor_name(anchor) if anchor.present?
Chris@0 590 # check if page exists
Chris@0 591 wiki_page = link_project.wiki.find_page(page)
Chris@909 592 url = if anchor.present? && wiki_page.present? && (obj.is_a?(WikiContent) || obj.is_a?(WikiContent::Version)) && obj.page == wiki_page
Chris@909 593 "##{anchor}"
Chris@909 594 else
Chris@909 595 case options[:wiki_links]
Chris@909 596 when :local; "#{page.present? ? Wiki.titleize(page) : ''}.html" + (anchor.present? ? "##{anchor}" : '')
Chris@909 597 when :anchor; "##{page.present? ? Wiki.titleize(page) : title}" + (anchor.present? ? "_#{anchor}" : '') # used for single-file wiki export
Chris@0 598 else
chris@37 599 wiki_page_id = page.present? ? Wiki.titleize(page) : nil
chris@37 600 url_for(:only_path => only_path, :controller => 'wiki', :action => 'show', :project_id => link_project, :id => wiki_page_id, :anchor => anchor)
Chris@0 601 end
Chris@909 602 end
Chris@909 603 link_to(title.present? ? title.html_safe : h(page), url, :class => ('wiki-page' + (wiki_page ? '' : ' new')))
Chris@0 604 else
Chris@0 605 # project or wiki doesn't exist
Chris@909 606 all.html_safe
Chris@0 607 end
Chris@0 608 else
Chris@909 609 all.html_safe
Chris@0 610 end
Chris@0 611 end
Chris@0 612 end
Chris@441 613
Chris@0 614 # Redmine links
Chris@0 615 #
Chris@0 616 # Examples:
Chris@0 617 # Issues:
Chris@0 618 # #52 -> Link to issue #52
Chris@0 619 # Changesets:
Chris@0 620 # r52 -> Link to revision 52
Chris@0 621 # commit:a85130f -> Link to scmid starting with a85130f
Chris@0 622 # Documents:
Chris@0 623 # document#17 -> Link to document with id 17
Chris@0 624 # document:Greetings -> Link to the document with title "Greetings"
Chris@0 625 # document:"Some document" -> Link to the document with title "Some document"
Chris@0 626 # Versions:
Chris@0 627 # version#3 -> Link to version with id 3
Chris@0 628 # version:1.0.0 -> Link to version named "1.0.0"
Chris@0 629 # version:"1.0 beta 2" -> Link to version named "1.0 beta 2"
Chris@0 630 # Attachments:
Chris@0 631 # attachment:file.zip -> Link to the attachment of the current object named file.zip
Chris@0 632 # Source files:
Chris@0 633 # source:some/file -> Link to the file located at /some/file in the project's repository
Chris@0 634 # source:some/file@52 -> Link to the file's revision 52
Chris@0 635 # source:some/file#L120 -> Link to line 120 of the file
Chris@0 636 # source:some/file@52#L120 -> Link to line 120 of the file's revision 52
Chris@0 637 # export:some/file -> Force the download of the file
Chris@210 638 # Forum messages:
Chris@0 639 # message#1218 -> Link to message with id 1218
Chris@210 640 #
Chris@210 641 # Links can refer other objects from other projects, using project identifier:
Chris@210 642 # identifier:r52
Chris@210 643 # identifier:document:"Some document"
Chris@210 644 # identifier:version:1.0.0
Chris@210 645 # identifier:source:some/file
Chris@0 646 def parse_redmine_links(text, project, obj, attr, only_path, options)
Chris@909 647 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 648 leading, esc, project_prefix, project_identifier, prefix, sep, identifier = $1, $2, $3, $4, $5, $7 || $9, $8 || $10
Chris@0 649 link = nil
Chris@210 650 if project_identifier
Chris@210 651 project = Project.visible.find_by_identifier(project_identifier)
Chris@210 652 end
Chris@0 653 if esc.nil?
Chris@0 654 if prefix.nil? && sep == 'r'
Chris@210 655 # project.changesets.visible raises an SQL error because of a double join on repositories
Chris@210 656 if project && project.repository && (changeset = Changeset.visible.find_by_repository_id_and_revision(project.repository.id, identifier))
Chris@909 657 link = link_to(h("#{project_prefix}r#{identifier}"), {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :rev => changeset.revision},
Chris@0 658 :class => 'changeset',
Chris@0 659 :title => truncate_single_line(changeset.comments, :length => 100))
Chris@0 660 end
Chris@0 661 elsif sep == '#'
Chris@0 662 oid = identifier.to_i
Chris@0 663 case prefix
Chris@0 664 when nil
Chris@0 665 if issue = Issue.visible.find_by_id(oid, :include => :status)
Chris@0 666 link = link_to("##{oid}", {:only_path => only_path, :controller => 'issues', :action => 'show', :id => oid},
Chris@0 667 :class => issue.css_classes,
Chris@0 668 :title => "#{truncate(issue.subject, :length => 100)} (#{issue.status.name})")
Chris@0 669 end
Chris@0 670 when 'document'
Chris@210 671 if document = Document.visible.find_by_id(oid)
Chris@0 672 link = link_to h(document.title), {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document},
Chris@0 673 :class => 'document'
Chris@0 674 end
Chris@0 675 when 'version'
Chris@210 676 if version = Version.visible.find_by_id(oid)
Chris@0 677 link = link_to h(version.name), {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version},
Chris@0 678 :class => 'version'
Chris@0 679 end
Chris@0 680 when 'message'
Chris@210 681 if message = Message.visible.find_by_id(oid, :include => :parent)
Chris@210 682 link = link_to_message(message, {:only_path => only_path}, :class => 'message')
Chris@0 683 end
Chris@909 684 when 'forum'
Chris@909 685 if board = Board.visible.find_by_id(oid)
Chris@909 686 link = link_to h(board.name), {:only_path => only_path, :controller => 'boards', :action => 'show', :id => board, :project_id => board.project},
Chris@909 687 :class => 'board'
Chris@909 688 end
Chris@909 689 when 'news'
Chris@909 690 if news = News.visible.find_by_id(oid)
Chris@909 691 link = link_to h(news.title), {:only_path => only_path, :controller => 'news', :action => 'show', :id => news},
Chris@909 692 :class => 'news'
Chris@909 693 end
Chris@0 694 when 'project'
Chris@0 695 if p = Project.visible.find_by_id(oid)
Chris@14 696 link = link_to_project(p, {:only_path => only_path}, :class => 'project')
Chris@0 697 end
Chris@0 698 end
Chris@0 699 elsif sep == ':'
Chris@0 700 # removes the double quotes if any
Chris@0 701 name = identifier.gsub(%r{^"(.*)"$}, "\\1")
Chris@0 702 case prefix
Chris@0 703 when 'document'
Chris@210 704 if project && document = project.documents.visible.find_by_title(name)
Chris@0 705 link = link_to h(document.title), {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document},
Chris@0 706 :class => 'document'
Chris@0 707 end
Chris@0 708 when 'version'
Chris@210 709 if project && version = project.versions.visible.find_by_name(name)
Chris@0 710 link = link_to h(version.name), {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version},
Chris@0 711 :class => 'version'
Chris@0 712 end
Chris@909 713 when 'forum'
Chris@909 714 if project && board = project.boards.visible.find_by_name(name)
Chris@909 715 link = link_to h(board.name), {:only_path => only_path, :controller => 'boards', :action => 'show', :id => board, :project_id => board.project},
Chris@909 716 :class => 'board'
Chris@909 717 end
Chris@909 718 when 'news'
Chris@909 719 if project && news = project.news.visible.find_by_title(name)
Chris@909 720 link = link_to h(news.title), {:only_path => only_path, :controller => 'news', :action => 'show', :id => news},
Chris@909 721 :class => 'news'
Chris@909 722 end
Chris@0 723 when 'commit'
Chris@210 724 if project && project.repository && (changeset = Changeset.visible.find(:first, :conditions => ["repository_id = ? AND scmid LIKE ?", project.repository.id, "#{name}%"]))
Chris@210 725 link = link_to h("#{project_prefix}#{name}"), {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :rev => changeset.identifier},
Chris@0 726 :class => 'changeset',
Chris@909 727 :title => truncate_single_line(h(changeset.comments), :length => 100)
Chris@0 728 end
Chris@0 729 when 'source', 'export'
Chris@210 730 if project && project.repository && User.current.allowed_to?(:browse_repository, project)
Chris@0 731 name =~ %r{^[/\\]*(.*?)(@([0-9a-f]+))?(#(L\d+))?$}
Chris@0 732 path, rev, anchor = $1, $3, $5
Chris@210 733 link = link_to h("#{project_prefix}#{prefix}:#{name}"), {:controller => 'repositories', :action => 'entry', :id => project,
Chris@0 734 :path => to_path_param(path),
Chris@0 735 :rev => rev,
Chris@0 736 :anchor => anchor,
Chris@0 737 :format => (prefix == 'export' ? 'raw' : nil)},
Chris@0 738 :class => (prefix == 'export' ? 'source download' : 'source')
Chris@0 739 end
Chris@0 740 when 'attachment'
Chris@0 741 attachments = options[:attachments] || (obj && obj.respond_to?(:attachments) ? obj.attachments : nil)
Chris@0 742 if attachments && attachment = attachments.detect {|a| a.filename == name }
Chris@0 743 link = link_to h(attachment.filename), {:only_path => only_path, :controller => 'attachments', :action => 'download', :id => attachment},
Chris@0 744 :class => 'attachment'
Chris@0 745 end
Chris@0 746 when 'project'
Chris@0 747 if p = Project.visible.find(:first, :conditions => ["identifier = :s OR LOWER(name) = :s", {:s => name.downcase}])
Chris@14 748 link = link_to_project(p, {:only_path => only_path}, :class => 'project')
Chris@0 749 end
Chris@0 750 end
Chris@0 751 end
Chris@0 752 end
Chris@909 753 (leading + (link || "#{project_prefix}#{prefix}#{sep}#{identifier}")).html_safe
Chris@0 754 end
Chris@0 755 end
Chris@441 756
Chris@909 757 HEADING_RE = /(<h(1|2|3|4)( [^>]+)?>(.+?)<\/h(1|2|3|4)>)/i unless const_defined?(:HEADING_RE)
Chris@909 758
Chris@909 759 def parse_sections(text, project, obj, attr, only_path, options)
Chris@909 760 return unless options[:edit_section_links]
Chris@909 761 text.gsub!(HEADING_RE) do
Chris@909 762 @current_section += 1
Chris@909 763 if @current_section > 1
Chris@909 764 content_tag('div',
Chris@909 765 link_to(image_tag('edit.png'), options[:edit_section_links].merge(:section => @current_section)),
Chris@909 766 :class => 'contextual',
Chris@909 767 :title => l(:button_edit_section)) + $1
Chris@909 768 else
Chris@909 769 $1
Chris@909 770 end
Chris@909 771 end
Chris@909 772 end
Chris@441 773
chris@37 774 # Headings and TOC
Chris@119 775 # Adds ids and links to headings unless options[:headings] is set to false
chris@37 776 def parse_headings(text, project, obj, attr, only_path, options)
Chris@119 777 return if options[:headings] == false
Chris@441 778
chris@37 779 text.gsub!(HEADING_RE) do
Chris@909 780 level, attrs, content = $2.to_i, $3, $4
chris@37 781 item = strip_tags(content).strip
Chris@909 782 anchor = sanitize_anchor_name(item)
Chris@909 783 # used for single-file wiki export
Chris@909 784 anchor = "#{obj.page.title}_#{anchor}" if options[:wiki_links] == :anchor && (obj.is_a?(WikiContent) || obj.is_a?(WikiContent::Version))
Chris@929 785 @heading_anchors[anchor] ||= 0
Chris@929 786 idx = (@heading_anchors[anchor] += 1)
Chris@929 787 if idx > 1
Chris@929 788 anchor = "#{anchor}-#{idx}"
Chris@929 789 end
Chris@119 790 @parsed_headings << [level, anchor, item]
Chris@441 791 "<a name=\"#{anchor}\"></a>\n<h#{level} #{attrs}>#{content}<a href=\"##{anchor}\" class=\"wiki-anchor\">&para;</a></h#{level}>"
Chris@119 792 end
Chris@119 793 end
Chris@441 794
Chris@909 795 MACROS_RE = /
Chris@909 796 (!)? # escaping
Chris@909 797 (
Chris@909 798 \{\{ # opening tag
Chris@909 799 ([\w]+) # macro name
Chris@909 800 (\(([^\}]*)\))? # optional arguments
Chris@909 801 \}\} # closing tag
Chris@909 802 )
Chris@909 803 /x unless const_defined?(:MACROS_RE)
Chris@909 804
Chris@909 805 # Macros substitution
Chris@909 806 def parse_macros(text, project, obj, attr, only_path, options)
Chris@909 807 text.gsub!(MACROS_RE) do
Chris@909 808 esc, all, macro = $1, $2, $3.downcase
Chris@909 809 args = ($5 || '').split(',').each(&:strip)
Chris@909 810 if esc.nil?
Chris@909 811 begin
Chris@909 812 exec_macro(macro, obj, args)
Chris@909 813 rescue => e
Chris@909 814 "<div class=\"flash error\">Error executing the <strong>#{macro}</strong> macro (#{e})</div>"
Chris@909 815 end || all
Chris@909 816 else
Chris@909 817 all
Chris@909 818 end
Chris@909 819 end
Chris@909 820 end
Chris@909 821
Chris@119 822 TOC_RE = /<p>\{\{([<>]?)toc\}\}<\/p>/i unless const_defined?(:TOC_RE)
Chris@441 823
Chris@119 824 # Renders the TOC with given headings
Chris@119 825 def replace_toc(text, headings)
chris@37 826 text.gsub!(TOC_RE) do
chris@37 827 if headings.empty?
chris@37 828 ''
chris@37 829 else
chris@37 830 div_class = 'toc'
chris@37 831 div_class << ' right' if $1 == '>'
chris@37 832 div_class << ' left' if $1 == '<'
chris@37 833 out = "<ul class=\"#{div_class}\"><li>"
chris@37 834 root = headings.map(&:first).min
chris@37 835 current = root
chris@37 836 started = false
chris@37 837 headings.each do |level, anchor, item|
chris@37 838 if level > current
chris@37 839 out << '<ul><li>' * (level - current)
chris@37 840 elsif level < current
chris@37 841 out << "</li></ul>\n" * (current - level) + "</li><li>"
chris@37 842 elsif started
chris@37 843 out << '</li><li>'
chris@37 844 end
chris@37 845 out << "<a href=\"##{anchor}\">#{item}</a>"
chris@37 846 current = level
chris@37 847 started = true
chris@37 848 end
chris@37 849 out << '</li></ul>' * (current - root)
chris@37 850 out << '</li></ul>'
chris@37 851 end
chris@37 852 end
chris@37 853 end
Chris@0 854
Chris@0 855 # Same as Rails' simple_format helper without using paragraphs
Chris@0 856 def simple_format_without_paragraph(text)
Chris@0 857 text.to_s.
Chris@0 858 gsub(/\r\n?/, "\n"). # \r\n and \r -> \n
Chris@0 859 gsub(/\n\n+/, "<br /><br />"). # 2+ newline -> 2 br
Chris@909 860 gsub(/([^\n]\n)(?=[^\n])/, '\1<br />'). # 1 newline -> br
Chris@909 861 html_safe
Chris@0 862 end
Chris@0 863
Chris@0 864 def lang_options_for_select(blank=true)
Chris@0 865 (blank ? [["(auto)", ""]] : []) +
Chris@0 866 valid_languages.collect{|lang| [ ll(lang.to_s, :general_lang_name), lang.to_s]}.sort{|x,y| x.last <=> y.last }
Chris@0 867 end
Chris@0 868
Chris@0 869 def label_tag_for(name, option_tags = nil, options = {})
Chris@0 870 label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "")
Chris@0 871 content_tag("label", label_text)
Chris@0 872 end
Chris@0 873
Chris@909 874 def labelled_tabular_form_for(*args, &proc)
Chris@909 875 args << {} unless args.last.is_a?(Hash)
Chris@909 876 options = args.last
Chris@0 877 options[:html] ||= {}
Chris@0 878 options[:html][:class] = 'tabular' unless options[:html].has_key?(:class)
Chris@909 879 options.merge!({:builder => TabularFormBuilder})
Chris@909 880 form_for(*args, &proc)
Chris@909 881 end
Chris@909 882
Chris@909 883 def labelled_form_for(*args, &proc)
Chris@909 884 args << {} unless args.last.is_a?(Hash)
Chris@909 885 options = args.last
Chris@909 886 options.merge!({:builder => TabularFormBuilder})
Chris@909 887 form_for(*args, &proc)
Chris@0 888 end
Chris@0 889
Chris@0 890 def back_url_hidden_field_tag
Chris@0 891 back_url = params[:back_url] || request.env['HTTP_REFERER']
Chris@0 892 back_url = CGI.unescape(back_url.to_s)
Chris@0 893 hidden_field_tag('back_url', CGI.escape(back_url)) unless back_url.blank?
Chris@0 894 end
Chris@0 895
Chris@0 896 def check_all_links(form_name)
Chris@0 897 link_to_function(l(:button_check_all), "checkAll('#{form_name}', true)") +
Chris@909 898 " | ".html_safe +
Chris@0 899 link_to_function(l(:button_uncheck_all), "checkAll('#{form_name}', false)")
Chris@0 900 end
Chris@0 901
Chris@0 902 def progress_bar(pcts, options={})
Chris@0 903 pcts = [pcts, pcts] unless pcts.is_a?(Array)
Chris@0 904 pcts = pcts.collect(&:round)
Chris@0 905 pcts[1] = pcts[1] - pcts[0]
Chris@0 906 pcts << (100 - pcts[1] - pcts[0])
Chris@0 907 width = options[:width] || '100px;'
Chris@0 908 legend = options[:legend] || ''
Chris@0 909 content_tag('table',
Chris@0 910 content_tag('tr',
Chris@909 911 (pcts[0] > 0 ? content_tag('td', '', :style => "width: #{pcts[0]}%;", :class => 'closed') : ''.html_safe) +
Chris@909 912 (pcts[1] > 0 ? content_tag('td', '', :style => "width: #{pcts[1]}%;", :class => 'done') : ''.html_safe) +
Chris@909 913 (pcts[2] > 0 ? content_tag('td', '', :style => "width: #{pcts[2]}%;", :class => 'todo') : ''.html_safe)
Chris@909 914 ), :class => 'progress', :style => "width: #{width};").html_safe +
Chris@909 915 content_tag('p', legend, :class => 'pourcent').html_safe
Chris@0 916 end
Chris@441 917
Chris@0 918 def checked_image(checked=true)
Chris@0 919 if checked
Chris@0 920 image_tag 'toggle_check.png'
Chris@0 921 end
Chris@0 922 end
Chris@441 923
Chris@0 924 def context_menu(url)
Chris@0 925 unless @context_menu_included
Chris@0 926 content_for :header_tags do
Chris@0 927 javascript_include_tag('context_menu') +
Chris@0 928 stylesheet_link_tag('context_menu')
Chris@0 929 end
Chris@14 930 if l(:direction) == 'rtl'
Chris@14 931 content_for :header_tags do
Chris@14 932 stylesheet_link_tag('context_menu_rtl')
Chris@14 933 end
Chris@14 934 end
Chris@0 935 @context_menu_included = true
Chris@0 936 end
Chris@0 937 javascript_tag "new ContextMenu('#{ url_for(url) }')"
Chris@0 938 end
Chris@0 939
Chris@0 940 def context_menu_link(name, url, options={})
Chris@0 941 options[:class] ||= ''
Chris@0 942 if options.delete(:selected)
Chris@0 943 options[:class] << ' icon-checked disabled'
Chris@0 944 options[:disabled] = true
Chris@0 945 end
Chris@0 946 if options.delete(:disabled)
Chris@0 947 options.delete(:method)
Chris@0 948 options.delete(:confirm)
Chris@0 949 options.delete(:onclick)
Chris@0 950 options[:class] << ' disabled'
Chris@0 951 url = '#'
Chris@0 952 end
Chris@909 953 link_to h(name), url, options
Chris@0 954 end
Chris@0 955
Chris@0 956 def calendar_for(field_id)
Chris@0 957 include_calendar_headers_tags
Chris@0 958 image_tag("calendar.png", {:id => "#{field_id}_trigger",:class => "calendar-trigger"}) +
Chris@0 959 javascript_tag("Calendar.setup({inputField : '#{field_id}', ifFormat : '%Y-%m-%d', button : '#{field_id}_trigger' });")
Chris@0 960 end
Chris@0 961
Chris@0 962 def include_calendar_headers_tags
Chris@0 963 unless @calendar_headers_tags_included
Chris@0 964 @calendar_headers_tags_included = true
Chris@0 965 content_for :header_tags do
Chris@0 966 start_of_week = case Setting.start_of_week.to_i
Chris@0 967 when 1
Chris@0 968 'Calendar._FD = 1;' # Monday
Chris@0 969 when 7
Chris@0 970 'Calendar._FD = 0;' # Sunday
Chris@441 971 when 6
Chris@441 972 'Calendar._FD = 6;' # Saturday
Chris@0 973 else
Chris@0 974 '' # use language
Chris@0 975 end
Chris@441 976
Chris@0 977 javascript_include_tag('calendar/calendar') +
Chris@0 978 javascript_include_tag("calendar/lang/calendar-#{current_language.to_s.downcase}.js") +
Chris@441 979 javascript_tag(start_of_week) +
Chris@0 980 javascript_include_tag('calendar/calendar-setup') +
Chris@0 981 stylesheet_link_tag('calendar')
Chris@0 982 end
Chris@0 983 end
Chris@0 984 end
Chris@0 985
Chris@0 986 def content_for(name, content = nil, &block)
Chris@0 987 @has_content ||= {}
Chris@0 988 @has_content[name] = true
Chris@0 989 super(name, content, &block)
Chris@0 990 end
Chris@0 991
Chris@0 992 def has_content?(name)
Chris@0 993 (@has_content && @has_content[name]) || false
Chris@0 994 end
Chris@0 995
Chris@909 996 def email_delivery_enabled?
Chris@909 997 !!ActionMailer::Base.perform_deliveries
Chris@909 998 end
Chris@909 999
Chris@0 1000 # Returns the avatar image tag for the given +user+ if avatars are enabled
Chris@0 1001 # +user+ can be a User or a string that will be scanned for an email address (eg. 'joe <joe@foo.bar>')
Chris@0 1002 def avatar(user, options = { })
Chris@0 1003 if Setting.gravatar_enabled?
chris@22 1004 options.merge!({:ssl => (defined?(request) && request.ssl?), :default => Setting.gravatar_default})
Chris@0 1005 email = nil
Chris@0 1006 if user.respond_to?(:mail)
Chris@0 1007 email = user.mail
Chris@0 1008 elsif user.to_s =~ %r{<(.+?)>}
Chris@0 1009 email = $1
Chris@0 1010 end
Chris@0 1011 return gravatar(email.to_s.downcase, options) unless email.blank? rescue nil
chris@22 1012 else
chris@22 1013 ''
Chris@0 1014 end
Chris@0 1015 end
Chris@441 1016
Chris@909 1017 def sanitize_anchor_name(anchor)
Chris@909 1018 anchor.gsub(%r{[^\w\s\-]}, '').gsub(%r{\s+(\-+\s*)?}, '-')
Chris@909 1019 end
Chris@909 1020
Chris@245 1021 # Returns the javascript tags that are included in the html layout head
Chris@245 1022 def javascript_heads
Chris@245 1023 tags = javascript_include_tag(:defaults)
Chris@245 1024 unless User.current.pref.warn_on_leaving_unsaved == '0'
Chris@909 1025 tags << "\n".html_safe + javascript_tag("Event.observe(window, 'load', function(){ new WarnLeavingUnsaved('#{escape_javascript( l(:text_warn_on_leaving_unsaved) )}'); });")
Chris@245 1026 end
Chris@245 1027 tags
Chris@245 1028 end
Chris@0 1029
Chris@14 1030 def favicon
Chris@909 1031 "<link rel='shortcut icon' href='#{image_path('/favicon.ico')}' />".html_safe
Chris@14 1032 end
Chris@441 1033
Chris@441 1034 def robot_exclusion_tag
Chris@909 1035 '<meta name="robots" content="noindex,follow,noarchive" />'.html_safe
Chris@441 1036 end
Chris@441 1037
Chris@119 1038 # Returns true if arg is expected in the API response
Chris@119 1039 def include_in_api_response?(arg)
Chris@119 1040 unless @included_in_api_response
Chris@119 1041 param = params[:include]
Chris@119 1042 @included_in_api_response = param.is_a?(Array) ? param.collect(&:to_s) : param.to_s.split(',')
Chris@119 1043 @included_in_api_response.collect!(&:strip)
Chris@119 1044 end
Chris@119 1045 @included_in_api_response.include?(arg.to_s)
Chris@119 1046 end
Chris@14 1047
Chris@119 1048 # Returns options or nil if nometa param or X-Redmine-Nometa header
Chris@119 1049 # was set in the request
Chris@119 1050 def api_meta(options)
Chris@119 1051 if params[:nometa].present? || request.headers['X-Redmine-Nometa']
Chris@119 1052 # compatibility mode for activeresource clients that raise
Chris@119 1053 # an error when unserializing an array with attributes
Chris@119 1054 nil
Chris@119 1055 else
Chris@119 1056 options
Chris@119 1057 end
Chris@119 1058 end
Chris@441 1059
Chris@0 1060 private
Chris@0 1061
Chris@0 1062 def wiki_helper
Chris@0 1063 helper = Redmine::WikiFormatting.helper_for(Setting.text_formatting)
Chris@0 1064 extend helper
Chris@0 1065 return self
Chris@0 1066 end
Chris@441 1067
Chris@441 1068 def link_to_content_update(text, url_params = {}, html_options = {})
Chris@441 1069 link_to(text, url_params, html_options)
Chris@0 1070 end
Chris@0 1071 end