annotate app/helpers/application_helper.rb @ 36:de76cd3e8c8e cc-branches

* Probably abortive experiments in extracting the branch from Hg
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Wed, 20 Oct 2010 10:07:29 +0100
parents ca82a3468d27
children 33d69fee1d99
rev   line source
Chris@0 1 # redMine - project management software
Chris@0 2 # Copyright (C) 2006-2007 Jean-Philippe Lang
Chris@0 3 #
Chris@0 4 # This program is free software; you can redistribute it and/or
Chris@0 5 # modify it under the terms of the GNU General Public License
Chris@0 6 # as published by the Free Software Foundation; either version 2
Chris@0 7 # of the License, or (at your option) any later version.
Chris@0 8 #
Chris@0 9 # This program is distributed in the hope that it will be useful,
Chris@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 # GNU General Public License for more details.
Chris@0 13 #
Chris@0 14 # You should have received a copy of the GNU General Public License
Chris@0 15 # along with this program; if not, write to the Free Software
Chris@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 17
Chris@0 18 require 'forwardable'
Chris@0 19 require 'cgi'
Chris@0 20
Chris@0 21 module ApplicationHelper
Chris@0 22 include Redmine::WikiFormatting::Macros::Definitions
Chris@0 23 include Redmine::I18n
Chris@0 24 include GravatarHelper::PublicMethods
Chris@0 25
Chris@0 26 extend Forwardable
Chris@0 27 def_delegators :wiki_helper, :wikitoolbar_for, :heads_for_wiki_formatter
Chris@0 28
Chris@0 29 # Return true if user is authorized for controller/action, otherwise false
Chris@0 30 def authorize_for(controller, action)
Chris@0 31 User.current.allowed_to?({:controller => controller, :action => action}, @project)
Chris@0 32 end
Chris@0 33
Chris@0 34 # Display a link if user is authorized
chris@22 35 #
chris@22 36 # @param [String] name Anchor text (passed to link_to)
chris@22 37 # @param [Hash, String] options Hash params or url for the link target (passed to link_to).
chris@22 38 # This will checked by authorize_for to see if the user is authorized
chris@22 39 # @param [optional, Hash] html_options Options passed to link_to
chris@22 40 # @param [optional, Hash] parameters_for_method_reference Extra parameters for link_to
Chris@0 41 def link_to_if_authorized(name, options = {}, html_options = nil, *parameters_for_method_reference)
chris@22 42 if options.is_a?(String)
chris@22 43 begin
chris@22 44 route = ActionController::Routing::Routes.recognize_path(options.gsub(/\?.*/,''), :method => options[:method] || :get)
chris@22 45 link_controller = route[:controller]
chris@22 46 link_action = route[:action]
chris@22 47 rescue ActionController::RoutingError # Parse failed, not a route
chris@22 48 link_controller, link_action = nil, nil
chris@22 49 end
chris@22 50 else
chris@22 51 link_controller = options[:controller] || params[:controller]
chris@22 52 link_action = options[:action]
chris@22 53 end
chris@22 54
chris@22 55 link_to(name, options, html_options, *parameters_for_method_reference) if authorize_for(link_controller, link_action)
Chris@0 56 end
Chris@0 57
Chris@0 58 # Display a link to remote if user is authorized
Chris@0 59 def link_to_remote_if_authorized(name, options = {}, html_options = nil)
Chris@0 60 url = options[:url] || {}
Chris@0 61 link_to_remote(name, options, html_options) if authorize_for(url[:controller] || params[:controller], url[:action])
Chris@0 62 end
Chris@0 63
Chris@0 64 # Displays a link to user's account page if active
Chris@0 65 def link_to_user(user, options={})
Chris@0 66 if user.is_a?(User)
Chris@0 67 name = h(user.name(options[:format]))
Chris@0 68 if user.active?
Chris@0 69 link_to name, :controller => 'users', :action => 'show', :id => user
Chris@0 70 else
Chris@0 71 name
Chris@0 72 end
Chris@0 73 else
Chris@0 74 h(user.to_s)
Chris@0 75 end
Chris@0 76 end
Chris@0 77
Chris@0 78 # Displays a link to +issue+ with its subject.
Chris@0 79 # Examples:
Chris@0 80 #
Chris@0 81 # link_to_issue(issue) # => Defect #6: This is the subject
Chris@0 82 # link_to_issue(issue, :truncate => 6) # => Defect #6: This i...
Chris@0 83 # link_to_issue(issue, :subject => false) # => Defect #6
Chris@0 84 # link_to_issue(issue, :project => true) # => Foo - Defect #6
Chris@0 85 #
Chris@0 86 def link_to_issue(issue, options={})
Chris@0 87 title = nil
Chris@0 88 subject = nil
Chris@0 89 if options[:subject] == false
Chris@0 90 title = truncate(issue.subject, :length => 60)
Chris@0 91 else
Chris@0 92 subject = issue.subject
Chris@0 93 if options[:truncate]
Chris@0 94 subject = truncate(subject, :length => options[:truncate])
Chris@0 95 end
Chris@0 96 end
Chris@0 97 s = link_to "#{issue.tracker} ##{issue.id}", {:controller => "issues", :action => "show", :id => issue},
Chris@0 98 :class => issue.css_classes,
Chris@0 99 :title => title
Chris@0 100 s << ": #{h subject}" if subject
Chris@0 101 s = "#{h issue.project} - " + s if options[:project]
Chris@0 102 s
Chris@0 103 end
Chris@0 104
Chris@0 105 # Generates a link to an attachment.
Chris@0 106 # Options:
Chris@0 107 # * :text - Link text (default to attachment filename)
Chris@0 108 # * :download - Force download (default: false)
Chris@0 109 def link_to_attachment(attachment, options={})
Chris@0 110 text = options.delete(:text) || attachment.filename
Chris@0 111 action = options.delete(:download) ? 'download' : 'show'
Chris@0 112
Chris@0 113 link_to(h(text), {:controller => 'attachments', :action => action, :id => attachment, :filename => attachment.filename }, options)
Chris@0 114 end
Chris@0 115
Chris@0 116 # Generates a link to a SCM revision
Chris@0 117 # Options:
Chris@0 118 # * :text - Link text (default to the formatted revision)
Chris@0 119 def link_to_revision(revision, project, options={})
Chris@0 120 text = options.delete(:text) || format_revision(revision)
Chris@3 121 rev = revision.respond_to?(:identifier) ? revision.identifier : revision
Chris@0 122
Chris@3 123 link_to(text, {:controller => 'repositories', :action => 'revision', :id => project, :rev => rev},
Chris@3 124 :title => l(:label_revision_id, format_revision(revision)))
Chris@0 125 end
chris@22 126
chris@22 127 def link_to_project(project, options={})
chris@22 128 options[:class] ||= 'project'
chris@22 129 link_to(h(project), {:controller => 'projects', :action => 'show', :id => project}, :class => options[:class])
chris@22 130 end
Chris@0 131
Chris@14 132 # Generates a link to a project if active
Chris@14 133 # Examples:
Chris@14 134 #
Chris@14 135 # link_to_project(project) # => link to the specified project overview
Chris@14 136 # link_to_project(project, :action=>'settings') # => link to project settings
Chris@14 137 # link_to_project(project, {:only_path => false}, :class => "project") # => 3rd arg adds html options
Chris@14 138 # link_to_project(project, {}, :class => "project") # => html options with default url (project overview)
Chris@14 139 #
Chris@14 140 def link_to_project(project, options={}, html_options = nil)
Chris@14 141 if project.active?
Chris@14 142 url = {:controller => 'projects', :action => 'show', :id => project}.merge(options)
Chris@14 143 link_to(h(project), url, html_options)
Chris@14 144 else
Chris@14 145 h(project)
Chris@14 146 end
Chris@14 147 end
Chris@14 148
Chris@0 149 def toggle_link(name, id, options={})
Chris@0 150 onclick = "Element.toggle('#{id}'); "
Chris@0 151 onclick << (options[:focus] ? "Form.Element.focus('#{options[:focus]}'); " : "this.blur(); ")
Chris@0 152 onclick << "return false;"
Chris@0 153 link_to(name, "#", :onclick => onclick)
Chris@0 154 end
Chris@0 155
Chris@0 156 def image_to_function(name, function, html_options = {})
Chris@0 157 html_options.symbolize_keys!
Chris@0 158 tag(:input, html_options.merge({
Chris@0 159 :type => "image", :src => image_path(name),
Chris@0 160 :onclick => (html_options[:onclick] ? "#{html_options[:onclick]}; " : "") + "#{function};"
Chris@0 161 }))
Chris@0 162 end
Chris@0 163
Chris@0 164 def prompt_to_remote(name, text, param, url, html_options = {})
Chris@0 165 html_options[:onclick] = "promptToRemote('#{text}', '#{param}', '#{url_for(url)}'); return false;"
Chris@0 166 link_to name, {}, html_options
Chris@0 167 end
Chris@0 168
Chris@0 169 def format_activity_title(text)
Chris@0 170 h(truncate_single_line(text, :length => 100))
Chris@0 171 end
Chris@0 172
Chris@0 173 def format_activity_day(date)
Chris@0 174 date == Date.today ? l(:label_today).titleize : format_date(date)
Chris@0 175 end
Chris@0 176
Chris@0 177 def format_activity_description(text)
Chris@0 178 h(truncate(text.to_s, :length => 120).gsub(%r{[\r\n]*<(pre|code)>.*$}m, '...')).gsub(/[\r\n]+/, "<br />")
Chris@0 179 end
Chris@0 180
Chris@0 181 def format_version_name(version)
Chris@0 182 if version.project == @project
Chris@0 183 h(version)
Chris@0 184 else
Chris@0 185 h("#{version.project} - #{version}")
Chris@0 186 end
Chris@0 187 end
Chris@0 188
Chris@0 189 def due_date_distance_in_words(date)
Chris@0 190 if date
Chris@0 191 l((date < Date.today ? :label_roadmap_overdue : :label_roadmap_due_in), distance_of_date_in_words(Date.today, date))
Chris@0 192 end
Chris@0 193 end
Chris@0 194
Chris@0 195 def render_page_hierarchy(pages, node=nil)
Chris@0 196 content = ''
Chris@0 197 if pages[node]
Chris@0 198 content << "<ul class=\"pages-hierarchy\">\n"
Chris@0 199 pages[node].each do |page|
Chris@0 200 content << "<li>"
Chris@0 201 content << link_to(h(page.pretty_title), {:controller => 'wiki', :action => 'index', :id => page.project, :page => page.title},
Chris@0 202 :title => (page.respond_to?(:updated_on) ? l(:label_updated_time, distance_of_time_in_words(Time.now, page.updated_on)) : nil))
Chris@0 203 content << "\n" + render_page_hierarchy(pages, page.id) if pages[page.id]
Chris@0 204 content << "</li>\n"
Chris@0 205 end
Chris@0 206 content << "</ul>\n"
Chris@0 207 end
Chris@0 208 content
Chris@0 209 end
Chris@0 210
Chris@0 211 # Renders flash messages
Chris@0 212 def render_flash_messages
Chris@0 213 s = ''
Chris@0 214 flash.each do |k,v|
Chris@0 215 s << content_tag('div', v, :class => "flash #{k}")
Chris@0 216 end
Chris@0 217 s
Chris@0 218 end
Chris@0 219
Chris@0 220 # Renders tabs and their content
Chris@0 221 def render_tabs(tabs)
Chris@0 222 if tabs.any?
Chris@0 223 render :partial => 'common/tabs', :locals => {:tabs => tabs}
Chris@0 224 else
Chris@0 225 content_tag 'p', l(:label_no_data), :class => "nodata"
Chris@0 226 end
Chris@0 227 end
Chris@0 228
Chris@0 229 # Renders the project quick-jump box
Chris@0 230 def render_project_jump_box
Chris@0 231 # Retrieve them now to avoid a COUNT query
Chris@0 232 projects = User.current.projects.all
Chris@0 233 if projects.any?
Chris@0 234 s = '<select onchange="if (this.value != \'\') { window.location = this.value; }">' +
Chris@0 235 "<option value=''>#{ l(:label_jump_to_a_project) }</option>" +
Chris@0 236 '<option value="" disabled="disabled">---</option>'
Chris@0 237 s << project_tree_options_for_select(projects, :selected => @project) do |p|
Chris@0 238 { :value => url_for(:controller => 'projects', :action => 'show', :id => p, :jump => current_menu_item) }
Chris@0 239 end
Chris@0 240 s << '</select>'
Chris@0 241 s
Chris@0 242 end
Chris@0 243 end
Chris@0 244
Chris@0 245 def project_tree_options_for_select(projects, options = {})
Chris@0 246 s = ''
Chris@0 247 project_tree(projects) do |project, level|
Chris@0 248 name_prefix = (level > 0 ? ('&nbsp;' * 2 * level + '&#187; ') : '')
Chris@0 249 tag_options = {:value => project.id}
Chris@0 250 if project == options[:selected] || (options[:selected].respond_to?(:include?) && options[:selected].include?(project))
Chris@0 251 tag_options[:selected] = 'selected'
Chris@0 252 else
Chris@0 253 tag_options[:selected] = nil
Chris@0 254 end
Chris@0 255 tag_options.merge!(yield(project)) if block_given?
Chris@0 256 s << content_tag('option', name_prefix + h(project), tag_options)
Chris@0 257 end
Chris@0 258 s
Chris@0 259 end
Chris@0 260
Chris@0 261 # Yields the given block for each project with its level in the tree
Chris@0 262 def project_tree(projects, &block)
Chris@0 263 ancestors = []
Chris@0 264 projects.sort_by(&:lft).each do |project|
Chris@0 265 while (ancestors.any? && !project.is_descendant_of?(ancestors.last))
Chris@0 266 ancestors.pop
Chris@0 267 end
Chris@0 268 yield project, ancestors.size
Chris@0 269 ancestors << project
Chris@0 270 end
Chris@0 271 end
Chris@0 272
Chris@0 273 def project_nested_ul(projects, &block)
Chris@0 274 s = ''
Chris@0 275 if projects.any?
Chris@0 276 ancestors = []
Chris@0 277 projects.sort_by(&:lft).each do |project|
Chris@0 278 if (ancestors.empty? || project.is_descendant_of?(ancestors.last))
Chris@0 279 s << "<ul>\n"
Chris@0 280 else
Chris@0 281 ancestors.pop
Chris@0 282 s << "</li>"
Chris@0 283 while (ancestors.any? && !project.is_descendant_of?(ancestors.last))
Chris@0 284 ancestors.pop
Chris@0 285 s << "</ul></li>\n"
Chris@0 286 end
Chris@0 287 end
Chris@0 288 s << "<li>"
Chris@0 289 s << yield(project).to_s
Chris@0 290 ancestors << project
Chris@0 291 end
Chris@0 292 s << ("</li></ul>\n" * ancestors.size)
Chris@0 293 end
Chris@0 294 s
Chris@0 295 end
Chris@0 296
Chris@0 297 def principals_check_box_tags(name, principals)
Chris@0 298 s = ''
Chris@0 299 principals.sort.each do |principal|
Chris@0 300 s << "<label>#{ check_box_tag name, principal.id, false } #{h principal}</label>\n"
Chris@0 301 end
Chris@0 302 s
Chris@0 303 end
Chris@0 304
Chris@0 305 # Truncates and returns the string as a single line
Chris@0 306 def truncate_single_line(string, *args)
Chris@0 307 truncate(string.to_s, *args).gsub(%r{[\r\n]+}m, ' ')
Chris@0 308 end
Chris@0 309
Chris@0 310 # Truncates at line break after 250 characters or options[:length]
Chris@0 311 def truncate_lines(string, options={})
Chris@0 312 length = options[:length] || 250
Chris@0 313 if string.to_s =~ /\A(.{#{length}}.*?)$/m
Chris@0 314 "#{$1}..."
Chris@0 315 else
Chris@0 316 string
Chris@0 317 end
Chris@0 318 end
Chris@0 319
Chris@0 320 def html_hours(text)
Chris@0 321 text.gsub(%r{(\d+)\.(\d+)}, '<span class="hours hours-int">\1</span><span class="hours hours-dec">.\2</span>')
Chris@0 322 end
Chris@0 323
Chris@0 324 def authoring(created, author, options={})
Chris@0 325 l(options[:label] || :label_added_time_by, :author => link_to_user(author), :age => time_tag(created))
Chris@0 326 end
Chris@0 327
Chris@0 328 def time_tag(time)
Chris@0 329 text = distance_of_time_in_words(Time.now, time)
Chris@0 330 if @project
chris@22 331 link_to(text, {:controller => 'activities', :action => 'index', :id => @project, :from => time.to_date}, :title => format_time(time))
Chris@0 332 else
Chris@0 333 content_tag('acronym', text, :title => format_time(time))
Chris@0 334 end
Chris@0 335 end
Chris@0 336
Chris@0 337 def syntax_highlight(name, content)
Chris@0 338 Redmine::SyntaxHighlighting.highlight_by_filename(content, name)
Chris@0 339 end
Chris@0 340
Chris@0 341 def to_path_param(path)
Chris@0 342 path.to_s.split(%r{[/\\]}).select {|p| !p.blank?}
Chris@0 343 end
Chris@0 344
Chris@0 345 def pagination_links_full(paginator, count=nil, options={})
Chris@0 346 page_param = options.delete(:page_param) || :page
Chris@0 347 per_page_links = options.delete(:per_page_links)
Chris@0 348 url_param = params.dup
Chris@0 349 # don't reuse query params if filters are present
Chris@0 350 url_param.merge!(:fields => nil, :values => nil, :operators => nil) if url_param.delete(:set_filter)
Chris@0 351
Chris@0 352 html = ''
Chris@0 353 if paginator.current.previous
Chris@0 354 html << link_to_remote_content_update('&#171; ' + l(:label_previous), url_param.merge(page_param => paginator.current.previous)) + ' '
Chris@0 355 end
Chris@0 356
Chris@0 357 html << (pagination_links_each(paginator, options) do |n|
Chris@0 358 link_to_remote_content_update(n.to_s, url_param.merge(page_param => n))
Chris@0 359 end || '')
Chris@0 360
Chris@0 361 if paginator.current.next
Chris@0 362 html << ' ' + link_to_remote_content_update((l(:label_next) + ' &#187;'), url_param.merge(page_param => paginator.current.next))
Chris@0 363 end
Chris@0 364
Chris@0 365 unless count.nil?
Chris@0 366 html << " (#{paginator.current.first_item}-#{paginator.current.last_item}/#{count})"
Chris@0 367 if per_page_links != false && links = per_page_links(paginator.items_per_page)
Chris@0 368 html << " | #{links}"
Chris@0 369 end
Chris@0 370 end
Chris@0 371
Chris@0 372 html
Chris@0 373 end
Chris@0 374
Chris@0 375 def per_page_links(selected=nil)
Chris@0 376 url_param = params.dup
Chris@0 377 url_param.clear if url_param.has_key?(:set_filter)
Chris@0 378
Chris@0 379 links = Setting.per_page_options_array.collect do |n|
Chris@0 380 n == selected ? n : link_to_remote(n, {:update => "content",
Chris@0 381 :url => params.dup.merge(:per_page => n),
Chris@0 382 :method => :get},
Chris@0 383 {:href => url_for(url_param.merge(:per_page => n))})
Chris@0 384 end
Chris@0 385 links.size > 1 ? l(:label_display_per_page, links.join(', ')) : nil
Chris@0 386 end
Chris@0 387
Chris@0 388 def reorder_links(name, url)
Chris@0 389 link_to(image_tag('2uparrow.png', :alt => l(:label_sort_highest)), url.merge({"#{name}[move_to]" => 'highest'}), :method => :post, :title => l(:label_sort_highest)) +
Chris@0 390 link_to(image_tag('1uparrow.png', :alt => l(:label_sort_higher)), url.merge({"#{name}[move_to]" => 'higher'}), :method => :post, :title => l(:label_sort_higher)) +
Chris@0 391 link_to(image_tag('1downarrow.png', :alt => l(:label_sort_lower)), url.merge({"#{name}[move_to]" => 'lower'}), :method => :post, :title => l(:label_sort_lower)) +
Chris@0 392 link_to(image_tag('2downarrow.png', :alt => l(:label_sort_lowest)), url.merge({"#{name}[move_to]" => 'lowest'}), :method => :post, :title => l(:label_sort_lowest))
Chris@0 393 end
Chris@0 394
Chris@0 395 def breadcrumb(*args)
Chris@0 396 elements = args.flatten
Chris@0 397 elements.any? ? content_tag('p', args.join(' &#187; ') + ' &#187; ', :class => 'breadcrumb') : nil
Chris@0 398 end
Chris@0 399
Chris@0 400 def other_formats_links(&block)
Chris@0 401 concat('<p class="other-formats">' + l(:label_export_to))
Chris@0 402 yield Redmine::Views::OtherFormatsBuilder.new(self)
Chris@0 403 concat('</p>')
Chris@0 404 end
Chris@0 405
Chris@0 406 def page_header_title
Chris@0 407 if @project.nil? || @project.new_record?
Chris@0 408 h(Setting.app_title)
Chris@0 409 else
Chris@0 410 b = []
Chris@0 411 ancestors = (@project.root? ? [] : @project.ancestors.visible)
Chris@0 412 if ancestors.any?
Chris@0 413 root = ancestors.shift
Chris@14 414 b << link_to_project(root, {:jump => current_menu_item}, :class => 'root')
Chris@0 415 if ancestors.size > 2
Chris@0 416 b << '&#8230;'
Chris@0 417 ancestors = ancestors[-2, 2]
Chris@0 418 end
Chris@14 419 b += ancestors.collect {|p| link_to_project(p, {:jump => current_menu_item}, :class => 'ancestor') }
Chris@0 420 end
Chris@0 421 b << h(@project)
Chris@0 422 b.join(' &#187; ')
Chris@0 423 end
Chris@0 424 end
Chris@0 425
Chris@0 426 def html_title(*args)
Chris@0 427 if args.empty?
Chris@0 428 title = []
Chris@0 429 title << @project.name if @project
Chris@0 430 title += @html_title if @html_title
Chris@0 431 title << Setting.app_title
Chris@0 432 title.select {|t| !t.blank? }.join(' - ')
Chris@0 433 else
Chris@0 434 @html_title ||= []
Chris@0 435 @html_title += args
Chris@0 436 end
Chris@0 437 end
Chris@0 438
Chris@14 439 # Returns the theme, controller name, and action as css classes for the
Chris@14 440 # HTML body.
Chris@14 441 def body_css_classes
Chris@14 442 css = []
Chris@14 443 if theme = Redmine::Themes.theme(Setting.ui_theme)
Chris@14 444 css << 'theme-' + theme.name
Chris@14 445 end
Chris@14 446
Chris@14 447 css << 'controller-' + params[:controller]
Chris@14 448 css << 'action-' + params[:action]
Chris@14 449 css.join(' ')
Chris@14 450 end
Chris@14 451
Chris@0 452 def accesskey(s)
Chris@0 453 Redmine::AccessKeys.key_for s
Chris@0 454 end
Chris@0 455
Chris@0 456 # Formats text according to system settings.
Chris@0 457 # 2 ways to call this method:
Chris@0 458 # * with a String: textilizable(text, options)
Chris@0 459 # * with an object and one of its attribute: textilizable(issue, :description, options)
Chris@0 460 def textilizable(*args)
Chris@0 461 options = args.last.is_a?(Hash) ? args.pop : {}
Chris@0 462 case args.size
Chris@0 463 when 1
Chris@0 464 obj = options[:object]
Chris@0 465 text = args.shift
Chris@0 466 when 2
Chris@0 467 obj = args.shift
Chris@0 468 attr = args.shift
Chris@0 469 text = obj.send(attr).to_s
Chris@0 470 else
Chris@0 471 raise ArgumentError, 'invalid arguments to textilizable'
Chris@0 472 end
Chris@0 473 return '' if text.blank?
Chris@0 474 project = options[:project] || @project || (obj && obj.respond_to?(:project) ? obj.project : nil)
Chris@0 475 only_path = options.delete(:only_path) == false ? false : true
Chris@0 476
Chris@0 477 text = Redmine::WikiFormatting.to_html(Setting.text_formatting, text, :object => obj, :attribute => attr) { |macro, args| exec_macro(macro, obj, args) }
Chris@0 478
Chris@0 479 parse_non_pre_blocks(text) do |text|
Chris@0 480 [:parse_inline_attachments, :parse_wiki_links, :parse_redmine_links].each do |method_name|
Chris@0 481 send method_name, text, project, obj, attr, only_path, options
Chris@0 482 end
Chris@0 483 end
Chris@0 484 end
Chris@0 485
Chris@0 486 def parse_non_pre_blocks(text)
Chris@0 487 s = StringScanner.new(text)
Chris@0 488 tags = []
Chris@0 489 parsed = ''
Chris@0 490 while !s.eos?
Chris@0 491 s.scan(/(.*?)(<(\/)?(pre|code)(.*?)>|\z)/im)
Chris@0 492 text, full_tag, closing, tag = s[1], s[2], s[3], s[4]
Chris@0 493 if tags.empty?
Chris@0 494 yield text
Chris@0 495 end
Chris@0 496 parsed << text
Chris@0 497 if tag
Chris@0 498 if closing
Chris@0 499 if tags.last == tag.downcase
Chris@0 500 tags.pop
Chris@0 501 end
Chris@0 502 else
Chris@0 503 tags << tag.downcase
Chris@0 504 end
Chris@0 505 parsed << full_tag
Chris@0 506 end
Chris@0 507 end
Chris@0 508 # Close any non closing tags
Chris@0 509 while tag = tags.pop
Chris@0 510 parsed << "</#{tag}>"
Chris@0 511 end
Chris@0 512 parsed
Chris@0 513 end
Chris@0 514
Chris@0 515 def parse_inline_attachments(text, project, obj, attr, only_path, options)
Chris@0 516 # when using an image link, try to use an attachment, if possible
Chris@0 517 if options[:attachments] || (obj && obj.respond_to?(:attachments))
Chris@0 518 attachments = nil
Chris@0 519 text.gsub!(/src="([^\/"]+\.(bmp|gif|jpg|jpeg|png))"(\s+alt="([^"]*)")?/i) do |m|
Chris@0 520 filename, ext, alt, alttext = $1.downcase, $2, $3, $4
Chris@0 521 attachments ||= (options[:attachments] || obj.attachments).sort_by(&:created_on).reverse
Chris@0 522 # search for the picture in attachments
Chris@0 523 if found = attachments.detect { |att| att.filename.downcase == filename }
Chris@0 524 image_url = url_for :only_path => only_path, :controller => 'attachments', :action => 'download', :id => found
Chris@0 525 desc = found.description.to_s.gsub('"', '')
Chris@0 526 if !desc.blank? && alttext.blank?
Chris@0 527 alt = " title=\"#{desc}\" alt=\"#{desc}\""
Chris@0 528 end
Chris@0 529 "src=\"#{image_url}\"#{alt}"
Chris@0 530 else
Chris@0 531 m
Chris@0 532 end
Chris@0 533 end
Chris@0 534 end
Chris@0 535 end
Chris@0 536
Chris@0 537 # Wiki links
Chris@0 538 #
Chris@0 539 # Examples:
Chris@0 540 # [[mypage]]
Chris@0 541 # [[mypage|mytext]]
Chris@0 542 # wiki links can refer other project wikis, using project name or identifier:
Chris@0 543 # [[project:]] -> wiki starting page
Chris@0 544 # [[project:|mytext]]
Chris@0 545 # [[project:mypage]]
Chris@0 546 # [[project:mypage|mytext]]
Chris@0 547 def parse_wiki_links(text, project, obj, attr, only_path, options)
Chris@0 548 text.gsub!(/(!)?(\[\[([^\]\n\|]+)(\|([^\]\n\|]+))?\]\])/) do |m|
Chris@0 549 link_project = project
Chris@0 550 esc, all, page, title = $1, $2, $3, $5
Chris@0 551 if esc.nil?
Chris@0 552 if page =~ /^([^\:]+)\:(.*)$/
Chris@0 553 link_project = Project.find_by_name($1) || Project.find_by_identifier($1)
Chris@0 554 page = $2
Chris@0 555 title ||= $1 if page.blank?
Chris@0 556 end
Chris@0 557
Chris@0 558 if link_project && link_project.wiki
Chris@0 559 # extract anchor
Chris@0 560 anchor = nil
Chris@0 561 if page =~ /^(.+?)\#(.+)$/
Chris@0 562 page, anchor = $1, $2
Chris@0 563 end
Chris@0 564 # check if page exists
Chris@0 565 wiki_page = link_project.wiki.find_page(page)
Chris@0 566 url = case options[:wiki_links]
Chris@0 567 when :local; "#{title}.html"
Chris@0 568 when :anchor; "##{title}" # used for single-file wiki export
Chris@0 569 else
Chris@0 570 url_for(:only_path => only_path, :controller => 'wiki', :action => 'index', :id => link_project, :page => Wiki.titleize(page), :anchor => anchor)
Chris@0 571 end
Chris@0 572 link_to((title || page), url, :class => ('wiki-page' + (wiki_page ? '' : ' new')))
Chris@0 573 else
Chris@0 574 # project or wiki doesn't exist
Chris@0 575 all
Chris@0 576 end
Chris@0 577 else
Chris@0 578 all
Chris@0 579 end
Chris@0 580 end
Chris@0 581 end
Chris@0 582
Chris@0 583 # Redmine links
Chris@0 584 #
Chris@0 585 # Examples:
Chris@0 586 # Issues:
Chris@0 587 # #52 -> Link to issue #52
Chris@0 588 # Changesets:
Chris@0 589 # r52 -> Link to revision 52
Chris@0 590 # commit:a85130f -> Link to scmid starting with a85130f
Chris@0 591 # Documents:
Chris@0 592 # document#17 -> Link to document with id 17
Chris@0 593 # document:Greetings -> Link to the document with title "Greetings"
Chris@0 594 # document:"Some document" -> Link to the document with title "Some document"
Chris@0 595 # Versions:
Chris@0 596 # version#3 -> Link to version with id 3
Chris@0 597 # version:1.0.0 -> Link to version named "1.0.0"
Chris@0 598 # version:"1.0 beta 2" -> Link to version named "1.0 beta 2"
Chris@0 599 # Attachments:
Chris@0 600 # attachment:file.zip -> Link to the attachment of the current object named file.zip
Chris@0 601 # Source files:
Chris@0 602 # source:some/file -> Link to the file located at /some/file in the project's repository
Chris@0 603 # source:some/file@52 -> Link to the file's revision 52
Chris@0 604 # source:some/file#L120 -> Link to line 120 of the file
Chris@0 605 # source:some/file@52#L120 -> Link to line 120 of the file's revision 52
Chris@0 606 # export:some/file -> Force the download of the file
Chris@0 607 # Forum messages:
Chris@0 608 # message#1218 -> Link to message with id 1218
Chris@0 609 def parse_redmine_links(text, project, obj, attr, only_path, options)
Chris@0 610 text.gsub!(%r{([\s\(,\-\[\>]|^)(!)?(attachment|document|version|commit|source|export|message|project)?((#|r)(\d+)|(:)([^"\s<>][^\s<>]*?|"[^"]+?"))(?=(?=[[:punct:]]\W)|,|\s|\]|<|$)}) do |m|
Chris@0 611 leading, esc, prefix, sep, identifier = $1, $2, $3, $5 || $7, $6 || $8
Chris@0 612 link = nil
Chris@0 613 if esc.nil?
Chris@0 614 if prefix.nil? && sep == 'r'
Chris@0 615 if project && (changeset = project.changesets.find_by_revision(identifier))
Chris@0 616 link = link_to("r#{identifier}", {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :rev => changeset.revision},
Chris@0 617 :class => 'changeset',
Chris@0 618 :title => truncate_single_line(changeset.comments, :length => 100))
Chris@0 619 end
Chris@0 620 elsif sep == '#'
Chris@0 621 oid = identifier.to_i
Chris@0 622 case prefix
Chris@0 623 when nil
Chris@0 624 if issue = Issue.visible.find_by_id(oid, :include => :status)
Chris@0 625 link = link_to("##{oid}", {:only_path => only_path, :controller => 'issues', :action => 'show', :id => oid},
Chris@0 626 :class => issue.css_classes,
Chris@0 627 :title => "#{truncate(issue.subject, :length => 100)} (#{issue.status.name})")
Chris@0 628 end
Chris@0 629 when 'document'
Chris@0 630 if document = Document.find_by_id(oid, :include => [:project], :conditions => Project.visible_by(User.current))
Chris@0 631 link = link_to h(document.title), {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document},
Chris@0 632 :class => 'document'
Chris@0 633 end
Chris@0 634 when 'version'
Chris@0 635 if version = Version.find_by_id(oid, :include => [:project], :conditions => Project.visible_by(User.current))
Chris@0 636 link = link_to h(version.name), {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version},
Chris@0 637 :class => 'version'
Chris@0 638 end
Chris@0 639 when 'message'
Chris@0 640 if message = Message.find_by_id(oid, :include => [:parent, {:board => :project}], :conditions => Project.visible_by(User.current))
Chris@0 641 link = link_to h(truncate(message.subject, :length => 60)), {:only_path => only_path,
Chris@0 642 :controller => 'messages',
Chris@0 643 :action => 'show',
Chris@0 644 :board_id => message.board,
Chris@0 645 :id => message.root,
Chris@0 646 :anchor => (message.parent ? "message-#{message.id}" : nil)},
Chris@0 647 :class => 'message'
Chris@0 648 end
Chris@0 649 when 'project'
Chris@0 650 if p = Project.visible.find_by_id(oid)
Chris@14 651 link = link_to_project(p, {:only_path => only_path}, :class => 'project')
Chris@0 652 end
Chris@0 653 end
Chris@0 654 elsif sep == ':'
Chris@0 655 # removes the double quotes if any
Chris@0 656 name = identifier.gsub(%r{^"(.*)"$}, "\\1")
Chris@0 657 case prefix
Chris@0 658 when 'document'
Chris@0 659 if project && document = project.documents.find_by_title(name)
Chris@0 660 link = link_to h(document.title), {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document},
Chris@0 661 :class => 'document'
Chris@0 662 end
Chris@0 663 when 'version'
Chris@0 664 if project && version = project.versions.find_by_name(name)
Chris@0 665 link = link_to h(version.name), {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version},
Chris@0 666 :class => 'version'
Chris@0 667 end
Chris@0 668 when 'commit'
Chris@0 669 if project && (changeset = project.changesets.find(:first, :conditions => ["scmid LIKE ?", "#{name}%"]))
Chris@3 670 link = link_to h("#{name}"), {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :rev => changeset.identifier},
Chris@0 671 :class => 'changeset',
Chris@0 672 :title => truncate_single_line(changeset.comments, :length => 100)
Chris@0 673 end
Chris@0 674 when 'source', 'export'
Chris@0 675 if project && project.repository
Chris@0 676 name =~ %r{^[/\\]*(.*?)(@([0-9a-f]+))?(#(L\d+))?$}
Chris@0 677 path, rev, anchor = $1, $3, $5
Chris@0 678 link = link_to h("#{prefix}:#{name}"), {:controller => 'repositories', :action => 'entry', :id => project,
Chris@0 679 :path => to_path_param(path),
Chris@0 680 :rev => rev,
Chris@0 681 :anchor => anchor,
Chris@0 682 :format => (prefix == 'export' ? 'raw' : nil)},
Chris@0 683 :class => (prefix == 'export' ? 'source download' : 'source')
Chris@0 684 end
Chris@0 685 when 'attachment'
Chris@0 686 attachments = options[:attachments] || (obj && obj.respond_to?(:attachments) ? obj.attachments : nil)
Chris@0 687 if attachments && attachment = attachments.detect {|a| a.filename == name }
Chris@0 688 link = link_to h(attachment.filename), {:only_path => only_path, :controller => 'attachments', :action => 'download', :id => attachment},
Chris@0 689 :class => 'attachment'
Chris@0 690 end
Chris@0 691 when 'project'
Chris@0 692 if p = Project.visible.find(:first, :conditions => ["identifier = :s OR LOWER(name) = :s", {:s => name.downcase}])
Chris@14 693 link = link_to_project(p, {:only_path => only_path}, :class => 'project')
Chris@0 694 end
Chris@0 695 end
Chris@0 696 end
Chris@0 697 end
Chris@0 698 leading + (link || "#{prefix}#{sep}#{identifier}")
Chris@0 699 end
Chris@0 700 end
Chris@0 701
Chris@0 702 # Same as Rails' simple_format helper without using paragraphs
Chris@0 703 def simple_format_without_paragraph(text)
Chris@0 704 text.to_s.
Chris@0 705 gsub(/\r\n?/, "\n"). # \r\n and \r -> \n
Chris@0 706 gsub(/\n\n+/, "<br /><br />"). # 2+ newline -> 2 br
Chris@0 707 gsub(/([^\n]\n)(?=[^\n])/, '\1<br />') # 1 newline -> br
Chris@0 708 end
Chris@0 709
Chris@0 710 def lang_options_for_select(blank=true)
Chris@0 711 (blank ? [["(auto)", ""]] : []) +
Chris@0 712 valid_languages.collect{|lang| [ ll(lang.to_s, :general_lang_name), lang.to_s]}.sort{|x,y| x.last <=> y.last }
Chris@0 713 end
Chris@0 714
Chris@0 715 def label_tag_for(name, option_tags = nil, options = {})
Chris@0 716 label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "")
Chris@0 717 content_tag("label", label_text)
Chris@0 718 end
Chris@0 719
Chris@0 720 def labelled_tabular_form_for(name, object, options, &proc)
Chris@0 721 options[:html] ||= {}
Chris@0 722 options[:html][:class] = 'tabular' unless options[:html].has_key?(:class)
Chris@0 723 form_for(name, object, options.merge({ :builder => TabularFormBuilder, :lang => current_language}), &proc)
Chris@0 724 end
Chris@0 725
Chris@0 726 def back_url_hidden_field_tag
Chris@0 727 back_url = params[:back_url] || request.env['HTTP_REFERER']
Chris@0 728 back_url = CGI.unescape(back_url.to_s)
Chris@0 729 hidden_field_tag('back_url', CGI.escape(back_url)) unless back_url.blank?
Chris@0 730 end
Chris@0 731
Chris@0 732 def check_all_links(form_name)
Chris@0 733 link_to_function(l(:button_check_all), "checkAll('#{form_name}', true)") +
Chris@0 734 " | " +
Chris@0 735 link_to_function(l(:button_uncheck_all), "checkAll('#{form_name}', false)")
Chris@0 736 end
Chris@0 737
Chris@0 738 def progress_bar(pcts, options={})
Chris@0 739 pcts = [pcts, pcts] unless pcts.is_a?(Array)
Chris@0 740 pcts = pcts.collect(&:round)
Chris@0 741 pcts[1] = pcts[1] - pcts[0]
Chris@0 742 pcts << (100 - pcts[1] - pcts[0])
Chris@0 743 width = options[:width] || '100px;'
Chris@0 744 legend = options[:legend] || ''
Chris@0 745 content_tag('table',
Chris@0 746 content_tag('tr',
Chris@0 747 (pcts[0] > 0 ? content_tag('td', '', :style => "width: #{pcts[0]}%;", :class => 'closed') : '') +
Chris@0 748 (pcts[1] > 0 ? content_tag('td', '', :style => "width: #{pcts[1]}%;", :class => 'done') : '') +
Chris@0 749 (pcts[2] > 0 ? content_tag('td', '', :style => "width: #{pcts[2]}%;", :class => 'todo') : '')
Chris@0 750 ), :class => 'progress', :style => "width: #{width};") +
Chris@0 751 content_tag('p', legend, :class => 'pourcent')
Chris@0 752 end
Chris@0 753
Chris@0 754 def checked_image(checked=true)
Chris@0 755 if checked
Chris@0 756 image_tag 'toggle_check.png'
Chris@0 757 end
Chris@0 758 end
Chris@0 759
Chris@0 760 def context_menu(url)
Chris@0 761 unless @context_menu_included
Chris@0 762 content_for :header_tags do
Chris@0 763 javascript_include_tag('context_menu') +
Chris@0 764 stylesheet_link_tag('context_menu')
Chris@0 765 end
Chris@14 766 if l(:direction) == 'rtl'
Chris@14 767 content_for :header_tags do
Chris@14 768 stylesheet_link_tag('context_menu_rtl')
Chris@14 769 end
Chris@14 770 end
Chris@0 771 @context_menu_included = true
Chris@0 772 end
Chris@0 773 javascript_tag "new ContextMenu('#{ url_for(url) }')"
Chris@0 774 end
Chris@0 775
Chris@0 776 def context_menu_link(name, url, options={})
Chris@0 777 options[:class] ||= ''
Chris@0 778 if options.delete(:selected)
Chris@0 779 options[:class] << ' icon-checked disabled'
Chris@0 780 options[:disabled] = true
Chris@0 781 end
Chris@0 782 if options.delete(:disabled)
Chris@0 783 options.delete(:method)
Chris@0 784 options.delete(:confirm)
Chris@0 785 options.delete(:onclick)
Chris@0 786 options[:class] << ' disabled'
Chris@0 787 url = '#'
Chris@0 788 end
Chris@0 789 link_to name, url, options
Chris@0 790 end
Chris@0 791
Chris@0 792 def calendar_for(field_id)
Chris@0 793 include_calendar_headers_tags
Chris@0 794 image_tag("calendar.png", {:id => "#{field_id}_trigger",:class => "calendar-trigger"}) +
Chris@0 795 javascript_tag("Calendar.setup({inputField : '#{field_id}', ifFormat : '%Y-%m-%d', button : '#{field_id}_trigger' });")
Chris@0 796 end
Chris@0 797
Chris@0 798 def include_calendar_headers_tags
Chris@0 799 unless @calendar_headers_tags_included
Chris@0 800 @calendar_headers_tags_included = true
Chris@0 801 content_for :header_tags do
Chris@0 802 start_of_week = case Setting.start_of_week.to_i
Chris@0 803 when 1
Chris@0 804 'Calendar._FD = 1;' # Monday
Chris@0 805 when 7
Chris@0 806 'Calendar._FD = 0;' # Sunday
Chris@0 807 else
Chris@0 808 '' # use language
Chris@0 809 end
Chris@0 810
Chris@0 811 javascript_include_tag('calendar/calendar') +
Chris@0 812 javascript_include_tag("calendar/lang/calendar-#{current_language.to_s.downcase}.js") +
Chris@0 813 javascript_tag(start_of_week) +
Chris@0 814 javascript_include_tag('calendar/calendar-setup') +
Chris@0 815 stylesheet_link_tag('calendar')
Chris@0 816 end
Chris@0 817 end
Chris@0 818 end
Chris@0 819
Chris@0 820 def content_for(name, content = nil, &block)
Chris@0 821 @has_content ||= {}
Chris@0 822 @has_content[name] = true
Chris@0 823 super(name, content, &block)
Chris@0 824 end
Chris@0 825
Chris@0 826 def has_content?(name)
Chris@0 827 (@has_content && @has_content[name]) || false
Chris@0 828 end
Chris@0 829
Chris@0 830 # Returns the avatar image tag for the given +user+ if avatars are enabled
Chris@0 831 # +user+ can be a User or a string that will be scanned for an email address (eg. 'joe <joe@foo.bar>')
Chris@0 832 def avatar(user, options = { })
Chris@0 833 if Setting.gravatar_enabled?
chris@22 834 options.merge!({:ssl => (defined?(request) && request.ssl?), :default => Setting.gravatar_default})
Chris@0 835 email = nil
Chris@0 836 if user.respond_to?(:mail)
Chris@0 837 email = user.mail
Chris@0 838 elsif user.to_s =~ %r{<(.+?)>}
Chris@0 839 email = $1
Chris@0 840 end
Chris@0 841 return gravatar(email.to_s.downcase, options) unless email.blank? rescue nil
chris@22 842 else
chris@22 843 ''
Chris@0 844 end
Chris@0 845 end
Chris@0 846
Chris@14 847 def favicon
Chris@14 848 "<link rel='shortcut icon' href='#{image_path('/favicon.ico')}' />"
Chris@14 849 end
Chris@14 850
Chris@0 851 private
Chris@0 852
Chris@0 853 def wiki_helper
Chris@0 854 helper = Redmine::WikiFormatting.helper_for(Setting.text_formatting)
Chris@0 855 extend helper
Chris@0 856 return self
Chris@0 857 end
Chris@0 858
Chris@0 859 def link_to_remote_content_update(text, url_params)
Chris@0 860 link_to_remote(text,
Chris@0 861 {:url => url_params, :method => :get, :update => 'content', :complete => 'window.scrollTo(0,0)'},
Chris@0 862 {:href => url_for(:params => url_params)}
Chris@0 863 )
Chris@0 864 end
Chris@0 865
Chris@0 866 end