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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2019 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 = [{:name => 'general', :partial => 'settings/general', :label => :label_general},
  21. {:name => 'display', :partial => 'settings/display', :label => :label_display},
  22. {:name => 'authentication', :partial => 'settings/authentication', :label => :label_authentication},
  23. {:name => 'api', :partial => 'settings/api', :label => :label_api},
  24. {:name => 'projects', :partial => 'settings/projects', :label => :label_project_plural},
  25. {:name => 'users', :partial => 'settings/users', :label => :label_user_plural},
  26. {:name => 'issues', :partial => 'settings/issues', :label => :label_issue_tracking},
  27. {:name => 'timelog', :partial => 'settings/timelog', :label => :label_time_tracking},
  28. {:name => 'attachments', :partial => 'settings/attachments', :label => :label_attachment_plural},
  29. {:name => 'notifications', :partial => 'settings/notifications', :label => :field_mail_notification},
  30. {:name => 'mail_handler', :partial => 'settings/mail_handler', :label => :label_incoming_emails},
  31. {:name => 'repositories', :partial => 'settings/repositories', :label => :label_repository_plural}
  32. ]
  33. end
  34. def render_settings_error(errors)
  35. return if errors.blank?
  36. s = ''.html_safe
  37. errors.each do |name, message|
  38. s << content_tag('li', content_tag('b', l("setting_#{name}")) + " " + message)
  39. end
  40. content_tag('div', content_tag('ul', s), :id => 'errorExplanation')
  41. end
  42. def setting_value(setting)
  43. value = nil
  44. if params[:settings]
  45. value = params[:settings][setting]
  46. end
  47. value || Setting.send(setting)
  48. end
  49. def setting_select(setting, choices, options={})
  50. if blank_text = options.delete(:blank)
  51. choices = [[blank_text.is_a?(Symbol) ? l(blank_text) : blank_text, '']] + choices
  52. end
  53. setting_label(setting, options).html_safe +
  54. select_tag("settings[#{setting}]",
  55. options_for_select(choices, setting_value(setting).to_s),
  56. options).html_safe
  57. end
  58. def setting_multiselect(setting, choices, options={})
  59. setting_values = setting_value(setting)
  60. setting_values = [] unless setting_values.is_a?(Array)
  61. content_tag("label", l(options[:label] || "setting_#{setting}")) +
  62. hidden_field_tag("settings[#{setting}][]", '').html_safe +
  63. choices.collect do |choice|
  64. text, value = (choice.is_a?(Array) ? choice : [choice, choice])
  65. content_tag(
  66. 'label',
  67. check_box_tag(
  68. "settings[#{setting}][]",
  69. value,
  70. setting_values.include?(value),
  71. :id => nil
  72. ) + text.to_s,
  73. :class => (options[:inline] ? 'inline' : 'block')
  74. )
  75. end.join.html_safe
  76. end
  77. def setting_text_field(setting, options={})
  78. setting_label(setting, options).html_safe +
  79. text_field_tag("settings[#{setting}]", setting_value(setting), options).html_safe
  80. end
  81. def setting_text_area(setting, options={})
  82. setting_label(setting, options).html_safe +
  83. text_area_tag("settings[#{setting}]", setting_value(setting), options).html_safe
  84. end
  85. def setting_check_box(setting, options={})
  86. setting_label(setting, options).html_safe +
  87. hidden_field_tag("settings[#{setting}]", 0, :id => nil).html_safe +
  88. check_box_tag("settings[#{setting}]", 1, setting_value(setting).to_s != '0', options).html_safe
  89. end
  90. def setting_label(setting, options={})
  91. label = options.delete(:label)
  92. if label == false
  93. ''
  94. else
  95. text = label.is_a?(String) ? label : l(label || "setting_#{setting}")
  96. label_tag("settings_#{setting}", text, options[:label_options])
  97. end
  98. end
  99. # Renders a notification field for a Redmine::Notifiable option
  100. def notification_field(notifiable)
  101. tag_data = notifiable.parent.present? ?
  102. {:parent_notifiable => notifiable.parent} :
  103. {:disables => "input[data-parent-notifiable=#{notifiable.name}]"}
  104. tag = check_box_tag('settings[notified_events][]',
  105. notifiable.name,
  106. setting_value('notified_events').include?(notifiable.name),
  107. :id => nil,
  108. :data => tag_data)
  109. text = l_or_humanize(notifiable.name, :prefix => 'label_')
  110. options = {}
  111. if notifiable.parent.present?
  112. options[:class] = "parent"
  113. end
  114. content_tag(:label, tag + text, options)
  115. end
  116. def session_lifetime_options
  117. options = [[l(:label_disabled), 0]]
  118. options += [4, 8, 12].map {|hours|
  119. [l('datetime.distance_in_words.x_hours', :count => hours), (hours * 60).to_s]
  120. }
  121. options += [1, 7, 30, 60, 365].map {|days|
  122. [l('datetime.distance_in_words.x_days', :count => days), (days * 24 * 60).to_s]
  123. }
  124. options
  125. end
  126. def session_timeout_options
  127. options = [[l(:label_disabled), 0]]
  128. options += [1, 2, 4, 8, 12, 24, 48].map {|hours|
  129. [l('datetime.distance_in_words.x_hours', :count => hours), (hours * 60).to_s]
  130. }
  131. options
  132. end
  133. def link_copied_issue_options
  134. options = [
  135. [:general_text_Yes, 'yes'],
  136. [:general_text_No, 'no'],
  137. [:label_ask, 'ask']
  138. ]
  139. options.map {|label, value| [l(label), value.to_s]}
  140. end
  141. def cross_project_subtasks_options
  142. options = [
  143. [:label_disabled, ''],
  144. [:label_cross_project_system, 'system'],
  145. [:label_cross_project_tree, 'tree'],
  146. [:label_cross_project_hierarchy, 'hierarchy'],
  147. [:label_cross_project_descendants, 'descendants']
  148. ]
  149. options.map {|label, value| [l(label), value.to_s]}
  150. end
  151. def parent_issue_dates_options
  152. options = [
  153. [:label_parent_task_attributes_derived, 'derived'],
  154. [:label_parent_task_attributes_independent, 'independent']
  155. ]
  156. options.map {|label, value| [l(label), value.to_s]}
  157. end
  158. def parent_issue_priority_options
  159. options = [
  160. [:label_parent_task_attributes_derived, 'derived'],
  161. [:label_parent_task_attributes_independent, 'independent']
  162. ]
  163. options.map {|label, value| [l(label), value.to_s]}
  164. end
  165. def parent_issue_done_ratio_options
  166. options = [
  167. [:label_parent_task_attributes_derived, 'derived'],
  168. [:label_parent_task_attributes_independent, 'independent']
  169. ]
  170. options.map {|label, value| [l(label), value.to_s]}
  171. end
  172. # Returns the options for the date_format setting
  173. def date_format_setting_options(locale)
  174. Setting::DATE_FORMATS.map do |f|
  175. today = ::I18n.l(User.current.today, :locale => locale, :format => f)
  176. format = f.gsub('%', '').gsub(/[dmY]/) do
  177. {'d' => 'dd', 'm' => 'mm', 'Y' => 'yyyy'}[$&]
  178. end
  179. ["#{today} (#{format})", f]
  180. end
  181. end
  182. def gravatar_default_setting_options
  183. [['Identicons', 'identicon'],
  184. ['Monster ids', 'monsterid'],
  185. ['Mystery man', 'mm'],
  186. ['Retro', 'retro'],
  187. ['Robohash', 'robohash'],
  188. ['Wavatars', 'wavatar']]
  189. end
  190. end