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