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 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 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. css = "#{custom_value.custom_field.field_format}_cf"
  72. css += ' wiki-edit' if custom_value.custom_field.full_text_formatting?
  73. custom_value.custom_field.format.edit_tag self,
  74. custom_field_tag_id(prefix, custom_value.custom_field),
  75. custom_field_tag_name(prefix, custom_value.custom_field),
  76. custom_value,
  77. :class => css
  78. end
  79. # Return custom field name tag
  80. def custom_field_name_tag(custom_field)
  81. title = custom_field.description.presence
  82. css = title ? "field-description" : nil
  83. content_tag 'span', custom_field.name, :title => title, :class => css
  84. end
  85. # Return custom field label tag
  86. def custom_field_label_tag(name, custom_value, options={})
  87. required = options[:required] || custom_value.custom_field.is_required?
  88. for_tag_id = options.fetch(:for_tag_id, "#{name}_custom_field_values_#{custom_value.custom_field.id}")
  89. content = custom_field_name_tag custom_value.custom_field
  90. content_tag "label", content +
  91. (required ? " <span class=\"required\">*</span>".html_safe : ""),
  92. :for => for_tag_id
  93. end
  94. # Return custom field tag with its label tag
  95. def custom_field_tag_with_label(name, custom_value, options={})
  96. tag = custom_field_tag(name, custom_value)
  97. tag_id = nil
  98. ids = tag.scan(/ id="(.+?)"/)
  99. if ids.size == 1
  100. tag_id = ids.first.first
  101. end
  102. custom_field_label_tag(name, custom_value, options.merge(:for_tag_id => tag_id)) + tag
  103. end
  104. # Returns the custom field tag for when bulk editing objects
  105. def custom_field_tag_for_bulk_edit(prefix, custom_field, objects=nil, value='')
  106. custom_field.format.bulk_edit_tag self,
  107. custom_field_tag_id(prefix, custom_field),
  108. custom_field_tag_name(prefix, custom_field),
  109. custom_field,
  110. objects,
  111. value,
  112. :class => "#{custom_field.field_format}_cf"
  113. end
  114. # Returns custom field value tag
  115. def custom_field_value_tag(value)
  116. attr_value = show_value(value)
  117. if !attr_value.blank? && value.custom_field.full_text_formatting?
  118. content_tag('div', attr_value, :class => 'wiki')
  119. else
  120. attr_value
  121. end
  122. end
  123. # Return a string used to display a custom value
  124. def show_value(custom_value, html=true)
  125. format_object(custom_value, html)
  126. end
  127. # Return a string used to display a custom value
  128. def format_value(value, custom_field)
  129. format_object(custom_field.format.formatted_value(self, custom_field, value, false), false)
  130. end
  131. # Return an array of custom field formats which can be used in select_tag
  132. def custom_field_formats_for_select(custom_field)
  133. Redmine::FieldFormat.as_select(custom_field.class.customized_class.name)
  134. end
  135. # Yields the given block for each custom field value of object that should be
  136. # displayed, with the custom field and the formatted value as arguments
  137. def render_custom_field_values(object, &block)
  138. object.visible_custom_field_values.each do |custom_value|
  139. formatted = show_value(custom_value)
  140. if formatted.present?
  141. yield custom_value.custom_field, formatted
  142. end
  143. end
  144. end
  145. # Renders the custom_values in api views
  146. def render_api_custom_values(custom_values, api)
  147. api.array :custom_fields do
  148. custom_values.each do |custom_value|
  149. attrs = {:id => custom_value.custom_field_id, :name => custom_value.custom_field.name}
  150. attrs.merge!(:multiple => true) if custom_value.custom_field.multiple?
  151. api.custom_field attrs do
  152. if custom_value.value.is_a?(Array)
  153. api.array :value do
  154. custom_value.value.each do |value|
  155. api.value value unless value.blank?
  156. end
  157. end
  158. else
  159. api.value custom_value.value
  160. end
  161. end
  162. end
  163. end unless custom_values.empty?
  164. end
  165. def edit_tag_style_tag(form, options={})
  166. select_options = [[l(:label_drop_down_list), ''], [l(:label_checkboxes), 'check_box']]
  167. if options[:include_radio]
  168. select_options << [l(:label_radio_buttons), 'radio']
  169. end
  170. form.select :edit_tag_style, select_options, :label => :label_display
  171. end
  172. def select_type_radio_buttons(default_type)
  173. if CUSTOM_FIELDS_TABS.none? {|tab| tab[:name] == default_type}
  174. default_type = 'IssueCustomField'
  175. end
  176. custom_field_type_options.map do |name, type|
  177. content_tag(:label, :style => 'display:block;') do
  178. radio_button_tag('type', type, type == default_type) + name
  179. end
  180. end.join("\n").html_safe
  181. end
  182. end