annotate .svn/pristine/a9/a9d205d21aabcb1747a69fc0912d6918652b3b67.svn-base @ 1524:82fac3dcf466 redmine-2.5-integration

Fix failure to interpret Javascript when autocompleting members for project
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Thu, 11 Sep 2014 10:24:38 +0100
parents e248c7af89ec
children
rev   line source
Chris@1494 1 # encoding: utf-8
Chris@1494 2 #
Chris@1494 3 # Redmine - project management software
Chris@1494 4 # Copyright (C) 2006-2014 Jean-Philippe Lang
Chris@1494 5 #
Chris@1494 6 # This program is free software; you can redistribute it and/or
Chris@1494 7 # modify it under the terms of the GNU General Public License
Chris@1494 8 # as published by the Free Software Foundation; either version 2
Chris@1494 9 # of the License, or (at your option) any later version.
Chris@1494 10 #
Chris@1494 11 # This program is distributed in the hope that it will be useful,
Chris@1494 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1494 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1494 14 # GNU General Public License for more details.
Chris@1494 15 #
Chris@1494 16 # You should have received a copy of the GNU General Public License
Chris@1494 17 # along with this program; if not, write to the Free Software
Chris@1494 18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1494 19
Chris@1494 20 require 'forwardable'
Chris@1494 21 require 'cgi'
Chris@1494 22
Chris@1494 23 module ApplicationHelper
Chris@1494 24 include Redmine::WikiFormatting::Macros::Definitions
Chris@1494 25 include Redmine::I18n
Chris@1494 26 include GravatarHelper::PublicMethods
Chris@1494 27 include Redmine::Pagination::Helper
Chris@1494 28
Chris@1494 29 extend Forwardable
Chris@1494 30 def_delegators :wiki_helper, :wikitoolbar_for, :heads_for_wiki_formatter
Chris@1494 31
Chris@1494 32 # Return true if user is authorized for controller/action, otherwise false
Chris@1494 33 def authorize_for(controller, action)
Chris@1494 34 User.current.allowed_to?({:controller => controller, :action => action}, @project)
Chris@1494 35 end
Chris@1494 36
Chris@1494 37 # Display a link if user is authorized
Chris@1494 38 #
Chris@1494 39 # @param [String] name Anchor text (passed to link_to)
Chris@1494 40 # @param [Hash] options Hash params. This will checked by authorize_for to see if the user is authorized
Chris@1494 41 # @param [optional, Hash] html_options Options passed to link_to
Chris@1494 42 # @param [optional, Hash] parameters_for_method_reference Extra parameters for link_to
Chris@1494 43 def link_to_if_authorized(name, options = {}, html_options = nil, *parameters_for_method_reference)
Chris@1494 44 link_to(name, options, html_options, *parameters_for_method_reference) if authorize_for(options[:controller] || params[:controller], options[:action])
Chris@1494 45 end
Chris@1494 46
Chris@1494 47 # Displays a link to user's account page if active
Chris@1494 48 def link_to_user(user, options={})
Chris@1494 49 if user.is_a?(User)
Chris@1494 50 name = h(user.name(options[:format]))
Chris@1494 51 if user.active? || (User.current.admin? && user.logged?)
Chris@1494 52 link_to name, user_path(user), :class => user.css_classes
Chris@1494 53 else
Chris@1494 54 name
Chris@1494 55 end
Chris@1494 56 else
Chris@1494 57 h(user.to_s)
Chris@1494 58 end
Chris@1494 59 end
Chris@1494 60
Chris@1494 61 # Displays a link to +issue+ with its subject.
Chris@1494 62 # Examples:
Chris@1494 63 #
Chris@1494 64 # link_to_issue(issue) # => Defect #6: This is the subject
Chris@1494 65 # link_to_issue(issue, :truncate => 6) # => Defect #6: This i...
Chris@1494 66 # link_to_issue(issue, :subject => false) # => Defect #6
Chris@1494 67 # link_to_issue(issue, :project => true) # => Foo - Defect #6
Chris@1494 68 # link_to_issue(issue, :subject => false, :tracker => false) # => #6
Chris@1494 69 #
Chris@1494 70 def link_to_issue(issue, options={})
Chris@1494 71 title = nil
Chris@1494 72 subject = nil
Chris@1494 73 text = options[:tracker] == false ? "##{issue.id}" : "#{issue.tracker} ##{issue.id}"
Chris@1494 74 if options[:subject] == false
Chris@1494 75 title = truncate(issue.subject, :length => 60)
Chris@1494 76 else
Chris@1494 77 subject = issue.subject
Chris@1494 78 if options[:truncate]
Chris@1494 79 subject = truncate(subject, :length => options[:truncate])
Chris@1494 80 end
Chris@1494 81 end
Chris@1494 82 only_path = options[:only_path].nil? ? true : options[:only_path]
Chris@1494 83 s = link_to text, issue_path(issue, :only_path => only_path), :class => issue.css_classes, :title => title
Chris@1494 84 s << h(": #{subject}") if subject
Chris@1494 85 s = h("#{issue.project} - ") + s if options[:project]
Chris@1494 86 s
Chris@1494 87 end
Chris@1494 88
Chris@1494 89 # Generates a link to an attachment.
Chris@1494 90 # Options:
Chris@1494 91 # * :text - Link text (default to attachment filename)
Chris@1494 92 # * :download - Force download (default: false)
Chris@1494 93 def link_to_attachment(attachment, options={})
Chris@1494 94 text = options.delete(:text) || attachment.filename
Chris@1494 95 route_method = options.delete(:download) ? :download_named_attachment_path : :named_attachment_path
Chris@1494 96 html_options = options.slice!(:only_path)
Chris@1494 97 url = send(route_method, attachment, attachment.filename, options)
Chris@1494 98 link_to text, url, html_options
Chris@1494 99 end
Chris@1494 100
Chris@1494 101 # Generates a link to a SCM revision
Chris@1494 102 # Options:
Chris@1494 103 # * :text - Link text (default to the formatted revision)
Chris@1494 104 def link_to_revision(revision, repository, options={})
Chris@1494 105 if repository.is_a?(Project)
Chris@1494 106 repository = repository.repository
Chris@1494 107 end
Chris@1494 108 text = options.delete(:text) || format_revision(revision)
Chris@1494 109 rev = revision.respond_to?(:identifier) ? revision.identifier : revision
Chris@1494 110 link_to(
Chris@1494 111 h(text),
Chris@1494 112 {:controller => 'repositories', :action => 'revision', :id => repository.project, :repository_id => repository.identifier_param, :rev => rev},
Chris@1494 113 :title => l(:label_revision_id, format_revision(revision))
Chris@1494 114 )
Chris@1494 115 end
Chris@1494 116
Chris@1494 117 # Generates a link to a message
Chris@1494 118 def link_to_message(message, options={}, html_options = nil)
Chris@1494 119 link_to(
Chris@1494 120 truncate(message.subject, :length => 60),
Chris@1494 121 board_message_path(message.board_id, message.parent_id || message.id, {
Chris@1494 122 :r => (message.parent_id && message.id),
Chris@1494 123 :anchor => (message.parent_id ? "message-#{message.id}" : nil)
Chris@1494 124 }.merge(options)),
Chris@1494 125 html_options
Chris@1494 126 )
Chris@1494 127 end
Chris@1494 128
Chris@1494 129 # Generates a link to a project if active
Chris@1494 130 # Examples:
Chris@1494 131 #
Chris@1494 132 # link_to_project(project) # => link to the specified project overview
Chris@1494 133 # link_to_project(project, {:only_path => false}, :class => "project") # => 3rd arg adds html options
Chris@1494 134 # link_to_project(project, {}, :class => "project") # => html options with default url (project overview)
Chris@1494 135 #
Chris@1494 136 def link_to_project(project, options={}, html_options = nil)
Chris@1494 137 if project.archived?
Chris@1494 138 h(project.name)
Chris@1494 139 elsif options.key?(:action)
Chris@1494 140 ActiveSupport::Deprecation.warn "#link_to_project with :action option is deprecated and will be removed in Redmine 3.0."
Chris@1494 141 url = {:controller => 'projects', :action => 'show', :id => project}.merge(options)
Chris@1494 142 link_to project.name, url, html_options
Chris@1494 143 else
Chris@1494 144 link_to project.name, project_path(project, options), html_options
Chris@1494 145 end
Chris@1494 146 end
Chris@1494 147
Chris@1494 148 # Generates a link to a project settings if active
Chris@1494 149 def link_to_project_settings(project, options={}, html_options=nil)
Chris@1494 150 if project.active?
Chris@1494 151 link_to project.name, settings_project_path(project, options), html_options
Chris@1494 152 elsif project.archived?
Chris@1494 153 h(project.name)
Chris@1494 154 else
Chris@1494 155 link_to project.name, project_path(project, options), html_options
Chris@1494 156 end
Chris@1494 157 end
Chris@1494 158
Chris@1494 159 def wiki_page_path(page, options={})
Chris@1494 160 url_for({:controller => 'wiki', :action => 'show', :project_id => page.project, :id => page.title}.merge(options))
Chris@1494 161 end
Chris@1494 162
Chris@1494 163 def thumbnail_tag(attachment)
Chris@1494 164 link_to image_tag(thumbnail_path(attachment)),
Chris@1494 165 named_attachment_path(attachment, attachment.filename),
Chris@1494 166 :title => attachment.filename
Chris@1494 167 end
Chris@1494 168
Chris@1494 169 def toggle_link(name, id, options={})
Chris@1494 170 onclick = "$('##{id}').toggle(); "
Chris@1494 171 onclick << (options[:focus] ? "$('##{options[:focus]}').focus(); " : "this.blur(); ")
Chris@1494 172 onclick << "return false;"
Chris@1494 173 link_to(name, "#", :onclick => onclick)
Chris@1494 174 end
Chris@1494 175
Chris@1494 176 def image_to_function(name, function, html_options = {})
Chris@1494 177 html_options.symbolize_keys!
Chris@1494 178 tag(:input, html_options.merge({
Chris@1494 179 :type => "image", :src => image_path(name),
Chris@1494 180 :onclick => (html_options[:onclick] ? "#{html_options[:onclick]}; " : "") + "#{function};"
Chris@1494 181 }))
Chris@1494 182 end
Chris@1494 183
Chris@1494 184 def format_activity_title(text)
Chris@1494 185 h(truncate_single_line(text, :length => 100))
Chris@1494 186 end
Chris@1494 187
Chris@1494 188 def format_activity_day(date)
Chris@1494 189 date == User.current.today ? l(:label_today).titleize : format_date(date)
Chris@1494 190 end
Chris@1494 191
Chris@1494 192 def format_activity_description(text)
Chris@1494 193 h(truncate(text.to_s, :length => 120).gsub(%r{[\r\n]*<(pre|code)>.*$}m, '...')
Chris@1494 194 ).gsub(/[\r\n]+/, "<br />").html_safe
Chris@1494 195 end
Chris@1494 196
Chris@1494 197 def format_version_name(version)
Chris@1494 198 if version.project == @project
Chris@1494 199 h(version)
Chris@1494 200 else
Chris@1494 201 h("#{version.project} - #{version}")
Chris@1494 202 end
Chris@1494 203 end
Chris@1494 204
Chris@1494 205 def due_date_distance_in_words(date)
Chris@1494 206 if date
Chris@1494 207 l((date < Date.today ? :label_roadmap_overdue : :label_roadmap_due_in), distance_of_date_in_words(Date.today, date))
Chris@1494 208 end
Chris@1494 209 end
Chris@1494 210
Chris@1494 211 # Renders a tree of projects as a nested set of unordered lists
Chris@1494 212 # The given collection may be a subset of the whole project tree
Chris@1494 213 # (eg. some intermediate nodes are private and can not be seen)
Chris@1494 214 def render_project_nested_lists(projects)
Chris@1494 215 s = ''
Chris@1494 216 if projects.any?
Chris@1494 217 ancestors = []
Chris@1494 218 original_project = @project
Chris@1494 219 projects.sort_by(&:lft).each do |project|
Chris@1494 220 # set the project environment to please macros.
Chris@1494 221 @project = project
Chris@1494 222 if (ancestors.empty? || project.is_descendant_of?(ancestors.last))
Chris@1494 223 s << "<ul class='projects #{ ancestors.empty? ? 'root' : nil}'>\n"
Chris@1494 224 else
Chris@1494 225 ancestors.pop
Chris@1494 226 s << "</li>"
Chris@1494 227 while (ancestors.any? && !project.is_descendant_of?(ancestors.last))
Chris@1494 228 ancestors.pop
Chris@1494 229 s << "</ul></li>\n"
Chris@1494 230 end
Chris@1494 231 end
Chris@1494 232 classes = (ancestors.empty? ? 'root' : 'child')
Chris@1494 233 s << "<li class='#{classes}'><div class='#{classes}'>"
Chris@1494 234 s << h(block_given? ? yield(project) : project.name)
Chris@1494 235 s << "</div>\n"
Chris@1494 236 ancestors << project
Chris@1494 237 end
Chris@1494 238 s << ("</li></ul>\n" * ancestors.size)
Chris@1494 239 @project = original_project
Chris@1494 240 end
Chris@1494 241 s.html_safe
Chris@1494 242 end
Chris@1494 243
Chris@1494 244 def render_page_hierarchy(pages, node=nil, options={})
Chris@1494 245 content = ''
Chris@1494 246 if pages[node]
Chris@1494 247 content << "<ul class=\"pages-hierarchy\">\n"
Chris@1494 248 pages[node].each do |page|
Chris@1494 249 content << "<li>"
Chris@1494 250 content << link_to(h(page.pretty_title), {:controller => 'wiki', :action => 'show', :project_id => page.project, :id => page.title, :version => nil},
Chris@1494 251 :title => (options[:timestamp] && page.updated_on ? l(:label_updated_time, distance_of_time_in_words(Time.now, page.updated_on)) : nil))
Chris@1494 252 content << "\n" + render_page_hierarchy(pages, page.id, options) if pages[page.id]
Chris@1494 253 content << "</li>\n"
Chris@1494 254 end
Chris@1494 255 content << "</ul>\n"
Chris@1494 256 end
Chris@1494 257 content.html_safe
Chris@1494 258 end
Chris@1494 259
Chris@1494 260 # Renders flash messages
Chris@1494 261 def render_flash_messages
Chris@1494 262 s = ''
Chris@1494 263 flash.each do |k,v|
Chris@1494 264 s << content_tag('div', v.html_safe, :class => "flash #{k}", :id => "flash_#{k}")
Chris@1494 265 end
Chris@1494 266 s.html_safe
Chris@1494 267 end
Chris@1494 268
Chris@1494 269 # Renders tabs and their content
Chris@1494 270 def render_tabs(tabs)
Chris@1494 271 if tabs.any?
Chris@1494 272 render :partial => 'common/tabs', :locals => {:tabs => tabs}
Chris@1494 273 else
Chris@1494 274 content_tag 'p', l(:label_no_data), :class => "nodata"
Chris@1494 275 end
Chris@1494 276 end
Chris@1494 277
Chris@1494 278 # Renders the project quick-jump box
Chris@1494 279 def render_project_jump_box
Chris@1494 280 return unless User.current.logged?
Chris@1494 281 projects = User.current.memberships.collect(&:project).compact.select(&:active?).uniq
Chris@1494 282 if projects.any?
Chris@1494 283 options =
Chris@1494 284 ("<option value=''>#{ l(:label_jump_to_a_project) }</option>" +
Chris@1494 285 '<option value="" disabled="disabled">---</option>').html_safe
Chris@1494 286
Chris@1494 287 options << project_tree_options_for_select(projects, :selected => @project) do |p|
Chris@1494 288 { :value => project_path(:id => p, :jump => current_menu_item) }
Chris@1494 289 end
Chris@1494 290
Chris@1494 291 select_tag('project_quick_jump_box', options, :onchange => 'if (this.value != \'\') { window.location = this.value; }')
Chris@1494 292 end
Chris@1494 293 end
Chris@1494 294
Chris@1494 295 def project_tree_options_for_select(projects, options = {})
Chris@1494 296 s = ''
Chris@1494 297 project_tree(projects) do |project, level|
Chris@1494 298 name_prefix = (level > 0 ? '&nbsp;' * 2 * level + '&#187; ' : '').html_safe
Chris@1494 299 tag_options = {:value => project.id}
Chris@1494 300 if project == options[:selected] || (options[:selected].respond_to?(:include?) && options[:selected].include?(project))
Chris@1494 301 tag_options[:selected] = 'selected'
Chris@1494 302 else
Chris@1494 303 tag_options[:selected] = nil
Chris@1494 304 end
Chris@1494 305 tag_options.merge!(yield(project)) if block_given?
Chris@1494 306 s << content_tag('option', name_prefix + h(project), tag_options)
Chris@1494 307 end
Chris@1494 308 s.html_safe
Chris@1494 309 end
Chris@1494 310
Chris@1494 311 # Yields the given block for each project with its level in the tree
Chris@1494 312 #
Chris@1494 313 # Wrapper for Project#project_tree
Chris@1494 314 def project_tree(projects, &block)
Chris@1494 315 Project.project_tree(projects, &block)
Chris@1494 316 end
Chris@1494 317
Chris@1494 318 def principals_check_box_tags(name, principals)
Chris@1494 319 s = ''
Chris@1494 320 principals.each do |principal|
Chris@1494 321 s << "<label>#{ check_box_tag name, principal.id, false, :id => nil } #{h principal}</label>\n"
Chris@1494 322 end
Chris@1494 323 s.html_safe
Chris@1494 324 end
Chris@1494 325
Chris@1494 326 # Returns a string for users/groups option tags
Chris@1494 327 def principals_options_for_select(collection, selected=nil)
Chris@1494 328 s = ''
Chris@1494 329 if collection.include?(User.current)
Chris@1494 330 s << content_tag('option', "<< #{l(:label_me)} >>", :value => User.current.id)
Chris@1494 331 end
Chris@1494 332 groups = ''
Chris@1494 333 collection.sort.each do |element|
Chris@1494 334 selected_attribute = ' selected="selected"' if option_value_selected?(element, selected) || element.id.to_s == selected
Chris@1494 335 (element.is_a?(Group) ? groups : s) << %(<option value="#{element.id}"#{selected_attribute}>#{h element.name}</option>)
Chris@1494 336 end
Chris@1494 337 unless groups.empty?
Chris@1494 338 s << %(<optgroup label="#{h(l(:label_group_plural))}">#{groups}</optgroup>)
Chris@1494 339 end
Chris@1494 340 s.html_safe
Chris@1494 341 end
Chris@1494 342
Chris@1494 343 # Options for the new membership projects combo-box
Chris@1494 344 def options_for_membership_project_select(principal, projects)
Chris@1494 345 options = content_tag('option', "--- #{l(:actionview_instancetag_blank_option)} ---")
Chris@1494 346 options << project_tree_options_for_select(projects) do |p|
Chris@1494 347 {:disabled => principal.projects.to_a.include?(p)}
Chris@1494 348 end
Chris@1494 349 options
Chris@1494 350 end
Chris@1494 351
Chris@1494 352 def option_tag(name, text, value, selected=nil, options={})
Chris@1494 353 content_tag 'option', value, options.merge(:value => value, :selected => (value == selected))
Chris@1494 354 end
Chris@1494 355
Chris@1494 356 # Truncates and returns the string as a single line
Chris@1494 357 def truncate_single_line(string, *args)
Chris@1494 358 truncate(string.to_s, *args).gsub(%r{[\r\n]+}m, ' ')
Chris@1494 359 end
Chris@1494 360
Chris@1494 361 # Truncates at line break after 250 characters or options[:length]
Chris@1494 362 def truncate_lines(string, options={})
Chris@1494 363 length = options[:length] || 250
Chris@1494 364 if string.to_s =~ /\A(.{#{length}}.*?)$/m
Chris@1494 365 "#{$1}..."
Chris@1494 366 else
Chris@1494 367 string
Chris@1494 368 end
Chris@1494 369 end
Chris@1494 370
Chris@1494 371 def anchor(text)
Chris@1494 372 text.to_s.gsub(' ', '_')
Chris@1494 373 end
Chris@1494 374
Chris@1494 375 def html_hours(text)
Chris@1494 376 text.gsub(%r{(\d+)\.(\d+)}, '<span class="hours hours-int">\1</span><span class="hours hours-dec">.\2</span>').html_safe
Chris@1494 377 end
Chris@1494 378
Chris@1494 379 def authoring(created, author, options={})
Chris@1494 380 l(options[:label] || :label_added_time_by, :author => link_to_user(author), :age => time_tag(created)).html_safe
Chris@1494 381 end
Chris@1494 382
Chris@1494 383 def time_tag(time)
Chris@1494 384 text = distance_of_time_in_words(Time.now, time)
Chris@1494 385 if @project
Chris@1494 386 link_to(text, {:controller => 'activities', :action => 'index', :id => @project, :from => User.current.time_to_date(time)}, :title => format_time(time))
Chris@1494 387 else
Chris@1494 388 content_tag('abbr', text, :title => format_time(time))
Chris@1494 389 end
Chris@1494 390 end
Chris@1494 391
Chris@1494 392 def syntax_highlight_lines(name, content)
Chris@1494 393 lines = []
Chris@1494 394 syntax_highlight(name, content).each_line { |line| lines << line }
Chris@1494 395 lines
Chris@1494 396 end
Chris@1494 397
Chris@1494 398 def syntax_highlight(name, content)
Chris@1494 399 Redmine::SyntaxHighlighting.highlight_by_filename(content, name)
Chris@1494 400 end
Chris@1494 401
Chris@1494 402 def to_path_param(path)
Chris@1494 403 str = path.to_s.split(%r{[/\\]}).select{|p| !p.blank?}.join("/")
Chris@1494 404 str.blank? ? nil : str
Chris@1494 405 end
Chris@1494 406
Chris@1494 407 def reorder_links(name, url, method = :post)
Chris@1494 408 link_to(image_tag('2uparrow.png', :alt => l(:label_sort_highest)),
Chris@1494 409 url.merge({"#{name}[move_to]" => 'highest'}),
Chris@1494 410 :method => method, :title => l(:label_sort_highest)) +
Chris@1494 411 link_to(image_tag('1uparrow.png', :alt => l(:label_sort_higher)),
Chris@1494 412 url.merge({"#{name}[move_to]" => 'higher'}),
Chris@1494 413 :method => method, :title => l(:label_sort_higher)) +
Chris@1494 414 link_to(image_tag('1downarrow.png', :alt => l(:label_sort_lower)),
Chris@1494 415 url.merge({"#{name}[move_to]" => 'lower'}),
Chris@1494 416 :method => method, :title => l(:label_sort_lower)) +
Chris@1494 417 link_to(image_tag('2downarrow.png', :alt => l(:label_sort_lowest)),
Chris@1494 418 url.merge({"#{name}[move_to]" => 'lowest'}),
Chris@1494 419 :method => method, :title => l(:label_sort_lowest))
Chris@1494 420 end
Chris@1494 421
Chris@1494 422 def breadcrumb(*args)
Chris@1494 423 elements = args.flatten
Chris@1494 424 elements.any? ? content_tag('p', (args.join(" \xc2\xbb ") + " \xc2\xbb ").html_safe, :class => 'breadcrumb') : nil
Chris@1494 425 end
Chris@1494 426
Chris@1494 427 def other_formats_links(&block)
Chris@1494 428 concat('<p class="other-formats">'.html_safe + l(:label_export_to))
Chris@1494 429 yield Redmine::Views::OtherFormatsBuilder.new(self)
Chris@1494 430 concat('</p>'.html_safe)
Chris@1494 431 end
Chris@1494 432
Chris@1494 433 def page_header_title
Chris@1494 434 if @project.nil? || @project.new_record?
Chris@1494 435 h(Setting.app_title)
Chris@1494 436 else
Chris@1494 437 b = []
Chris@1494 438 ancestors = (@project.root? ? [] : @project.ancestors.visible.all)
Chris@1494 439 if ancestors.any?
Chris@1494 440 root = ancestors.shift
Chris@1494 441 b << link_to_project(root, {:jump => current_menu_item}, :class => 'root')
Chris@1494 442 if ancestors.size > 2
Chris@1494 443 b << "\xe2\x80\xa6"
Chris@1494 444 ancestors = ancestors[-2, 2]
Chris@1494 445 end
Chris@1494 446 b += ancestors.collect {|p| link_to_project(p, {:jump => current_menu_item}, :class => 'ancestor') }
Chris@1494 447 end
Chris@1494 448 b << h(@project)
Chris@1494 449 b.join(" \xc2\xbb ").html_safe
Chris@1494 450 end
Chris@1494 451 end
Chris@1494 452
Chris@1494 453 # Returns a h2 tag and sets the html title with the given arguments
Chris@1494 454 def title(*args)
Chris@1494 455 strings = args.map do |arg|
Chris@1494 456 if arg.is_a?(Array) && arg.size >= 2
Chris@1494 457 link_to(*arg)
Chris@1494 458 else
Chris@1494 459 h(arg.to_s)
Chris@1494 460 end
Chris@1494 461 end
Chris@1494 462 html_title args.reverse.map {|s| (s.is_a?(Array) ? s.first : s).to_s}
Chris@1494 463 content_tag('h2', strings.join(' &#187; ').html_safe)
Chris@1494 464 end
Chris@1494 465
Chris@1494 466 # Sets the html title
Chris@1494 467 # Returns the html title when called without arguments
Chris@1494 468 # Current project name and app_title and automatically appended
Chris@1494 469 # Exemples:
Chris@1494 470 # html_title 'Foo', 'Bar'
Chris@1494 471 # html_title # => 'Foo - Bar - My Project - Redmine'
Chris@1494 472 def html_title(*args)
Chris@1494 473 if args.empty?
Chris@1494 474 title = @html_title || []
Chris@1494 475 title << @project.name if @project
Chris@1494 476 title << Setting.app_title unless Setting.app_title == title.last
Chris@1494 477 title.reject(&:blank?).join(' - ')
Chris@1494 478 else
Chris@1494 479 @html_title ||= []
Chris@1494 480 @html_title += args
Chris@1494 481 end
Chris@1494 482 end
Chris@1494 483
Chris@1494 484 # Returns the theme, controller name, and action as css classes for the
Chris@1494 485 # HTML body.
Chris@1494 486 def body_css_classes
Chris@1494 487 css = []
Chris@1494 488 if theme = Redmine::Themes.theme(Setting.ui_theme)
Chris@1494 489 css << 'theme-' + theme.name
Chris@1494 490 end
Chris@1494 491
Chris@1494 492 css << 'project-' + @project.identifier if @project && @project.identifier.present?
Chris@1494 493 css << 'controller-' + controller_name
Chris@1494 494 css << 'action-' + action_name
Chris@1494 495 css.join(' ')
Chris@1494 496 end
Chris@1494 497
Chris@1494 498 def accesskey(s)
Chris@1494 499 @used_accesskeys ||= []
Chris@1494 500 key = Redmine::AccessKeys.key_for(s)
Chris@1494 501 return nil if @used_accesskeys.include?(key)
Chris@1494 502 @used_accesskeys << key
Chris@1494 503 key
Chris@1494 504 end
Chris@1494 505
Chris@1494 506 # Formats text according to system settings.
Chris@1494 507 # 2 ways to call this method:
Chris@1494 508 # * with a String: textilizable(text, options)
Chris@1494 509 # * with an object and one of its attribute: textilizable(issue, :description, options)
Chris@1494 510 def textilizable(*args)
Chris@1494 511 options = args.last.is_a?(Hash) ? args.pop : {}
Chris@1494 512 case args.size
Chris@1494 513 when 1
Chris@1494 514 obj = options[:object]
Chris@1494 515 text = args.shift
Chris@1494 516 when 2
Chris@1494 517 obj = args.shift
Chris@1494 518 attr = args.shift
Chris@1494 519 text = obj.send(attr).to_s
Chris@1494 520 else
Chris@1494 521 raise ArgumentError, 'invalid arguments to textilizable'
Chris@1494 522 end
Chris@1494 523 return '' if text.blank?
Chris@1494 524 project = options[:project] || @project || (obj && obj.respond_to?(:project) ? obj.project : nil)
Chris@1494 525 only_path = options.delete(:only_path) == false ? false : true
Chris@1494 526
Chris@1494 527 text = text.dup
Chris@1494 528 macros = catch_macros(text)
Chris@1494 529 text = Redmine::WikiFormatting.to_html(Setting.text_formatting, text, :object => obj, :attribute => attr)
Chris@1494 530
Chris@1494 531 @parsed_headings = []
Chris@1494 532 @heading_anchors = {}
Chris@1494 533 @current_section = 0 if options[:edit_section_links]
Chris@1494 534
Chris@1494 535 parse_sections(text, project, obj, attr, only_path, options)
Chris@1494 536 text = parse_non_pre_blocks(text, obj, macros) do |text|
Chris@1494 537 [:parse_inline_attachments, :parse_wiki_links, :parse_redmine_links].each do |method_name|
Chris@1494 538 send method_name, text, project, obj, attr, only_path, options
Chris@1494 539 end
Chris@1494 540 end
Chris@1494 541 parse_headings(text, project, obj, attr, only_path, options)
Chris@1494 542
Chris@1494 543 if @parsed_headings.any?
Chris@1494 544 replace_toc(text, @parsed_headings)
Chris@1494 545 end
Chris@1494 546
Chris@1494 547 text.html_safe
Chris@1494 548 end
Chris@1494 549
Chris@1494 550 def parse_non_pre_blocks(text, obj, macros)
Chris@1494 551 s = StringScanner.new(text)
Chris@1494 552 tags = []
Chris@1494 553 parsed = ''
Chris@1494 554 while !s.eos?
Chris@1494 555 s.scan(/(.*?)(<(\/)?(pre|code)(.*?)>|\z)/im)
Chris@1494 556 text, full_tag, closing, tag = s[1], s[2], s[3], s[4]
Chris@1494 557 if tags.empty?
Chris@1494 558 yield text
Chris@1494 559 inject_macros(text, obj, macros) if macros.any?
Chris@1494 560 else
Chris@1494 561 inject_macros(text, obj, macros, false) if macros.any?
Chris@1494 562 end
Chris@1494 563 parsed << text
Chris@1494 564 if tag
Chris@1494 565 if closing
Chris@1494 566 if tags.last == tag.downcase
Chris@1494 567 tags.pop
Chris@1494 568 end
Chris@1494 569 else
Chris@1494 570 tags << tag.downcase
Chris@1494 571 end
Chris@1494 572 parsed << full_tag
Chris@1494 573 end
Chris@1494 574 end
Chris@1494 575 # Close any non closing tags
Chris@1494 576 while tag = tags.pop
Chris@1494 577 parsed << "</#{tag}>"
Chris@1494 578 end
Chris@1494 579 parsed
Chris@1494 580 end
Chris@1494 581
Chris@1494 582 def parse_inline_attachments(text, project, obj, attr, only_path, options)
Chris@1494 583 # when using an image link, try to use an attachment, if possible
Chris@1494 584 attachments = options[:attachments] || []
Chris@1494 585 attachments += obj.attachments if obj.respond_to?(:attachments)
Chris@1494 586 if attachments.present?
Chris@1494 587 text.gsub!(/src="([^\/"]+\.(bmp|gif|jpg|jpe|jpeg|png))"(\s+alt="([^"]*)")?/i) do |m|
Chris@1494 588 filename, ext, alt, alttext = $1.downcase, $2, $3, $4
Chris@1494 589 # search for the picture in attachments
Chris@1494 590 if found = Attachment.latest_attach(attachments, filename)
Chris@1494 591 image_url = download_named_attachment_path(found, found.filename, :only_path => only_path)
Chris@1494 592 desc = found.description.to_s.gsub('"', '')
Chris@1494 593 if !desc.blank? && alttext.blank?
Chris@1494 594 alt = " title=\"#{desc}\" alt=\"#{desc}\""
Chris@1494 595 end
Chris@1494 596 "src=\"#{image_url}\"#{alt}"
Chris@1494 597 else
Chris@1494 598 m
Chris@1494 599 end
Chris@1494 600 end
Chris@1494 601 end
Chris@1494 602 end
Chris@1494 603
Chris@1494 604 # Wiki links
Chris@1494 605 #
Chris@1494 606 # Examples:
Chris@1494 607 # [[mypage]]
Chris@1494 608 # [[mypage|mytext]]
Chris@1494 609 # wiki links can refer other project wikis, using project name or identifier:
Chris@1494 610 # [[project:]] -> wiki starting page
Chris@1494 611 # [[project:|mytext]]
Chris@1494 612 # [[project:mypage]]
Chris@1494 613 # [[project:mypage|mytext]]
Chris@1494 614 def parse_wiki_links(text, project, obj, attr, only_path, options)
Chris@1494 615 text.gsub!(/(!)?(\[\[([^\]\n\|]+)(\|([^\]\n\|]+))?\]\])/) do |m|
Chris@1494 616 link_project = project
Chris@1494 617 esc, all, page, title = $1, $2, $3, $5
Chris@1494 618 if esc.nil?
Chris@1494 619 if page =~ /^([^\:]+)\:(.*)$/
Chris@1494 620 identifier, page = $1, $2
Chris@1494 621 link_project = Project.find_by_identifier(identifier) || Project.find_by_name(identifier)
Chris@1494 622 title ||= identifier if page.blank?
Chris@1494 623 end
Chris@1494 624
Chris@1494 625 if link_project && link_project.wiki
Chris@1494 626 # extract anchor
Chris@1494 627 anchor = nil
Chris@1494 628 if page =~ /^(.+?)\#(.+)$/
Chris@1494 629 page, anchor = $1, $2
Chris@1494 630 end
Chris@1494 631 anchor = sanitize_anchor_name(anchor) if anchor.present?
Chris@1494 632 # check if page exists
Chris@1494 633 wiki_page = link_project.wiki.find_page(page)
Chris@1494 634 url = if anchor.present? && wiki_page.present? && (obj.is_a?(WikiContent) || obj.is_a?(WikiContent::Version)) && obj.page == wiki_page
Chris@1494 635 "##{anchor}"
Chris@1494 636 else
Chris@1494 637 case options[:wiki_links]
Chris@1494 638 when :local; "#{page.present? ? Wiki.titleize(page) : ''}.html" + (anchor.present? ? "##{anchor}" : '')
Chris@1494 639 when :anchor; "##{page.present? ? Wiki.titleize(page) : title}" + (anchor.present? ? "_#{anchor}" : '') # used for single-file wiki export
Chris@1494 640 else
Chris@1494 641 wiki_page_id = page.present? ? Wiki.titleize(page) : nil
Chris@1494 642 parent = wiki_page.nil? && obj.is_a?(WikiContent) && obj.page && project == link_project ? obj.page.title : nil
Chris@1494 643 url_for(:only_path => only_path, :controller => 'wiki', :action => 'show', :project_id => link_project,
Chris@1494 644 :id => wiki_page_id, :version => nil, :anchor => anchor, :parent => parent)
Chris@1494 645 end
Chris@1494 646 end
Chris@1494 647 link_to(title.present? ? title.html_safe : h(page), url, :class => ('wiki-page' + (wiki_page ? '' : ' new')))
Chris@1494 648 else
Chris@1494 649 # project or wiki doesn't exist
Chris@1494 650 all
Chris@1494 651 end
Chris@1494 652 else
Chris@1494 653 all
Chris@1494 654 end
Chris@1494 655 end
Chris@1494 656 end
Chris@1494 657
Chris@1494 658 # Redmine links
Chris@1494 659 #
Chris@1494 660 # Examples:
Chris@1494 661 # Issues:
Chris@1494 662 # #52 -> Link to issue #52
Chris@1494 663 # Changesets:
Chris@1494 664 # r52 -> Link to revision 52
Chris@1494 665 # commit:a85130f -> Link to scmid starting with a85130f
Chris@1494 666 # Documents:
Chris@1494 667 # document#17 -> Link to document with id 17
Chris@1494 668 # document:Greetings -> Link to the document with title "Greetings"
Chris@1494 669 # document:"Some document" -> Link to the document with title "Some document"
Chris@1494 670 # Versions:
Chris@1494 671 # version#3 -> Link to version with id 3
Chris@1494 672 # version:1.0.0 -> Link to version named "1.0.0"
Chris@1494 673 # version:"1.0 beta 2" -> Link to version named "1.0 beta 2"
Chris@1494 674 # Attachments:
Chris@1494 675 # attachment:file.zip -> Link to the attachment of the current object named file.zip
Chris@1494 676 # Source files:
Chris@1494 677 # source:some/file -> Link to the file located at /some/file in the project's repository
Chris@1494 678 # source:some/file@52 -> Link to the file's revision 52
Chris@1494 679 # source:some/file#L120 -> Link to line 120 of the file
Chris@1494 680 # source:some/file@52#L120 -> Link to line 120 of the file's revision 52
Chris@1494 681 # export:some/file -> Force the download of the file
Chris@1494 682 # Forum messages:
Chris@1494 683 # message#1218 -> Link to message with id 1218
Chris@1494 684 # Projects:
Chris@1494 685 # project:someproject -> Link to project named "someproject"
Chris@1494 686 # project#3 -> Link to project with id 3
Chris@1494 687 #
Chris@1494 688 # Links can refer other objects from other projects, using project identifier:
Chris@1494 689 # identifier:r52
Chris@1494 690 # identifier:document:"Some document"
Chris@1494 691 # identifier:version:1.0.0
Chris@1494 692 # identifier:source:some/file
Chris@1494 693 def parse_redmine_links(text, default_project, obj, attr, only_path, options)
Chris@1494 694 text.gsub!(%r{([\s\(,\-\[\>]|^)(!)?(([a-z0-9\-_]+):)?(attachment|document|version|forum|news|message|project|commit|source|export)?(((#)|((([a-z0-9\-_]+)\|)?(r)))((\d+)((#note)?-(\d+))?)|(:)([^"\s<>][^\s<>]*?|"[^"]+?"))(?=(?=[[:punct:]][^A-Za-z0-9_/])|,|\s|\]|<|$)}) do |m|
Chris@1494 695 leading, esc, project_prefix, project_identifier, prefix, repo_prefix, repo_identifier, sep, identifier, comment_suffix, comment_id = $1, $2, $3, $4, $5, $10, $11, $8 || $12 || $18, $14 || $19, $15, $17
Chris@1494 696 link = nil
Chris@1494 697 project = default_project
Chris@1494 698 if project_identifier
Chris@1494 699 project = Project.visible.find_by_identifier(project_identifier)
Chris@1494 700 end
Chris@1494 701 if esc.nil?
Chris@1494 702 if prefix.nil? && sep == 'r'
Chris@1494 703 if project
Chris@1494 704 repository = nil
Chris@1494 705 if repo_identifier
Chris@1494 706 repository = project.repositories.detect {|repo| repo.identifier == repo_identifier}
Chris@1494 707 else
Chris@1494 708 repository = project.repository
Chris@1494 709 end
Chris@1494 710 # project.changesets.visible raises an SQL error because of a double join on repositories
Chris@1494 711 if repository && (changeset = Changeset.visible.find_by_repository_id_and_revision(repository.id, identifier))
Chris@1494 712 link = link_to(h("#{project_prefix}#{repo_prefix}r#{identifier}"), {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :repository_id => repository.identifier_param, :rev => changeset.revision},
Chris@1494 713 :class => 'changeset',
Chris@1494 714 :title => truncate_single_line(changeset.comments, :length => 100))
Chris@1494 715 end
Chris@1494 716 end
Chris@1494 717 elsif sep == '#'
Chris@1494 718 oid = identifier.to_i
Chris@1494 719 case prefix
Chris@1494 720 when nil
Chris@1494 721 if oid.to_s == identifier && issue = Issue.visible.find_by_id(oid, :include => :status)
Chris@1494 722 anchor = comment_id ? "note-#{comment_id}" : nil
Chris@1494 723 link = link_to(h("##{oid}#{comment_suffix}"), {:only_path => only_path, :controller => 'issues', :action => 'show', :id => oid, :anchor => anchor},
Chris@1494 724 :class => issue.css_classes,
Chris@1494 725 :title => "#{truncate(issue.subject, :length => 100)} (#{issue.status.name})")
Chris@1494 726 end
Chris@1494 727 when 'document'
Chris@1494 728 if document = Document.visible.find_by_id(oid)
Chris@1494 729 link = link_to h(document.title), {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document},
Chris@1494 730 :class => 'document'
Chris@1494 731 end
Chris@1494 732 when 'version'
Chris@1494 733 if version = Version.visible.find_by_id(oid)
Chris@1494 734 link = link_to h(version.name), {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version},
Chris@1494 735 :class => 'version'
Chris@1494 736 end
Chris@1494 737 when 'message'
Chris@1494 738 if message = Message.visible.find_by_id(oid, :include => :parent)
Chris@1494 739 link = link_to_message(message, {:only_path => only_path}, :class => 'message')
Chris@1494 740 end
Chris@1494 741 when 'forum'
Chris@1494 742 if board = Board.visible.find_by_id(oid)
Chris@1494 743 link = link_to h(board.name), {:only_path => only_path, :controller => 'boards', :action => 'show', :id => board, :project_id => board.project},
Chris@1494 744 :class => 'board'
Chris@1494 745 end
Chris@1494 746 when 'news'
Chris@1494 747 if news = News.visible.find_by_id(oid)
Chris@1494 748 link = link_to h(news.title), {:only_path => only_path, :controller => 'news', :action => 'show', :id => news},
Chris@1494 749 :class => 'news'
Chris@1494 750 end
Chris@1494 751 when 'project'
Chris@1494 752 if p = Project.visible.find_by_id(oid)
Chris@1494 753 link = link_to_project(p, {:only_path => only_path}, :class => 'project')
Chris@1494 754 end
Chris@1494 755 end
Chris@1494 756 elsif sep == ':'
Chris@1494 757 # removes the double quotes if any
Chris@1494 758 name = identifier.gsub(%r{^"(.*)"$}, "\\1")
Chris@1494 759 case prefix
Chris@1494 760 when 'document'
Chris@1494 761 if project && document = project.documents.visible.find_by_title(name)
Chris@1494 762 link = link_to h(document.title), {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document},
Chris@1494 763 :class => 'document'
Chris@1494 764 end
Chris@1494 765 when 'version'
Chris@1494 766 if project && version = project.versions.visible.find_by_name(name)
Chris@1494 767 link = link_to h(version.name), {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version},
Chris@1494 768 :class => 'version'
Chris@1494 769 end
Chris@1494 770 when 'forum'
Chris@1494 771 if project && board = project.boards.visible.find_by_name(name)
Chris@1494 772 link = link_to h(board.name), {:only_path => only_path, :controller => 'boards', :action => 'show', :id => board, :project_id => board.project},
Chris@1494 773 :class => 'board'
Chris@1494 774 end
Chris@1494 775 when 'news'
Chris@1494 776 if project && news = project.news.visible.find_by_title(name)
Chris@1494 777 link = link_to h(news.title), {:only_path => only_path, :controller => 'news', :action => 'show', :id => news},
Chris@1494 778 :class => 'news'
Chris@1494 779 end
Chris@1494 780 when 'commit', 'source', 'export'
Chris@1494 781 if project
Chris@1494 782 repository = nil
Chris@1494 783 if name =~ %r{^(([a-z0-9\-_]+)\|)(.+)$}
Chris@1494 784 repo_prefix, repo_identifier, name = $1, $2, $3
Chris@1494 785 repository = project.repositories.detect {|repo| repo.identifier == repo_identifier}
Chris@1494 786 else
Chris@1494 787 repository = project.repository
Chris@1494 788 end
Chris@1494 789 if prefix == 'commit'
Chris@1494 790 if repository && (changeset = Changeset.visible.where("repository_id = ? AND scmid LIKE ?", repository.id, "#{name}%").first)
Chris@1494 791 link = link_to h("#{project_prefix}#{repo_prefix}#{name}"), {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :repository_id => repository.identifier_param, :rev => changeset.identifier},
Chris@1494 792 :class => 'changeset',
Chris@1494 793 :title => truncate_single_line(changeset.comments, :length => 100)
Chris@1494 794 end
Chris@1494 795 else
Chris@1494 796 if repository && User.current.allowed_to?(:browse_repository, project)
Chris@1494 797 name =~ %r{^[/\\]*(.*?)(@([^/\\@]+?))?(#(L\d+))?$}
Chris@1494 798 path, rev, anchor = $1, $3, $5
Chris@1494 799 link = link_to h("#{project_prefix}#{prefix}:#{repo_prefix}#{name}"), {:controller => 'repositories', :action => (prefix == 'export' ? 'raw' : 'entry'), :id => project, :repository_id => repository.identifier_param,
Chris@1494 800 :path => to_path_param(path),
Chris@1494 801 :rev => rev,
Chris@1494 802 :anchor => anchor},
Chris@1494 803 :class => (prefix == 'export' ? 'source download' : 'source')
Chris@1494 804 end
Chris@1494 805 end
Chris@1494 806 repo_prefix = nil
Chris@1494 807 end
Chris@1494 808 when 'attachment'
Chris@1494 809 attachments = options[:attachments] || (obj && obj.respond_to?(:attachments) ? obj.attachments : nil)
Chris@1494 810 if attachments && attachment = Attachment.latest_attach(attachments, name)
Chris@1494 811 link = link_to_attachment(attachment, :only_path => only_path, :download => true, :class => 'attachment')
Chris@1494 812 end
Chris@1494 813 when 'project'
Chris@1494 814 if p = Project.visible.where("identifier = :s OR LOWER(name) = :s", :s => name.downcase).first
Chris@1494 815 link = link_to_project(p, {:only_path => only_path}, :class => 'project')
Chris@1494 816 end
Chris@1494 817 end
Chris@1494 818 end
Chris@1494 819 end
Chris@1494 820 (leading + (link || "#{project_prefix}#{prefix}#{repo_prefix}#{sep}#{identifier}#{comment_suffix}"))
Chris@1494 821 end
Chris@1494 822 end
Chris@1494 823
Chris@1494 824 HEADING_RE = /(<h(\d)( [^>]+)?>(.+?)<\/h(\d)>)/i unless const_defined?(:HEADING_RE)
Chris@1494 825
Chris@1494 826 def parse_sections(text, project, obj, attr, only_path, options)
Chris@1494 827 return unless options[:edit_section_links]
Chris@1494 828 text.gsub!(HEADING_RE) do
Chris@1494 829 heading = $1
Chris@1494 830 @current_section += 1
Chris@1494 831 if @current_section > 1
Chris@1494 832 content_tag('div',
Chris@1494 833 link_to(image_tag('edit.png'), options[:edit_section_links].merge(:section => @current_section)),
Chris@1494 834 :class => 'contextual',
Chris@1494 835 :title => l(:button_edit_section),
Chris@1494 836 :id => "section-#{@current_section}") + heading.html_safe
Chris@1494 837 else
Chris@1494 838 heading
Chris@1494 839 end
Chris@1494 840 end
Chris@1494 841 end
Chris@1494 842
Chris@1494 843 # Headings and TOC
Chris@1494 844 # Adds ids and links to headings unless options[:headings] is set to false
Chris@1494 845 def parse_headings(text, project, obj, attr, only_path, options)
Chris@1494 846 return if options[:headings] == false
Chris@1494 847
Chris@1494 848 text.gsub!(HEADING_RE) do
Chris@1494 849 level, attrs, content = $2.to_i, $3, $4
Chris@1494 850 item = strip_tags(content).strip
Chris@1494 851 anchor = sanitize_anchor_name(item)
Chris@1494 852 # used for single-file wiki export
Chris@1494 853 anchor = "#{obj.page.title}_#{anchor}" if options[:wiki_links] == :anchor && (obj.is_a?(WikiContent) || obj.is_a?(WikiContent::Version))
Chris@1494 854 @heading_anchors[anchor] ||= 0
Chris@1494 855 idx = (@heading_anchors[anchor] += 1)
Chris@1494 856 if idx > 1
Chris@1494 857 anchor = "#{anchor}-#{idx}"
Chris@1494 858 end
Chris@1494 859 @parsed_headings << [level, anchor, item]
Chris@1494 860 "<a name=\"#{anchor}\"></a>\n<h#{level} #{attrs}>#{content}<a href=\"##{anchor}\" class=\"wiki-anchor\">&para;</a></h#{level}>"
Chris@1494 861 end
Chris@1494 862 end
Chris@1494 863
Chris@1494 864 MACROS_RE = /(
Chris@1494 865 (!)? # escaping
Chris@1494 866 (
Chris@1494 867 \{\{ # opening tag
Chris@1494 868 ([\w]+) # macro name
Chris@1494 869 (\(([^\n\r]*?)\))? # optional arguments
Chris@1494 870 ([\n\r].*?[\n\r])? # optional block of text
Chris@1494 871 \}\} # closing tag
Chris@1494 872 )
Chris@1494 873 )/mx unless const_defined?(:MACROS_RE)
Chris@1494 874
Chris@1494 875 MACRO_SUB_RE = /(
Chris@1494 876 \{\{
Chris@1494 877 macro\((\d+)\)
Chris@1494 878 \}\}
Chris@1494 879 )/x unless const_defined?(:MACRO_SUB_RE)
Chris@1494 880
Chris@1494 881 # Extracts macros from text
Chris@1494 882 def catch_macros(text)
Chris@1494 883 macros = {}
Chris@1494 884 text.gsub!(MACROS_RE) do
Chris@1494 885 all, macro = $1, $4.downcase
Chris@1494 886 if macro_exists?(macro) || all =~ MACRO_SUB_RE
Chris@1494 887 index = macros.size
Chris@1494 888 macros[index] = all
Chris@1494 889 "{{macro(#{index})}}"
Chris@1494 890 else
Chris@1494 891 all
Chris@1494 892 end
Chris@1494 893 end
Chris@1494 894 macros
Chris@1494 895 end
Chris@1494 896
Chris@1494 897 # Executes and replaces macros in text
Chris@1494 898 def inject_macros(text, obj, macros, execute=true)
Chris@1494 899 text.gsub!(MACRO_SUB_RE) do
Chris@1494 900 all, index = $1, $2.to_i
Chris@1494 901 orig = macros.delete(index)
Chris@1494 902 if execute && orig && orig =~ MACROS_RE
Chris@1494 903 esc, all, macro, args, block = $2, $3, $4.downcase, $6.to_s, $7.try(:strip)
Chris@1494 904 if esc.nil?
Chris@1494 905 h(exec_macro(macro, obj, args, block) || all)
Chris@1494 906 else
Chris@1494 907 h(all)
Chris@1494 908 end
Chris@1494 909 elsif orig
Chris@1494 910 h(orig)
Chris@1494 911 else
Chris@1494 912 h(all)
Chris@1494 913 end
Chris@1494 914 end
Chris@1494 915 end
Chris@1494 916
Chris@1494 917 TOC_RE = /<p>\{\{([<>]?)toc\}\}<\/p>/i unless const_defined?(:TOC_RE)
Chris@1494 918
Chris@1494 919 # Renders the TOC with given headings
Chris@1494 920 def replace_toc(text, headings)
Chris@1494 921 text.gsub!(TOC_RE) do
Chris@1494 922 # Keep only the 4 first levels
Chris@1494 923 headings = headings.select{|level, anchor, item| level <= 4}
Chris@1494 924 if headings.empty?
Chris@1494 925 ''
Chris@1494 926 else
Chris@1494 927 div_class = 'toc'
Chris@1494 928 div_class << ' right' if $1 == '>'
Chris@1494 929 div_class << ' left' if $1 == '<'
Chris@1494 930 out = "<ul class=\"#{div_class}\"><li>"
Chris@1494 931 root = headings.map(&:first).min
Chris@1494 932 current = root
Chris@1494 933 started = false
Chris@1494 934 headings.each do |level, anchor, item|
Chris@1494 935 if level > current
Chris@1494 936 out << '<ul><li>' * (level - current)
Chris@1494 937 elsif level < current
Chris@1494 938 out << "</li></ul>\n" * (current - level) + "</li><li>"
Chris@1494 939 elsif started
Chris@1494 940 out << '</li><li>'
Chris@1494 941 end
Chris@1494 942 out << "<a href=\"##{anchor}\">#{item}</a>"
Chris@1494 943 current = level
Chris@1494 944 started = true
Chris@1494 945 end
Chris@1494 946 out << '</li></ul>' * (current - root)
Chris@1494 947 out << '</li></ul>'
Chris@1494 948 end
Chris@1494 949 end
Chris@1494 950 end
Chris@1494 951
Chris@1494 952 # Same as Rails' simple_format helper without using paragraphs
Chris@1494 953 def simple_format_without_paragraph(text)
Chris@1494 954 text.to_s.
Chris@1494 955 gsub(/\r\n?/, "\n"). # \r\n and \r -> \n
Chris@1494 956 gsub(/\n\n+/, "<br /><br />"). # 2+ newline -> 2 br
Chris@1494 957 gsub(/([^\n]\n)(?=[^\n])/, '\1<br />'). # 1 newline -> br
Chris@1494 958 html_safe
Chris@1494 959 end
Chris@1494 960
Chris@1494 961 def lang_options_for_select(blank=true)
Chris@1494 962 (blank ? [["(auto)", ""]] : []) + languages_options
Chris@1494 963 end
Chris@1494 964
Chris@1494 965 def label_tag_for(name, option_tags = nil, options = {})
Chris@1494 966 label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "")
Chris@1494 967 content_tag("label", label_text)
Chris@1494 968 end
Chris@1494 969
Chris@1494 970 def labelled_form_for(*args, &proc)
Chris@1494 971 args << {} unless args.last.is_a?(Hash)
Chris@1494 972 options = args.last
Chris@1494 973 if args.first.is_a?(Symbol)
Chris@1494 974 options.merge!(:as => args.shift)
Chris@1494 975 end
Chris@1494 976 options.merge!({:builder => Redmine::Views::LabelledFormBuilder})
Chris@1494 977 form_for(*args, &proc)
Chris@1494 978 end
Chris@1494 979
Chris@1494 980 def labelled_fields_for(*args, &proc)
Chris@1494 981 args << {} unless args.last.is_a?(Hash)
Chris@1494 982 options = args.last
Chris@1494 983 options.merge!({:builder => Redmine::Views::LabelledFormBuilder})
Chris@1494 984 fields_for(*args, &proc)
Chris@1494 985 end
Chris@1494 986
Chris@1494 987 def labelled_remote_form_for(*args, &proc)
Chris@1494 988 ActiveSupport::Deprecation.warn "ApplicationHelper#labelled_remote_form_for is deprecated and will be removed in Redmine 2.2."
Chris@1494 989 args << {} unless args.last.is_a?(Hash)
Chris@1494 990 options = args.last
Chris@1494 991 options.merge!({:builder => Redmine::Views::LabelledFormBuilder, :remote => true})
Chris@1494 992 form_for(*args, &proc)
Chris@1494 993 end
Chris@1494 994
Chris@1494 995 def error_messages_for(*objects)
Chris@1494 996 html = ""
Chris@1494 997 objects = objects.map {|o| o.is_a?(String) ? instance_variable_get("@#{o}") : o}.compact
Chris@1494 998 errors = objects.map {|o| o.errors.full_messages}.flatten
Chris@1494 999 if errors.any?
Chris@1494 1000 html << "<div id='errorExplanation'><ul>\n"
Chris@1494 1001 errors.each do |error|
Chris@1494 1002 html << "<li>#{h error}</li>\n"
Chris@1494 1003 end
Chris@1494 1004 html << "</ul></div>\n"
Chris@1494 1005 end
Chris@1494 1006 html.html_safe
Chris@1494 1007 end
Chris@1494 1008
Chris@1494 1009 def delete_link(url, options={})
Chris@1494 1010 options = {
Chris@1494 1011 :method => :delete,
Chris@1494 1012 :data => {:confirm => l(:text_are_you_sure)},
Chris@1494 1013 :class => 'icon icon-del'
Chris@1494 1014 }.merge(options)
Chris@1494 1015
Chris@1494 1016 link_to l(:button_delete), url, options
Chris@1494 1017 end
Chris@1494 1018
Chris@1494 1019 def preview_link(url, form, target='preview', options={})
Chris@1494 1020 content_tag 'a', l(:label_preview), {
Chris@1494 1021 :href => "#",
Chris@1494 1022 :onclick => %|submitPreview("#{escape_javascript url_for(url)}", "#{escape_javascript form}", "#{escape_javascript target}"); return false;|,
Chris@1494 1023 :accesskey => accesskey(:preview)
Chris@1494 1024 }.merge(options)
Chris@1494 1025 end
Chris@1494 1026
Chris@1494 1027 def link_to_function(name, function, html_options={})
Chris@1494 1028 content_tag(:a, name, {:href => '#', :onclick => "#{function}; return false;"}.merge(html_options))
Chris@1494 1029 end
Chris@1494 1030
Chris@1494 1031 # Helper to render JSON in views
Chris@1494 1032 def raw_json(arg)
Chris@1494 1033 arg.to_json.to_s.gsub('/', '\/').html_safe
Chris@1494 1034 end
Chris@1494 1035
Chris@1494 1036 def back_url
Chris@1494 1037 url = params[:back_url]
Chris@1494 1038 if url.nil? && referer = request.env['HTTP_REFERER']
Chris@1494 1039 url = CGI.unescape(referer.to_s)
Chris@1494 1040 end
Chris@1494 1041 url
Chris@1494 1042 end
Chris@1494 1043
Chris@1494 1044 def back_url_hidden_field_tag
Chris@1494 1045 url = back_url
Chris@1494 1046 hidden_field_tag('back_url', url, :id => nil) unless url.blank?
Chris@1494 1047 end
Chris@1494 1048
Chris@1494 1049 def check_all_links(form_name)
Chris@1494 1050 link_to_function(l(:button_check_all), "checkAll('#{form_name}', true)") +
Chris@1494 1051 " | ".html_safe +
Chris@1494 1052 link_to_function(l(:button_uncheck_all), "checkAll('#{form_name}', false)")
Chris@1494 1053 end
Chris@1494 1054
Chris@1494 1055 def progress_bar(pcts, options={})
Chris@1494 1056 pcts = [pcts, pcts] unless pcts.is_a?(Array)
Chris@1494 1057 pcts = pcts.collect(&:round)
Chris@1494 1058 pcts[1] = pcts[1] - pcts[0]
Chris@1494 1059 pcts << (100 - pcts[1] - pcts[0])
Chris@1494 1060 width = options[:width] || '100px;'
Chris@1494 1061 legend = options[:legend] || ''
Chris@1494 1062 content_tag('table',
Chris@1494 1063 content_tag('tr',
Chris@1494 1064 (pcts[0] > 0 ? content_tag('td', '', :style => "width: #{pcts[0]}%;", :class => 'closed') : ''.html_safe) +
Chris@1494 1065 (pcts[1] > 0 ? content_tag('td', '', :style => "width: #{pcts[1]}%;", :class => 'done') : ''.html_safe) +
Chris@1494 1066 (pcts[2] > 0 ? content_tag('td', '', :style => "width: #{pcts[2]}%;", :class => 'todo') : ''.html_safe)
Chris@1494 1067 ), :class => "progress progress-#{pcts[0]}", :style => "width: #{width};").html_safe +
Chris@1494 1068 content_tag('p', legend, :class => 'percent').html_safe
Chris@1494 1069 end
Chris@1494 1070
Chris@1494 1071 def checked_image(checked=true)
Chris@1494 1072 if checked
Chris@1494 1073 image_tag 'toggle_check.png'
Chris@1494 1074 end
Chris@1494 1075 end
Chris@1494 1076
Chris@1494 1077 def context_menu(url)
Chris@1494 1078 unless @context_menu_included
Chris@1494 1079 content_for :header_tags do
Chris@1494 1080 javascript_include_tag('context_menu') +
Chris@1494 1081 stylesheet_link_tag('context_menu')
Chris@1494 1082 end
Chris@1494 1083 if l(:direction) == 'rtl'
Chris@1494 1084 content_for :header_tags do
Chris@1494 1085 stylesheet_link_tag('context_menu_rtl')
Chris@1494 1086 end
Chris@1494 1087 end
Chris@1494 1088 @context_menu_included = true
Chris@1494 1089 end
Chris@1494 1090 javascript_tag "contextMenuInit('#{ url_for(url) }')"
Chris@1494 1091 end
Chris@1494 1092
Chris@1494 1093 def calendar_for(field_id)
Chris@1494 1094 include_calendar_headers_tags
Chris@1494 1095 javascript_tag("$(function() { $('##{field_id}').datepicker(datepickerOptions); });")
Chris@1494 1096 end
Chris@1494 1097
Chris@1494 1098 def include_calendar_headers_tags
Chris@1494 1099 unless @calendar_headers_tags_included
Chris@1494 1100 tags = javascript_include_tag("datepicker")
Chris@1494 1101 @calendar_headers_tags_included = true
Chris@1494 1102 content_for :header_tags do
Chris@1494 1103 start_of_week = Setting.start_of_week
Chris@1494 1104 start_of_week = l(:general_first_day_of_week, :default => '1') if start_of_week.blank?
Chris@1494 1105 # Redmine uses 1..7 (monday..sunday) in settings and locales
Chris@1494 1106 # JQuery uses 0..6 (sunday..saturday), 7 needs to be changed to 0
Chris@1494 1107 start_of_week = start_of_week.to_i % 7
Chris@1494 1108 tags << javascript_tag(
Chris@1494 1109 "var datepickerOptions={dateFormat: 'yy-mm-dd', firstDay: #{start_of_week}, " +
Chris@1494 1110 "showOn: 'button', buttonImageOnly: true, buttonImage: '" +
Chris@1494 1111 path_to_image('/images/calendar.png') +
Chris@1494 1112 "', showButtonPanel: true, showWeek: true, showOtherMonths: true, " +
Chris@1494 1113 "selectOtherMonths: true, changeMonth: true, changeYear: true, " +
Chris@1494 1114 "beforeShow: beforeShowDatePicker};")
Chris@1494 1115 jquery_locale = l('jquery.locale', :default => current_language.to_s)
Chris@1494 1116 unless jquery_locale == 'en'
Chris@1494 1117 tags << javascript_include_tag("i18n/jquery.ui.datepicker-#{jquery_locale}.js")
Chris@1494 1118 end
Chris@1494 1119 tags
Chris@1494 1120 end
Chris@1494 1121 end
Chris@1494 1122 end
Chris@1494 1123
Chris@1494 1124 # Overrides Rails' stylesheet_link_tag with themes and plugins support.
Chris@1494 1125 # Examples:
Chris@1494 1126 # stylesheet_link_tag('styles') # => picks styles.css from the current theme or defaults
Chris@1494 1127 # stylesheet_link_tag('styles', :plugin => 'foo) # => picks styles.css from plugin's assets
Chris@1494 1128 #
Chris@1494 1129 def stylesheet_link_tag(*sources)
Chris@1494 1130 options = sources.last.is_a?(Hash) ? sources.pop : {}
Chris@1494 1131 plugin = options.delete(:plugin)
Chris@1494 1132 sources = sources.map do |source|
Chris@1494 1133 if plugin
Chris@1494 1134 "/plugin_assets/#{plugin}/stylesheets/#{source}"
Chris@1494 1135 elsif current_theme && current_theme.stylesheets.include?(source)
Chris@1494 1136 current_theme.stylesheet_path(source)
Chris@1494 1137 else
Chris@1494 1138 source
Chris@1494 1139 end
Chris@1494 1140 end
Chris@1494 1141 super sources, options
Chris@1494 1142 end
Chris@1494 1143
Chris@1494 1144 # Overrides Rails' image_tag with themes and plugins support.
Chris@1494 1145 # Examples:
Chris@1494 1146 # image_tag('image.png') # => picks image.png from the current theme or defaults
Chris@1494 1147 # image_tag('image.png', :plugin => 'foo) # => picks image.png from plugin's assets
Chris@1494 1148 #
Chris@1494 1149 def image_tag(source, options={})
Chris@1494 1150 if plugin = options.delete(:plugin)
Chris@1494 1151 source = "/plugin_assets/#{plugin}/images/#{source}"
Chris@1494 1152 elsif current_theme && current_theme.images.include?(source)
Chris@1494 1153 source = current_theme.image_path(source)
Chris@1494 1154 end
Chris@1494 1155 super source, options
Chris@1494 1156 end
Chris@1494 1157
Chris@1494 1158 # Overrides Rails' javascript_include_tag with plugins support
Chris@1494 1159 # Examples:
Chris@1494 1160 # javascript_include_tag('scripts') # => picks scripts.js from defaults
Chris@1494 1161 # javascript_include_tag('scripts', :plugin => 'foo) # => picks scripts.js from plugin's assets
Chris@1494 1162 #
Chris@1494 1163 def javascript_include_tag(*sources)
Chris@1494 1164 options = sources.last.is_a?(Hash) ? sources.pop : {}
Chris@1494 1165 if plugin = options.delete(:plugin)
Chris@1494 1166 sources = sources.map do |source|
Chris@1494 1167 if plugin
Chris@1494 1168 "/plugin_assets/#{plugin}/javascripts/#{source}"
Chris@1494 1169 else
Chris@1494 1170 source
Chris@1494 1171 end
Chris@1494 1172 end
Chris@1494 1173 end
Chris@1494 1174 super sources, options
Chris@1494 1175 end
Chris@1494 1176
Chris@1494 1177 # TODO: remove this in 2.5.0
Chris@1494 1178 def has_content?(name)
Chris@1494 1179 content_for?(name)
Chris@1494 1180 end
Chris@1494 1181
Chris@1494 1182 def sidebar_content?
Chris@1494 1183 content_for?(:sidebar) || view_layouts_base_sidebar_hook_response.present?
Chris@1494 1184 end
Chris@1494 1185
Chris@1494 1186 def view_layouts_base_sidebar_hook_response
Chris@1494 1187 @view_layouts_base_sidebar_hook_response ||= call_hook(:view_layouts_base_sidebar)
Chris@1494 1188 end
Chris@1494 1189
Chris@1494 1190 def email_delivery_enabled?
Chris@1494 1191 !!ActionMailer::Base.perform_deliveries
Chris@1494 1192 end
Chris@1494 1193
Chris@1494 1194 # Returns the avatar image tag for the given +user+ if avatars are enabled
Chris@1494 1195 # +user+ can be a User or a string that will be scanned for an email address (eg. 'joe <joe@foo.bar>')
Chris@1494 1196 def avatar(user, options = { })
Chris@1494 1197 if Setting.gravatar_enabled?
Chris@1494 1198 options.merge!({:ssl => (request && request.ssl?), :default => Setting.gravatar_default})
Chris@1494 1199 email = nil
Chris@1494 1200 if user.respond_to?(:mail)
Chris@1494 1201 email = user.mail
Chris@1494 1202 elsif user.to_s =~ %r{<(.+?)>}
Chris@1494 1203 email = $1
Chris@1494 1204 end
Chris@1494 1205 return gravatar(email.to_s.downcase, options) unless email.blank? rescue nil
Chris@1494 1206 else
Chris@1494 1207 ''
Chris@1494 1208 end
Chris@1494 1209 end
Chris@1494 1210
Chris@1494 1211 def sanitize_anchor_name(anchor)
Chris@1494 1212 if ''.respond_to?(:encoding) || RUBY_PLATFORM == 'java'
Chris@1494 1213 anchor.gsub(%r{[^\s\-\p{Word}]}, '').gsub(%r{\s+(\-+\s*)?}, '-')
Chris@1494 1214 else
Chris@1494 1215 # TODO: remove when ruby1.8 is no longer supported
Chris@1494 1216 anchor.gsub(%r{[^\w\s\-]}, '').gsub(%r{\s+(\-+\s*)?}, '-')
Chris@1494 1217 end
Chris@1494 1218 end
Chris@1494 1219
Chris@1494 1220 # Returns the javascript tags that are included in the html layout head
Chris@1494 1221 def javascript_heads
Chris@1494 1222 tags = javascript_include_tag('jquery-1.8.3-ui-1.9.2-ujs-2.0.3', 'application')
Chris@1494 1223 unless User.current.pref.warn_on_leaving_unsaved == '0'
Chris@1494 1224 tags << "\n".html_safe + javascript_tag("$(window).load(function(){ warnLeavingUnsaved('#{escape_javascript l(:text_warn_on_leaving_unsaved)}'); });")
Chris@1494 1225 end
Chris@1494 1226 tags
Chris@1494 1227 end
Chris@1494 1228
Chris@1494 1229 def favicon
Chris@1494 1230 "<link rel='shortcut icon' href='#{image_path('/favicon.ico')}' />".html_safe
Chris@1494 1231 end
Chris@1494 1232
Chris@1494 1233 def robot_exclusion_tag
Chris@1494 1234 '<meta name="robots" content="noindex,follow,noarchive" />'.html_safe
Chris@1494 1235 end
Chris@1494 1236
Chris@1494 1237 # Returns true if arg is expected in the API response
Chris@1494 1238 def include_in_api_response?(arg)
Chris@1494 1239 unless @included_in_api_response
Chris@1494 1240 param = params[:include]
Chris@1494 1241 @included_in_api_response = param.is_a?(Array) ? param.collect(&:to_s) : param.to_s.split(',')
Chris@1494 1242 @included_in_api_response.collect!(&:strip)
Chris@1494 1243 end
Chris@1494 1244 @included_in_api_response.include?(arg.to_s)
Chris@1494 1245 end
Chris@1494 1246
Chris@1494 1247 # Returns options or nil if nometa param or X-Redmine-Nometa header
Chris@1494 1248 # was set in the request
Chris@1494 1249 def api_meta(options)
Chris@1494 1250 if params[:nometa].present? || request.headers['X-Redmine-Nometa']
Chris@1494 1251 # compatibility mode for activeresource clients that raise
Chris@1494 1252 # an error when unserializing an array with attributes
Chris@1494 1253 nil
Chris@1494 1254 else
Chris@1494 1255 options
Chris@1494 1256 end
Chris@1494 1257 end
Chris@1494 1258
Chris@1494 1259 private
Chris@1494 1260
Chris@1494 1261 def wiki_helper
Chris@1494 1262 helper = Redmine::WikiFormatting.helper_for(Setting.text_formatting)
Chris@1494 1263 extend helper
Chris@1494 1264 return self
Chris@1494 1265 end
Chris@1494 1266
Chris@1494 1267 def link_to_content_update(text, url_params = {}, html_options = {})
Chris@1494 1268 link_to(text, url_params, html_options)
Chris@1494 1269 end
Chris@1494 1270 end