annotate app/helpers/application_helper.rb @ 931:ec1c49528f36 cannam_integration

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