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.

custom_fields_helper.rb 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006- 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 CustomFieldsHelper
  19. CUSTOM_FIELDS_TABS = [
  20. {:name => 'IssueCustomField', :partial => 'custom_fields/index',
  21. :label => :label_issue_plural},
  22. {:name => 'TimeEntryCustomField', :partial => 'custom_fields/index',
  23. :label => :label_spent_time},
  24. {:name => 'ProjectCustomField', :partial => 'custom_fields/index',
  25. :label => :label_project_plural},
  26. {:name => 'VersionCustomField', :partial => 'custom_fields/index',
  27. :label => :label_version_plural},
  28. {:name => 'DocumentCustomField', :partial => 'custom_fields/index',
  29. :label => :label_document_plural},
  30. {:name => 'UserCustomField', :partial => 'custom_fields/index',
  31. :label => :label_user_plural},
  32. {:name => 'GroupCustomField', :partial => 'custom_fields/index',
  33. :label => :label_group_plural},
  34. {:name => 'TimeEntryActivityCustomField', :partial => 'custom_fields/index',
  35. :label => TimeEntryActivity::OptionName},
  36. {:name => 'IssuePriorityCustomField', :partial => 'custom_fields/index',
  37. :label => IssuePriority::OptionName},
  38. {:name => 'DocumentCategoryCustomField', :partial => 'custom_fields/index',
  39. :label => DocumentCategory::OptionName}
  40. ]
  41. def render_custom_fields_tabs(types)
  42. tabs = CUSTOM_FIELDS_TABS.select {|h| types.include?(h[:name])}
  43. render_tabs tabs
  44. end
  45. def custom_field_type_options
  46. CUSTOM_FIELDS_TABS.map {|h| [l(h[:label]), h[:name]]}
  47. end
  48. def custom_field_title(custom_field)
  49. items = []
  50. items << [l(:label_custom_field_plural), custom_fields_path]
  51. items << [l(custom_field.type_name), custom_fields_path(:tab => custom_field.class.name)] if custom_field
  52. items << (custom_field.nil? || custom_field.new_record? ? l(:label_custom_field_new) : custom_field.name)
  53. title(*items)
  54. end
  55. def render_custom_field_format_partial(form, custom_field)
  56. partial = custom_field.format.form_partial
  57. if partial
  58. render :partial => custom_field.format.form_partial, :locals => {:f => form, :custom_field => custom_field}
  59. end
  60. end
  61. def custom_field_tag_name(prefix, custom_field)
  62. name = "#{prefix}[custom_field_values][#{custom_field.id}]"
  63. name += "[]" if custom_field.multiple?
  64. name
  65. end
  66. def custom_field_tag_id(prefix, custom_field)
  67. "#{prefix}_custom_field_values_#{custom_field.id}"
  68. end
  69. # Return custom field html tag corresponding to its format
  70. def custom_field_tag(prefix, custom_value)
  71. cf = custom_value.custom_field
  72. css = cf.css_classes
  73. placeholder = cf.description
  74. placeholder&.tr!("\n", ' ') if cf.field_format != 'text'
  75. data = nil
  76. if cf.full_text_formatting?
  77. css += ' wiki-edit'
  78. data = {
  79. :auto_complete => true
  80. }
  81. end
  82. cf.format.edit_tag(
  83. self,
  84. custom_field_tag_id(prefix, cf),
  85. custom_field_tag_name(prefix, cf),
  86. custom_value,
  87. :class => css,
  88. :placeholder => placeholder,
  89. :data => data)
  90. end
  91. # Return custom field name tag
  92. def custom_field_name_tag(custom_field)
  93. title = custom_field.description.presence
  94. css = title ? "field-description" : nil
  95. content_tag 'span', custom_field.name, :title => title, :class => css
  96. end
  97. # Return custom field label tag
  98. def custom_field_label_tag(name, custom_value, options={})
  99. required = options[:required] || custom_value.custom_field.is_required?
  100. for_tag_id = options.fetch(:for_tag_id, "#{name}_custom_field_values_#{custom_value.custom_field.id}")
  101. content = custom_field_name_tag custom_value.custom_field
  102. content_tag(
  103. "label", content +
  104. (required ? " <span class=\"required\">*</span>".html_safe : ""),
  105. :for => for_tag_id,
  106. :class => custom_value.customized && custom_value.customized.errors[custom_value.custom_field.name].present? ? 'error' : nil)
  107. end
  108. # Return custom field tag with its label tag
  109. def custom_field_tag_with_label(name, custom_value, options={})
  110. tag = custom_field_tag(name, custom_value)
  111. tag_id = nil
  112. ids = tag.scan(/ id="(.+?)"/)
  113. if ids.size == 1
  114. tag_id = ids.first.first
  115. end
  116. custom_field_label_tag(name, custom_value, options.merge(:for_tag_id => tag_id)) + tag
  117. end
  118. # Returns the custom field tag for when bulk editing objects
  119. def custom_field_tag_for_bulk_edit(prefix, custom_field, objects=nil, value='')
  120. css = custom_field.css_classes
  121. data = nil
  122. if custom_field.full_text_formatting?
  123. css += ' wiki-edit'
  124. data = {
  125. :auto_complete => true
  126. }
  127. end
  128. custom_field.format.bulk_edit_tag(
  129. self,
  130. custom_field_tag_id(prefix, custom_field),
  131. custom_field_tag_name(prefix, custom_field),
  132. custom_field,
  133. objects,
  134. value,
  135. :class => css,
  136. :data => data)
  137. end
  138. # Returns custom field value tag
  139. def custom_field_value_tag(value)
  140. attr_value = show_value(value)
  141. if attr_value.present? && value.custom_field.full_text_formatting?
  142. content_tag('div', attr_value, :class => 'wiki')
  143. else
  144. attr_value
  145. end
  146. end
  147. # Return a string used to display a custom value
  148. def show_value(custom_value, html=true)
  149. format_object(custom_value, html)
  150. end
  151. # Return a string used to display a custom value
  152. def format_value(value, custom_field)
  153. format_object(custom_field.format.formatted_value(self, custom_field, value, false), false)
  154. end
  155. # Return an array of custom field formats which can be used in select_tag
  156. def custom_field_formats_for_select(custom_field)
  157. Redmine::FieldFormat.as_select(custom_field.class.customized_class.name)
  158. end
  159. # Yields the given block for each custom field value of object that should be
  160. # displayed, with the custom field and the formatted value as arguments
  161. def render_custom_field_values(object, &block)
  162. object.visible_custom_field_values.each do |custom_value|
  163. formatted = show_value(custom_value)
  164. if formatted.present?
  165. yield custom_value.custom_field, formatted
  166. end
  167. end
  168. end
  169. # Renders the custom_values in api views
  170. def render_api_custom_values(custom_values, api)
  171. api.array :custom_fields do
  172. custom_values.each do |custom_value|
  173. attrs = {:id => custom_value.custom_field_id, :name => custom_value.custom_field.name}
  174. attrs[:multiple] = true if custom_value.custom_field.multiple?
  175. api.custom_field attrs do
  176. if custom_value.value.is_a?(Array)
  177. api.array :value do
  178. custom_value.value.each do |value|
  179. api.value value unless value.blank?
  180. end
  181. end
  182. else
  183. api.value custom_value.value
  184. end
  185. end
  186. end
  187. end unless custom_values.empty?
  188. end
  189. def edit_tag_style_tag(form, options={})
  190. select_options = [[l(:label_drop_down_list), ''], [l(:label_checkboxes), 'check_box']]
  191. if options[:include_radio]
  192. select_options << [l(:label_radio_buttons), 'radio']
  193. end
  194. form.select :edit_tag_style, select_options, :label => :label_display
  195. end
  196. def select_type_radio_buttons(default_type)
  197. if CUSTOM_FIELDS_TABS.none? {|tab| tab[:name] == default_type}
  198. default_type = 'IssueCustomField'
  199. end
  200. custom_field_type_options.map do |name, type|
  201. content_tag(:label, :style => 'display:block;') do
  202. radio_button_tag('type', type, type == default_type) + name
  203. end
  204. end.join("\n").html_safe
  205. end
  206. end