annotate .svn/pristine/6c/6c4dd8ce0226a8bc5d18fafda9466bf4106576da.svn-base @ 1478:5ca1f4a47171 bibplugin_db_migrations

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