You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

settings_helper.rb 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2020 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. module SettingsHelper
  19. def administration_settings_tabs
  20. tabs =
  21. [
  22. {:name => 'general', :partial => 'settings/general', :label => :label_general},
  23. {:name => 'display', :partial => 'settings/display', :label => :label_display},
  24. {:name => 'authentication', :partial => 'settings/authentication',
  25. :label => :label_authentication},
  26. {:name => 'api', :partial => 'settings/api', :label => :label_api},
  27. {:name => 'projects', :partial => 'settings/projects', :label => :label_project_plural},
  28. {:name => 'users', :partial => 'settings/users', :label => :label_user_plural},
  29. {:name => 'issues', :partial => 'settings/issues', :label => :label_issue_tracking},
  30. {:name => 'timelog', :partial => 'settings/timelog', :label => :label_time_tracking},
  31. {:name => 'attachments', :partial => 'settings/attachments',
  32. :label => :label_attachment_plural},
  33. {:name => 'notifications', :partial => 'settings/notifications',
  34. :label => :field_mail_notification},
  35. {:name => 'mail_handler', :partial => 'settings/mail_handler',
  36. :label => :label_incoming_emails},
  37. {:name => 'repositories', :partial => 'settings/repositories',
  38. :label => :label_repository_plural}
  39. ]
  40. end
  41. def render_settings_error(errors)
  42. return if errors.blank?
  43. s = ''.html_safe
  44. errors.each do |name, message|
  45. s << content_tag('li', content_tag('b', l("setting_#{name}")) + " " + message)
  46. end
  47. content_tag('div', content_tag('ul', s), :id => 'errorExplanation')
  48. end
  49. def setting_value(setting)
  50. value = nil
  51. if params[:settings]
  52. value = params[:settings][setting]
  53. end
  54. value || Setting.send(setting)
  55. end
  56. def setting_select(setting, choices, options={})
  57. if blank_text = options.delete(:blank)
  58. choices = [[blank_text.is_a?(Symbol) ? l(blank_text) : blank_text, '']] + choices
  59. end
  60. setting_label(setting, options).html_safe +
  61. select_tag("settings[#{setting}]",
  62. options_for_select(choices, setting_value(setting).to_s),
  63. options).html_safe
  64. end
  65. def setting_multiselect(setting, choices, options={})
  66. setting_values = setting_value(setting)
  67. setting_values = [] unless setting_values.is_a?(Array)
  68. content_tag("label", l(options[:label] || "setting_#{setting}")) +
  69. hidden_field_tag("settings[#{setting}][]", '').html_safe +
  70. choices.collect do |choice|
  71. text, value = (choice.is_a?(Array) ? choice : [choice, choice])
  72. content_tag(
  73. 'label',
  74. check_box_tag(
  75. "settings[#{setting}][]",
  76. value,
  77. setting_values.include?(value),
  78. :id => nil
  79. ) + text.to_s,
  80. :class => (options[:inline] ? 'inline' : 'block')
  81. )
  82. end.join.html_safe
  83. end
  84. def setting_text_field(setting, options={})
  85. setting_label(setting, options).html_safe +
  86. text_field_tag("settings[#{setting}]", setting_value(setting), options).html_safe
  87. end
  88. def setting_text_area(setting, options={})
  89. setting_label(setting, options).html_safe +
  90. text_area_tag("settings[#{setting}]", setting_value(setting), options).html_safe
  91. end
  92. def setting_check_box(setting, options={})
  93. setting_label(setting, options).html_safe +
  94. hidden_field_tag("settings[#{setting}]", 0, :id => nil).html_safe +
  95. check_box_tag("settings[#{setting}]", 1, setting_value(setting).to_s != '0', options).html_safe
  96. end
  97. def setting_label(setting, options={})
  98. label = options.delete(:label)
  99. if label == false
  100. ''
  101. else
  102. text = label.is_a?(String) ? label : l(label || "setting_#{setting}")
  103. label_tag("settings_#{setting}", text, options[:label_options])
  104. end
  105. end
  106. # Renders a notification field for a Redmine::Notifiable option
  107. def notification_field(notifiable)
  108. tag_data =
  109. if notifiable.parent.present?
  110. {:parent_notifiable => notifiable.parent}
  111. else
  112. {:disables => "input[data-parent-notifiable=#{notifiable.name}]"}
  113. end
  114. tag = check_box_tag('settings[notified_events][]',
  115. notifiable.name,
  116. setting_value('notified_events').include?(notifiable.name),
  117. :id => nil,
  118. :data => tag_data)
  119. text = l_or_humanize(notifiable.name, :prefix => 'label_')
  120. options = {}
  121. if notifiable.parent.present?
  122. options[:class] = "parent"
  123. end
  124. content_tag(:label, tag + text, options)
  125. end
  126. def session_lifetime_options
  127. options = [[l(:label_disabled), 0]]
  128. options += [4, 8, 12].map do |hours|
  129. [l('datetime.distance_in_words.x_hours', :count => hours), (hours * 60).to_s]
  130. end
  131. options += [1, 7, 30, 60, 365].map do |days|
  132. [l('datetime.distance_in_words.x_days', :count => days), (days * 24 * 60).to_s]
  133. end
  134. options
  135. end
  136. def session_timeout_options
  137. options = [[l(:label_disabled), 0]]
  138. options += [1, 2, 4, 8, 12, 24, 48].map do |hours|
  139. [l('datetime.distance_in_words.x_hours', :count => hours), (hours * 60).to_s]
  140. end
  141. options
  142. end
  143. def link_copied_issue_options
  144. options = [
  145. [:general_text_Yes, 'yes'],
  146. [:general_text_No, 'no'],
  147. [:label_ask, 'ask']
  148. ]
  149. options.map {|label, value| [l(label), value.to_s]}
  150. end
  151. def cross_project_subtasks_options
  152. options = [
  153. [:label_disabled, ''],
  154. [:label_cross_project_system, 'system'],
  155. [:label_cross_project_tree, 'tree'],
  156. [:label_cross_project_hierarchy, 'hierarchy'],
  157. [:label_cross_project_descendants, 'descendants']
  158. ]
  159. options.map {|label, value| [l(label), value.to_s]}
  160. end
  161. def parent_issue_dates_options
  162. options = [
  163. [:label_parent_task_attributes_derived, 'derived'],
  164. [:label_parent_task_attributes_independent, 'independent']
  165. ]
  166. options.map {|label, value| [l(label), value.to_s]}
  167. end
  168. def parent_issue_priority_options
  169. options = [
  170. [:label_parent_task_attributes_derived, 'derived'],
  171. [:label_parent_task_attributes_independent, 'independent']
  172. ]
  173. options.map {|label, value| [l(label), value.to_s]}
  174. end
  175. def parent_issue_done_ratio_options
  176. options = [
  177. [:label_parent_task_attributes_derived, 'derived'],
  178. [:label_parent_task_attributes_independent, 'independent']
  179. ]
  180. options.map {|label, value| [l(label), value.to_s]}
  181. end
  182. # Returns the options for the date_format setting
  183. def date_format_setting_options(locale)
  184. Setting::DATE_FORMATS.map do |f|
  185. today = ::I18n.l(User.current.today, :locale => locale, :format => f)
  186. format = f.delete('%').gsub(/[dmY]/) do
  187. {'d' => 'dd', 'm' => 'mm', 'Y' => 'yyyy'}[$&]
  188. end
  189. ["#{today} (#{format})", f]
  190. end
  191. end
  192. def gravatar_default_setting_options
  193. [['Identicons', 'identicon'],
  194. ['Monster ids', 'monsterid'],
  195. ['Mystery man', 'mm'],
  196. ['Retro', 'retro'],
  197. ['Robohash', 'robohash'],
  198. ['Wavatars', 'wavatar']]
  199. end
  200. end