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