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