PageRenderTime 56ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/app/helpers/application_helper.rb

https://github.com/kdmny/redmine
Ruby | 628 lines | 477 code | 69 blank | 82 comment | 98 complexity | cbd52f19c5f5093e02c23033a7ef7fe8 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. # redMine - project management software
  2. # Copyright (C) 2006-2007 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. require 'coderay'
  18. require 'coderay/helpers/file_type'
  19. require 'forwardable'
  20. require 'cgi'
  21. module ApplicationHelper
  22. include Redmine::WikiFormatting::Macros::Definitions
  23. include GravatarHelper::PublicMethods
  24. extend Forwardable
  25. def_delegators :wiki_helper, :wikitoolbar_for, :heads_for_wiki_formatter
  26. def current_role
  27. @current_role ||= User.current.role_for_project(@project)
  28. end
  29. # Return true if user is authorized for controller/action, otherwise false
  30. def authorize_for(controller, action)
  31. User.current.allowed_to?({:controller => controller, :action => action}, @project)
  32. end
  33. # Display a link if user is authorized
  34. def link_to_if_authorized(name, options = {}, html_options = nil, *parameters_for_method_reference)
  35. link_to(name, options, html_options, *parameters_for_method_reference) if authorize_for(options[:controller] || params[:controller], options[:action])
  36. end
  37. # Display a link to remote if user is authorized
  38. def link_to_remote_if_authorized(name, options = {}, html_options = nil)
  39. url = options[:url] || {}
  40. link_to_remote(name, options, html_options) if authorize_for(url[:controller] || params[:controller], url[:action])
  41. end
  42. # Display a link to user's account page
  43. def link_to_user(user, options={})
  44. (user && !user.anonymous?) ? link_to(user.name(options[:format]), :controller => 'account', :action => 'show', :id => user) : 'Anonymous'
  45. end
  46. def link_to_issue(issue, options={})
  47. options[:class] ||= ''
  48. options[:class] << ' issue'
  49. options[:class] << ' closed' if issue.closed?
  50. link_to "#{issue.tracker.name} ##{issue.id}", {:controller => "issues", :action => "show", :id => issue}, options
  51. end
  52. # Generates a link to an attachment.
  53. # Options:
  54. # * :text - Link text (default to attachment filename)
  55. # * :download - Force download (default: false)
  56. def link_to_attachment(attachment, options={})
  57. text = options.delete(:text) || attachment.filename
  58. action = options.delete(:download) ? 'download' : 'show'
  59. link_to(h(text), {:controller => 'attachments', :action => action, :id => attachment, :filename => attachment.filename }, options)
  60. end
  61. def toggle_link(name, id, options={})
  62. onclick = "Element.toggle('#{id}'); "
  63. onclick << (options[:focus] ? "Form.Element.focus('#{options[:focus]}'); " : "this.blur(); ")
  64. onclick << "return false;"
  65. link_to(name, "#", :onclick => onclick)
  66. end
  67. def image_to_function(name, function, html_options = {})
  68. html_options.symbolize_keys!
  69. tag(:input, html_options.merge({
  70. :type => "image", :src => image_path(name),
  71. :onclick => (html_options[:onclick] ? "#{html_options[:onclick]}; " : "") + "#{function};"
  72. }))
  73. end
  74. def prompt_to_remote(name, text, param, url, html_options = {})
  75. html_options[:onclick] = "promptToRemote('#{text}', '#{param}', '#{url_for(url)}'); return false;"
  76. link_to name, {}, html_options
  77. end
  78. def format_date(date)
  79. return nil unless date
  80. # "Setting.date_format.size < 2" is a temporary fix (content of date_format setting changed)
  81. @date_format ||= (Setting.date_format.blank? || Setting.date_format.size < 2 ? l(:general_fmt_date) : Setting.date_format)
  82. date.strftime(@date_format)
  83. end
  84. def format_time(time, include_date = true)
  85. return nil unless time
  86. time = time.to_time if time.is_a?(String)
  87. zone = User.current.time_zone
  88. local = zone ? time.in_time_zone(zone) : (time.utc? ? time.localtime : time)
  89. @date_format ||= (Setting.date_format.blank? || Setting.date_format.size < 2 ? l(:general_fmt_date) : Setting.date_format)
  90. @time_format ||= (Setting.time_format.blank? ? l(:general_fmt_time) : Setting.time_format)
  91. include_date ? local.strftime("#{@date_format} #{@time_format}") : local.strftime(@time_format)
  92. end
  93. def format_activity_title(text)
  94. h(truncate_single_line(text, 100))
  95. end
  96. def format_activity_day(date)
  97. date == Date.today ? l(:label_today).titleize : format_date(date)
  98. end
  99. def format_activity_description(text)
  100. h(truncate(text.to_s, 250).gsub(%r{<(pre|code)>.*$}m, '...'))
  101. end
  102. def distance_of_date_in_words(from_date, to_date = 0)
  103. from_date = from_date.to_date if from_date.respond_to?(:to_date)
  104. to_date = to_date.to_date if to_date.respond_to?(:to_date)
  105. distance_in_days = (to_date - from_date).abs
  106. lwr(:actionview_datehelper_time_in_words_day, distance_in_days)
  107. end
  108. def due_date_distance_in_words(date)
  109. if date
  110. l((date < Date.today ? :label_roadmap_overdue : :label_roadmap_due_in), distance_of_date_in_words(Date.today, date))
  111. end
  112. end
  113. def render_page_hierarchy(pages, node=nil)
  114. content = ''
  115. if pages[node]
  116. content << "<ul class=\"pages-hierarchy\">\n"
  117. pages[node].each do |page|
  118. content << "<li>"
  119. content << link_to(h(page.pretty_title), {:controller => 'wiki', :action => 'index', :id => page.project, :page => page.title},
  120. :title => (page.respond_to?(:updated_on) ? l(:label_updated_time, distance_of_time_in_words(Time.now, page.updated_on)) : nil))
  121. content << "\n" + render_page_hierarchy(pages, page.id) if pages[page.id]
  122. content << "</li>\n"
  123. end
  124. content << "</ul>\n"
  125. end
  126. content
  127. end
  128. # Renders flash messages
  129. def render_flash_messages
  130. s = ''
  131. flash.each do |k,v|
  132. s << content_tag('div', v, :class => "flash #{k}")
  133. end
  134. s
  135. end
  136. # Truncates and returns the string as a single line
  137. def truncate_single_line(string, *args)
  138. truncate(string, *args).gsub(%r{[\r\n]+}m, ' ')
  139. end
  140. def html_hours(text)
  141. text.gsub(%r{(\d+)\.(\d+)}, '<span class="hours hours-int">\1</span><span class="hours hours-dec">.\2</span>')
  142. end
  143. def authoring(created, author, options={})
  144. time_tag = @project.nil? ? content_tag('acronym', distance_of_time_in_words(Time.now, created), :title => format_time(created)) :
  145. link_to(distance_of_time_in_words(Time.now, created),
  146. {:controller => 'projects', :action => 'activity', :id => @project, :from => created.to_date},
  147. :title => format_time(created))
  148. author_tag = (author.is_a?(User) && !author.anonymous?) ? link_to(h(author), :controller => 'account', :action => 'show', :id => author) : h(author || 'Anonymous')
  149. l(options[:label] || :label_added_time_by, author_tag, time_tag)
  150. end
  151. def l_or_humanize(s, options={})
  152. k = "#{options[:prefix]}#{s}".to_sym
  153. l_has_string?(k) ? l(k) : s.to_s.humanize
  154. end
  155. def day_name(day)
  156. l(:general_day_names).split(',')[day-1]
  157. end
  158. def month_name(month)
  159. l(:actionview_datehelper_select_month_names).split(',')[month-1]
  160. end
  161. def syntax_highlight(name, content)
  162. type = CodeRay::FileType[name]
  163. type ? CodeRay.scan(content, type).html : h(content)
  164. end
  165. def to_path_param(path)
  166. path.to_s.split(%r{[/\\]}).select {|p| !p.blank?}
  167. end
  168. def pagination_links_full(paginator, count=nil, options={})
  169. page_param = options.delete(:page_param) || :page
  170. url_param = params.dup
  171. # don't reuse params if filters are present
  172. url_param.clear if url_param.has_key?(:set_filter)
  173. html = ''
  174. html << link_to_remote(('&#171; ' + l(:label_previous)),
  175. {:update => 'content',
  176. :url => url_param.merge(page_param => paginator.current.previous),
  177. :complete => 'window.scrollTo(0,0)'},
  178. {:href => url_for(:params => url_param.merge(page_param => paginator.current.previous))}) + ' ' if paginator.current.previous
  179. html << (pagination_links_each(paginator, options) do |n|
  180. link_to_remote(n.to_s,
  181. {:url => {:params => url_param.merge(page_param => n)},
  182. :update => 'content',
  183. :complete => 'window.scrollTo(0,0)'},
  184. {:href => url_for(:params => url_param.merge(page_param => n))})
  185. end || '')
  186. html << ' ' + link_to_remote((l(:label_next) + ' &#187;'),
  187. {:update => 'content',
  188. :url => url_param.merge(page_param => paginator.current.next),
  189. :complete => 'window.scrollTo(0,0)'},
  190. {:href => url_for(:params => url_param.merge(page_param => paginator.current.next))}) if paginator.current.next
  191. unless count.nil?
  192. html << [" (#{paginator.current.first_item}-#{paginator.current.last_item}/#{count})", per_page_links(paginator.items_per_page)].compact.join(' | ')
  193. end
  194. html
  195. end
  196. def per_page_links(selected=nil)
  197. url_param = params.dup
  198. url_param.clear if url_param.has_key?(:set_filter)
  199. links = Setting.per_page_options_array.collect do |n|
  200. n == selected ? n : link_to_remote(n, {:update => "content", :url => params.dup.merge(:per_page => n)},
  201. {:href => url_for(url_param.merge(:per_page => n))})
  202. end
  203. links.size > 1 ? l(:label_display_per_page, links.join(', ')) : nil
  204. end
  205. def breadcrumb(*args)
  206. elements = args.flatten
  207. elements.any? ? content_tag('p', args.join(' &#187; ') + ' &#187; ', :class => 'breadcrumb') : nil
  208. end
  209. def html_title(*args)
  210. if args.empty?
  211. title = []
  212. title << @project.name if @project
  213. title += @html_title if @html_title
  214. title << Setting.app_title
  215. title.compact.join(' - ')
  216. else
  217. @html_title ||= []
  218. @html_title += args
  219. end
  220. end
  221. def accesskey(s)
  222. Redmine::AccessKeys.key_for s
  223. end
  224. # Formats text according to system settings.
  225. # 2 ways to call this method:
  226. # * with a String: textilizable(text, options)
  227. # * with an object and one of its attribute: textilizable(issue, :description, options)
  228. def textilizable(*args)
  229. options = args.last.is_a?(Hash) ? args.pop : {}
  230. case args.size
  231. when 1
  232. obj = options[:object]
  233. text = args.shift
  234. when 2
  235. obj = args.shift
  236. text = obj.send(args.shift).to_s
  237. else
  238. raise ArgumentError, 'invalid arguments to textilizable'
  239. end
  240. return '' if text.blank?
  241. only_path = options.delete(:only_path) == false ? false : true
  242. # when using an image link, try to use an attachment, if possible
  243. attachments = options[:attachments] || (obj && obj.respond_to?(:attachments) ? obj.attachments : nil)
  244. if attachments
  245. attachments = attachments.sort_by(&:created_on).reverse
  246. text = text.gsub(/!((\<|\=|\>)?(\([^\)]+\))?(\[[^\]]+\])?(\{[^\}]+\})?)(\S+\.(bmp|gif|jpg|jpeg|png))!/i) do |m|
  247. style = $1
  248. filename = $6
  249. rf = Regexp.new(Regexp.escape(filename), Regexp::IGNORECASE)
  250. # search for the picture in attachments
  251. if found = attachments.detect { |att| att.filename =~ rf }
  252. image_url = url_for :only_path => only_path, :controller => 'attachments', :action => 'download', :id => found
  253. desc = found.description.to_s.gsub(/^([^\(\)]*).*$/, "\\1")
  254. alt = desc.blank? ? nil : "(#{desc})"
  255. "!#{style}#{image_url}#{alt}!"
  256. else
  257. "!#{style}#{filename}!"
  258. end
  259. end
  260. end
  261. text = Redmine::WikiFormatting.to_html(Setting.text_formatting, text) { |macro, args| exec_macro(macro, obj, args) }
  262. # different methods for formatting wiki links
  263. case options[:wiki_links]
  264. when :local
  265. # used for local links to html files
  266. format_wiki_link = Proc.new {|project, title, anchor| "#{title}.html" }
  267. when :anchor
  268. # used for single-file wiki export
  269. format_wiki_link = Proc.new {|project, title, anchor| "##{title}" }
  270. else
  271. format_wiki_link = Proc.new {|project, title, anchor| url_for(:only_path => only_path, :controller => 'wiki', :action => 'index', :id => project, :page => title, :anchor => anchor) }
  272. end
  273. project = options[:project] || @project || (obj && obj.respond_to?(:project) ? obj.project : nil)
  274. # Wiki links
  275. #
  276. # Examples:
  277. # [[mypage]]
  278. # [[mypage|mytext]]
  279. # wiki links can refer other project wikis, using project name or identifier:
  280. # [[project:]] -> wiki starting page
  281. # [[project:|mytext]]
  282. # [[project:mypage]]
  283. # [[project:mypage|mytext]]
  284. text = text.gsub(/(!)?(\[\[([^\]\n\|]+)(\|([^\]\n\|]+))?\]\])/) do |m|
  285. link_project = project
  286. esc, all, page, title = $1, $2, $3, $5
  287. if esc.nil?
  288. if page =~ /^([^\:]+)\:(.*)$/
  289. link_project = Project.find_by_name($1) || Project.find_by_identifier($1)
  290. page = $2
  291. title ||= $1 if page.blank?
  292. end
  293. if link_project && link_project.wiki
  294. # extract anchor
  295. anchor = nil
  296. if page =~ /^(.+?)\#(.+)$/
  297. page, anchor = $1, $2
  298. end
  299. # check if page exists
  300. wiki_page = link_project.wiki.find_page(page)
  301. link_to((title || page), format_wiki_link.call(link_project, Wiki.titleize(page), anchor),
  302. :class => ('wiki-page' + (wiki_page ? '' : ' new')))
  303. else
  304. # project or wiki doesn't exist
  305. title || page
  306. end
  307. else
  308. all
  309. end
  310. end
  311. # Redmine links
  312. #
  313. # Examples:
  314. # Issues:
  315. # #52 -> Link to issue #52
  316. # Changesets:
  317. # r52 -> Link to revision 52
  318. # commit:a85130f -> Link to scmid starting with a85130f
  319. # Documents:
  320. # document#17 -> Link to document with id 17
  321. # document:Greetings -> Link to the document with title "Greetings"
  322. # document:"Some document" -> Link to the document with title "Some document"
  323. # Versions:
  324. # version#3 -> Link to version with id 3
  325. # version:1.0.0 -> Link to version named "1.0.0"
  326. # version:"1.0 beta 2" -> Link to version named "1.0 beta 2"
  327. # Attachments:
  328. # attachment:file.zip -> Link to the attachment of the current object named file.zip
  329. # Source files:
  330. # source:some/file -> Link to the file located at /some/file in the project's repository
  331. # source:some/file@52 -> Link to the file's revision 52
  332. # source:some/file#L120 -> Link to line 120 of the file
  333. # source:some/file@52#L120 -> Link to line 120 of the file's revision 52
  334. # export:some/file -> Force the download of the file
  335. # Forum messages:
  336. # message#1218 -> Link to message with id 1218
  337. text = text.gsub(%r{([\s\(,\-\>]|^)(!)?(attachment|document|version|commit|source|export|message)?((#|r)(\d+)|(:)([^"\s<>][^\s<>]*?|"[^"]+?"))(?=(?=[[:punct:]]\W)|\s|<|$)}) do |m|
  338. leading, esc, prefix, sep, oid = $1, $2, $3, $5 || $7, $6 || $8
  339. link = nil
  340. if esc.nil?
  341. if prefix.nil? && sep == 'r'
  342. if project && (changeset = project.changesets.find_by_revision(oid))
  343. link = link_to("r#{oid}", {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :rev => oid},
  344. :class => 'changeset',
  345. :title => truncate_single_line(changeset.comments, 100))
  346. end
  347. elsif sep == '#'
  348. oid = oid.to_i
  349. case prefix
  350. when nil
  351. if issue = Issue.find_by_id(oid, :include => [:project, :status], :conditions => Project.visible_by(User.current))
  352. link = link_to("##{oid}", {:only_path => only_path, :controller => 'issues', :action => 'show', :id => oid},
  353. :class => (issue.closed? ? 'issue closed' : 'issue'),
  354. :title => "#{truncate(issue.subject, 100)} (#{issue.status.name})")
  355. link = content_tag('del', link) if issue.closed?
  356. end
  357. when 'document'
  358. if document = Document.find_by_id(oid, :include => [:project], :conditions => Project.visible_by(User.current))
  359. link = link_to h(document.title), {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document},
  360. :class => 'document'
  361. end
  362. when 'version'
  363. if version = Version.find_by_id(oid, :include => [:project], :conditions => Project.visible_by(User.current))
  364. link = link_to h(version.name), {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version},
  365. :class => 'version'
  366. end
  367. when 'message'
  368. if message = Message.find_by_id(oid, :include => [:parent, {:board => :project}], :conditions => Project.visible_by(User.current))
  369. link = link_to h(truncate(message.subject, 60)), {:only_path => only_path,
  370. :controller => 'messages',
  371. :action => 'show',
  372. :board_id => message.board,
  373. :id => message.root,
  374. :anchor => (message.parent ? "message-#{message.id}" : nil)},
  375. :class => 'message'
  376. end
  377. end
  378. elsif sep == ':'
  379. # removes the double quotes if any
  380. name = oid.gsub(%r{^"(.*)"$}, "\\1")
  381. case prefix
  382. when 'document'
  383. if project && document = project.documents.find_by_title(name)
  384. link = link_to h(document.title), {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document},
  385. :class => 'document'
  386. end
  387. when 'version'
  388. if project && version = project.versions.find_by_name(name)
  389. link = link_to h(version.name), {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version},
  390. :class => 'version'
  391. end
  392. when 'commit'
  393. if project && (changeset = project.changesets.find(:first, :conditions => ["scmid LIKE ?", "#{name}%"]))
  394. link = link_to h("#{name}"), {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :rev => changeset.revision},
  395. :class => 'changeset',
  396. :title => truncate_single_line(changeset.comments, 100)
  397. end
  398. when 'source', 'export'
  399. if project && project.repository
  400. name =~ %r{^[/\\]*(.*?)(@([0-9a-f]+))?(#(L\d+))?$}
  401. path, rev, anchor = $1, $3, $5
  402. link = link_to h("#{prefix}:#{name}"), {:controller => 'repositories', :action => 'entry', :id => project,
  403. :path => to_path_param(path),
  404. :rev => rev,
  405. :anchor => anchor,
  406. :format => (prefix == 'export' ? 'raw' : nil)},
  407. :class => (prefix == 'export' ? 'source download' : 'source')
  408. end
  409. when 'attachment'
  410. if attachments && attachment = attachments.detect {|a| a.filename == name }
  411. link = link_to h(attachment.filename), {:only_path => only_path, :controller => 'attachments', :action => 'download', :id => attachment},
  412. :class => 'attachment'
  413. end
  414. end
  415. end
  416. end
  417. leading + (link || "#{prefix}#{sep}#{oid}")
  418. end
  419. text
  420. end
  421. # Same as Rails' simple_format helper without using paragraphs
  422. def simple_format_without_paragraph(text)
  423. text.to_s.
  424. gsub(/\r\n?/, "\n"). # \r\n and \r -> \n
  425. gsub(/\n\n+/, "<br /><br />"). # 2+ newline -> 2 br
  426. gsub(/([^\n]\n)(?=[^\n])/, '\1<br />') # 1 newline -> br
  427. end
  428. def error_messages_for(object_name, options = {})
  429. options = options.symbolize_keys
  430. object = instance_variable_get("@#{object_name}")
  431. if object && !object.errors.empty?
  432. # build full_messages here with controller current language
  433. full_messages = []
  434. object.errors.each do |attr, msg|
  435. next if msg.nil?
  436. msg = msg.first if msg.is_a? Array
  437. if attr == "base"
  438. full_messages << l(msg)
  439. else
  440. full_messages << "&#171; " + (l_has_string?("field_" + attr) ? l("field_" + attr) : object.class.human_attribute_name(attr)) + " &#187; " + l(msg) unless attr == "custom_values"
  441. end
  442. end
  443. # retrieve custom values error messages
  444. if object.errors[:custom_values]
  445. object.custom_values.each do |v|
  446. v.errors.each do |attr, msg|
  447. next if msg.nil?
  448. msg = msg.first if msg.is_a? Array
  449. full_messages << "&#171; " + v.custom_field.name + " &#187; " + l(msg)
  450. end
  451. end
  452. end
  453. content_tag("div",
  454. content_tag(
  455. options[:header_tag] || "span", lwr(:gui_validation_error, full_messages.length) + ":"
  456. ) +
  457. content_tag("ul", full_messages.collect { |msg| content_tag("li", msg) }),
  458. "id" => options[:id] || "errorExplanation", "class" => options[:class] || "errorExplanation"
  459. )
  460. else
  461. ""
  462. end
  463. end
  464. def lang_options_for_select(blank=true)
  465. (blank ? [["(auto)", ""]] : []) +
  466. GLoc.valid_languages.collect{|lang| [ ll(lang.to_s, :general_lang_name), lang.to_s]}.sort{|x,y| x.last <=> y.last }
  467. end
  468. def label_tag_for(name, option_tags = nil, options = {})
  469. label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "")
  470. content_tag("label", label_text)
  471. end
  472. def labelled_tabular_form_for(name, object, options, &proc)
  473. options[:html] ||= {}
  474. options[:html][:class] = 'tabular' unless options[:html].has_key?(:class)
  475. form_for(name, object, options.merge({ :builder => TabularFormBuilder, :lang => current_language}), &proc)
  476. end
  477. def back_url_hidden_field_tag
  478. back_url = params[:back_url] || request.env['HTTP_REFERER']
  479. back_url = CGI.unescape(back_url.to_s)
  480. hidden_field_tag('back_url', CGI.escape(back_url)) unless back_url.blank?
  481. end
  482. def check_all_links(form_name)
  483. link_to_function(l(:button_check_all), "checkAll('#{form_name}', true)") +
  484. " | " +
  485. link_to_function(l(:button_uncheck_all), "checkAll('#{form_name}', false)")
  486. end
  487. def progress_bar(pcts, options={})
  488. pcts = [pcts, pcts] unless pcts.is_a?(Array)
  489. pcts[1] = pcts[1] - pcts[0]
  490. pcts << (100 - pcts[1] - pcts[0])
  491. width = options[:width] || '100px;'
  492. legend = options[:legend] || ''
  493. content_tag('table',
  494. content_tag('tr',
  495. (pcts[0] > 0 ? content_tag('td', '', :style => "width: #{pcts[0].floor}%;", :class => 'closed') : '') +
  496. (pcts[1] > 0 ? content_tag('td', '', :style => "width: #{pcts[1].floor}%;", :class => 'done') : '') +
  497. (pcts[2] > 0 ? content_tag('td', '', :style => "width: #{pcts[2].floor}%;", :class => 'todo') : '')
  498. ), :class => 'progress', :style => "width: #{width};") +
  499. content_tag('p', legend, :class => 'pourcent')
  500. end
  501. def context_menu_link(name, url, options={})
  502. options[:class] ||= ''
  503. if options.delete(:selected)
  504. options[:class] << ' icon-checked disabled'
  505. options[:disabled] = true
  506. end
  507. if options.delete(:disabled)
  508. options.delete(:method)
  509. options.delete(:confirm)
  510. options.delete(:onclick)
  511. options[:class] << ' disabled'
  512. url = '#'
  513. end
  514. link_to name, url, options
  515. end
  516. def calendar_for(field_id)
  517. include_calendar_headers_tags
  518. image_tag("calendar.png", {:id => "#{field_id}_trigger",:class => "calendar-trigger"}) +
  519. javascript_tag("Calendar.setup({inputField : '#{field_id}', ifFormat : '%Y-%m-%d', button : '#{field_id}_trigger' });")
  520. end
  521. def include_calendar_headers_tags
  522. unless @calendar_headers_tags_included
  523. @calendar_headers_tags_included = true
  524. content_for :header_tags do
  525. javascript_include_tag('calendar/calendar') +
  526. javascript_include_tag("calendar/lang/calendar-#{current_language}.js") +
  527. javascript_include_tag('calendar/calendar-setup') +
  528. stylesheet_link_tag('calendar')
  529. end
  530. end
  531. end
  532. def content_for(name, content = nil, &block)
  533. @has_content ||= {}
  534. @has_content[name] = true
  535. super(name, content, &block)
  536. end
  537. def has_content?(name)
  538. (@has_content && @has_content[name]) || false
  539. end
  540. # Returns the avatar image tag for the given +user+ if avatars are enabled
  541. # +user+ can be a User or a string that will be scanned for an email address (eg. 'joe <joe@foo.bar>')
  542. def avatar(user, options = { })
  543. if Setting.gravatar_enabled?
  544. email = nil
  545. if user.respond_to?(:mail)
  546. email = user.mail
  547. elsif user.to_s =~ %r{<(.+?)>}
  548. email = $1
  549. end
  550. return gravatar(email.to_s.downcase, options) unless email.blank? rescue nil
  551. end
  552. end
  553. private
  554. def wiki_helper
  555. helper = Redmine::WikiFormatting.helper_for(Setting.text_formatting)
  556. extend helper
  557. return self
  558. end
  559. end