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