您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

application_helper.rb 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. # redMine - project management software
  2. # Copyright (C) 2006 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. module ApplicationHelper
  18. # Return current logged in user or nil
  19. def loggedin?
  20. @logged_in_user
  21. end
  22. # Return true if user is logged in and is admin, otherwise false
  23. def admin_loggedin?
  24. @logged_in_user and @logged_in_user.admin?
  25. end
  26. # Return true if user is authorized for controller/action, otherwise false
  27. def authorize_for(controller, action)
  28. # check if action is allowed on public projects
  29. if @project.is_public? and Permission.allowed_to_public "%s/%s" % [ controller, action ]
  30. return true
  31. end
  32. # check if user is authorized
  33. if @logged_in_user and (@logged_in_user.admin? or Permission.allowed_to_role( "%s/%s" % [ controller, action ], @logged_in_user.role_for_project(@project.id) ) )
  34. return true
  35. end
  36. return false
  37. end
  38. # Display a link if user is authorized
  39. def link_to_if_authorized(name, options = {}, html_options = nil, *parameters_for_method_reference)
  40. link_to(name, options, html_options, *parameters_for_method_reference) if authorize_for(options[:controller], options[:action])
  41. end
  42. # Display a link to user's account page
  43. def link_to_user(user)
  44. link_to user.display_name, :controller => 'account', :action => 'show', :id => user
  45. end
  46. def format_date(date)
  47. l_date(date) if date
  48. end
  49. def format_time(time)
  50. l_datetime(time) if time
  51. end
  52. def day_name(day)
  53. l(:general_day_names).split(',')[day-1]
  54. end
  55. def pagination_links_full(paginator, options={}, html_options={})
  56. html = ''
  57. html << link_to_remote(('&#171; ' + l(:label_previous)),
  58. {:update => "content", :url => { :page => paginator.current.previous }},
  59. {:href => url_for(:action => 'list', :params => @params.merge({:page => paginator.current.previous}))}) + ' ' if paginator.current.previous
  60. html << (pagination_links_each(paginator, options) do |n|
  61. link_to_remote(n.to_s,
  62. {:url => {:action => 'list', :params => @params.merge({:page => n})}, :update => 'content'},
  63. {:href => url_for(:action => 'list', :params => @params.merge({:page => n}))})
  64. end || '')
  65. html << ' ' + link_to_remote((l(:label_next) + ' &#187;'),
  66. {:update => "content", :url => { :page => paginator.current.next }},
  67. {:href => url_for(:action => 'list', :params => @params.merge({:page => paginator.current.next}))}) if paginator.current.next
  68. html
  69. end
  70. def textilizable(text)
  71. $RDM_TEXTILE_DISABLED ? text : RedCloth.new(text).to_html
  72. end
  73. def error_messages_for(object_name, options = {})
  74. options = options.symbolize_keys
  75. object = instance_variable_get("@#{object_name}")
  76. if object && !object.errors.empty?
  77. # build full_messages here with controller current language
  78. full_messages = []
  79. object.errors.each do |attr, msg|
  80. next if msg.nil?
  81. if attr == "base"
  82. full_messages << l(msg)
  83. else
  84. full_messages << "&#171; " + (l_has_string?("field_" + attr) ? l("field_" + attr) : object.class.human_attribute_name(attr)) + " &#187; " + l(msg) unless attr == "custom_values"
  85. end
  86. end
  87. # retrieve custom values error messages
  88. if object.errors[:custom_values]
  89. object.custom_values.each do |v|
  90. v.errors.each do |attr, msg|
  91. next if msg.nil?
  92. full_messages << "&#171; " + v.custom_field.name + " &#187; " + l(msg)
  93. end
  94. end
  95. end
  96. content_tag("div",
  97. content_tag(
  98. options[:header_tag] || "h2", lwr(:gui_validation_error, full_messages.length) + " :"
  99. ) +
  100. content_tag("ul", full_messages.collect { |msg| content_tag("li", msg) }),
  101. "id" => options[:id] || "errorExplanation", "class" => options[:class] || "errorExplanation"
  102. )
  103. else
  104. ""
  105. end
  106. end
  107. def lang_options_for_select
  108. (GLoc.valid_languages.sort {|x,y| x.to_s <=> y.to_s }).collect {|lang| [ l_lang_name(lang.to_s, lang), lang.to_s]}
  109. end
  110. def label_tag_for(name, option_tags = nil, options = {})
  111. label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "")
  112. content_tag("label", label_text)
  113. end
  114. def labelled_tabular_form_for(name, object, options, &proc)
  115. options[:html] ||= {}
  116. options[:html].store :class, "tabular"
  117. form_for(name, object, options.merge({ :builder => TabularFormBuilder, :lang => current_language}), &proc)
  118. end
  119. def check_all_links(form_name)
  120. link_to_function(l(:button_check_all), "checkAll('#{form_name}', true)") +
  121. " | " +
  122. link_to_function(l(:button_uncheck_all), "checkAll('#{form_name}', false)")
  123. end
  124. def calendar_for(field_id)
  125. image_tag("calendar", {:id => "#{field_id}_trigger",:class => "calendar-trigger"}) +
  126. javascript_tag("Calendar.setup({inputField : '#{field_id}', ifFormat : '%Y-%m-%d', button : '#{field_id}_trigger' });")
  127. end
  128. end
  129. class TabularFormBuilder < ActionView::Helpers::FormBuilder
  130. include GLoc
  131. def initialize(object_name, object, template, options, proc)
  132. set_language_if_valid options.delete(:lang)
  133. @object_name, @object, @template, @options, @proc = object_name, object, template, options, proc
  134. end
  135. (field_helpers - %w(radio_button hidden_field) + %w(date_select)).each do |selector|
  136. src = <<-END_SRC
  137. def #{selector}(field, options = {})
  138. label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "")
  139. label = @template.content_tag("label", label_text,
  140. :class => (@object.errors[field] ? "error" : nil),
  141. :for => (@object_name.to_s + "_" + field.to_s))
  142. label + super
  143. end
  144. END_SRC
  145. class_eval src, __FILE__, __LINE__
  146. end
  147. def select(field, choices, options = {})
  148. label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "")
  149. label = @template.content_tag("label", label_text,
  150. :class => (@object.errors[field] ? "error" : nil),
  151. :for => (@object_name.to_s + "_" + field.to_s))
  152. label + super
  153. end
  154. end