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