annotate app/helpers/application_helper.rb @ 909:cbb26bc654de redmine-1.3

Update to Redmine 1.3-stable branch (Redmine SVN rev 8964)
author Chris Cannam
date Fri, 24 Feb 2012 19:09:32 +0000
parents cbce1fd3b1b7
children 5e80956cc792 5f33065ddc4b
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@909 493 @current_section = 0 if options[:edit_section_links]
Chris@119 494 text = parse_non_pre_blocks(text) do |text|
Chris@909 495 [:parse_sections, :parse_inline_attachments, :parse_wiki_links, :parse_redmine_links, :parse_macros, :parse_headings].each do |method_name|
Chris@0 496 send method_name, text, project, obj, attr, only_path, options
Chris@0 497 end
Chris@0 498 end
Chris@441 499
Chris@119 500 if @parsed_headings.any?
Chris@119 501 replace_toc(text, @parsed_headings)
Chris@119 502 end
Chris@441 503
Chris@119 504 text
Chris@0 505 end
Chris@441 506
Chris@0 507 def parse_non_pre_blocks(text)
Chris@0 508 s = StringScanner.new(text)
Chris@0 509 tags = []
Chris@0 510 parsed = ''
Chris@0 511 while !s.eos?
Chris@0 512 s.scan(/(.*?)(<(\/)?(pre|code)(.*?)>|\z)/im)
Chris@0 513 text, full_tag, closing, tag = s[1], s[2], s[3], s[4]
Chris@0 514 if tags.empty?
Chris@0 515 yield text
Chris@0 516 end
Chris@0 517 parsed << text
Chris@0 518 if tag
Chris@0 519 if closing
Chris@0 520 if tags.last == tag.downcase
Chris@0 521 tags.pop
Chris@0 522 end
Chris@0 523 else
Chris@0 524 tags << tag.downcase
Chris@0 525 end
Chris@0 526 parsed << full_tag
Chris@0 527 end
Chris@0 528 end
Chris@0 529 # Close any non closing tags
Chris@0 530 while tag = tags.pop
Chris@0 531 parsed << "</#{tag}>"
Chris@0 532 end
Chris@909 533 parsed.html_safe
Chris@0 534 end
Chris@441 535
Chris@0 536 def parse_inline_attachments(text, project, obj, attr, only_path, options)
Chris@0 537 # when using an image link, try to use an attachment, if possible
Chris@0 538 if options[:attachments] || (obj && obj.respond_to?(:attachments))
Chris@909 539 attachments = options[:attachments] || obj.attachments
Chris@909 540 text.gsub!(/src="([^\/"]+\.(bmp|gif|jpg|jpe|jpeg|png))"(\s+alt="([^"]*)")?/i) do |m|
Chris@441 541 filename, ext, alt, alttext = $1.downcase, $2, $3, $4
Chris@0 542 # search for the picture in attachments
Chris@909 543 if found = Attachment.latest_attach(attachments, filename)
Chris@909 544 image_url = url_for :only_path => only_path, :controller => 'attachments',
Chris@909 545 :action => 'download', :id => found
Chris@0 546 desc = found.description.to_s.gsub('"', '')
Chris@0 547 if !desc.blank? && alttext.blank?
Chris@0 548 alt = " title=\"#{desc}\" alt=\"#{desc}\""
Chris@0 549 end
Chris@909 550 "src=\"#{image_url}\"#{alt}".html_safe
Chris@0 551 else
Chris@909 552 m.html_safe
Chris@0 553 end
Chris@0 554 end
Chris@0 555 end
Chris@0 556 end
Chris@0 557
Chris@0 558 # Wiki links
Chris@0 559 #
Chris@0 560 # Examples:
Chris@0 561 # [[mypage]]
Chris@0 562 # [[mypage|mytext]]
Chris@0 563 # wiki links can refer other project wikis, using project name or identifier:
Chris@0 564 # [[project:]] -> wiki starting page
Chris@0 565 # [[project:|mytext]]
Chris@0 566 # [[project:mypage]]
Chris@0 567 # [[project:mypage|mytext]]
Chris@0 568 def parse_wiki_links(text, project, obj, attr, only_path, options)
Chris@0 569 text.gsub!(/(!)?(\[\[([^\]\n\|]+)(\|([^\]\n\|]+))?\]\])/) do |m|
Chris@0 570 link_project = project
Chris@0 571 esc, all, page, title = $1, $2, $3, $5
Chris@0 572 if esc.nil?
Chris@0 573 if page =~ /^([^\:]+)\:(.*)$/
chris@37 574 link_project = Project.find_by_identifier($1) || Project.find_by_name($1)
Chris@0 575 page = $2
Chris@0 576 title ||= $1 if page.blank?
Chris@0 577 end
Chris@0 578
Chris@0 579 if link_project && link_project.wiki
Chris@0 580 # extract anchor
Chris@0 581 anchor = nil
Chris@0 582 if page =~ /^(.+?)\#(.+)$/
Chris@0 583 page, anchor = $1, $2
Chris@0 584 end
Chris@909 585 anchor = sanitize_anchor_name(anchor) if anchor.present?
Chris@0 586 # check if page exists
Chris@0 587 wiki_page = link_project.wiki.find_page(page)
Chris@909 588 url = if anchor.present? && wiki_page.present? && (obj.is_a?(WikiContent) || obj.is_a?(WikiContent::Version)) && obj.page == wiki_page
Chris@909 589 "##{anchor}"
Chris@909 590 else
Chris@909 591 case options[:wiki_links]
Chris@909 592 when :local; "#{page.present? ? Wiki.titleize(page) : ''}.html" + (anchor.present? ? "##{anchor}" : '')
Chris@909 593 when :anchor; "##{page.present? ? Wiki.titleize(page) : title}" + (anchor.present? ? "_#{anchor}" : '') # used for single-file wiki export
Chris@0 594 else
chris@37 595 wiki_page_id = page.present? ? Wiki.titleize(page) : nil
chris@37 596 url_for(:only_path => only_path, :controller => 'wiki', :action => 'show', :project_id => link_project, :id => wiki_page_id, :anchor => anchor)
Chris@0 597 end
Chris@909 598 end
Chris@909 599 link_to(title.present? ? title.html_safe : h(page), url, :class => ('wiki-page' + (wiki_page ? '' : ' new')))
Chris@0 600 else
Chris@0 601 # project or wiki doesn't exist
Chris@909 602 all.html_safe
Chris@0 603 end
Chris@0 604 else
Chris@909 605 all.html_safe
Chris@0 606 end
Chris@0 607 end
Chris@0 608 end
Chris@441 609
Chris@0 610 # Redmine links
Chris@0 611 #
Chris@0 612 # Examples:
Chris@0 613 # Issues:
Chris@0 614 # #52 -> Link to issue #52
Chris@0 615 # Changesets:
Chris@0 616 # r52 -> Link to revision 52
Chris@0 617 # commit:a85130f -> Link to scmid starting with a85130f
Chris@0 618 # Documents:
Chris@0 619 # document#17 -> Link to document with id 17
Chris@0 620 # document:Greetings -> Link to the document with title "Greetings"
Chris@0 621 # document:"Some document" -> Link to the document with title "Some document"
Chris@0 622 # Versions:
Chris@0 623 # version#3 -> Link to version with id 3
Chris@0 624 # version:1.0.0 -> Link to version named "1.0.0"
Chris@0 625 # version:"1.0 beta 2" -> Link to version named "1.0 beta 2"
Chris@0 626 # Attachments:
Chris@0 627 # attachment:file.zip -> Link to the attachment of the current object named file.zip
Chris@0 628 # Source files:
Chris@0 629 # source:some/file -> Link to the file located at /some/file in the project's repository
Chris@0 630 # source:some/file@52 -> Link to the file's revision 52
Chris@0 631 # source:some/file#L120 -> Link to line 120 of the file
Chris@0 632 # source:some/file@52#L120 -> Link to line 120 of the file's revision 52
Chris@0 633 # export:some/file -> Force the download of the file
Chris@210 634 # Forum messages:
Chris@0 635 # message#1218 -> Link to message with id 1218
Chris@210 636 #
Chris@210 637 # Links can refer other objects from other projects, using project identifier:
Chris@210 638 # identifier:r52
Chris@210 639 # identifier:document:"Some document"
Chris@210 640 # identifier:version:1.0.0
Chris@210 641 # identifier:source:some/file
Chris@0 642 def parse_redmine_links(text, project, obj, attr, only_path, options)
Chris@909 643 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 644 leading, esc, project_prefix, project_identifier, prefix, sep, identifier = $1, $2, $3, $4, $5, $7 || $9, $8 || $10
Chris@0 645 link = nil
Chris@210 646 if project_identifier
Chris@210 647 project = Project.visible.find_by_identifier(project_identifier)
Chris@210 648 end
Chris@0 649 if esc.nil?
Chris@0 650 if prefix.nil? && sep == 'r'
Chris@210 651 # project.changesets.visible raises an SQL error because of a double join on repositories
Chris@210 652 if project && project.repository && (changeset = Changeset.visible.find_by_repository_id_and_revision(project.repository.id, identifier))
Chris@909 653 link = link_to(h("#{project_prefix}r#{identifier}"), {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :rev => changeset.revision},
Chris@0 654 :class => 'changeset',
Chris@0 655 :title => truncate_single_line(changeset.comments, :length => 100))
Chris@0 656 end
Chris@0 657 elsif sep == '#'
Chris@0 658 oid = identifier.to_i
Chris@0 659 case prefix
Chris@0 660 when nil
Chris@0 661 if issue = Issue.visible.find_by_id(oid, :include => :status)
Chris@0 662 link = link_to("##{oid}", {:only_path => only_path, :controller => 'issues', :action => 'show', :id => oid},
Chris@0 663 :class => issue.css_classes,
Chris@0 664 :title => "#{truncate(issue.subject, :length => 100)} (#{issue.status.name})")
Chris@0 665 end
Chris@0 666 when 'document'
Chris@210 667 if document = Document.visible.find_by_id(oid)
Chris@0 668 link = link_to h(document.title), {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document},
Chris@0 669 :class => 'document'
Chris@0 670 end
Chris@0 671 when 'version'
Chris@210 672 if version = Version.visible.find_by_id(oid)
Chris@0 673 link = link_to h(version.name), {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version},
Chris@0 674 :class => 'version'
Chris@0 675 end
Chris@0 676 when 'message'
Chris@210 677 if message = Message.visible.find_by_id(oid, :include => :parent)
Chris@210 678 link = link_to_message(message, {:only_path => only_path}, :class => 'message')
Chris@0 679 end
Chris@909 680 when 'forum'
Chris@909 681 if board = Board.visible.find_by_id(oid)
Chris@909 682 link = link_to h(board.name), {:only_path => only_path, :controller => 'boards', :action => 'show', :id => board, :project_id => board.project},
Chris@909 683 :class => 'board'
Chris@909 684 end
Chris@909 685 when 'news'
Chris@909 686 if news = News.visible.find_by_id(oid)
Chris@909 687 link = link_to h(news.title), {:only_path => only_path, :controller => 'news', :action => 'show', :id => news},
Chris@909 688 :class => 'news'
Chris@909 689 end
Chris@0 690 when 'project'
Chris@0 691 if p = Project.visible.find_by_id(oid)
Chris@14 692 link = link_to_project(p, {:only_path => only_path}, :class => 'project')
Chris@0 693 end
Chris@0 694 end
Chris@0 695 elsif sep == ':'
Chris@0 696 # removes the double quotes if any
Chris@0 697 name = identifier.gsub(%r{^"(.*)"$}, "\\1")
Chris@0 698 case prefix
Chris@0 699 when 'document'
Chris@210 700 if project && document = project.documents.visible.find_by_title(name)
Chris@0 701 link = link_to h(document.title), {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document},
Chris@0 702 :class => 'document'
Chris@0 703 end
Chris@0 704 when 'version'
Chris@210 705 if project && version = project.versions.visible.find_by_name(name)
Chris@0 706 link = link_to h(version.name), {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version},
Chris@0 707 :class => 'version'
Chris@0 708 end
Chris@909 709 when 'forum'
Chris@909 710 if project && board = project.boards.visible.find_by_name(name)
Chris@909 711 link = link_to h(board.name), {:only_path => only_path, :controller => 'boards', :action => 'show', :id => board, :project_id => board.project},
Chris@909 712 :class => 'board'
Chris@909 713 end
Chris@909 714 when 'news'
Chris@909 715 if project && news = project.news.visible.find_by_title(name)
Chris@909 716 link = link_to h(news.title), {:only_path => only_path, :controller => 'news', :action => 'show', :id => news},
Chris@909 717 :class => 'news'
Chris@909 718 end
Chris@0 719 when 'commit'
Chris@210 720 if project && project.repository && (changeset = Changeset.visible.find(:first, :conditions => ["repository_id = ? AND scmid LIKE ?", project.repository.id, "#{name}%"]))
Chris@210 721 link = link_to h("#{project_prefix}#{name}"), {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :rev => changeset.identifier},
Chris@0 722 :class => 'changeset',
Chris@909 723 :title => truncate_single_line(h(changeset.comments), :length => 100)
Chris@0 724 end
Chris@0 725 when 'source', 'export'
Chris@210 726 if project && project.repository && User.current.allowed_to?(:browse_repository, project)
Chris@0 727 name =~ %r{^[/\\]*(.*?)(@([0-9a-f]+))?(#(L\d+))?$}
Chris@0 728 path, rev, anchor = $1, $3, $5
Chris@210 729 link = link_to h("#{project_prefix}#{prefix}:#{name}"), {:controller => 'repositories', :action => 'entry', :id => project,
Chris@0 730 :path => to_path_param(path),
Chris@0 731 :rev => rev,
Chris@0 732 :anchor => anchor,
Chris@0 733 :format => (prefix == 'export' ? 'raw' : nil)},
Chris@0 734 :class => (prefix == 'export' ? 'source download' : 'source')
Chris@0 735 end
Chris@0 736 when 'attachment'
Chris@0 737 attachments = options[:attachments] || (obj && obj.respond_to?(:attachments) ? obj.attachments : nil)
Chris@0 738 if attachments && attachment = attachments.detect {|a| a.filename == name }
Chris@0 739 link = link_to h(attachment.filename), {:only_path => only_path, :controller => 'attachments', :action => 'download', :id => attachment},
Chris@0 740 :class => 'attachment'
Chris@0 741 end
Chris@0 742 when 'project'
Chris@0 743 if p = Project.visible.find(:first, :conditions => ["identifier = :s OR LOWER(name) = :s", {:s => name.downcase}])
Chris@14 744 link = link_to_project(p, {:only_path => only_path}, :class => 'project')
Chris@0 745 end
Chris@0 746 end
Chris@0 747 end
Chris@0 748 end
Chris@909 749 (leading + (link || "#{project_prefix}#{prefix}#{sep}#{identifier}")).html_safe
Chris@0 750 end
Chris@0 751 end
Chris@441 752
Chris@909 753 HEADING_RE = /(<h(1|2|3|4)( [^>]+)?>(.+?)<\/h(1|2|3|4)>)/i unless const_defined?(:HEADING_RE)
Chris@909 754
Chris@909 755 def parse_sections(text, project, obj, attr, only_path, options)
Chris@909 756 return unless options[:edit_section_links]
Chris@909 757 text.gsub!(HEADING_RE) do
Chris@909 758 @current_section += 1
Chris@909 759 if @current_section > 1
Chris@909 760 content_tag('div',
Chris@909 761 link_to(image_tag('edit.png'), options[:edit_section_links].merge(:section => @current_section)),
Chris@909 762 :class => 'contextual',
Chris@909 763 :title => l(:button_edit_section)) + $1
Chris@909 764 else
Chris@909 765 $1
Chris@909 766 end
Chris@909 767 end
Chris@909 768 end
Chris@441 769
chris@37 770 # Headings and TOC
Chris@119 771 # Adds ids and links to headings unless options[:headings] is set to false
chris@37 772 def parse_headings(text, project, obj, attr, only_path, options)
Chris@119 773 return if options[:headings] == false
Chris@441 774
chris@37 775 text.gsub!(HEADING_RE) do
Chris@909 776 level, attrs, content = $2.to_i, $3, $4
chris@37 777 item = strip_tags(content).strip
Chris@909 778 anchor = sanitize_anchor_name(item)
Chris@909 779 # used for single-file wiki export
Chris@909 780 anchor = "#{obj.page.title}_#{anchor}" if options[:wiki_links] == :anchor && (obj.is_a?(WikiContent) || obj.is_a?(WikiContent::Version))
Chris@119 781 @parsed_headings << [level, anchor, item]
Chris@441 782 "<a name=\"#{anchor}\"></a>\n<h#{level} #{attrs}>#{content}<a href=\"##{anchor}\" class=\"wiki-anchor\">&para;</a></h#{level}>"
Chris@119 783 end
Chris@119 784 end
Chris@441 785
Chris@909 786 MACROS_RE = /
Chris@909 787 (!)? # escaping
Chris@909 788 (
Chris@909 789 \{\{ # opening tag
Chris@909 790 ([\w]+) # macro name
Chris@909 791 (\(([^\}]*)\))? # optional arguments
Chris@909 792 \}\} # closing tag
Chris@909 793 )
Chris@909 794 /x unless const_defined?(:MACROS_RE)
Chris@909 795
Chris@909 796 # Macros substitution
Chris@909 797 def parse_macros(text, project, obj, attr, only_path, options)
Chris@909 798 text.gsub!(MACROS_RE) do
Chris@909 799 esc, all, macro = $1, $2, $3.downcase
Chris@909 800 args = ($5 || '').split(',').each(&:strip)
Chris@909 801 if esc.nil?
Chris@909 802 begin
Chris@909 803 exec_macro(macro, obj, args)
Chris@909 804 rescue => e
Chris@909 805 "<div class=\"flash error\">Error executing the <strong>#{macro}</strong> macro (#{e})</div>"
Chris@909 806 end || all
Chris@909 807 else
Chris@909 808 all
Chris@909 809 end
Chris@909 810 end
Chris@909 811 end
Chris@909 812
Chris@119 813 TOC_RE = /<p>\{\{([<>]?)toc\}\}<\/p>/i unless const_defined?(:TOC_RE)
Chris@441 814
Chris@119 815 # Renders the TOC with given headings
Chris@119 816 def replace_toc(text, headings)
chris@37 817 text.gsub!(TOC_RE) do
chris@37 818 if headings.empty?
chris@37 819 ''
chris@37 820 else
chris@37 821 div_class = 'toc'
chris@37 822 div_class << ' right' if $1 == '>'
chris@37 823 div_class << ' left' if $1 == '<'
chris@37 824 out = "<ul class=\"#{div_class}\"><li>"
chris@37 825 root = headings.map(&:first).min
chris@37 826 current = root
chris@37 827 started = false
chris@37 828 headings.each do |level, anchor, item|
chris@37 829 if level > current
chris@37 830 out << '<ul><li>' * (level - current)
chris@37 831 elsif level < current
chris@37 832 out << "</li></ul>\n" * (current - level) + "</li><li>"
chris@37 833 elsif started
chris@37 834 out << '</li><li>'
chris@37 835 end
chris@37 836 out << "<a href=\"##{anchor}\">#{item}</a>"
chris@37 837 current = level
chris@37 838 started = true
chris@37 839 end
chris@37 840 out << '</li></ul>' * (current - root)
chris@37 841 out << '</li></ul>'
chris@37 842 end
chris@37 843 end
chris@37 844 end
Chris@0 845
Chris@0 846 # Same as Rails' simple_format helper without using paragraphs
Chris@0 847 def simple_format_without_paragraph(text)
Chris@0 848 text.to_s.
Chris@0 849 gsub(/\r\n?/, "\n"). # \r\n and \r -> \n
Chris@0 850 gsub(/\n\n+/, "<br /><br />"). # 2+ newline -> 2 br
Chris@909 851 gsub(/([^\n]\n)(?=[^\n])/, '\1<br />'). # 1 newline -> br
Chris@909 852 html_safe
Chris@0 853 end
Chris@0 854
Chris@0 855 def lang_options_for_select(blank=true)
Chris@0 856 (blank ? [["(auto)", ""]] : []) +
Chris@0 857 valid_languages.collect{|lang| [ ll(lang.to_s, :general_lang_name), lang.to_s]}.sort{|x,y| x.last <=> y.last }
Chris@0 858 end
Chris@0 859
Chris@0 860 def label_tag_for(name, option_tags = nil, options = {})
Chris@0 861 label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "")
Chris@0 862 content_tag("label", label_text)
Chris@0 863 end
Chris@0 864
Chris@909 865 def labelled_tabular_form_for(*args, &proc)
Chris@909 866 args << {} unless args.last.is_a?(Hash)
Chris@909 867 options = args.last
Chris@0 868 options[:html] ||= {}
Chris@0 869 options[:html][:class] = 'tabular' unless options[:html].has_key?(:class)
Chris@909 870 options.merge!({:builder => TabularFormBuilder})
Chris@909 871 form_for(*args, &proc)
Chris@909 872 end
Chris@909 873
Chris@909 874 def labelled_form_for(*args, &proc)
Chris@909 875 args << {} unless args.last.is_a?(Hash)
Chris@909 876 options = args.last
Chris@909 877 options.merge!({:builder => TabularFormBuilder})
Chris@909 878 form_for(*args, &proc)
Chris@0 879 end
Chris@0 880
Chris@0 881 def back_url_hidden_field_tag
Chris@0 882 back_url = params[:back_url] || request.env['HTTP_REFERER']
Chris@0 883 back_url = CGI.unescape(back_url.to_s)
Chris@0 884 hidden_field_tag('back_url', CGI.escape(back_url)) unless back_url.blank?
Chris@0 885 end
Chris@0 886
Chris@0 887 def check_all_links(form_name)
Chris@0 888 link_to_function(l(:button_check_all), "checkAll('#{form_name}', true)") +
Chris@909 889 " | ".html_safe +
Chris@0 890 link_to_function(l(:button_uncheck_all), "checkAll('#{form_name}', false)")
Chris@0 891 end
Chris@0 892
Chris@0 893 def progress_bar(pcts, options={})
Chris@0 894 pcts = [pcts, pcts] unless pcts.is_a?(Array)
Chris@0 895 pcts = pcts.collect(&:round)
Chris@0 896 pcts[1] = pcts[1] - pcts[0]
Chris@0 897 pcts << (100 - pcts[1] - pcts[0])
Chris@0 898 width = options[:width] || '100px;'
Chris@0 899 legend = options[:legend] || ''
Chris@0 900 content_tag('table',
Chris@0 901 content_tag('tr',
Chris@909 902 (pcts[0] > 0 ? content_tag('td', '', :style => "width: #{pcts[0]}%;", :class => 'closed') : ''.html_safe) +
Chris@909 903 (pcts[1] > 0 ? content_tag('td', '', :style => "width: #{pcts[1]}%;", :class => 'done') : ''.html_safe) +
Chris@909 904 (pcts[2] > 0 ? content_tag('td', '', :style => "width: #{pcts[2]}%;", :class => 'todo') : ''.html_safe)
Chris@909 905 ), :class => 'progress', :style => "width: #{width};").html_safe +
Chris@909 906 content_tag('p', legend, :class => 'pourcent').html_safe
Chris@0 907 end
Chris@441 908
Chris@0 909 def checked_image(checked=true)
Chris@0 910 if checked
Chris@0 911 image_tag 'toggle_check.png'
Chris@0 912 end
Chris@0 913 end
Chris@441 914
Chris@0 915 def context_menu(url)
Chris@0 916 unless @context_menu_included
Chris@0 917 content_for :header_tags do
Chris@0 918 javascript_include_tag('context_menu') +
Chris@0 919 stylesheet_link_tag('context_menu')
Chris@0 920 end
Chris@14 921 if l(:direction) == 'rtl'
Chris@14 922 content_for :header_tags do
Chris@14 923 stylesheet_link_tag('context_menu_rtl')
Chris@14 924 end
Chris@14 925 end
Chris@0 926 @context_menu_included = true
Chris@0 927 end
Chris@0 928 javascript_tag "new ContextMenu('#{ url_for(url) }')"
Chris@0 929 end
Chris@0 930
Chris@0 931 def context_menu_link(name, url, options={})
Chris@0 932 options[:class] ||= ''
Chris@0 933 if options.delete(:selected)
Chris@0 934 options[:class] << ' icon-checked disabled'
Chris@0 935 options[:disabled] = true
Chris@0 936 end
Chris@0 937 if options.delete(:disabled)
Chris@0 938 options.delete(:method)
Chris@0 939 options.delete(:confirm)
Chris@0 940 options.delete(:onclick)
Chris@0 941 options[:class] << ' disabled'
Chris@0 942 url = '#'
Chris@0 943 end
Chris@909 944 link_to h(name), url, options
Chris@0 945 end
Chris@0 946
Chris@0 947 def calendar_for(field_id)
Chris@0 948 include_calendar_headers_tags
Chris@0 949 image_tag("calendar.png", {:id => "#{field_id}_trigger",:class => "calendar-trigger"}) +
Chris@0 950 javascript_tag("Calendar.setup({inputField : '#{field_id}', ifFormat : '%Y-%m-%d', button : '#{field_id}_trigger' });")
Chris@0 951 end
Chris@0 952
Chris@0 953 def include_calendar_headers_tags
Chris@0 954 unless @calendar_headers_tags_included
Chris@0 955 @calendar_headers_tags_included = true
Chris@0 956 content_for :header_tags do
Chris@0 957 start_of_week = case Setting.start_of_week.to_i
Chris@0 958 when 1
Chris@0 959 'Calendar._FD = 1;' # Monday
Chris@0 960 when 7
Chris@0 961 'Calendar._FD = 0;' # Sunday
Chris@441 962 when 6
Chris@441 963 'Calendar._FD = 6;' # Saturday
Chris@0 964 else
Chris@0 965 '' # use language
Chris@0 966 end
Chris@441 967
Chris@0 968 javascript_include_tag('calendar/calendar') +
Chris@0 969 javascript_include_tag("calendar/lang/calendar-#{current_language.to_s.downcase}.js") +
Chris@441 970 javascript_tag(start_of_week) +
Chris@0 971 javascript_include_tag('calendar/calendar-setup') +
Chris@0 972 stylesheet_link_tag('calendar')
Chris@0 973 end
Chris@0 974 end
Chris@0 975 end
Chris@0 976
Chris@0 977 def content_for(name, content = nil, &block)
Chris@0 978 @has_content ||= {}
Chris@0 979 @has_content[name] = true
Chris@0 980 super(name, content, &block)
Chris@0 981 end
Chris@0 982
Chris@0 983 def has_content?(name)
Chris@0 984 (@has_content && @has_content[name]) || false
Chris@0 985 end
Chris@0 986
Chris@909 987 def email_delivery_enabled?
Chris@909 988 !!ActionMailer::Base.perform_deliveries
Chris@909 989 end
Chris@909 990
Chris@0 991 # Returns the avatar image tag for the given +user+ if avatars are enabled
Chris@0 992 # +user+ can be a User or a string that will be scanned for an email address (eg. 'joe <joe@foo.bar>')
Chris@0 993 def avatar(user, options = { })
Chris@0 994 if Setting.gravatar_enabled?
chris@22 995 options.merge!({:ssl => (defined?(request) && request.ssl?), :default => Setting.gravatar_default})
Chris@0 996 email = nil
Chris@0 997 if user.respond_to?(:mail)
Chris@0 998 email = user.mail
Chris@0 999 elsif user.to_s =~ %r{<(.+?)>}
Chris@0 1000 email = $1
Chris@0 1001 end
Chris@0 1002 return gravatar(email.to_s.downcase, options) unless email.blank? rescue nil
chris@22 1003 else
chris@22 1004 ''
Chris@0 1005 end
Chris@0 1006 end
Chris@441 1007
Chris@909 1008 def sanitize_anchor_name(anchor)
Chris@909 1009 anchor.gsub(%r{[^\w\s\-]}, '').gsub(%r{\s+(\-+\s*)?}, '-')
Chris@909 1010 end
Chris@909 1011
Chris@245 1012 # Returns the javascript tags that are included in the html layout head
Chris@245 1013 def javascript_heads
Chris@245 1014 tags = javascript_include_tag(:defaults)
Chris@245 1015 unless User.current.pref.warn_on_leaving_unsaved == '0'
Chris@909 1016 tags << "\n".html_safe + javascript_tag("Event.observe(window, 'load', function(){ new WarnLeavingUnsaved('#{escape_javascript( l(:text_warn_on_leaving_unsaved) )}'); });")
Chris@245 1017 end
Chris@245 1018 tags
Chris@245 1019 end
Chris@0 1020
Chris@14 1021 def favicon
Chris@909 1022 "<link rel='shortcut icon' href='#{image_path('/favicon.ico')}' />".html_safe
Chris@14 1023 end
Chris@441 1024
Chris@441 1025 def robot_exclusion_tag
Chris@909 1026 '<meta name="robots" content="noindex,follow,noarchive" />'.html_safe
Chris@441 1027 end
Chris@441 1028
Chris@119 1029 # Returns true if arg is expected in the API response
Chris@119 1030 def include_in_api_response?(arg)
Chris@119 1031 unless @included_in_api_response
Chris@119 1032 param = params[:include]
Chris@119 1033 @included_in_api_response = param.is_a?(Array) ? param.collect(&:to_s) : param.to_s.split(',')
Chris@119 1034 @included_in_api_response.collect!(&:strip)
Chris@119 1035 end
Chris@119 1036 @included_in_api_response.include?(arg.to_s)
Chris@119 1037 end
Chris@14 1038
Chris@119 1039 # Returns options or nil if nometa param or X-Redmine-Nometa header
Chris@119 1040 # was set in the request
Chris@119 1041 def api_meta(options)
Chris@119 1042 if params[:nometa].present? || request.headers['X-Redmine-Nometa']
Chris@119 1043 # compatibility mode for activeresource clients that raise
Chris@119 1044 # an error when unserializing an array with attributes
Chris@119 1045 nil
Chris@119 1046 else
Chris@119 1047 options
Chris@119 1048 end
Chris@119 1049 end
Chris@441 1050
Chris@0 1051 private
Chris@0 1052
Chris@0 1053 def wiki_helper
Chris@0 1054 helper = Redmine::WikiFormatting.helper_for(Setting.text_formatting)
Chris@0 1055 extend helper
Chris@0 1056 return self
Chris@0 1057 end
Chris@441 1058
Chris@441 1059 def link_to_content_update(text, url_params = {}, html_options = {})
Chris@441 1060 link_to(text, url_params, html_options)
Chris@0 1061 end
Chris@0 1062 end