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